use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class HrdCatalogTest method simpleFormTest.
@Test
public void simpleFormTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
Optional<FormStorage> storage = catalog.getForm(collectionId);
assertTrue(storage.isPresent());
TypedRecordUpdate village1 = new TypedRecordUpdate();
village1.setUserId(userId);
village1.setRecordId(ResourceId.generateSubmissionId(formClass));
village1.set(villageField, TextValue.valueOf("Rutshuru"));
village1.set(countField, new Quantity(1000));
TypedRecordUpdate village2 = new TypedRecordUpdate();
village2.setUserId(userId);
village2.setRecordId(ResourceId.generateSubmissionId(formClass));
village2.set(villageField, TextValue.valueOf("Beni"));
village2.set(countField, new Quantity(230));
storage.get().add(village1);
storage.get().add(village2);
QueryModel queryModel = new QueryModel(collectionId);
queryModel.selectResourceId().as("id");
queryModel.selectField("VILLAGE").as("village");
queryModel.selectField("BENE").as("family_count");
queryModel.selectExpr("BENE*5").as("individual_count");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
System.out.println(columnSet);
assertThat(columnSet.getNumRows(), equalTo(2));
List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
assertThat(versions1, hasSize(1));
RecordVersion version = versions1.get(0);
assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
assertThat(version.getUserId(), equalTo((long) userId));
assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class CurlExamplesGenerator method getRecord.
public static Example getRecord() {
ResourceId formId = ResourceId.generateId();
ResourceId recordId = ResourceId.generateId();
FormRecord.Builder record = new FormRecord.Builder();
record.setFormId(formId);
record.setRecordId(recordId);
record.setFieldValue(ResourceId.generateId(), TextValue.valueOf("Text Value"));
record.setFieldValue(ResourceId.generateId(), new Quantity(1500));
record.setFieldValue(ResourceId.generateId(), new LocalDate(2016, 10, 5));
record.setFieldValue(ResourceId.generateId(), new GeoPoint(52.078663, 4.288788));
record.setFieldValue(ResourceId.generateId(), new EnumValue(ResourceId.generateId()));
return new Example(String.format("curl https://www.activityinfo.org/resources/form/%s/record/%s", formId, recordId), record.build().toJson());
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class RealValuedFunction method binaryFunctionCall.
private FieldValue binaryFunctionCall(List<FieldValue> arguments) {
// Excel-style (sort-of) missing value handling
// (1) If at least one value is non-missing, however, treat the other as zero
// (2) If both values are missing, then the result is also missing (NaN)
// The first case is consistent with Excel, and the second with SQL and R
Quantity qa = Casting.toQuantity(arguments.get(0));
Quantity qb = Casting.toQuantity(arguments.get(1));
if (Double.isNaN(qa.getValue()) && Double.isNaN(qb.getValue())) {
return new Quantity(Double.NaN);
}
double da = toDouble(qa);
double db = toDouble(qb);
double value = apply(da, db);
if (Double.isNaN(value)) {
return new Quantity(Double.NaN);
} else {
return new Quantity(value);
}
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class HrdCatalogTest method subFormTest.
@Test
public void subFormTest() {
// Typical scenario with a household interview form
// and a repeating househould member form
FormClass hhForm = new FormClass(ResourceId.generateId());
FormClass memberForm = new FormClass(ResourceId.generateId());
memberForm.setParentFormId(hhForm.getId());
hhForm.setParentFormId(ResourceId.valueOf("foo"));
hhForm.setLabel("Household interview");
FormField hhIdField = hhForm.addField().setLabel("Household ID").setType(TextType.SIMPLE);
hhForm.addField().setLabel("Household Memmbers").setType(new SubFormReferenceType(memberForm.getId()));
memberForm.setLabel("Household Members");
FormField nameField = memberForm.addField().setLabel("Name").setType(TextType.SIMPLE);
FormField ageField = memberForm.addField().setLabel("Age").setType(new QuantityType("years"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(hhForm);
catalog.create(memberForm);
TypedRecordUpdate hh1 = new TypedRecordUpdate();
hh1.setUserId(userId);
hh1.setRecordId(ResourceId.generateSubmissionId(hhForm));
hh1.set(hhIdField.getId(), TextValue.valueOf("HH1"));
TypedRecordUpdate hh2 = new TypedRecordUpdate();
hh2.setUserId(userId);
hh2.setRecordId(ResourceId.generateSubmissionId(hhForm));
hh2.set(hhIdField.getId(), TextValue.valueOf("HH2"));
TypedRecordUpdate father1 = new TypedRecordUpdate();
father1.setUserId(userId);
father1.setRecordId(ResourceId.generateSubmissionId(memberForm));
father1.setParentId(hh1.getRecordId());
father1.set(nameField.getId(), TextValue.valueOf("Homer"));
father1.set(ageField.getId(), new Quantity(40));
TypedRecordUpdate father2 = new TypedRecordUpdate();
father2.setUserId(userId);
father2.setRecordId(ResourceId.generateSubmissionId(memberForm));
father2.setParentId(hh2.getRecordId());
father2.set(nameField.getId(), TextValue.valueOf("Ned"));
father2.set(ageField.getId(), new Quantity(41));
Optional<FormStorage> hhCollection = catalog.getForm(hhForm.getId());
assertTrue(hhCollection.isPresent());
hhCollection.get().add(hh1);
hhCollection.get().add(hh2);
Optional<FormStorage> memberCollection = catalog.getForm(memberForm.getId());
assertTrue(memberCollection.isPresent());
memberCollection.get().add(father1);
memberCollection.get().add(father2);
QueryModel queryModel = new QueryModel(memberForm.getId());
queryModel.selectResourceId().as("id");
queryModel.selectField("Household ID").as("hh_id");
queryModel.selectField("Name").as("member_name");
queryModel.selectField("Age").as("member_age");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
System.out.println(columnSet);
assertThat(columnSet.getNumRows(), equalTo(2));
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class SubFormAggregationTest method subFormAggregationTest.
@Test
public void subFormAggregationTest() {
// Typical scenario with a household interview form
// and a repeating househould member form
FormClass siteForm = new FormClass(ResourceId.generateId());
siteForm.setParentFormId(ResourceId.ROOT_ID);
FormClass monthlyForm = new FormClass(ResourceId.generateId());
monthlyForm.setParentFormId(siteForm.getId());
monthlyForm.setSubFormKind(SubFormKind.MONTHLY);
siteForm.setLabel("Household interview");
FormField villageField = siteForm.addField().setLabel("Village Name").setType(TextType.SIMPLE);
siteForm.addField().setLabel("Maximum Beneficiaries").setCode("BENE").setType(new CalculatedFieldType("MAX(HH)"));
siteForm.addField().setLabel("Monthly Activities").setType(new SubFormReferenceType(monthlyForm.getId()));
monthlyForm.setLabel("Monthly Activities");
FormField countField = monthlyForm.addField().setLabel("Number of Beneficiaries").setCode("HH").setType(new QuantityType("households"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(siteForm);
catalog.create(monthlyForm);
TypedRecordUpdate v1 = new TypedRecordUpdate();
v1.setUserId(userId);
v1.setRecordId(ResourceId.generateSubmissionId(siteForm));
v1.set(villageField.getId(), TextValue.valueOf("Rutshuru"));
TypedRecordUpdate v2 = new TypedRecordUpdate();
v2.setUserId(userId);
v2.setRecordId(ResourceId.generateSubmissionId(siteForm));
v2.set(villageField.getId(), TextValue.valueOf("Beni"));
TypedRecordUpdate month1 = new TypedRecordUpdate();
month1.setUserId(userId);
month1.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
month1.setParentId(v1.getRecordId());
month1.set(countField.getId(), new Quantity(40));
TypedRecordUpdate month2 = new TypedRecordUpdate();
month2.setUserId(userId);
month2.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
month2.setParentId(v1.getRecordId());
month2.set(countField.getId(), new Quantity(30));
TypedRecordUpdate month3 = new TypedRecordUpdate();
month3.setUserId(userId);
month3.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
month3.setParentId(v2.getRecordId());
month3.set(countField.getId(), new Quantity(47));
FormStorage siteCollection = catalog.getForm(siteForm.getId()).get();
siteCollection.add(v1);
siteCollection.add(v2);
Optional<FormStorage> monthCollection = catalog.getForm(monthlyForm.getId());
assertTrue(monthCollection.isPresent());
monthCollection.get().add(month1);
monthCollection.get().add(month2);
monthCollection.get().add(month3);
QueryModel queryModel = new QueryModel(siteForm.getId());
queryModel.selectResourceId().as("id");
queryModel.selectField("Village Name").as("village");
queryModel.selectField("BENE").as("max_hh");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
System.out.println(columnSet);
assertThat(columnSet.getNumRows(), equalTo(2));
}
Aggregations