Search in sources :

Example 16 with FieldValue

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

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

the class SerialNumberTest method testWithPrefix.

@Test
public void testWithPrefix() {
    TestDatabase database = harness.createDatabase();
    ActivityInfoClient client = harness.client();
    FormClass formClass = new FormClass(harness.newFormId());
    formClass.setLabel("Test Prefix");
    formClass.setDatabaseId(database.getId());
    FormField partnerField = CuidAdapter.partnerField(formClass);
    formClass.addElement(partnerField);
    FormField provinceField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
    FormField fileNumberField = formClass.addField(harness.newFieldId()).setCode("SN").setType(new SerialNumberType("PROVINCE", 5)).setLabel("Record Number");
    client.createForm(formClass);
    FormInstance kunduz1 = new FormInstance(harness.newRecordId(), formClass.getId());
    kunduz1.set(provinceField.getId(), "KUNDUZ");
    kunduz1.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(kunduz1);
    FormInstance kunduz2 = new FormInstance(harness.newRecordId(), formClass.getId());
    kunduz2.set(provinceField.getId(), "KUNDUZ");
    kunduz2.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(kunduz2);
    FormInstance takhar = new FormInstance(harness.newRecordId(), formClass.getId());
    takhar.set(provinceField.getId(), "TAKHAR");
    takhar.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(takhar);
    // Verify that the serial numbers have been assigned
    kunduz1 = client.getTypedRecord(formClass, kunduz1.getId());
    kunduz2 = client.getTypedRecord(formClass, kunduz2.getId());
    takhar = client.getTypedRecord(formClass, takhar.getId());
    assertThat(kunduz1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
    assertThat(kunduz2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 2)));
    assertThat(takhar.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("TAKHAR", 1)));
    // Now verify that we can query a table with serial numbers
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("SN");
    ColumnSet columnSet = client.queryTable(queryModel);
    ColumnView sn = columnSet.getColumnView("SN");
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ActivityInfoClient(org.activityinfo.client.ActivityInfoClient) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 18 with FieldValue

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

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

Example 20 with FieldValue

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

the class AttributeBuffer method next.

@Override
public void next() {
    EnumValue value;
    int cardinality = bitSet.cardinality();
    if (cardinality == 1) {
        // Single item is set, use our cached FieldValues
        int index = bitSet.nextSetBit(0);
        value = singletonValues.get(index);
    } else if (cardinality == 0) {
        // No items set, null value
        value = null;
    } else {
        // Multiple items set, construct a new EnumValue
        Set<ResourceId> set = new HashSet<>();
        for (int i = 0; i < itemIds.size(); i++) {
            if (bitSet.get(i)) {
                set.add(itemIds.get(i));
            }
        }
        value = new EnumValue(set);
    }
    for (CursorObserver<FieldValue> observer : observers) {
        observer.onNext(value);
    }
    bitSet.clear();
}
Also used : ResultSet(java.sql.ResultSet) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) FieldValue(org.activityinfo.model.type.FieldValue)

Aggregations

FieldValue (org.activityinfo.model.type.FieldValue)49 ResourceId (org.activityinfo.model.resource.ResourceId)16 FormField (org.activityinfo.model.form.FormField)13 Test (org.junit.Test)12 FormInstance (org.activityinfo.model.form.FormInstance)10 Quantity (org.activityinfo.model.type.number.Quantity)9 JsonValue (org.activityinfo.json.JsonValue)8 FormClass (org.activityinfo.model.form.FormClass)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 RecordRef (org.activityinfo.model.type.RecordRef)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 BooleanFieldValue (org.activityinfo.model.type.primitive.BooleanFieldValue)5 SerialNumber (org.activityinfo.model.type.SerialNumber)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 LocalDate (org.activityinfo.model.type.time.LocalDate)4 JsonMappingException (org.activityinfo.json.JsonMappingException)3 FormulaNode (org.activityinfo.model.formula.FormulaNode)3