Search in sources :

Example 41 with RecordRef

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

Example 42 with RecordRef

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);
    }
}
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 43 with RecordRef

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();
    }
}
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 44 with RecordRef

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;
}
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)

Example 45 with RecordRef

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

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)66 Test (org.junit.Test)31 ResourceId (org.activityinfo.model.resource.ResourceId)26 ReferenceValue (org.activityinfo.model.type.ReferenceValue)26 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)14 FormInstance (org.activityinfo.model.form.FormInstance)7 FormTree (org.activityinfo.model.formTree.FormTree)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 Optional (com.google.common.base.Optional)5 FieldValue (org.activityinfo.model.type.FieldValue)5 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)5 FormClass (org.activityinfo.model.form.FormClass)4 ResultSet (java.sql.ResultSet)3 FormField (org.activityinfo.model.form.FormField)3 RecordTree (org.activityinfo.model.formTree.RecordTree)3 ColumnSet (org.activityinfo.model.query.ColumnSet)3 ColumnView (org.activityinfo.model.query.ColumnView)3 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)3