Search in sources :

Example 11 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class ExprEvalTest method evaluate.

private void evaluate(String exprString, double expectedValue) {
    FormulaLexer lexer = new FormulaLexer(exprString);
    FormulaParser parser = new FormulaParser(lexer);
    FormulaNode expr = parser.parse();
    FieldValue value = expr.evaluate(EmptyEvalContext.INSTANCE);
    if (Double.isNaN(expectedValue)) {
        assertTrue(exprString + " is NaN", Double.isNaN(Casting.toQuantity(value).getValue()));
    } else {
        assertThat(exprString, Casting.toQuantity(value).getValue(), closeTo(expectedValue, 0));
    }
}
Also used : FieldValue(org.activityinfo.model.type.FieldValue)

Example 12 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class ExprEvalTest method evaluate.

private void evaluate(String exprString, boolean expectedValue) {
    FormulaLexer lexer = new FormulaLexer(exprString);
    FormulaParser parser = new FormulaParser(lexer);
    FormulaNode expr = parser.parse();
    FieldValue result = expr.evaluate(EmptyEvalContext.INSTANCE);
    assertThat(exprString, Casting.toBoolean(result), equalTo(expectedValue));
}
Also used : FieldValue(org.activityinfo.model.type.FieldValue)

Example 13 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class MySqlRecordHistoryBuilder method computeDeltas.

private List<RecordDelta> computeDeltas(FormClass formClass, FormField subFormField, List<RecordVersion> versions, Map<ResourceId, String> monthlyFieldLabels) {
    List<RecordDelta> deltas = new ArrayList<>();
    Map<ResourceId, Map<ResourceId, FieldValue>> currentStateMap = new HashMap<>();
    for (RecordVersion version : versions) {
        RecordDelta delta = new RecordDelta();
        delta.version = version;
        delta.subFormField = subFormField;
        Map<ResourceId, FieldValue> currentState = currentStateMap.get(version.getRecordId());
        if (currentState == null) {
            // Initialize our state map for this record
            currentStateMap.put(version.getRecordId(), new HashMap<>(delta.version.getValues()));
        } else {
            // version.
            for (FormField field : formClass.getFields()) {
                if (version.getValues().containsKey(field.getId())) {
                    FieldValue oldValue = currentState.get(field.getId());
                    FieldValue newValue = version.getValues().get(field.getId());
                    if (!Objects.equals(oldValue, newValue)) {
                        FieldDelta fieldDelta = new FieldDelta();
                        fieldDelta.field = field;
                        fieldDelta.oldValue = oldValue;
                        fieldDelta.newValue = newValue;
                        if (!Strings.isNullOrEmpty(version.getSubformKey())) {
                            // subforms
                            fieldDelta.subFormKey = version.getSubformKey();
                            fieldDelta.subFormKind = version.getSubformKind();
                        }
                        delta.changes.add(fieldDelta);
                    }
                    currentState.put(field.getId(), newValue);
                }
            }
            // special handling for old legacy history
            for (Map.Entry<ResourceId, FieldValue> entry : version.getValues().entrySet()) {
                ResourceId fieldId = entry.getKey();
                String fieldIdAsString = fieldId.asString();
                if (fieldIdAsString.startsWith("I") && fieldIdAsString.contains("M")) {
                    // e.g. I309566527M2016-8
                    int idInt = CuidAdapter.getLegacyIdFromCuid(fieldIdAsString.substring(fieldIdAsString.indexOf("I"), fieldIdAsString.indexOf("M")));
                    ResourceId id = CuidAdapter.indicatorField(idInt);
                    if (monthlyFieldLabels.containsKey(id)) {
                        // check to ensure field still exists
                        FieldValue oldValue = currentState.get(fieldId);
                        FieldValue newValue = version.getValues().get(fieldId);
                        if (!Objects.equals(oldValue, newValue)) {
                            String month = fieldIdAsString.substring(fieldIdAsString.indexOf("M") + 1);
                            FieldDelta fieldDelta = new FieldDelta();
                            fieldDelta.field = new FormField(fieldId);
                            fieldDelta.field.setLabel(Strings.nullToEmpty(monthlyFieldLabels.get(id)) + " (" + month + ")");
                            fieldDelta.field.setType(new QuantityType());
                            fieldDelta.oldValue = oldValue;
                            fieldDelta.newValue = newValue;
                            delta.changes.add(fieldDelta);
                        }
                        currentState.put(fieldId, newValue);
                    }
                }
            }
        }
        deltas.add(delta);
    }
    return deltas;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FieldValue(org.activityinfo.model.type.FieldValue)

Example 14 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class FormRecordEntity method toFieldValueMap.

public Map<ResourceId, FieldValue> toFieldValueMap(FormClass formClass) {
    Map<ResourceId, FieldValue> map = new HashMap<>();
    for (FormField formField : formClass.getFields()) {
        Object value = fieldValues.getProperty(formField.getName());
        if (value != null) {
            FieldConverter<?> converter = FieldConverters.forType(formField.getType());
            map.put(formField.getId(), converter.toFieldValue(value));
        }
    }
    return map;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) HashMap(java.util.HashMap) FieldValue(org.activityinfo.model.type.FieldValue) FormField(org.activityinfo.model.form.FormField)

Example 15 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class Updater method computeSerialNumberPrefix.

private String computeSerialNumberPrefix(FormClass formClass, SerialNumberType type, FormInstance effectiveRecord) {
    if (!type.hasPrefix()) {
        return null;
    }
    try {
        FormEvalContext evalContext = new FormEvalContext(formClass);
        evalContext.setInstance(effectiveRecord);
        FormulaNode formula = FormulaParser.parse(type.getPrefixFormula());
        FieldValue prefixValue = formula.evaluate(evalContext);
        if (prefixValue instanceof TextValue) {
            return ((TextValue) prefixValue).asString();
        } else {
            throw new IllegalStateException("Prefix " + type.getPrefixFormula() + " resolves to type " + prefixValue.getTypeClass().getId());
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to compute prefix for serial number", e);
        return null;
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) TextValue(org.activityinfo.model.type.primitive.TextValue) FieldValue(org.activityinfo.model.type.FieldValue) JsonMappingException(org.activityinfo.json.JsonMappingException)

Aggregations

FieldValue (org.activityinfo.model.type.FieldValue)49 ResourceId (org.activityinfo.model.resource.ResourceId)16 FormField (org.activityinfo.model.form.FormField)13 Test (org.junit.Test)12 FormInstance (org.activityinfo.model.form.FormInstance)10 Quantity (org.activityinfo.model.type.number.Quantity)9 JsonValue (org.activityinfo.json.JsonValue)8 FormClass (org.activityinfo.model.form.FormClass)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 RecordRef (org.activityinfo.model.type.RecordRef)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 BooleanFieldValue (org.activityinfo.model.type.primitive.BooleanFieldValue)5 SerialNumber (org.activityinfo.model.type.SerialNumber)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 LocalDate (org.activityinfo.model.type.time.LocalDate)4 JsonMappingException (org.activityinfo.json.JsonMappingException)3 FormulaNode (org.activityinfo.model.formula.FormulaNode)3