use of org.activityinfo.model.type.ReferenceValue in project activityinfo by bedatadriven.
the class Level method toChoice.
public Choice toChoice(FormInstance instance) {
if (isRoot()) {
return new Choice(getFormId(), instance.getId(), instance.getString(labelFieldId));
} else {
ReferenceValue parentRefValue = (ReferenceValue) instance.get(parentFieldId);
RecordRef parentRef = parentRefValue.getOnlyReference();
return new Choice(getFormId(), instance.getId(), instance.getString(labelFieldId), parentRef);
}
}
use of org.activityinfo.model.type.ReferenceValue in project activityinfo by bedatadriven.
the class SiteFormStorage method dummyLocationReference.
private FieldValue dummyLocationReference(RecordRef ref) {
if (activity.getAdminLevelId() == null) {
throw new IllegalStateException("Location type is not bound, but value is admin entity");
}
int adminEntityId = CuidAdapter.getLegacyIdFromCuid(ref.getRecordId());
try {
String sql = "SELECT l.locationId FROM location l " + "LEFT JOIN locationadminlink k ON (k.locationId = l.locationId) " + "WHERE l.locationTypeId = " + activity.getLocationTypeId() + " AND k.adminEntityId = " + adminEntityId;
try (ResultSet rs = queryExecutor.query(sql)) {
if (rs.next()) {
return new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(activity.getLocationTypeId()), CuidAdapter.locationInstanceId(rs.getInt(1))));
}
}
// No existing dummy location entry, create one :-(
ResourceId locationId = CuidAdapter.generateLocationCuid();
SqlInsert locationInsert = SqlInsert.insertInto("location");
locationInsert.value("locationId", CuidAdapter.getLegacyIdFromCuid(locationId));
locationInsert.value("locationTypeId", activity.getLocationTypeId());
locationInsert.value("name", queryAdminName(adminEntityId));
locationInsert.execute(queryExecutor);
while (adminEntityId > 0) {
SqlInsert linkInsert = SqlInsert.insertInto("locationadminlink");
linkInsert.value("locationId", CuidAdapter.getLegacyIdFromCuid(locationId));
linkInsert.value("adminEntityId", adminEntityId);
linkInsert.execute(queryExecutor);
adminEntityId = queryAdminParent(adminEntityId);
}
return new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(activity.getLocationTypeId()), locationId));
} catch (SQLException e) {
throw new RuntimeException("Failed to create dummy location row", e);
}
}
use of org.activityinfo.model.type.ReferenceValue in project activityinfo by bedatadriven.
the class SiteFormStorage method update.
@Override
public void update(TypedRecordUpdate update) {
FormRecord formRecord = get(update.getRecordId()).get();
FormInstance formInstance = FormInstance.toFormInstance(getFormClass(), formRecord);
BaseTableUpdater baseTable = new BaseTableUpdater(baseMapping, update.getRecordId());
IndicatorValueTableUpdater indicatorValues = new IndicatorValueTableUpdater(update.getRecordId());
AttributeValueTableUpdater attributeValues = new AttributeValueTableUpdater(activity, update.getRecordId());
if (update.isDeleted()) {
baseTable.delete();
indicatorValues.delete();
} else {
for (Map.Entry<ResourceId, FieldValue> change : update.getChangedFieldValues().entrySet()) {
if (change.getKey().getDomain() == CuidAdapter.INDICATOR_DOMAIN) {
indicatorValues.update(change.getKey(), change.getValue());
} else if (change.getKey().getDomain() == CuidAdapter.ATTRIBUTE_GROUP_FIELD_DOMAIN) {
attributeValues.update(change.getKey(), change.getValue());
} else if (change.getKey().equals(CuidAdapter.locationField(activity.getId()))) {
ReferenceValue value = (ReferenceValue) change.getValue();
if (value.getOnlyReference().getRecordId().getDomain() == CuidAdapter.LOCATION_DOMAIN) {
baseTable.update(change.getKey(), change.getValue());
} else {
baseTable.update(change.getKey(), dummyLocationReference(value.getOnlyReference()));
}
} else {
baseTable.update(change.getKey(), change.getValue());
}
}
}
long newVersion;
try {
newVersion = incrementSiteVersion();
baseTable.executeUpdates(queryExecutor);
indicatorValues.execute(queryExecutor);
attributeValues.executeUpdates(queryExecutor);
} catch (SQLException e) {
throw new RuntimeException(e);
}
Map<ResourceId, FieldValue> fieldValues = new HashMap<>();
fieldValues.putAll(formInstance.getFieldValueMap());
fieldValues.putAll(update.getChangedFieldValues());
RecordChangeType changeType = update.isDeleted() ? RecordChangeType.DELETED : RecordChangeType.UPDATED;
dualWriteToHrd(changeType, update, newVersion, fieldValues);
}
use of org.activityinfo.model.type.ReferenceValue in project activityinfo by bedatadriven.
the class BoundLocationBuilder method executeSimpleQuery.
/**
* Maps each location reference to the corresponding admin entity reference. We only look at the
* one admin level that is set as the current location type of the activity.
*
* If there are references to old location types, we just use the locationadminlink table to map
* them to a reference in the new bound admin level.
*/
private void executeSimpleQuery(QueryExecutor executor) throws SQLException {
ResourceId referenceFormId = CuidAdapter.adminLevelFormClass(activity.getAdminLevelId());
String sql = "SELECT s.siteId, k.adminEntityId " + "FROM site s " + "LEFT JOIN locationadminlink k ON (s.locationId = k.locationId AND k.adminLevelId = " + activity.getAdminLevelId() + ") " + "WHERE s.deleted = 0 AND s.activityId = " + activity.getId();
if (siteId != null) {
sql += " AND s.siteId=" + siteId;
}
sql += " ORDER BY s.siteId";
System.out.println(sql);
int lastSiteId = -1;
try (ResultSet rs = executor.query(sql)) {
while (rs.next()) {
int siteId = rs.getInt(1);
if (siteId != lastSiteId) {
int adminEntityId = rs.getInt(2);
if (rs.wasNull()) {
emit(null);
} else {
emit(new ReferenceValue(new RecordRef(referenceFormId, CuidAdapter.entity(adminEntityId))));
}
}
lastSiteId = siteId;
}
}
for (CursorObserver<FieldValue> observer : observers) {
observer.done();
}
}
use of org.activityinfo.model.type.ReferenceValue in project activityinfo by bedatadriven.
the class SiteHistoryReader method parseRef.
private FieldValue parseRef(JsonValue value, ResourceId formId, char domain) {
if (value.isJsonObject()) {
JsonValue object = value;
if (object.get("type").asString().equals("Integer")) {
int id = object.get("value").asInt();
ResourceId recordId = CuidAdapter.cuid(domain, id);
return new ReferenceValue(new RecordRef(formId, recordId));
}
}
return null;
}
Aggregations