Search in sources :

Example 21 with Quantity

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));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Quantity(org.activityinfo.model.type.number.Quantity) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 22 with Quantity

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()));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity)

Example 23 with Quantity

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;
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) FilterConfig(com.sencha.gxt.data.shared.loader.FilterConfig) FieldValue(org.activityinfo.model.type.FieldValue) LocalDate(org.activityinfo.model.type.time.LocalDate)

Aggregations

Quantity (org.activityinfo.model.type.number.Quantity)23 Test (org.junit.Test)13 FieldValue (org.activityinfo.model.type.FieldValue)9 LocalDate (org.activityinfo.model.type.time.LocalDate)8 FormClass (org.activityinfo.model.form.FormClass)6 ResourceId (org.activityinfo.model.resource.ResourceId)6 QuantityType (org.activityinfo.model.type.number.QuantityType)6 FormField (org.activityinfo.model.form.FormField)5 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)5 QueryModel (org.activityinfo.model.query.QueryModel)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 ColumnSet (org.activityinfo.model.query.ColumnSet)3 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)3 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)3 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)3 JsonValue (org.activityinfo.json.JsonValue)2 FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)2 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)2 RecordRef (org.activityinfo.model.type.RecordRef)2 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)2