use of org.javarosa.core.model.data.helper.Selection 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.helper.Selection in project javarosa by opendatakit.
the class SelectMultiData method cast.
@Override
public SelectMultiData cast(UncastData data) throws IllegalArgumentException {
List<String> choices = DateUtils.split(data.value, " ", true);
List<Selection> v = new ArrayList<Selection>(choices.size());
for (String s : choices) {
v.add(new Selection(s));
}
return new SelectMultiData(v);
}
use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.
the class SelectMultiDataTests method assertVectorIdentity.
private void assertVectorIdentity(String messageHeader, List<Selection> v, Selection[] a) {
assertEquals(messageHeader + "SelectMultiData's internal representation was violated. Vector size changed.", v.size(), a.length);
for (int i = 0; i < v.size(); ++i) {
Selection internalValue = v.get(i);
Selection copyValue = a[i];
assertEquals(messageHeader + "SelectMultiData's internal representation was violated. Element " + i + "changed.", internalValue, copyValue);
}
}
use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.
the class SelectMultiDataTests method testVectorImmutability.
public void testVectorImmutability() {
SelectMultiData data = new SelectMultiData(firstTwo);
Selection[] copy = new Selection[firstTwo.size()];
firstTwo.toArray(copy);
firstTwo.set(0, two);
firstTwo.remove(1);
List<Selection> internal = (List<Selection>) data.getValue();
assertVectorIdentity("External Reference: ", internal, copy);
data.setValue(lastTwo);
List<Selection> start = (List<Selection>) data.getValue();
Selection[] external = new Selection[start.size()];
start.toArray(external);
start.remove(1);
start.set(0, one);
assertVectorIdentity("Internal Reference: ", (List<Selection>) data.getValue(), external);
}
use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.
the class SelectMultiDataTests method setUp.
/* (non-Javadoc)
* @see j2meunit.framework.TestCase#setUp()
*/
public void setUp() throws Exception {
super.setUp();
question = new QuestionDef();
for (int i = 0; i < 4; i++) {
question.addSelectChoice(new SelectChoice("", "Selection" + i, "Selection " + i, false));
}
one = new Selection("Selection 1");
one.attachChoice(question);
two = new Selection("Selection 2");
two.attachChoice(question);
three = new Selection("Selection 3");
three.attachChoice(question);
firstTwo = new ArrayList<Selection>();
firstTwo.add(one);
firstTwo.add(two);
lastTwo = new ArrayList<Selection>();
lastTwo.add(two);
lastTwo.add(three);
invalid = new ArrayList<Object>();
invalid.add(three);
invalid.add(new Integer(12));
invalid.add(one);
}
Aggregations