Skip to content

Commit

Permalink
Merge pull request #755 from seadowg/entity-actions
Browse files Browse the repository at this point in the history
Expose action with entity
  • Loading branch information
seadowg authored Apr 4, 2024
2 parents 6585c0a + 97357c3 commit 5ae6894
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/javarosa/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public class Entity {

@Nullable
public final String label;
public Integer version;
public final Integer version;
public final EntityAction action;

public Entity(String dataset, String id, @Nullable String label, Integer version, List<Pair<String, String>> properties) {
public Entity(EntityAction action, String dataset, String id, @Nullable String label, Integer version, List<Pair<String, String>> properties) {
this.dataset = dataset;
this.id = id;
this.label = label;
this.version = version;
this.properties = properties;
this.action = action;
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/javarosa/entities/EntityAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.javarosa.entities;

public enum EntityAction {
CREATE,
UPDATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ public void processForm(FormEntryModel formEntryModel) {

TreeElement entityElement = EntityFormParser.getEntityElement(mainInstance);
if (entityElement != null) {
EntityFormParser.EntityAction action = EntityFormParser.parseAction(entityElement);
EntityAction action = EntityFormParser.parseAction(entityElement);
String dataset = EntityFormParser.parseDataset(entityElement);

if (action == EntityFormParser.EntityAction.CREATE) {
Entity entity = createEntity(entityElement, 1, dataset, saveTos, mainInstance);
if (action == EntityAction.CREATE) {
Entity entity = createEntity(entityElement, 1, dataset, saveTos, mainInstance, action);
formEntryModel.getExtras().put(new Entities(asList(entity)));
} else if (action == EntityFormParser.EntityAction.UPDATE){
} else if (action == EntityAction.UPDATE){
int baseVersion = EntityFormParser.parseBaseVersion(entityElement);
int newVersion = baseVersion + 1;
Entity entity = createEntity(entityElement, newVersion, dataset, saveTos, mainInstance);
Entity entity = createEntity(entityElement, newVersion, dataset, saveTos, mainInstance, action);
formEntryModel.getExtras().put(new Entities(asList(entity)));
} else {
formEntryModel.getExtras().put(new Entities(emptyList()));
}
}
}

private Entity createEntity(TreeElement entityElement, int version, String dataset, List<Pair<XPathReference, String>> saveTos, FormInstance mainInstance) {
private Entity createEntity(TreeElement entityElement, int version, String dataset, List<Pair<XPathReference, String>> saveTos, FormInstance mainInstance, EntityAction action) {
List<Pair<String, String>> fields = saveTos.stream().map(saveTo -> {
IDataReference reference = saveTo.getFirst();
IAnswerData answerData = mainInstance.resolveReference(reference).getValue();
Expand All @@ -62,6 +62,6 @@ private Entity createEntity(TreeElement entityElement, int version, String datas

String id = EntityFormParser.parseId(entityElement);
String label = EntityFormParser.parseLabel(entityElement);
return new Entity(dataset, id, label, version, fields);
return new Entity(action, dataset, id, label, version, fields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeElement;
import org.javarosa.entities.EntityAction;
import org.javarosa.xpath.expr.XPathFuncExpr;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -70,9 +71,4 @@ public static EntityAction parseAction(@NotNull TreeElement entity) {

return null;
}

public enum EntityAction {
CREATE,
UPDATE
}
}
4 changes: 4 additions & 0 deletions src/test/java/org/javarosa/entities/EntitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void fillingFormWithCreate_makesEntityAvailable() throws IOException, XFo
assertThat(entities.get(0).label, equalTo("Tom Wambsgans"));
assertThat(entities.get(0).version, equalTo(1));
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
assertThat(entities.get(0).action, equalTo(EntityAction.CREATE));
}

@Test
Expand Down Expand Up @@ -168,6 +169,7 @@ public void fillingFormWithUpdate_makesEntityAvailable() throws IOException, XFo
assertThat(entities.get(0).label, equalTo("Tom Wambsgans"));
assertThat(entities.get(0).version, equalTo(2));
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
assertThat(entities.get(0).action, equalTo(EntityAction.UPDATE));
}

@Test
Expand Down Expand Up @@ -252,6 +254,7 @@ public void fillingFormWithCreateAndUpdate_makesEntityAvailableAsSecondVersion()
assertThat(entities.get(0).label, equalTo("Tom Wambsgans"));
assertThat(entities.get(0).version, equalTo(2));
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
assertThat(entities.get(0).action, equalTo(EntityAction.UPDATE));
}

@Test
Expand Down Expand Up @@ -295,6 +298,7 @@ public void fillingFormWithCreateAndUpdate_butNoBaseVersion_makesEntityAvailable
assertThat(entities.get(0).label, equalTo("Tom Wambsgans"));
assertThat(entities.get(0).version, equalTo(1));
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
assertThat(entities.get(0).action, equalTo(EntityAction.UPDATE));
}

@Test
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/javarosa/entities/EntityFormParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void parseAction_findsCreateWithTrueString() throws XFormParser.ParseExce
XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes())));
FormDef formDef = parser.parse(null);

EntityFormParser.EntityAction dataset = EntityFormParser.parseAction(EntityFormParser.getEntityElement(formDef.getMainInstance()));
assertThat(dataset, equalTo(EntityFormParser.EntityAction.CREATE));
EntityAction action = EntityFormParser.parseAction(EntityFormParser.getEntityElement(formDef.getMainInstance()));
assertThat(action, equalTo(EntityAction.CREATE));
}

@Test
Expand Down Expand Up @@ -84,7 +84,7 @@ public void parseAction_findsUpdateWithTrueString() throws XFormParser.ParseExce
XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes())));
FormDef formDef = parser.parse(null);

EntityFormParser.EntityAction dataset = EntityFormParser.parseAction(EntityFormParser.getEntityElement(formDef.getMainInstance()));
assertThat(dataset, equalTo(EntityFormParser.EntityAction.UPDATE));
EntityAction dataset = EntityFormParser.parseAction(EntityFormParser.getEntityElement(formDef.getMainInstance()));
assertThat(dataset, equalTo(EntityAction.UPDATE));
}
}

0 comments on commit 5ae6894

Please sign in to comment.