Search in sources :

Example 1 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class FormFieldWidgetFactory method createWidget.

public Promise<? extends FormFieldWidget> createWidget(FormClass formClass, FormField field, FieldUpdater valueUpdater) {
    FieldType type = field.getType();
    if (type instanceof QuantityType) {
        return Promise.resolved(new QuantityFieldWidget((QuantityType) type, valueUpdater));
    } else if (type instanceof SerialNumberType) {
        return Promise.resolved(new SerialNumberFieldWidget((SerialNumberType) type));
    } else if (type instanceof NarrativeType) {
        return Promise.resolved(new NarrativeFieldWidget(valueUpdater));
    } else if (type instanceof TextType) {
        return Promise.resolved(new TextFieldWidget((TextType) type, valueUpdater));
    } else if (type instanceof CalculatedFieldType) {
        return Promise.resolved(new CalculatedFieldWidget(valueUpdater));
    } else if (type instanceof LocalDateType) {
        return Promise.resolved(new DateFieldWidget(valueUpdater));
    } else if (type instanceof LocalDateIntervalType) {
        return Promise.resolved(new DateIntervalFieldWidget(valueUpdater));
    } else if (type instanceof GeoPointType) {
        return Promise.resolved(new GeographicPointWidget(valueUpdater));
    } else if (type instanceof EnumType) {
        return Promise.resolved(new EnumFieldWidget((EnumType) field.getType(), valueUpdater, fieldWidgetMode));
    } else if (type instanceof BooleanType) {
        return Promise.resolved(new BooleanFieldWidget(valueUpdater));
    } else if (type instanceof AttachmentType) {
        AttachmentType attachmentType = (AttachmentType) type;
        if (attachmentType.getKind() == AttachmentType.Kind.IMAGE) {
            return Promise.resolved(new ImageUploadFieldWidget(formClass.getId(), valueUpdater, fieldWidgetMode));
        } else {
            return Promise.resolved(new AttachmentUploadFieldWidget(formClass.getId(), valueUpdater, fieldWidgetMode));
        }
    } else if (type instanceof ReferenceType) {
        return createReferenceWidget(field, valueUpdater);
    } else if (type instanceof BarcodeType) {
        return Promise.resolved(new BarcodeFieldWidget(valueUpdater));
    }
    Log.error("Unexpected field type " + type.getTypeClass());
    throw new UnsupportedOperationException();
}
Also used : SerialNumberType(org.activityinfo.model.type.SerialNumberType) ReferenceType(org.activityinfo.model.type.ReferenceType) AttachmentUploadFieldWidget(org.activityinfo.ui.client.component.form.field.attachment.AttachmentUploadFieldWidget) ImageUploadFieldWidget(org.activityinfo.ui.client.component.form.field.attachment.ImageUploadFieldWidget) NarrativeType(org.activityinfo.model.type.NarrativeType) EnumType(org.activityinfo.model.type.enumerated.EnumType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) AttachmentType(org.activityinfo.model.type.attachment.AttachmentType) BooleanType(org.activityinfo.model.type.primitive.BooleanType) LocalDateIntervalType(org.activityinfo.model.type.time.LocalDateIntervalType) FieldType(org.activityinfo.model.type.FieldType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) TextType(org.activityinfo.model.type.primitive.TextType) GeoPointType(org.activityinfo.model.type.geo.GeoPointType) QuantityType(org.activityinfo.model.type.number.QuantityType) LocalDateType(org.activityinfo.model.type.time.LocalDateType) BarcodeType(org.activityinfo.model.type.barcode.BarcodeType)

Example 2 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class FormulaValidator method findCalculatedFieldType.

