Skip to content

Commit

Permalink
Option 2
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Sep 3, 2021
1 parent 2229e0c commit 0cba44e
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/main/java/org/javarosa/core/model/ItemsetBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ItemsetBinding implements Externalizable, Localizable {
// Values needed to determine whether the cached list should be expired (not serialized)
private Map<TreeReference, IAnswerData> cachedTriggerValues;
private Long cachedRandomizeSeed;
private DataInstance formInstance;
private List<TreeReference> filteredItemReferences;

/**
* note that storing both the ref and expr for everything is kind of redundant, but we're forced
Expand All @@ -55,8 +57,8 @@ public class ItemsetBinding implements Externalizable, Localizable {
public TreeReference nodesetRef; //absolute ref of itemset source nodes
public IConditionExpr nodesetExpr; //path expression for source nodes; may be relative, may contain predicates
public TreeReference contextRef; //context ref for nodesetExpr; ref of the control parent (group/formdef) of itemset question
//note: this is only here because its currently impossible to both (a) get a form control's parent, and (b)
//convert expressions into refs while preserving predicates. once these are fixed, this field can go away
//note: this is only here because its currently impossible to both (a) get a form control's parent, and (b)
//convert expressions into refs while preserving predicates. once these are fixed, this field can go away

public TreeReference labelRef; //absolute ref of label
public IConditionExpr labelExpr; //path expression for label; may be relative, no predicates
Expand Down Expand Up @@ -92,12 +94,13 @@ public List<SelectChoice> getChoices(FormDef formDef, TreeReference curQRef) {
// Return cached list if possible
if (cachedFilteredChoiceList != null && allTriggerRefsBound && Objects.equals(currentTriggerValues, cachedTriggerValues)
&& Objects.equals(currentRandomizeSeed, cachedRandomizeSeed)) {

updateQuestionAnswerInModel(formDef, curQRef, getSelectChoicesForAnswer(formDef, curQRef, formInstance, filteredItemReferences));
return randomize && cachedRandomizeSeed == null ? shuffle(cachedFilteredChoiceList) : cachedFilteredChoiceList;
}

formDef.getEventNotifier().publishEvent(new Event("Dynamic choices", new EvaluationResult(curQRef, null)));

DataInstance formInstance;
if (nodesetRef.getInstanceName() != null) { // the itemset is defined in a secondary instance
formInstance = formDef.getNonMainInstance(nodesetRef.getInstanceName());
if (formInstance == null) {
Expand All @@ -107,26 +110,20 @@ public List<SelectChoice> getChoices(FormDef formDef, TreeReference curQRef) {
formInstance = formDef.getMainInstance();
}

List<TreeReference> filteredItemReferences = nodesetExpr.evalNodeset(formDef.getMainInstance(),
filteredItemReferences = nodesetExpr.evalNodeset(formDef.getMainInstance(),
new EvaluationContext(formDef.getEvaluationContext(), contextRef.contextualize(curQRef)));

if (filteredItemReferences == null) {
throw new XPathException("Could not find references depended on by" + nodesetRef.getInstanceName());
}

Map<String, SelectChoice> selectChoicesForAnswer = initializeAnswerMap(formDef, curQRef);

List<SelectChoice> choices = new ArrayList<>();
for (int i = 0; i < filteredItemReferences.size(); i++) {
SelectChoice choice = getChoiceForTreeReference(formDef, formInstance, i, filteredItemReferences.get(i));
choices.add(choice);
if (selectChoicesForAnswer != null && selectChoicesForAnswer.containsKey(choice.getValue())) {
// Keys with values that don't get set here will have null values and must be filtered out of the answer.
selectChoicesForAnswer.put(choice.getValue(), choice);
}
}

updateQuestionAnswerInModel(formDef, curQRef, selectChoicesForAnswer);
updateQuestionAnswerInModel(formDef, curQRef, getSelectChoicesForAnswer(formDef, curQRef, formInstance, filteredItemReferences));

cachedFilteredChoiceList = randomize ? shuffle(choices, currentRandomizeSeed) : choices;

Expand All @@ -152,6 +149,21 @@ public List<SelectChoice> getChoices(FormDef formDef, TreeReference curQRef) {
return cachedFilteredChoiceList;
}

private Map<String, SelectChoice> getSelectChoicesForAnswer(FormDef formDef, TreeReference curQRef, DataInstance formInstance, List<TreeReference> filteredItemReferences) {
Map<String, SelectChoice> selectChoicesForAnswer = initializeAnswerMap(formDef, curQRef);

List<SelectChoice> choices = new ArrayList<>();
for (int i = 0; i < filteredItemReferences.size(); i++) {
SelectChoice choice = getChoiceForTreeReference(formDef, formInstance, i, filteredItemReferences.get(i));
choices.add(choice);
if (selectChoicesForAnswer != null && selectChoicesForAnswer.containsKey(choice.getValue())) {
// Keys with values that don't get set here will have null values and must be filtered out of the answer.
selectChoicesForAnswer.put(choice.getValue(), choice);
}
}
return selectChoicesForAnswer;
}

/**
* Returns a map:
* - keys: the references that are triggers for the nodeset expression
Expand Down

0 comments on commit 0cba44e

Please sign in to comment.