Search in sources :

Example 76 with ResourceId

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

the class RecordGenerator method get.

@Override
public FormInstance get() {
    ResourceId recordId = ids.recordId(schema.getId(), nextRecordIndex++);
    FormInstance record = new FormInstance(recordId, schema.getId());
    for (Map.Entry<ResourceId, FieldValue> entry : ids.builtinValues().entrySet()) {
        record.set(entry.getKey(), entry.getValue());
    }
    if (parentDistribution != null) {
        record.setParentRecordId(parentDistribution.get());
    }
    for (Map.Entry<ResourceId, Supplier<FieldValue>> entry : generators.entrySet()) {
        record.set(entry.getKey(), entry.getValue().get());
    }
    return record;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) Supplier(com.google.common.base.Supplier) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with ResourceId

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

the class XPathBuilderTest method containsAny.

@Test
public void containsAny() {
    TFormClass formClass = createFormClass();
    ResourceId male = formClass.getEnumValueByLabel("Male").getId();
    ResourceId female = formClass.getEnumValueByLabel("Female").getId();
    formClass.getFormClass().getField(GENDER_FIELD_ID).setRelevanceConditionExpression(String.format("containsAny({%s},{%s})", GENDER_FIELD_ID.asString(), male.asString()));
    XForm xForm = xForm(formClass.getFormClass());
    assertEquals(bindByFieldId(GENDER_FIELD_ID, xForm).getRelevant(), String.format("selected(/data/field_%s, '%s')", GENDER_FIELD_ID.asString(), male.asString()));
    formClass.getFormClass().getField(GENDER_FIELD_ID).setRelevanceConditionExpression(String.format("containsAny({%s},{%s},{%s})", GENDER_FIELD_ID.asString(), male.asString(), female.asString()));
    xForm = xForm(formClass.getFormClass());
    assertEquals(bindByFieldId(GENDER_FIELD_ID, xForm).getRelevant(), String.format("selected(/data/field_%s, '%s') or selected(/data/field_%s, '%s')", GENDER_FIELD_ID.asString(), male.asString(), GENDER_FIELD_ID.asString(), female.asString()));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) XForm(org.activityinfo.io.xform.form.XForm) TFormClass(org.activityinfo.model.form.TFormClass) Test(org.junit.Test)

Example 78 with ResourceId

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

the class MySqlCatalogTest method batchOpenCollections.

@Test
public void batchOpenCollections() {
    ResourceId activity1 = activityFormClass(1);
    ResourceId activity2 = activityFormClass(2);
    ResourceId provinceId = adminLevelFormClass(1);
    ResourceId monthlyId = reportingPeriodFormClass(4000);
    Map<ResourceId, FormClass> formClasses = catalog.getFormClasses(asList(activity1, activity2, provinceId, monthlyId));
    FormClass activityFormClass1 = formClasses.get(activity1);
    assertThat(activityFormClass1, notNullValue());
    assertThat(activityFormClass1.getLabel(), equalTo("NFI"));
    FormClass activityFormClass2 = formClasses.get(activity2);
    assertThat(activityFormClass2, notNullValue());
    assertThat(activityFormClass2.getLabel(), equalTo("Distribution de Kits Scolaire"));
    FormClass provinceFormClass = formClasses.get(provinceId);
    assertThat(provinceFormClass.getLabel(), equalTo("Province"));
    FormClass monthlyForm = formClasses.get(monthlyId);
    assertThat(monthlyForm, Matchers.notNullValue());
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) Test(org.junit.Test)

Example 79 with ResourceId

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

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

the class SiteHistoryReader method parseChanges.

private Map<ResourceId, FieldValue> parseChanges(JsonValue jsonObject) {
    Map<ResourceId, ResourceId> attributeToFieldMap = new HashMap<>();
    for (ActivityField activityField : activity.getAttributeAndIndicatorFields()) {
        if (activityField.getFormField().getType() instanceof EnumType) {
            EnumType type = (EnumType) activityField.getFormField().getType();
            for (EnumItem enumItem : type.getValues()) {
                attributeToFieldMap.put(enumItem.getId(), activityField.getResourceId());
            }
        }
    }
    Map<ResourceId, FieldValue> valueMap = new HashMap<>();
    Multimap<ResourceId, ResourceId> attributeValueMap = HashMultimap.create();
    for (Map.Entry<String, JsonValue> jsonEntry : jsonObject.entrySet()) {
        String fieldName = jsonEntry.getKey();
        if (fieldName.equals("comments")) {
            valueMap.put(fieldId(CuidAdapter.COMMENT_FIELD), NarrativeValue.valueOf(parseString(jsonEntry.getValue())));
        } else if (fieldName.equals("date1")) {
            valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
        } else if (fieldName.equals("date2")) {
            valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
        } else if (fieldName.equals("partnerId")) {
            valueMap.put(fieldId(CuidAdapter.PARTNER_FIELD), parseRef(jsonEntry.getValue(), activity.getPartnerFormClassId(), CuidAdapter.PARTNER_DOMAIN));
        } else if (fieldName.equals("projectId")) {
            valueMap.put(fieldId(CuidAdapter.PROJECT_FIELD), parseRef(jsonEntry.getValue(), activity.getProjectFormClassId(), CuidAdapter.PROJECT_DOMAIN));
        } else if (fieldName.equals("locationId")) {
            valueMap.put(fieldId(CuidAdapter.LOCATION_FIELD), parseRef(jsonEntry.getValue(), activity.getLocationFormClassId(), CuidAdapter.LOCATION_DOMAIN));
        } else if (fieldName.startsWith("I")) {
            int mIndex = fieldName.indexOf("M");
            if (mIndex == -1) {
                int indicatorId = Integer.parseInt(fieldName.substring(1));
                ResourceId fieldId = CuidAdapter.indicatorField(indicatorId);
                valueMap.put(fieldId, parseQuantity(jsonEntry.getValue()));
            } else {
                // old history
                valueMap.put(ResourceId.valueOf(fieldName), parseQuantity(jsonEntry.getValue()));
            }
        } else if (fieldName.startsWith("ATTRIB")) {
            if (parseBoolean(jsonEntry.getValue())) {
                int attributeId = Integer.parseInt(fieldName.substring("ATTRIB".length()));
                ResourceId attributeCuid = CuidAdapter.attributeId(attributeId);
                ResourceId fieldId = attributeToFieldMap.get(attributeCuid);
                if (fieldId != null) {
                    attributeValueMap.put(fieldId, attributeCuid);
                }
            }
        }
    }
    for (ResourceId fieldId : attributeValueMap.keySet()) {
        valueMap.put(fieldId, new EnumValue(attributeValueMap.get(fieldId)));
    }
    return valueMap;
}
Also used : HashMap(java.util.HashMap) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) JsonValue(org.activityinfo.json.JsonValue) ResourceId(org.activityinfo.model.resource.ResourceId) EnumType(org.activityinfo.model.type.enumerated.EnumType) FieldValue(org.activityinfo.model.type.FieldValue) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) HashMap(java.util.HashMap) Map(java.util.Map)

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