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