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();
}
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();
}
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);
}
});
}
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));
}
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"));
}
Aggregations