Skip to content

Commit

Permalink
Merge pull request #1428 from dimagi/customRepeatCaptions
Browse files Browse the repository at this point in the history
Custom repeat captions
  • Loading branch information
shubham1g5 authored Aug 22, 2024
2 parents 0e14c0f + 9189a58 commit 65233a6
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 119 deletions.
40 changes: 27 additions & 13 deletions src/main/java/org/javarosa/form/api/FormEntryCaption.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.javarosa.form.api;

import org.commcare.cases.util.StringUtils;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
import org.javarosa.core.model.IFormElement;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.services.locale.Localizer;

import java.util.Hashtable;
Expand Down Expand Up @@ -217,43 +219,45 @@ public String getRepeatText(String typeKey) {

String caption = null;
if ("mainheader".equals(typeKey)) {
caption = g.mainHeader;
caption = getCaptionText(g.mainHeader);
if (caption == null) {
return title;
}
} else if ("add".equals(typeKey)) {
caption = g.addCaption;
caption = getCaptionText(g.addCaption);
if (caption == null) {
return "Add another " + title;
return Localization.getWithDefault("repeat.dialog.add.another", new String[]{title},
"Add another " + title);
}
} else if ("add-empty".equals(typeKey)) {
caption = g.addEmptyCaption;
caption = getCaptionText(g.addEmptyCaption);
if (caption == null) {
caption = g.addCaption;
caption = getCaptionText(g.addCaption);
}
if (caption == null) {
return "None - Add " + title;
return Localization.getWithDefault("repeat.dialog.add.new", new String[]{title},
"Add a new " + title);
}
} else if ("del".equals(typeKey)) {
caption = g.delCaption;
caption = getCaptionText(g.delCaption);
if (caption == null) {
return "Delete " + title;
}
} else if ("done".equals(typeKey)) {
caption = g.doneCaption;
caption = getCaptionText(g.doneCaption);
if (caption == null) {
return "Done";
}
} else if ("done-empty".equals(typeKey)) {
caption = g.doneEmptyCaption;
caption = getCaptionText(g.doneEmptyCaption);
if (caption == null) {
caption = g.doneCaption;
}
if (caption == null) {
return "Skip";
}
} else if ("delheader".equals(typeKey)) {
caption = g.delHeader;
caption = getCaptionText(g.delHeader);
if (caption == null) {
return "Delete which " + title + "?";
}
Expand All @@ -265,6 +269,16 @@ public String getRepeatText(String typeKey) {
return form.fillTemplateString(caption, index.getReference(), vars);
}

private String getCaptionText(String textIdOrText) {
if (!StringUtils.isEmpty(textIdOrText)) {
String returnText = getIText(textIdOrText, null);
if (returnText != null) {
return substituteStringArgs(returnText);
}
}
return substituteStringArgs(textIdOrText);
}

//this should probably be somewhere better
public int getNumRepetitions() {
return form.getNumRepetitions(index);
Expand All @@ -284,11 +298,11 @@ private String getRepetitionText(String type, FormIndex index, boolean newrep) {

String caption = null;
if ("header".equals(type)) {
caption = g.entryHeader;
caption = getCaptionText(g.entryHeader);
} else if ("choose".equals(type)) {
caption = g.chooseCaption;
caption = getCaptionText(g.chooseCaption);
if (caption == null) {
caption = g.entryHeader;
caption = getCaptionText(g.entryHeader);
}
}
if (caption == null) {
Expand Down
51 changes: 32 additions & 19 deletions src/main/java/org/javarosa/xform/parse/XFormParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.javarosa.xform.parse;

import org.commcare.cases.util.StringUtils;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.DataBinding;
import org.javarosa.core.model.FormDef;
Expand Down Expand Up @@ -1082,27 +1083,38 @@ private void parseGroupLabel(GroupDef g, Element e) {
Vector<String> usedAtts = new Vector<>();
usedAtts.addElement(REF_ATTR);

String labelItextId = getItextReference(e);
g.setTextID(labelItextId);
if (labelItextId == null) {
String label = getLabel(e);
g.setLabelInnerText(label);
}

String label = getLabel(e);
String ref = e.getAttributeValue("", REF_ATTR);
if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
}
}

private String getItextReference(Element e) {
String ref = e.getAttributeValue("", REF_ATTR);
if (ref != null) {
if (ref.startsWith(ITEXT_OPEN) && ref.endsWith(ITEXT_CLOSE)) {
String textRef = ref.substring(ITEXT_OPEN.length(), ref.indexOf(ITEXT_CLOSE));

verifyTextMappings(textRef, "Group <label>", true);
g.setTextID(textRef);
return textRef;
} else {
throw new RuntimeException("malformed ref [" + ref + "] for <label>");
throw new XFormParseException("malformed ref [" + ref + "] for <label>");
}
} else {
g.setLabelInnerText(label);
}
return null;
}


if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
private String getLabelOrTextId(Element element) {
String labelItextId = getItextReference(element);
if (!StringUtils.isEmpty(labelItextId)) {
return labelItextId;
}
return getLabel(element);
}

private String getLabel(Element e) {
Expand Down Expand Up @@ -1475,23 +1487,23 @@ private void parseGroup(IFormElement parent, Element e, int groupType) {

if (group.isRepeat() && NAMESPACE_JAVAROSA.equals(childNamespace)) {
if ("chooseCaption".equals(childName)) {
group.chooseCaption = getLabel(child);
group.chooseCaption = getLabelOrTextId(child);
} else if ("addCaption".equals(childName)) {
group.addCaption = getLabel(child);
group.addCaption = getLabelOrTextId(child);
} else if ("delCaption".equals(childName)) {
group.delCaption = getLabel(child);
group.delCaption = getLabelOrTextId(child);
} else if ("doneCaption".equals(childName)) {
group.doneCaption = getLabel(child);
group.doneCaption = getLabelOrTextId(child);
} else if ("addEmptyCaption".equals(childName)) {
group.addEmptyCaption = getLabel(child);
group.addEmptyCaption = getLabelOrTextId(child);
} else if ("doneEmptyCaption".equals(childName)) {
group.doneEmptyCaption = getLabel(child);
group.doneEmptyCaption = getLabelOrTextId(child);
} else if ("entryHeader".equals(childName)) {
group.entryHeader = getLabel(child);
group.entryHeader = getLabelOrTextId(child);
} else if ("delHeader".equals(childName)) {
group.delHeader = getLabel(child);
group.delHeader = getLabelOrTextId(child);
} else if ("mainHeader".equals(childName)) {
group.mainHeader = getLabel(child);
group.mainHeader = getLabelOrTextId(child);
}
}
}
Expand All @@ -1512,6 +1524,7 @@ private void parseGroup(IFormElement parent, Element e, int groupType) {
parent.addChild(group);
}


private TreeReference getFormElementRef(IFormElement fe) {
if (fe instanceof FormDef) {
TreeReference ref = TreeReference.rootRef();
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/javarosa/core/model/test/FormDefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
import org.javarosa.core.model.IFormElement;
import org.javarosa.core.model.QuestionDef;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.condition.IFunctionHandler;
Expand All @@ -20,6 +22,7 @@
import org.javarosa.core.model.instance.test.DummyInstanceInitializationFactory;
import org.javarosa.core.model.utils.test.PersistableSandbox;
import org.javarosa.core.test.FormParseInit;
import org.javarosa.form.api.FormEntryCaption;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.test_utils.ExprEvalUtils;
import org.javarosa.xpath.parser.XPathSyntaxException;
Expand Down Expand Up @@ -666,4 +669,20 @@ public void testItemsetPopulationAndFilter() {

} while (fec.stepToNextEvent() != FormEntryController.EVENT_END_OF_FORM);
}

@Test
public void testRepeatCaptions() throws Exception {
FormParseInit fpi = new FormParseInit("/xform_tests/sweet_repeat_demo.xml");
FormEntryController fec = initFormEntry(fpi);
FormDef formDef = fec.getModel().getForm();
GroupDef repeat = (GroupDef)formDef.getChild(2);
assertEquals("main-header-label", repeat.mainHeader);
assertEquals("add-caption-label", repeat.addCaption);
assertEquals("add-empty-caption-label", repeat.addEmptyCaption);
assertEquals("del-caption-label", repeat.delCaption);
assertEquals("done-caption-label", repeat.doneCaption);
assertEquals("done-empty-caption-label", repeat.doneEmptyCaption);
assertEquals("choose-caption-label", repeat.chooseCaption);
assertEquals("entry-header-label", repeat.entryHeader);
}
}
87 changes: 0 additions & 87 deletions src/test/resources/sweet_repeat_demo.xml

This file was deleted.

Loading

0 comments on commit 65233a6

Please sign in to comment.