private FieldType findCalculatedFieldType(FormTree.Node fieldNode) {
    CalculatedFieldType fieldType = (CalculatedFieldType) fieldNode.getType();
    FormulaNode rootNode;
    try {
        rootNode = FormulaParser.parse(fieldType.getExpression());
    } catch (FormulaException e) {
        throw new ValidationFailed();
    }
    FormClass formClass = fieldNode.getDefiningFormClass();
    FormTree subTree = formTree.subTree(formClass.getId());
    FormulaValidator validator = new FormulaValidator(subTree);
    if (!validator.validate(rootNode)) {
        throw new ValidationFailed();
    }
    return validator.getResultType();
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Example 3 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class OldGetSitesHandler method joinCalculatedIndicatorValues.

private void joinCalculatedIndicatorValues(final Promise<Void> complete, SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap) {
    Log.trace("Starting joinIndicatorValues()");
    final Set<Integer> activityIds = Sets.newHashSet();
    for (SiteDTO siteDTO : siteMap.values()) {
        activityIds.add(siteDTO.getActivityId());
    }
    SqlQuery query = SqlQuery.select().appendColumn("I.IndicatorId", "indicatorId").appendColumn("I.Name", "indicatorName").appendColumn("I.ActivityId", "activityId").appendColumn("I.Type", "type").appendColumn("I.Expression", "expression").appendColumn("I.nameInExpression", "code").appendColumn("I.calculatedAutomatically", "calculatedAutomatically").from(Tables.INDICATOR, "I").where("I.ActivityId").in(activityIds).and("I.dateDeleted IS NULL").orderBy("I.SortOrder");
    Log.info(query.toString());
    query.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, final SqlResultSet results) {
            Multimap<Integer, FormField> fields = HashMultimap.create();
            for (SqlResultSetRow row : results.getRows()) {
                fields.put(row.getInt("activityId"), createField(row));
            }
            // Have to resolve symbols on a per-form basis
            for (Integer activityId : fields.keySet()) {
                Collection<FormField> activityFields = fields.get(activityId);
                FormSymbolTable symbolTable = new FormSymbolTable(activityFields);
                PartialEvaluator<SiteDTO> evaluator = new PartialEvaluator<>(symbolTable, new SiteFieldReaderFactory());
                List<CalculatedIndicatorReader> readers = Lists.newArrayList();
                for (FormField field : activityFields) {
                    if (field.getType() instanceof CalculatedFieldType) {
                        try {
                            FieldReader<SiteDTO> reader = evaluator.partiallyEvaluate(field);
                            if (reader.getType() instanceof QuantityType) {
                                readers.add(new CalculatedIndicatorReader(field, reader));
                            }
                        } catch (Exception e) {
                            // we don't want to fail whole GetSites command due to invalid expression.
                            Log.error("Failed to evaluate calculated field: " + field + ", expression: " + ((CalculatedFieldType) field.getType()).getExpression(), e);
                        }
                    }
                }
                for (SiteDTO site : siteMap.values()) {
                    for (CalculatedIndicatorReader reader : readers) {
                        reader.read(site);
                    }
                }
            }
            complete.onSuccess(null);
        }
    });
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) PartialEvaluator(org.activityinfo.model.formula.eval.PartialEvaluator) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) QuantityType(org.activityinfo.model.type.number.QuantityType) FormSymbolTable(org.activityinfo.model.formula.eval.FormSymbolTable) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) FormField(org.activityinfo.model.form.FormField) FieldReader(org.activityinfo.model.formula.eval.FieldReader)

Example 4 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class FormEvalContextTest method circularRefs.

@Test
public void circularRefs() {
    FormField a = new FormField(ResourceId.generateId());
    a.setCode("A");
    a.setType(new CalculatedFieldType("B+1"));
    a.setLabel("A");
    FormField b = new FormField(ResourceId.generateId());
    b.setCode("B");
    b.setType(new CalculatedFieldType("A/50"));
    b.setLabel("B");
    FormClass formClass = new FormClass(ResourceId.generateId());
    formClass.addElement(a);
    formClass.addElement(b);
    FormEvalContext context = new FormEvalContext(formClass);
    context.setInstance(new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId()));
    assertThat(context.getFieldValue(a.getId()), instanceOf(ErrorValue.class));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormClass(org.activityinfo.model.form.FormClass) ErrorValue(org.activityinfo.model.type.ErrorValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) FormEvalContext(org.activityinfo.model.form.FormEvalContext) Test(org.junit.Test)

Example 5 with CalculatedFieldType

use of org.activityinfo.model.type.expr.CalculatedFieldType in project activityinfo by bedatadriven.

the class FormClassTest method oldSerialization.

@Test
public void oldSerialization() throws IOException {
    FormClass formClass = parseResource();
    assertThat(formClass.getId(), equalTo(ResourceId.valueOf("a0000000728")));
    assertThat(formClass.getLabel(), equalTo("Youth Group Formation"));
    FormField partnerField = formClass.getFields().get(0);
    assertThat(partnerField.getLabel(), equalTo("Partner"));
    assertThat(partnerField.getType(), instanceOf(ReferenceType.class));
    ReferenceType partnerFieldType = (ReferenceType) partnerField.getType();
    assertThat(partnerFieldType.getCardinality(), equalTo(Cardinality.SINGLE));
    assertThat(partnerFieldType.getRange(), hasItem(ResourceId.valueOf("P0000000326")));
    FormField calcField = formClass.getFields().get(5);
    assertThat(calcField.getLabel(), equalTo("Number of attendees"));
    assertThat(calcField.getType(), instanceOf(CalculatedFieldType.class));
    CalculatedFieldType calcFieldType = (CalculatedFieldType) calcField.getType();
    assertThat(calcFieldType.getExpression(), equalTo("3250+3249"));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Aggregations

CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)16 QuantityType (org.activityinfo.model.type.number.QuantityType)12 FormClass (org.activityinfo.model.form.FormClass)7 FormField (org.activityinfo.model.form.FormField)7 Test (org.junit.Test)6 EnumType (org.activityinfo.model.type.enumerated.EnumType)4 FieldType (org.activityinfo.model.type.FieldType)3 ColumnView (org.activityinfo.model.query.ColumnView)2 QueryModel (org.activityinfo.model.query.QueryModel)2 ResourceId (org.activityinfo.model.resource.ResourceId)2 NarrativeType (org.activityinfo.model.type.NarrativeType)2 ReferenceType (org.activityinfo.model.type.ReferenceType)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 Quantity (org.activityinfo.model.type.number.Quantity)2 TextType (org.activityinfo.model.type.primitive.TextType)2 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)2 LocalDateType (org.activityinfo.model.type.time.LocalDateType)2 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)2 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1