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

Handle empty saveto questions #694

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public interface AbstractTreeElement<T extends AbstractTreeElement> {
//Support?
public abstract AbstractTreeElement getParent();

@Nullable
public abstract IAnswerData getValue();

public abstract int getDataType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import kotlin.Pair;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.IDataReference;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.entities.internal.Entities;
import org.javarosa.entities.internal.EntityFormParser;
Expand Down Expand Up @@ -31,8 +32,13 @@ public void processForm(FormEntryModel formEntryModel) {
if (dataset != null) {
List<Pair<String, String>> fields = saveTos.stream().map(saveTo -> {
IDataReference reference = saveTo.getFirst();
String answer = mainInstance.resolveReference(reference).getValue().getDisplayText();
return new Pair<>(saveTo.getSecond(), answer);
IAnswerData answerData = mainInstance.resolveReference(reference).getValue();

if (answerData != null) {
Copy link
Member

Choose a reason for hiding this comment

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

Arg, null.

return new Pair<>(saveTo.getSecond(), answerData.getDisplayText());
} else {
return new Pair<>(saveTo.getSecond(), "");
}
}).collect(Collectors.toList());

formEntryModel.getExtras().put(new Entities(asList(new Entity(dataset, fields))));
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/javarosa/entities/EntitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,41 @@ public void fillingFormWithSelectSaveTo_andWithCreate_savesValuesCorrectlyToEnti
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("team", "kendall"))));
}

@Test
public void whenSaveToQuestionIsNotAnswered_entityPropertyIsEmptyString() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Create entity form", XFormsElement.html(
asList(
new Pair<>("entities", "http://www.opendatakit.org/xforms/entities")
),
head(
title("Create entity form"),
model(asList(new Pair<>("entities:entities-version", EntityFormParseProcessor.SUPPORTED_VERSION + ".1")),
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
)
)
),
bind("/data/name").type("string").withAttribute("entities", "saveto", "name")
)
),
body(
input("/data/name")
)
));

scenario.getFormEntryController().addPostProcessor(new EntityFormFinalizationProcessor());
scenario.finalizeInstance();

List<Entity> entities = scenario.getFormEntryController().getModel().getExtras().get(Entities.class).getEntities();
assertThat(entities.size(), equalTo(1));
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", ""))));
}

@Test
public void savetoIsRemovedFromBindAttributesForClients() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Create entity form", XFormsElement.html(
Expand Down