Search in sources :

Example 31 with QuantityType

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);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 32 with QuantityType

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;
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) FieldType(org.activityinfo.model.type.FieldType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType)

Example 33 with QuantityType

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));
}
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 34 with QuantityType

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);
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) FormField(org.activityinfo.model.form.FormField)

Aggregations

QuantityType (org.activityinfo.model.type.number.QuantityType)34 FormClass (org.activityinfo.model.form.FormClass)20 FormField (org.activityinfo.model.form.FormField)19 Test (org.junit.Test)18 ResourceId (org.activityinfo.model.resource.ResourceId)13 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)12 EnumType (org.activityinfo.model.type.enumerated.EnumType)8 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)7 Quantity (org.activityinfo.model.type.number.Quantity)7 QueryModel (org.activityinfo.model.query.QueryModel)6 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)6 JsonValue (org.activityinfo.json.JsonValue)5 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)5 ColumnSet (org.activityinfo.model.query.ColumnSet)5 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)4 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)4 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)4 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 FieldType (org.activityinfo.model.type.FieldType)3 TextType (org.activityinfo.model.type.primitive.TextType)3