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