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;
}
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");
}
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());
}
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;
}
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();
}
Aggregations