Search in sources :

Example 6 with ReferenceValue

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

the class FormModelTest method subformInstancesPersistence.

@Test
public void subformInstancesPersistence() {
    setupForms();
    FormInstance rootInstance = new FormInstance(ResourceId.generateSubmissionId(masterFormClass), masterFormClass.getId());
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2016, 1, 1));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2016, 1, 1));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.PARTNER_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.partnerFormId(1), CuidAdapter.partnerRecordId(1))));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
    FormModel formModel = newFormModel();
    formModel.setWorkingRootInstance(rootInstance);
    String tab1 = new Month(2015, 3).toString();
    String tab2 = new Month(2015, 8).toString();
    // Tab1
    FormInstance valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
    valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab1"));
    // Tab2
    FormInstance valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
    valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab2"));
    formModel.getChangedInstances().add(valueInstance1);
    formModel.getChangedInstances().add(valueInstance2);
    // persist all value and tab/key instances
    FormActions actions = new FormActions(locator, formModel);
    assertResolves(actions.save());
    // make sure instances are persisted
    FormInstance fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
    FormInstance fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
    assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab1"));
    assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab2"));
    // Update value instances
    // Tab1
    valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
    valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab11"));
    // Tab2
    valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
    valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab22"));
    formModel.getChangedInstances().add(valueInstance1);
    formModel.getChangedInstances().add(valueInstance2);
    // persist updates
    assertResolves(actions.save());
    // make sure instances are persisted
    fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
    fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
    assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab11"));
    assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab22"));
    // check subform loader
    FormModel emptyModel = new FormModel(locator, new GxtStateProvider());
    emptyModel.setWorkingRootInstance(rootInstance);
    // load subform instances into empty model
    assertResolves(new SubFormInstanceLoader(emptyModel).load(subFormClass));
    Map<FormModel.SubformValueKey, Set<FormInstance>> loadedInstances = emptyModel.getSubFormInstances();
    assertEquals(1, loadedInstances.size());
    assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab1).get(), valueInstance1);
    assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab2).get(), valueInstance2);
}
Also used : SubFormInstanceLoader(org.activityinfo.ui.client.component.form.subform.SubFormInstanceLoader) Set(java.util.Set) OnDataSet(org.activityinfo.server.database.OnDataSet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) GxtStateProvider(org.activityinfo.ui.client.dispatch.state.GxtStateProvider) LocalDate(org.activityinfo.model.type.time.LocalDate) Month(org.activityinfo.model.type.time.Month) FormInstance(org.activityinfo.model.form.FormInstance) Test(org.junit.Test)

Example 7 with ReferenceValue

use of org.activityinfo.model.type.ReferenceValue 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 8 with ReferenceValue

use of org.activityinfo.model.type.ReferenceValue 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 9 with ReferenceValue

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

the class SiteFormStorage method add.

@Override
public void add(TypedRecordUpdate update) {
    ResourceId formClassId = getFormClass().getId();
    BaseTableInserter baseTable = new BaseTableInserter(baseMapping, update.getRecordId());
    baseTable.addValue("ActivityId", activity.getId());
    baseTable.addValue("DateCreated", new Date());
    baseTable.addValue("DateEdited", new Date());
    if (!activity.hasLocationType()) {
        baseTable.addValue("locationId", activity.getNullaryLocationId());
    }
    IndicatorValueTableUpdater indicatorValues = new IndicatorValueTableUpdater(update.getRecordId());
    AttributeValueTableUpdater attributeValues = new AttributeValueTableUpdater(activity, update.getRecordId());
    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.add(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.set(change.getKey(), change.getValue());
            } else {
                baseTable.set(change.getKey(), dummyLocationReference(value.getOnlyReference()));
            }
        } else {
            baseTable.set(change.getKey(), change.getValue());
        }
        if (change.getKey().equals(CuidAdapter.field(formClassId, CuidAdapter.START_DATE_FIELD))) {
            indicatorValues.setDate1(change.getValue());
        } else if (change.getKey().equals(CuidAdapter.field(formClassId, CuidAdapter.END_DATE_FIELD))) {
            indicatorValues.setDate2(change.getValue());
        }
    }
    long newVersion = incrementSiteVersion();
    baseTable.executeInsert(queryExecutor);
    attributeValues.executeUpdates(queryExecutor);
    indicatorValues.insert(queryExecutor);
    // Write the snapshot to HRD as a first step in the transition
    dualWriteToHrd(RecordChangeType.CREATED, update, newVersion, update.getChangedFieldValues());
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) FieldValue(org.activityinfo.model.type.FieldValue)

Example 10 with ReferenceValue

use of org.activityinfo.model.type.ReferenceValue 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

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