Search in sources :

Example 56 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class ResourceLocatorAdaptorTest method siteDeletion.

@Test
public void siteDeletion() {
    QueryModel query = new QueryModel(NFI_DIST_FORM_CLASS);
    query.selectResourceId().as("id");
    query.selectField(CuidAdapter.field(VILLAGE_CLASS, CuidAdapter.NAME_FIELD));
    ColumnSet columnSet = assertResolves(locator.queryTable(query));
    assertThat(columnSet.getNumRows(), equalTo(3));
    final ResourceId firstRecordId = ResourceId.valueOf(columnSet.getColumnView("id").getString(0));
    assertResolves(locator.remove(NFI_DIST_FORM_CLASS, firstRecordId));
    columnSet = assertResolves(locator.queryTable(query));
    // size is reduced
    assertThat(columnSet.getNumRows(), equalTo(2));
    assertThat(columnSet.getColumnView("id"), not(hasValue(firstRecordId)));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 57 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class ResourceLocatorAdaptorTest method deleteLocation.

@Test
public void deleteLocation() {
    ResourceId instanceToDelete = CuidAdapter.locationInstanceId(1);
    assertResolves(locator.remove(CuidAdapter.locationFormClass(1), instanceToDelete));
    QueryModel queryModel = new QueryModel(CuidAdapter.locationFormClass(1));
    queryModel.selectResourceId().as("id");
    ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
    ColumnView idColumn = columnSet.getColumnView("id");
    for (int i = 0; i < idColumn.numRows(); i++) {
        if (idColumn.getString(i).equals(instanceToDelete.asString())) {
            throw new AssertionError();
        }
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 58 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FormModelTest method setupForms.

/**
 * Sets up test fixtures. Must be called by each test to ensure that it runs AFTER
 * the dbunit setup
 */
public void setupForms() {
    ResourceId masterFormId = CuidAdapter.activityFormClass(3);
    masterFormClass = new FormClass(masterFormId);
    masterFormClass.setDatabaseId(CuidAdapter.databaseId(1));
    FormField partnerField = new FormField(CuidAdapter.partnerField(3));
    partnerField.setLabel("Partner");
    partnerField.setType(ReferenceType.single(CuidAdapter.partnerFormId(1)));
    masterFormClass.addElement(partnerField);
    FormField labelField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
    labelField.setLabel("label1");
    labelField.setType(TextType.SIMPLE);
    subFormClass = new FormClass(ResourceId.generateId());
    subFormClass.setDatabaseId(masterFormClass.getDatabaseId());
    subFormClass.setParentFormId(masterFormId);
    subFormClass.setSubFormKind(SubFormKind.MONTHLY);
    subFormChildField = subFormClass.addField();
    subFormChildField.setType(TextType.SIMPLE);
    subFormField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
    subFormField.setType(new SubFormReferenceType(subFormClass.getId()));
    assertResolves(locator.persist(subFormClass));
    assertResolves(locator.persist(masterFormClass));
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField)

Example 59 with ResourceId

use of org.activityinfo.model.resource.ResourceId 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 60 with ResourceId

use of org.activityinfo.model.resource.ResourceId 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)

Aggregations

ResourceId (org.activityinfo.model.resource.ResourceId)198 Test (org.junit.Test)42 FormClass (org.activityinfo.model.form.FormClass)41 FormField (org.activityinfo.model.form.FormField)34 RecordRef (org.activityinfo.model.type.RecordRef)30 JsonValue (org.activityinfo.json.JsonValue)21 FormTree (org.activityinfo.model.formTree.FormTree)18 ReferenceValue (org.activityinfo.model.type.ReferenceValue)17 FieldValue (org.activityinfo.model.type.FieldValue)16 FormInstance (org.activityinfo.model.form.FormInstance)15 HashMap (java.util.HashMap)14 QuantityType (org.activityinfo.model.type.number.QuantityType)13 ColumnSet (org.activityinfo.model.query.ColumnSet)12 QueryModel (org.activityinfo.model.query.QueryModel)12 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)11 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)11 ReferenceType (org.activityinfo.model.type.ReferenceType)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 FormStorage (org.activityinfo.store.spi.FormStorage)10 ArrayList (java.util.ArrayList)9