Search in sources :

Example 21 with ReferenceValue

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);
    }
}
Also used : ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef)

Example 22 with ReferenceValue

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);
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) SQLException(java.sql.SQLException) ReferenceValue(org.activityinfo.model.type.ReferenceValue) ResultSet(java.sql.ResultSet) RecordRef(org.activityinfo.model.type.RecordRef)

Example 23 with ReferenceValue

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);
}
Also used : SQLException(java.sql.SQLException) ReferenceValue(org.activityinfo.model.type.ReferenceValue) ResourceId(org.activityinfo.model.resource.ResourceId) FieldValue(org.activityinfo.model.type.FieldValue)

Example 24 with ReferenceValue

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();
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) ResultSet(java.sql.ResultSet) RecordRef(org.activityinfo.model.type.RecordRef) FieldValue(org.activityinfo.model.type.FieldValue)

Example 25 with ReferenceValue

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;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) JsonValue(org.activityinfo.json.JsonValue) RecordRef(org.activityinfo.model.type.RecordRef)

Aggregations

ReferenceValue (org.activityinfo.model.type.ReferenceValue)31 RecordRef (org.activityinfo.model.type.RecordRef)26 ResourceId (org.activityinfo.model.resource.ResourceId)17 Test (org.junit.Test)11 FormInstance (org.activityinfo.model.form.FormInstance)7 FieldValue (org.activityinfo.model.type.FieldValue)7 LocalDate (org.activityinfo.model.type.time.LocalDate)4 ResultSet (java.sql.ResultSet)3 OnDataSet (org.activityinfo.server.database.OnDataSet)3 ForeignKeyBuilder (org.activityinfo.store.query.server.join.ForeignKeyBuilder)3 ForeignKey (org.activityinfo.store.query.shared.columns.ForeignKey)3 SQLException (java.sql.SQLException)2 Set (java.util.Set)2 FormClass (org.activityinfo.model.form.FormClass)2 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)2 RecordTree (org.activityinfo.model.formTree.RecordTree)2 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)2 Maybe (org.activityinfo.promise.Maybe)2 ValidationResult (org.activityinfo.ui.client.component.importDialog.model.validation.ValidationResult)2 BitSet (java.util.BitSet)1