use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class UpdaterTest method invalidParsedQuantity.
@Test(expected = InvalidUpdateException.class)
public void invalidParsedQuantity() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = createObject();
fields.put("Q1", "4.1.3");
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
Updater.parseChange(formClass, change, userId);
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class PivotAdapter method selectedIndicators.
private List<ActivityField> selectedIndicators(Activity activity) {
if (filter.isRestricted(DimensionType.Activity)) {
if (!filter.getRestrictions(DimensionType.Activity).contains(activity.getId())) {
return Collections.emptyList();
}
}
if (filter.isRestricted(DimensionType.Database)) {
if (!filter.getRestrictions(DimensionType.Database).contains(activity.getDatabaseId())) {
return Collections.emptyList();
}
}
List<ActivityField> matching = Lists.newArrayList();
Set<Integer> restrictedIndicatorIds = filter.getRestrictions(DimensionType.Indicator);
for (ActivityField field : activity.getIndicatorFields()) {
FieldType type = field.getFormField().getType();
if (type instanceof QuantityType || type instanceof CalculatedFieldType) {
if (restrictedIndicatorIds.isEmpty() || restrictedIndicatorIds.contains(field.getId())) {
matching.add(field);
}
}
}
return matching;
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class MySqlUpdateTest method createFormWithCalculationAndRelevance.
@Test
public void createFormWithCalculationAndRelevance() {
userId = 1;
KeyGenerator generator = new KeyGenerator();
int activityId = generator.generateInt();
FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
formClass.setDatabaseId(1);
formClass.setLabel("New Form");
FormField numberField = new FormField(CuidAdapter.generateIndicatorId()).setType(new QuantityType("widgets")).setLabel("NUM").setRequired(true);
formClass.addElement(numberField);
FormField calculatedField = new FormField(CuidAdapter.generateIndicatorId()).setType(new CalculatedFieldType("1")).setLabel("Calculation").setRelevanceConditionExpression("NUM>42").setRequired(true);
formClass.addElement(calculatedField);
catalog.createOrUpdateFormSchema(formClass);
newRequest();
// Create two records
RecordTransactionBuilder tx = new RecordTransactionBuilder();
RecordUpdate site1 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
site1.setFieldValue(numberField.getId(), new Quantity(10));
site1.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
RecordUpdate site2 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
site2.setFieldValue(numberField.getId(), new Quantity(60));
site2.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
updater().execute(tx.build());
newRequest();
// Query results
QueryModel queryModel = new QueryModel(formClass.getId());
queryModel.selectResourceId();
queryModel.selectExpr("Num").as("num");
queryModel.selectExpr("Calculation").as("calc");
query(queryModel);
ColumnView num = columnSet.getColumnView("num");
ColumnView calculation = columnSet.getColumnView("calc");
assertThat(calculation.isMissing(0), equalTo(num.getDouble(0) <= 42));
assertThat(calculation.isMissing(1), equalTo(num.getDouble(1) <= 42));
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class ActivityLoader method addField.
private void addField(Activity activity, ResultSet rs, Map<Integer, List<EnumItem>> attributes) throws SQLException {
int id = rs.getInt("id");
FormField formField;
if (rs.getString("Type").equals("ENUM")) {
formField = new FormField(CuidAdapter.attributeGroupField(id));
} else {
formField = new FormField(CuidAdapter.indicatorField(id));
}
formField.setLabel(rs.getString("Name"));
formField.setRequired(getMandatory(rs));
formField.setVisible(getVisible(rs));
formField.setDescription(rs.getString("Description"));
formField.setCode(rs.getString("code"));
if (rs.getBoolean("ca") && rs.getString("expr") != null) {
formField.setType(new CalculatedFieldType(rs.getString("expr")));
} else {
switch(rs.getString("Type")) {
default:
case "QUANTITY":
formField.setType(new QuantityType().setUnits(rs.getString("units")).setAggregation(rs.getInt("Aggregation")));
break;
case "BARCODE":
formField.setType(BarcodeType.INSTANCE);
break;
case "FREE_TEXT":
formField.setType(TextType.SIMPLE);
break;
case "NARRATIVE":
formField.setType(NarrativeType.INSTANCE);
break;
case "ENUM":
formField.setType(createEnumType(rs, attributes));
break;
}
}
ActivityField field = new ActivityField(id, rs.getString("category"), formField, rs.getInt("sortOrder"));
field.sortOrder = rs.getInt("sortOrder");
activity.fields.add(field);
}
Aggregations