Search in sources :

Example 1 with IConditionExpr

use of org.javarosa.core.model.condition.IConditionExpr in project javarosa by opendatakit.

the class XFormsModule method registerModule.

public void registerModule() {
    String[] classes = { "org.javarosa.model.xform.XPathReference", "org.javarosa.xpath.XPathConditional" };
    PrototypeManager.registerPrototypes(classes);
    PrototypeManager.registerPrototypes(XPathParseTool.xpathClasses);
    RestoreUtils.xfFact = new IXFormyFactory() {

        public TreeReference ref(String refStr) {
            return FormInstance.unpackReference(new XPathReference(refStr));
        }

        public IDataPayload serializeInstance(FormInstance dm) {
            try {
                return (new XFormSerializingVisitor()).createSerializedPayload(dm);
            } catch (IOException e) {
                return null;
            }
        }

        public FormInstance parseRestore(byte[] data, Class restorableType) {
            return XFormParser.restoreDataModel(data, restorableType);
        }

        public IAnswerData parseData(String textVal, int dataType, TreeReference ref, FormDef f) {
            return XFormAnswerDataParser.getAnswerData(textVal, dataType, XFormParser.ghettoGetQuestionDef(dataType, f, ref));
        }

        public String serializeData(IAnswerData data) {
            return (String) (new XFormAnswerDataSerializer().serializeAnswerData(data));
        }

        public IConditionExpr refToPathExpr(TreeReference ref) {
            return new XPathConditional(XPathPathExpr.fromRef(ref));
        }
    };
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) IXFormyFactory(org.javarosa.core.model.util.restorable.IXFormyFactory) IOException(java.io.IOException) XFormAnswerDataSerializer(org.javarosa.xform.util.XFormAnswerDataSerializer) TreeReference(org.javarosa.core.model.instance.TreeReference) FormDef(org.javarosa.core.model.FormDef) FormInstance(org.javarosa.core.model.instance.FormInstance) XPathConditional(org.javarosa.xpath.XPathConditional) IDataPayload(org.javarosa.core.services.transport.payload.IDataPayload)

Example 2 with IConditionExpr

use of org.javarosa.core.model.condition.IConditionExpr in project javarosa by opendatakit.

the class FormDef method readExternal.

/**
 * Reads the form definition object from the supplied stream.
 * <p/>
 * Requires that the instance has been set to a prototype of the instance
 * that should be used for deserialization.
 *
 * @param dis - the stream to read from.
 * @throws IOException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
@Override
public void readExternal(DataInputStream dis, PrototypeFactory pf) throws IOException, DeserializationException {
    setID(ExtUtil.readInt(dis));
    setName(ExtUtil.nullIfEmpty(ExtUtil.readString(dis)));
    setTitle((String) ExtUtil.read(dis, new ExtWrapNullable(String.class), pf));
    setChildren((List<IFormElement>) ExtUtil.read(dis, new ExtWrapListPoly(), pf));
    setInstance((FormInstance) ExtUtil.read(dis, FormInstance.class, pf));
    setLocalizer((Localizer) ExtUtil.read(dis, new ExtWrapNullable(Localizer.class), pf));
    List<Condition> vcond = (List<Condition>) ExtUtil.read(dis, new ExtWrapList(Condition.class), pf);
    for (Condition condition : vcond) {
        addTriggerable(condition);
    }
    List<Recalculate> vcalc = (List<Recalculate>) ExtUtil.read(dis, new ExtWrapList(Recalculate.class), pf);
    for (Recalculate recalculate : vcalc) {
        addTriggerable(recalculate);
    }
    finalizeTriggerables();
    outputFragments = (List<IConditionExpr>) ExtUtil.read(dis, new ExtWrapListPoly(), pf);
    submissionProfiles = (HashMap<String, SubmissionProfile>) ExtUtil.read(dis, new ExtWrapMap(String.class, SubmissionProfile.class));
    formInstances = (HashMap<String, DataInstance>) ExtUtil.read(dis, new ExtWrapMap(String.class, new ExtWrapTagged()), pf);
    eventListeners = (HashMap<String, List<Action>>) ExtUtil.read(dis, new ExtWrapMap(String.class, new ExtWrapListPoly()), pf);
    extensions = (List<XFormExtension>) ExtUtil.read(dis, new ExtWrapListPoly(), pf);
    resetEvaluationContext();
}
Also used : Condition(org.javarosa.core.model.condition.Condition) IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) DataInstance(org.javarosa.core.model.instance.DataInstance) Recalculate(org.javarosa.core.model.condition.Recalculate) ExtWrapTagged(org.javarosa.core.util.externalizable.ExtWrapTagged) ExtWrapMap(org.javarosa.core.util.externalizable.ExtWrapMap) Localizer(org.javarosa.core.services.locale.Localizer) ExtWrapListPoly(org.javarosa.core.util.externalizable.ExtWrapListPoly) List(java.util.List) ArrayList(java.util.ArrayList) ExtWrapList(org.javarosa.core.util.externalizable.ExtWrapList) ExtWrapList(org.javarosa.core.util.externalizable.ExtWrapList) ExtWrapNullable(org.javarosa.core.util.externalizable.ExtWrapNullable)

Example 3 with IConditionExpr

use of org.javarosa.core.model.condition.IConditionExpr in project javarosa by opendatakit.

the class FormDef method fillTemplateString.

public String fillTemplateString(String template, TreeReference contextRef, HashMap<String, ?> variables) {
    HashMap<String, String> args = new HashMap<String, String>();
    int depth = 0;
    List<String> outstandingArgs = Localizer.getArgs(template);
    while (outstandingArgs.size() > 0) {
        for (String argName : outstandingArgs) {
            if (!args.containsKey(argName)) {
                int ix = -1;
                try {
                    ix = Integer.parseInt(argName);
                } catch (NumberFormatException nfe) {
                    Std.err.println("Warning: expect arguments to be numeric [" + argName + "]");
                }
                if (ix < 0 || ix >= outputFragments.size())
                    continue;
                IConditionExpr expr = outputFragments.get(ix);
                EvaluationContext ec = new EvaluationContext(exprEvalContext, contextRef);
                ec.setOriginalContext(contextRef);
                ec.setVariables(variables);
                String value = expr.evalReadable(this.getMainInstance(), ec);
                args.put(argName, value);
            }
        }
        template = Localizer.processArguments(template, args);
        outstandingArgs = Localizer.getArgs(template);
        depth++;
        if (depth >= TEMPLATING_RECURSION_LIMIT) {
            throw new RuntimeException("Dependency cycle in <output>s; recursion limit exceeded!!");
        }
    }
    return template;
}
Also used : IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) HashMap(java.util.HashMap) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Constraint(org.javarosa.core.model.condition.Constraint)

Example 4 with IConditionExpr

use of org.javarosa.core.model.condition.IConditionExpr in project javarosa by opendatakit.

the class IDag method getConditionExpressionForTrueAction.

/**
 * Pull this in from FormOverview so that we can make fields private.
 *
 * @param instanceNode
 * @param action
 * @return
 */
