Search in sources :

Example 26 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class MySqlRecordHistoryBuilder method queryLabels.

private List<String> queryLabels(ReferenceType type, ReferenceValue value) {
    Map<ResourceId, String> labelMap = new HashMap<>();
    for (ResourceId formId : type.getRange()) {
        Optional<FormStorage> form = catalog.getForm(formId);
        if (form.isPresent()) {
            Optional<ResourceId> labelFieldId = findLabelField(form.get().getFormClass());
            for (RecordRef ref : value.getReferences()) {
                Optional<FormRecord> record = form.get().get(ref.getRecordId());
                if (record.isPresent()) {
                    JsonValue labelValue = Json.createNull();
                    if (labelFieldId.isPresent()) {
                        labelValue = record.get().getFields().get(labelFieldId.get().asString());
                    }
                    if (labelValue.isJsonNull()) {
                        labelValue = record.get().getFields().get(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD).asString());
                    }
                    if (labelValue.isJsonPrimitive()) {
                        labelMap.put(ref.getRecordId(), labelValue.asString());
                    }
                }
            }
        }
    }
    List<String> list = new ArrayList<>();
    for (RecordRef ref : value.getReferences()) {
        String label = labelMap.get(ref.getRecordId());
        if (label == null) {
            list.add(ref.toQualifiedString());
        } else {
            list.add(label);
        }
    }
    return list;
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) JsonValue(org.activityinfo.json.JsonValue) ResourceId(org.activityinfo.model.resource.ResourceId)

Example 27 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class ForeignKeyTest method filtering.

@Test
public void filtering() {
    // Build the unfiltered foreign key map
    // That maps row indexes to foreign keys
    ResourceId formId = ResourceId.valueOf("a00001");
    ForeignKeyBuilder builder = new ForeignKeyBuilder(formId, new PendingSlot<ForeignKey>());
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0362622291"))));
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0362622291"))));
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0890848243"))));
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0890848243"))));
    ForeignKey fkMap = builder.build();
    // Now define a filter that includes only the 4th and 5th rows
    BitSet bitSet = new BitSet();
    bitSet.set(4);
    bitSet.set(5);
    TableFilter filter = new TableFilter(bitSet);
    // Apply the filter to the ForeignKey map and verify the results
    ForeignKey filteredKey = filter.apply(fkMap);
    assertThat(filteredKey.getKey(0), equalTo("s0890848243"));
    assertThat(filteredKey.getKey(1), equalTo("s0890848243"));
}
Also used : ForeignKeyBuilder(org.activityinfo.store.query.server.join.ForeignKeyBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) TableFilter(org.activityinfo.store.query.shared.TableFilter) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) BitSet(java.util.BitSet) ForeignKey(org.activityinfo.store.query.shared.columns.ForeignKey) Test(org.junit.Test)

Example 28 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class ForeignKeyTest method emptyValues.

@Test
public void emptyValues() {
    // Build the unfiltered foreign key map
    // That maps row indexes to foreign keys
    ResourceId formId = ResourceId.valueOf("a00001");
    ForeignKeyBuilder builder = new ForeignKeyBuilder(formId, new PendingSlot<ForeignKey>());
    builder.onNext(null);
    builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
    builder.onNext(null);
    builder.onNext(null);
    ForeignKey fkMap = builder.build();
    assertThat(fkMap.numRows(), equalTo(4));
}
Also used : ForeignKeyBuilder(org.activityinfo.store.query.server.join.ForeignKeyBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) ForeignKey(org.activityinfo.store.query.shared.columns.ForeignKey) Test(org.junit.Test)

Example 29 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class SubFormPatch method fromRecordId.

public static CursorObserver<ResourceId> fromRecordId(FormClass formClass, final CursorObserver<FieldValue> observer) {
    final ResourceId formId = formClass.getId();
    final PeriodType periodType = formClass.getSubFormKind().getPeriodType();
    return new CursorObserver<ResourceId>() {

        @Override
        public void onNext(ResourceId value) {
            observer.onNext(periodType.fromSubFormKey(new RecordRef(formId, value)));
        }

        @Override
        public void done() {
            observer.done();
        }
    };
}
Also used : PeriodType(org.activityinfo.model.type.time.PeriodType) ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef)

Example 30 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class BoundLocationBuilder method executeMixedQuery.

/**
 * Generates a list of references for a location field that references a mix of bound location types
 * and non-bound location types.
 */
private void executeMixedQuery(QueryExecutor executor) throws SQLException {
    String sql = "SELECT " + // (1)
    "s.siteId, " + // (2)
    "s.locationId, " + // (3)
    "t.locationTypeId, " + // (4)
    "t.boundAdminLevelId, " + // (5)
    "k.adminLevelId, " + // (6)
    "k.adminEntityId " + "FROM site s " + "LEFT JOIN location g ON (s.locationId = g.locationId) " + "LEFT JOIN locationtype t ON (g.locationTypeId = t.locationTypeId) " + "LEFT JOIN locationadminlink k ON (s.locationId = k.locationId) " + "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;
    boolean foundEntry = false;
    try (ResultSet rs = executor.query(sql)) {
        while (rs.next()) {
            int siteId = rs.getInt(1);
            if (lastSiteId > 0 && lastSiteId != siteId) {
                if (!foundEntry) {
                    emit(null);
                }
                foundEntry = false;
            }
            int locationId = rs.getInt(2);
            int locationTypeId = rs.getInt(3);
            int boundAdminLevelId = rs.getInt(4);
            if (rs.wasNull()) {
                // if we haven't already
                if (siteId != lastSiteId) {
                    foundEntry = true;
                    emit(new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(locationTypeId), CuidAdapter.locationInstanceId(locationId))));
                }
            } else {
                int adminLevelId = rs.getInt(5);
                if (boundAdminLevelId == adminLevelId) {
                    int adminEntityId = rs.getInt(6);
                    foundEntry = true;
                    emit(new ReferenceValue(new RecordRef(CuidAdapter.adminLevelFormClass(adminLevelId), CuidAdapter.entity(adminEntityId))));
                }
            }
            lastSiteId = siteId;
        }
    }
    if (lastSiteId != -1 && !foundEntry) {
        emit(null);
    }
    for (CursorObserver<FieldValue> observer : observers) {
        observer.done();
    }
}
Also used : ReferenceValue(org.activityinfo.model.type.ReferenceValue) ResultSet(java.sql.ResultSet) RecordRef(org.activityinfo.model.type.RecordRef) FieldValue(org.activityinfo.model.type.FieldValue)

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