use of org.activityinfo.model.type.RecordRef 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.RecordRef 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.RecordRef 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.RecordRef 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;
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class AdminColumnBuilder method emit.
private void emit(int[] adminEntity) {
// From the beginning, AI has always stored *all* admin level members in the
// locationadminlink table to make it easier to query
// This denormalized form, however, is not what we want with the new model,
// so we need to eliminate the redundant information
boolean nonNull = false;
for (int i = 0; i < adminEntity.length; i++) {
if (adminEntity[i] != 0) {
nonNull = true;
int parentIndex = adminParentMap[i];
if (parentIndex != -1) {
// remove the parent from the result -- it is redundant information
adminEntity[parentIndex] = 0;
}
}
}
// Now emit the field value
if (nonNull) {
Set<RecordRef> entityIds = new HashSet<>();
for (int i = 0; i < adminEntity.length; i++) {
int entityId = adminEntity[i];
if (entityId != 0) {
entityIds.add(new RecordRef(adminLevelFormIds[i], CuidAdapter.entity(entityId)));
}
}
emit(new ReferenceValue(entityIds));
} else {
emit(ReferenceValue.EMPTY);
}
}
Aggregations