use of org.activityinfo.model.type.number.Quantity 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.Quantity in project activityinfo by bedatadriven.
the class IndicatorValueTableUpdater method executeQuantityUpdate.
private void executeQuantityUpdate(QueryExecutor executor, IndicatorUpdate update) {
Quantity quantity = (Quantity) update.value;
executor.update("REPLACE INTO indicatorvalue (reportingPeriodId, indicatorId, Value) VALUES (?, ?, ?)", Arrays.asList(reportingPeriodId, update.indicatorId, quantity.getValue()));
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class ColumnFilterParser method parseComparison.
private boolean parseComparison(FormulaNode node, Multimap<Integer, FilterConfig> result) {
if (!(node instanceof FunctionCallNode)) {
return false;
}
// Check that this is a binary
FunctionCallNode callNode = (FunctionCallNode) node;
if (callNode.getArgumentCount() != 2) {
return false;
}
// Does this comparison involve one of our fields?
Integer columnIndex = findColumnIndex(callNode.getArgument(0));
if (columnIndex == null) {
return false;
}
// Is it compared with a constant value?
FieldValue value = parseLiteral(callNode.getArgument(1));
if (value == null) {
return false;
}
FilterConfig config;
if (value instanceof Quantity) {
config = numericFilter(callNode, (Quantity) value);
} else if (value instanceof LocalDate) {
config = dateFilter(callNode, (LocalDate) value);
} else {
return false;
}
result.put(columnIndex, config);
return true;
}
Aggregations