Search in sources :

Example 1 with FieldValue

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

the class SimpleFormPanel method setValue.

public Promise<Void> setValue(FormInstance instance) {
    model.setWorkingRootInstance(instance);
    List<Promise<Void>> tasks = Lists.newArrayList();
    for (FieldContainer container : widgetCreator.getContainers().values()) {
        FormField field = container.getField();
        FieldValue value = model.getWorkingRootInstance().get(field.getId(), field.getType());
        if (value != null && value.getTypeClass() == field.getType().getTypeClass()) {
            tasks.add(container.getFieldWidget().setValue(value));
        } else {
            container.getFieldWidget().clearValue();
        }
        container.setValid();
    }
    return Promise.waitAll(tasks).then(new Function<Void, Void>() {

        @Override
        public Void apply(Void input) {
            // invoke relevance handler once values are set
            relevanceHandler.onValueChange();
            return null;
        }
    });
}
Also used : Promise(org.activityinfo.promise.Promise) FieldValue(org.activityinfo.model.type.FieldValue) FormField(org.activityinfo.model.form.FormField)

Example 2 with FieldValue

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

the class XPathBuilder method appendTo.

private void appendTo(FormulaNode formulaNode, StringBuilder xpath) {
    if (formulaNode instanceof ConstantNode) {
        ConstantNode constantNode = (ConstantNode) formulaNode;
        FieldValue value = constantNode.getValue();
        if (value instanceof BooleanFieldValue) {
            BooleanFieldValue booleanFieldValue = (BooleanFieldValue) value;
            xpath.append(booleanFieldValue.asBoolean() ? TRUE : FALSE);
        } else if (value instanceof EnumValue) {
            String xpathExpr = resolveSymbol(formulaNode);
            xpath.append(xpathExpr);
        } else {
            xpath.append(constantNode.asExpression());
        }
    } else if (formulaNode instanceof FunctionCallNode) {
        FunctionCallNode functionCallNode = (FunctionCallNode) formulaNode;
        List<FormulaNode> arguments = functionCallNode.getArguments();
        FormulaFunction function = functionCallNode.getFunction();
        switch(function.getId()) {
            case "&&":
                appendBinaryInfixTo("and", arguments, xpath);
                break;
            case "==":
                appendBinaryInfixTo("=", arguments, xpath);
                break;
            case "||":
                appendBinaryInfixTo("or", arguments, xpath);
                break;
            case "containsAny":
            case "containsAll":
            case "notContainsAny":
            case "notContainsAll":
                appendFunction(function.getId(), arguments, xpath);
                break;
            case "!=":
            case ">":
            case ">=":
            case "<":
            case "<=":
                appendBinaryInfixTo(function.getId(), arguments, xpath);
                break;
            case "!":
                appendUnaryFunction("not", arguments.get(0), xpath);
                break;
            default:
                throw new XPathBuilderException("Unsupported function " + function.getId());
        }
    } else if (formulaNode instanceof GroupNode) {
        GroupNode groupNode = (GroupNode) formulaNode;
        FormulaNode expr = groupNode.getExpr();
        xpath.append("(");
        appendTo(expr, xpath);
        xpath.append(")");
    } else if (formulaNode instanceof SymbolNode) {
        SymbolNode symbolNode = (SymbolNode) formulaNode;
        String xpathExpr = resolveSymbol(symbolNode);
        xpath.append(xpathExpr);
    } else if (formulaNode instanceof CompoundExpr) {
        CompoundExpr compoundExpr = (CompoundExpr) formulaNode;
        List<FormulaNode> arguments = new ArrayList<>();
        arguments.add(compoundExpr.getValue());
        arguments.add(compoundExpr.getField());
        appendBinaryInfixTo("=", arguments, xpath);
    } else {
        throw new XPathBuilderException("Unknown expr node " + formulaNode);
    }
}
Also used : FormulaFunction(org.activityinfo.model.formula.functions.FormulaFunction) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) ArrayList(java.util.ArrayList) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue)

Example 3 with FieldValue

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

the class DateComponentFunction method apply.

@Override
public final FieldValue apply(List<FieldValue> arguments) {
    checkArity(arguments, 1);
    FieldValue argument = arguments.get(0);
    if (!(argument instanceof LocalDate)) {
        throw new FormulaSyntaxException("Expected date argument");
    }
    LocalDate date = (LocalDate) argument;
    return new Quantity(apply(date));
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 4 with FieldValue

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

the class SimpleConditionParser method parseBinary.

private static SimpleCondition parseBinary(FunctionCallNode call) {
    ResourceId fieldId = parseFieldId(call.getArgument(0));
    SimpleOperator op = parseOp(call.getFunction());
    FieldValue fieldValue = parseFieldValue(call.getArgument(1));
    return new SimpleCondition(fieldId, op, fieldValue);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FieldValue(org.activityinfo.model.type.FieldValue)

Example 5 with FieldValue

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

the class StatFunction method apply.

@Override
public final FieldValue apply(List<FieldValue> arguments) {
    double[] argumentValues = new double[arguments.size()];
    for (int i = 0; i < arguments.size(); i++) {
        FieldValue argument = arguments.get(i);
        if (argument instanceof Quantity) {
            Quantity quantity = (Quantity) argument;
            argumentValues[i] = quantity.getValue();
        } else {
            argumentValues[i] = Double.NaN;
        }
    }
    double result = compute(argumentValues, 0, argumentValues.length);
    return new Quantity(result);
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue)

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