Skip to content

Commit

Permalink
Allow entity update to not have label
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Feb 28, 2024
1 parent 64abd75 commit 38d57e8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/javarosa/entities/Entity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.javarosa.entities;

import kotlin.Pair;
import org.jetbrains.annotations.Nullable;

import java.util.List;

Expand All @@ -9,10 +10,12 @@ public class Entity {
public final String dataset;
public final List<Pair<String, String>> properties;
public final String id;

@Nullable
public final String label;
public Integer version;

public Entity(String dataset, String id, String label, Integer version, List<Pair<String, String>> properties) {
public Entity(String dataset, String id, @Nullable String label, Integer version, List<Pair<String, String>> properties) {
this.dataset = dataset;
this.id = id;
this.label = label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public static String parseDataset(TreeElement entity) {
return entity.getAttributeValue(null, "dataset");
}

@Nullable
public static String parseLabel(TreeElement entity) {
TreeElement labelElement = entity.getFirstChild("label");

if (labelElement != null) {
return (String) labelElement.getValue().getValue();
} else {
return "";
return null;
}
}

Expand Down
43 changes: 42 additions & 1 deletion src/test/java/org/javarosa/entities/EntitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void fillingFormWithUpdate_makesEntityAvailable() throws IOException, XFo
new Pair<>("entities", "https://www.opendatakit.org/xforms/entities")
),
head(
title("Uodate entity form"),
title("Update entity form"),
model(asList(new Pair<>("entities:entities-version", "2023.1.0")),
mainInstance(
t("data id=\"update-entity-form\"",
Expand Down Expand Up @@ -170,6 +170,47 @@ public void fillingFormWithUpdate_makesEntityAvailable() throws IOException, XFo
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
}

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

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

scenario.next();
scenario.answer("Tom Wambsgans");

scenario.finalizeInstance();
List<Entity> entities = scenario.getFormEntryController().getModel().getExtras().get(Entities.class).getEntities();
assertThat(entities.size(), equalTo(1));
assertThat(entities.get(0).dataset, equalTo("people"));
assertThat(entities.get(0).id, equalTo("123"));
assertThat(entities.get(0).label, equalTo(null));
assertThat(entities.get(0).version, equalTo(2));
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
}

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

0 comments on commit 38d57e8

Please sign in to comment.