use of org.javarosa.core.model.data.SelectOneData in project javarosa by opendatakit.
the class FormDef method copyItemsetAnswer.
public void copyItemsetAnswer(QuestionDef q, TreeElement targetNode, IAnswerData data, boolean midSurvey) throws InvalidReferenceException {
ItemsetBinding itemset = q.getDynamicChoices();
TreeReference targetRef = targetNode.getRef();
TreeReference destRef = itemset.getDestRef().contextualize(targetRef);
List<Selection> selections = null;
if (data instanceof SelectMultiData) {
selections = (List<Selection>) data.getValue();
} else if (data instanceof SelectOneData) {
selections = new ArrayList<Selection>(1);
selections.add((Selection) data.getValue());
}
List<String> selectedValues;
if (itemset.valueRef != null) {
selectedValues = new ArrayList<String>(selections.size());
for (Selection selection : selections) {
selectedValues.add(selection.choice.getValue());
}
} else {
selectedValues = new ArrayList<String>(0);
}
// delete existing dest nodes that are not in the answer selection
HashMap<String, TreeElement> existingValues = new HashMap<String, TreeElement>();
List<TreeReference> existingNodes = exprEvalContext.expandReference(destRef);
for (TreeReference existingNode : existingNodes) {
TreeElement node = getMainInstance().resolveReference(existingNode);
if (itemset.valueRef != null) {
String value = itemset.getRelativeValue().evalReadable(this.getMainInstance(), new EvaluationContext(exprEvalContext, node.getRef()));
if (selectedValues.contains(value)) {
// cache node if in selection
existingValues.put(value, node);
// and already exists
}
}
// delete from target
targetNode.removeChild(node);
}
// copy in nodes for new answer; preserve ordering in answer
for (int i = 0; i < selections.size(); i++) {
Selection s = selections.get(i);
SelectChoice ch = s.choice;
TreeElement cachedNode = null;
if (itemset.valueRef != null) {
String value = ch.getValue();
if (existingValues.containsKey(value)) {
cachedNode = existingValues.get(value);
}
}
if (cachedNode != null) {
cachedNode.setMult(i);
targetNode.addChild(cachedNode);
} else {
getMainInstance().copyItemsetNode(ch.copyNode, destRef, this);
}
}
dagImpl.copyItemsetAnswer(getMainInstance(), getEvaluationContext(), destRef, targetNode, midSurvey);
}
use of org.javarosa.core.model.data.SelectOneData in project javarosa by opendatakit.
the class Recalculate method wrapData.
// droos 1/29/10: we need to come up with a consistent rule for whether the resulting data is determined
// by the type of the instance node, or the type of the expression result. right now it's a mix and a mess
// note a caveat with going solely by instance node type is that untyped nodes default to string!
// for now, these are the rules:
// if node type == bool, convert to boolean (for numbers, zero = f, non-zero = t; empty string = f, all other datatypes -> error)
// if numeric data, convert to int if node type is int OR data is an integer; else convert to double
// if string data or date data, keep as is
// if NaN or empty string, null
/**
* convert the data object returned by the xpath expression into an IAnswerData suitable for
* storage in the FormInstance
*/
public static IAnswerData wrapData(Object val, int dataType) {
if ((val instanceof String && ((String) val).length() == 0) || (val instanceof Double && ((Double) val).isNaN())) {
return null;
}
if (Constants.DATATYPE_BOOLEAN == dataType || val instanceof Boolean) {
// ctsims: We should really be using the boolean datatype for real, it's
// necessary for backend calculations and XSD compliance
boolean b;
if (val instanceof Boolean) {
b = (Boolean) val;
} else if (val instanceof Double) {
Double d = (Double) val;
b = Math.abs(d) > 1.0e-12 && !Double.isNaN(d);
} else if (val instanceof String) {
String s = (String) val;
b = s.length() > 0;
} else {
throw new RuntimeException("unrecognized data representation while trying to convert to BOOLEAN");
}
return new BooleanData(b);
} else if (val instanceof Double) {
double d = (Double) val;
long l = (long) d;
boolean isIntegral = Math.abs(d - l) < 1.0e-9;
if (Constants.DATATYPE_INTEGER == dataType || (isIntegral && (Integer.MAX_VALUE >= l) && (Integer.MIN_VALUE <= l))) {
return new IntegerData((int) d);
} else if (Constants.DATATYPE_LONG == dataType || isIntegral) {
return new LongData((long) d);
} else {
return new DecimalData(d);
}
} else if (dataType == Constants.DATATYPE_GEOPOINT) {
return new GeoPointData().cast(new UncastData(String.valueOf(val)));
} else if (dataType == Constants.DATATYPE_GEOSHAPE) {
return new GeoShapeData().cast(new UncastData(String.valueOf(val)));
} else if (dataType == Constants.DATATYPE_GEOTRACE) {
return new GeoTraceData().cast(new UncastData(String.valueOf(val)));
} else if (dataType == Constants.DATATYPE_CHOICE) {
return new SelectOneData().cast(new UncastData(String.valueOf(val)));
} else if (dataType == Constants.DATATYPE_CHOICE_LIST) {
return new SelectMultiData().cast(new UncastData(String.valueOf(val)));
} else if (val instanceof String) {
return new StringData((String) val);
} else if (val instanceof Date) {
if (dataType == Constants.DATATYPE_TIME)
return new TimeData((Date) val);
if (dataType == Constants.DATATYPE_DATE)
return new DateData((Date) val);
return new DateTimeData((Date) val);
} else {
throw new RuntimeException("unrecognized data type in 'calculate' expression: " + val.getClass().getName());
}
}
use of org.javarosa.core.model.data.SelectOneData in project javarosa by opendatakit.
the class SelectOneDataTests method testGetData.
public void testGetData() {
SelectOneData data = new SelectOneData(one);
assertEquals("SelectOneData's getValue returned an incorrect SelectOne", data.getValue(), one);
}
use of org.javarosa.core.model.data.SelectOneData in project javarosa by opendatakit.
the class SelectMultiDataTests method testGetData.
public void testGetData() {
SelectOneData data = new SelectOneData(one);
assertEquals("SelectOneData's getValue returned an incorrect SelectOne", data.getValue(), one);
}
Aggregations