Search in sources :

Example 11 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.

the class TreeElement method populateTemplate.

// this method is for copying in the answers to an itemset. the template node of the destination
// is used for overall structure (including data types), and the itemset source node is used for
// raw data. note that data may be coerced across types, which may result in type conversion error
// very similar in structure to populate()
public void populateTemplate(TreeElement incoming, FormDef f) {
    if (this.isLeaf()) {
        IAnswerData value = incoming.getValue();
        if (value == null) {
            this.setValue(null);
        } else {
            Class classType = CompactInstanceWrapper.classForDataType(this.dataType);
            if (classType == null) {
                throw new RuntimeException("data type [" + value.getClass().getName() + "] not supported inside itemset");
            } else if (classType.isAssignableFrom(value.getClass()) && !(value instanceof SelectOneData || value instanceof SelectMultiData)) {
                this.setValue(value);
            } else {
                String textVal = RestoreUtils.xfFact.serializeData(value);
                IAnswerData typedVal = RestoreUtils.xfFact.parseData(textVal, this.dataType, this.getRef(), f);
                this.setValue(typedVal);
            }
        }
    } else {
        for (int i = 0; i < this.getNumChildren(); i++) {
            TreeElement child = this.getChildAt(i);
            List<TreeElement> newChildren = incoming.getChildrenWithName(child.getName());
            if (child.getMaskVar(MASK_REPEATABLE)) {
                for (int k = 0; k < newChildren.size(); k++) {
                    TreeElement template = f.getMainInstance().getTemplate(child.getRef());
                    TreeElement newChild = template.deepCopy(false);
                    newChild.setMult(k);
                    this.children.add(i + k + 1, newChild);
                    newChild.populateTemplate(newChildren.get(k), f);
                }
                i += newChildren.size();
            } else {
                child.populateTemplate(newChildren.get(0), f);
            }
        }
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) Constraint(org.javarosa.core.model.condition.Constraint)

Example 12 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.

the class QuestionPreloader method saveProperty.

private void saveProperty(String propName, TreeElement node) {
    IAnswerData answer = node.getValue();
    String value = (answer == null ? null : answer.getDisplayText());
    if (propName != null && propName.length() > 0 && value != null && value.length() > 0)
        PropertyManager._().setProperty(propName, value);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData)

Example 13 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.

the class QuestionDataElementTests method testGetValue.

public void testGetValue() {
    IAnswerData data = stringElement.getValue();
    assertEquals("Question Data Element did not return the correct value", data, stringData);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData)

Example 14 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData 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 15 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.

the class FormEntryPrompt method getAnswerText.

public String getAnswerText() {
    IAnswerData data = this.getAnswerValue();
    if (data == null)
        return null;
    else {
        String text;
        // and multi-selects.
        if (data instanceof SelectOneData) {
            text = this.getSelectItemText((Selection) data.getValue());
        } else if (data instanceof SelectMultiData) {
            StringBuilder b = new StringBuilder();
            List<Selection> values = (List<Selection>) data.getValue();
            for (Selection value : values) {
                b.append(this.getSelectItemText(value)).append(" ");
            }
            text = b.toString();
        } else {
            text = data.getDisplayText();
        }
        if (getControlType() == Constants.CONTROL_SECRET) {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < text.length(); ++i) {
                b.append("*");
            }
            text = b.toString();
        }
        return text;
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) List(java.util.List) ConstraintHint(org.javarosa.core.model.condition.pivot.ConstraintHint) Constraint(org.javarosa.core.model.condition.Constraint)

Aggregations

IAnswerData (org.javarosa.core.model.data.IAnswerData)31 Test (org.junit.Test)8 Constraint (org.javarosa.core.model.condition.Constraint)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)5 ArrayList (java.util.ArrayList)4 SelectOneData (org.javarosa.core.model.data.SelectOneData)4 Selection (org.javarosa.core.model.data.helper.Selection)4 TreeElement (org.javarosa.core.model.instance.TreeElement)4 List (java.util.List)3 FormDef (org.javarosa.core.model.FormDef)3 StringData (org.javarosa.core.model.data.StringData)3 BigDecimal (java.math.BigDecimal)2 FormIndex (org.javarosa.core.model.FormIndex)2 SelectChoice (org.javarosa.core.model.SelectChoice)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 ConstraintHint (org.javarosa.core.model.condition.pivot.ConstraintHint)2 DateData (org.javarosa.core.model.data.DateData)2 DateTimeData (org.javarosa.core.model.data.DateTimeData)2 DecimalData (org.javarosa.core.model.data.DecimalData)2