Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use first repeat instance(s) for jr:choice-name choices #758

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 30 additions & 23 deletions src/main/java/org/javarosa/core/model/FormDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@

package org.javarosa.core.model;

import static java.util.Collections.emptyList;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm likely the one who messed up this order previously. 😬 My IDE should now enforce the order that's mostly consistent across existing files.


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.javarosa.core.log.WrappedException;
import org.javarosa.core.model.TriggerableDag.EventNotifierAccessor;
import org.javarosa.core.model.actions.ActionController;
Expand Down Expand Up @@ -67,26 +86,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.emptyList;

/**
* Definition of a form. This has some meta data about the form definition and a
* collection of groups together with question branching or skipping rules.
Expand Down Expand Up @@ -866,10 +865,18 @@ public Object eval(Object[] args, EvaluationContext ec) {
// the situations where that context would be used don't make sense for trying to reverse a
// select value back to a label in an unrelated expression
if (ref.isAmbiguous()) {
// SurveyCTO: We need a absolute "ref" to populate the dynamic choices,
// like we do when we populate those at FormEntryPrompt (line 251).
// The "ref" here is ambiguous, so we need to make it concrete first.
// ref, the reference used to specify where choices are defined, could be an absolute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This IFunctionHandler implementation should be moved out to another file now we're touching it again.

// reference to a repeat nodeset. In that case, we need to convert that nodeset ref
// into a node ref. First try to contextualize based on the current repeat in case the
// choice-name call is from inside a repeat. Then use position 1 for any repeats that
// weren't contextualized (this is what a standards-compliant XPath engine would always do).
ref = ref.contextualize(ec.getContextRef());

for (int i = 0; i < ref.size(); i++) {
if (ref.getMultiplicity(i) == TreeReference.INDEX_UNBOUND) {
ref.setMultiplicity(i, TreeReference.DEFAULT_MULTIPLICITY);
}
}
}
choices = itemset.getChoices(f, ref);
} else { // static choices
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/javarosa/core/model/ChoiceNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import static org.javarosa.core.util.XFormsElement.item;
import static org.javarosa.core.util.XFormsElement.mainInstance;
import static org.javarosa.core.util.XFormsElement.model;
import static org.javarosa.core.util.XFormsElement.repeat;
import static org.javarosa.core.util.XFormsElement.select1;
import static org.javarosa.core.util.XFormsElement.select1Dynamic;
import static org.javarosa.core.util.XFormsElement.t;
import static org.javarosa.core.util.XFormsElement.title;
import static org.javarosa.test.utils.ResourcePathHelper.r;
Expand Down Expand Up @@ -117,4 +119,75 @@ public class ChoiceNameTest {
scenario.answer("/data/city", "grenoble");
assertThat(scenario.answerOf("/data/city_name"), is(stringAnswer("Grenoble")));
}

@Test public void choiceNameCallWithIndexedRepeatAndStaticChoices_worksWithMultipleRepeats() throws IOException, XFormParser.ParseException {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not affected by the code change. It's there to document behavior that has always worked.

Scenario scenario = Scenario.init("Static choices in repeat", html(
head(
title("Static choices in repeat"),
model(
mainInstance(t("data id=\"static-choices-repeat\"",
t("thing",
t("choice")),
t("choice1_label")
)),

bind("/data/thing/choice").type("string"),
bind("/data/choice1_label").type("string").calculate("jr:choice-name(indexed-repeat(/data/thing/choice, /data/thing, 1),'/data/thing/choice')")
)
),
body(
repeat("/data/thing",
select1("/data/thing/choice",
item("choice1", "Choice1"),
item("choice2", "Choice2"))
))));

scenario.next();
scenario.next();
scenario.next();
scenario.createNewRepeat();

scenario.answer("/data/thing[1]/choice", "choice1");
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1")));

scenario.answer("/data/thing[2]/choice", "choice2");
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1")));
}

@Test public void choiceNameCallWithIndexedRepeatAndDynamicChoices_worksWithMultipleRepeats() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Dynamic choices in repeat", html(
head(
title("Dynamic choices in repeat"),
model(
mainInstance(t("data id=\"dynamic-choices-repeat\"",
t("thing",
t("choice")),
t("choice1_label")
)),

t("instance id=\"choices\"",
t("root",
item("choice1", "Choice1"),
item("choice2", "Choice2"))
),
bind("/data/thing/choice").type("string"),
bind("/data/choice1_label").type("string").calculate("jr:choice-name(indexed-repeat(/data/thing/choice, /data/thing, 1),'/data/thing/choice')")
)
),
body(
repeat("/data/thing",
select1Dynamic("/data/thing/choice", "instance('choices')/root/item"))
)));

scenario.next();
scenario.next();
scenario.next();
scenario.createNewRepeat();

scenario.answer("/data/thing[1]/choice","choice1");
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1")));

scenario.answer("/data/thing[2]/choice", "choice2");
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1")));
}
}