public final IConditionExpr getConditionExpressionForTrueAction(FormInstance mainInstance, TreeElement instanceNode, int action) {
    IConditionExpr expr = null;
    for (int i = 0; i < triggerablesDAG.size() && expr == null; i++) {
        // Clayton Sims - Jun 1, 2009 : Not sure how legitimate this
        // cast is. It might work now, but break later.
        // Clayton Sims - Jun 24, 2009 : Yeah, that change broke things.
        // For now, we won't bother to print out anything that isn't
        // a condition.
        QuickTriggerable qt = triggerablesDAG.get(i);
        if (qt.t instanceof Condition) {
            Condition c = (Condition) qt.t;
            if (c.trueAction == action) {
                List<TreeReference> targets = c.getTargets();
                for (int j = 0; j < targets.size() && expr == null; j++) {
                    TreeReference target = targets.get(j);
                    TreeReference tr = (TreeReference) (new XPathReference(target)).getReference();
                    TreeElement element = mainInstance.getTemplatePath(tr);
                    if (instanceNode == element) {
                        expr = c.getExpr();
                    }
                }
            }
        }
    }
    return expr;
}
Also used : Condition(org.javarosa.core.model.condition.Condition) IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathReference(org.javarosa.model.xform.XPathReference) TreeElement(org.javarosa.core.model.instance.TreeElement)

Aggregations

IConditionExpr (org.javarosa.core.model.condition.IConditionExpr)4 Condition (org.javarosa.core.model.condition.Condition)2 TreeReference (org.javarosa.core.model.instance.TreeReference)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 FormDef (org.javarosa.core.model.FormDef)1 Constraint (org.javarosa.core.model.condition.Constraint)1 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)1 Recalculate (org.javarosa.core.model.condition.Recalculate)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 DataInstance (org.javarosa.core.model.instance.DataInstance)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 IXFormyFactory (org.javarosa.core.model.util.restorable.IXFormyFactory)1 Localizer (org.javarosa.core.services.locale.Localizer)1 IDataPayload (org.javarosa.core.services.transport.payload.IDataPayload)1 ExtWrapList (org.javarosa.core.util.externalizable.ExtWrapList)1 ExtWrapListPoly (org.javarosa.core.util.externalizable.ExtWrapListPoly)1