Search in sources :

Example 6 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class MySqlRecordHistoryBuilder method computeDeltas.

private List<RecordDelta> computeDeltas(FormClass formClass, FormField subFormField, List<RecordVersion> versions, Map<ResourceId, String> monthlyFieldLabels) {
    List<RecordDelta> deltas = new ArrayList<>();
    Map<ResourceId, Map<ResourceId, FieldValue>> currentStateMap = new HashMap<>();
    for (RecordVersion version : versions) {
        RecordDelta delta = new RecordDelta();
        delta.version = version;
        delta.subFormField = subFormField;
        Map<ResourceId, FieldValue> currentState = currentStateMap.get(version.getRecordId());
        if (currentState == null) {
            // Initialize our state map for this record
            currentStateMap.put(version.getRecordId(), new HashMap<>(delta.version.getValues()));
        } else {
            // version.
            for (FormField field : formClass.getFields()) {
                if (version.getValues().containsKey(field.getId())) {
                    FieldValue oldValue = currentState.get(field.getId());
                    FieldValue newValue = version.getValues().get(field.getId());
                    if (!Objects.equals(oldValue, newValue)) {
                        FieldDelta fieldDelta = new FieldDelta();
                        fieldDelta.field = field;
                        fieldDelta.oldValue = oldValue;
                        fieldDelta.newValue = newValue;
                        if (!Strings.isNullOrEmpty(version.getSubformKey())) {
                            // subforms
                            fieldDelta.subFormKey = version.getSubformKey();
                            fieldDelta.subFormKind = version.getSubformKind();
                        }
                        delta.changes.add(fieldDelta);
                    }
                    currentState.put(field.getId(), newValue);
                }
            }
            // special handling for old legacy history
            for (Map.Entry<ResourceId, FieldValue> entry : version.getValues().entrySet()) {
                ResourceId fieldId = entry.getKey();
                String fieldIdAsString = fieldId.asString();
                if (fieldIdAsString.startsWith("I") && fieldIdAsString.contains("M")) {
                    // e.g. I309566527M2016-8
                    int idInt = CuidAdapter.getLegacyIdFromCuid(fieldIdAsString.substring(fieldIdAsString.indexOf("I"), fieldIdAsString.indexOf("M")));
                    ResourceId id = CuidAdapter.indicatorField(idInt);
                    if (monthlyFieldLabels.containsKey(id)) {
                        // check to ensure field still exists
                        FieldValue oldValue = currentState.get(fieldId);
                        FieldValue newValue = version.getValues().get(fieldId);
                        if (!Objects.equals(oldValue, newValue)) {
                            String month = fieldIdAsString.substring(fieldIdAsString.indexOf("M") + 1);
                            FieldDelta fieldDelta = new FieldDelta();
                            fieldDelta.field = new FormField(fieldId);
                            fieldDelta.field.setLabel(Strings.nullToEmpty(monthlyFieldLabels.get(id)) + " (" + month + ")");
                            fieldDelta.field.setType(new QuantityType());
                            fieldDelta.oldValue = oldValue;
                            fieldDelta.newValue = newValue;
                            delta.changes.add(fieldDelta);
                        }
                        currentState.put(fieldId, newValue);
                    }
                }
            }
        }
        deltas.add(delta);
    }
    return deltas;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FieldValue(org.activityinfo.model.type.FieldValue)

Example 7 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class HrdCatalogTest method simpleFormTest.

@Test
public void simpleFormTest() {
    ResourceId collectionId = ResourceId.generateId();
    ResourceId villageField = ResourceId.valueOf("FV");
    ResourceId countField = ResourceId.valueOf("FC");
    FormClass formClass = new FormClass(collectionId);
    formClass.setParentFormId(ResourceId.valueOf("foo"));
    formClass.setLabel("NFI Distributions");
    formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
    formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(formClass);
    Optional<FormStorage> storage = catalog.getForm(collectionId);
    assertTrue(storage.isPresent());
    TypedRecordUpdate village1 = new TypedRecordUpdate();
    village1.setUserId(userId);
    village1.setRecordId(ResourceId.generateSubmissionId(formClass));
    village1.set(villageField, TextValue.valueOf("Rutshuru"));
    village1.set(countField, new Quantity(1000));
    TypedRecordUpdate village2 = new TypedRecordUpdate();
    village2.setUserId(userId);
    village2.setRecordId(ResourceId.generateSubmissionId(formClass));
    village2.set(villageField, TextValue.valueOf("Beni"));
    village2.set(countField, new Quantity(230));
    storage.get().add(village1);
    storage.get().add(village2);
    QueryModel queryModel = new QueryModel(collectionId);
    queryModel.selectResourceId().as("id");
    queryModel.selectField("VILLAGE").as("village");
    queryModel.selectField("BENE").as("family_count");
    queryModel.selectExpr("BENE*5").as("individual_count");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
    List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
    assertThat(versions1, hasSize(1));
    RecordVersion version = versions1.get(0);
    assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
    assertThat(version.getUserId(), equalTo((long) userId));
    assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) Test(org.junit.Test)

Example 8 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class HrdCatalogTest method versionRangeTest.

@Test
public void versionRangeTest() {
    ResourceId collectionId = ResourceId.generateId();
    ResourceId villageField = ResourceId.valueOf("FV");
    ResourceId countField = ResourceId.valueOf("FC");
    FormClass formClass = new FormClass(collectionId);
    formClass.setParentFormId(ResourceId.valueOf("foo"));
    formClass.setLabel("NFI Distributions");
    formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
    formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(formClass);
    VersionedFormStorage formStorage = (VersionedFormStorage) catalog.getForm(collectionId).get();
    // Initially, with no records added, the form should be at version 1
    // and the version range (0, 1] should be empty.
    assertThat(formStorage.cacheVersion(), equalTo(1L));
    FormSyncSet updatedRecords = formStorage.getVersionRange(0, 1L, resourceId -> true);
    assertTrue(updatedRecords.isEmpty());
    // Add a new record
    TypedRecordUpdate firstUpdate = new TypedRecordUpdate();
    firstUpdate.setUserId(1);
    firstUpdate.setRecordId(ResourceId.generateId());
    firstUpdate.set(villageField, TextValue.valueOf("Goma"));
    catalog.getForm(collectionId).get().add(firstUpdate);
    // Verify that the version is incremented and the version range
    // (0, 2] includes our new record
    assertThat(formStorage.cacheVersion(), equalTo(2L));
    FormSyncSet updated = formStorage.getVersionRange(0, 2L, resourceId -> true);
    assertThat(updated.getUpdatedRecordCount(), equalTo(1));
    // Update the first record and add a new one
    TypedRecordUpdate secondUpdate = new TypedRecordUpdate();
    secondUpdate.setUserId(1);
    secondUpdate.setRecordId(ResourceId.generateId());
    secondUpdate.set(villageField, TextValue.valueOf("Rutshuru"));
    catalog.getForm(collectionId).get().add(firstUpdate);
    // Verify that the version is incremented and the version range
    // (1, 2] includes our new record
    assertThat(formStorage.cacheVersion(), equalTo(3L));
    updated = formStorage.getVersionRange(2L, 3L, resourceId -> true);
    assertThat(updated.getUpdatedRecordCount(), equalTo(1));
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) org.activityinfo.store.spi(org.activityinfo.store.spi) BeforeClass(org.junit.BeforeClass) Cardinality(org.activityinfo.model.type.Cardinality) QuantityType(org.activityinfo.model.type.number.QuantityType) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) EnumType(org.activityinfo.model.type.enumerated.EnumType) Quantity(org.activityinfo.model.type.number.Quantity) Assert.assertThat(org.junit.Assert.assertThat) LocalDatastoreServiceTestConfig(com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) Optional(com.google.common.base.Optional) After(org.junit.After) TextValue(org.activityinfo.model.type.primitive.TextValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) VoidWork(com.googlecode.objectify.VoidWork) LocalServiceTestHelper(com.google.appengine.tools.development.testing.LocalServiceTestHelper) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Before(org.junit.Before) ObjectifyService(com.googlecode.objectify.ObjectifyService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Updater(org.activityinfo.store.query.server.Updater) FormSyncSet(org.activityinfo.model.form.FormSyncSet) List(java.util.List) FormField(org.activityinfo.model.form.FormField) TextType(org.activityinfo.model.type.primitive.TextType) LocaleProxy(net.lightoze.gwt.i18n.server.LocaleProxy) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Closeable(com.googlecode.objectify.util.Closeable) Collections(java.util.Collections) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormSyncSet(org.activityinfo.model.form.FormSyncSet) FormClass(org.activityinfo.model.form.FormClass) Test(org.junit.Test)

Example 9 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class UpdaterTest method missingValue.

@Test
public void missingValue() throws JsonMappingException {
    ResourceId fieldId = ResourceId.valueOf("Q1");
    FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
    formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
    JsonValue fields = createObject();
    fields.put("Q1", Json.createNull());
    JsonValue change = createObject();
    change.put("recordId", "A");
    change.put("formId", "XYZ123");
    change.put("fields", fields);
    TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
    assertTrue(update.getChangedFieldValues().containsKey(fieldId));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FormField(org.activityinfo.model.form.FormField) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 10 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class XFormResourceTest method invalidExpr.

@Test
@OnDataSet("/dbunit/invalid-relevance.db.xml")
public void invalidExpr() throws Exception {
    setUser(USER_ID);
    FormClass formClass = resourceLocator.getFormClass(CuidAdapter.activityFormClass(ACTIVITY_ID));
    FormField field = new FormField(ResourceId.generateFieldId(QuantityType.TYPE_CLASS));
    field.setLabel("Question");
    field.setType(new QuantityType());
    field.setRelevanceConditionExpression("DELETED_FIELD=1");
    formClass.addElement(field);
    resourceLocator.persist(formClass);
    Response form = this.formResource.form(ACTIVITY_ID);
    validate(form);
}
Also used : Response(javax.ws.rs.core.Response) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) CuidAdapter.activityFormClass(org.activityinfo.model.legacy.CuidAdapter.activityFormClass) FormField(org.activityinfo.model.form.FormField) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

QuantityType (org.activityinfo.model.type.number.QuantityType)34 FormClass (org.activityinfo.model.form.FormClass)20 FormField (org.activityinfo.model.form.FormField)19 Test (org.junit.Test)18 ResourceId (org.activityinfo.model.resource.ResourceId)13 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)12 EnumType (org.activityinfo.model.type.enumerated.EnumType)8 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)7 Quantity (org.activityinfo.model.type.number.Quantity)7 QueryModel (org.activityinfo.model.query.QueryModel)6 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)6 JsonValue (org.activityinfo.json.JsonValue)5 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)5 ColumnSet (org.activityinfo.model.query.ColumnSet)5 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)4 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)4 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)4 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 FieldType (org.activityinfo.model.type.FieldType)3 TextType (org.activityinfo.model.type.primitive.TextType)3