Skip to content

Commit

Permalink
[#3115]Make parser accept a form without question-groups. Add a test …
Browse files Browse the repository at this point in the history
…for that. Start on XML generation. Add a test class for that.
  • Loading branch information
stellanl committed Aug 7, 2019
1 parent 91071ad commit 368f27b
Show file tree
Hide file tree
Showing 13 changed files with 468 additions and 900 deletions.
826 changes: 91 additions & 735 deletions Dashboard/package-lock.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions GAE/src/org/akvo/flow/xml/PublishedForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ public static XmlForm parse(String xml, boolean strict) throws IOException {
return form;
}

// Generates XML from Java objects
public static String generate(XmlForm tree) throws IOException {

ObjectMapper objectMapper = new XmlMapper();

// Reads from POJO and converts to XML
String xml = objectMapper.writeValueAsString(tree);
return xml;
}

}
6 changes: 4 additions & 2 deletions GAE/src/org/akvo/flow/xml/XmlAltText.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public class XmlAltText {
public XmlAltText() {
}

public XmlAltText(QuestionOptionDto dto) {
//TODO
public XmlAltText(TranslationDto dto) {
language = dto.getLangCode();
text = dto.getText();
type = "translation";
}

public String getType() {
Expand Down
5 changes: 5 additions & 0 deletions GAE/src/org/akvo/flow/xml/XmlDependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class XmlDependency {
public XmlDependency() {
}

public XmlDependency(long questionId, String answer) {
question = questionId;
answerValue = answer;
}

@Override public String toString() {
return "dependency{" +
"question='" + question + '\'' +
Expand Down
40 changes: 30 additions & 10 deletions GAE/src/org/akvo/flow/xml/XmlForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class XmlForm {
private String app;

@JacksonXmlProperty(localName = "surveyGroupId", isAttribute = true)
private String surveyGroupId;
private Long surveyGroupId;

@JacksonXmlProperty(localName = "surveyGroupName", isAttribute = true)
private String surveyGroupName;
Expand All @@ -57,24 +57,44 @@ public final class XmlForm {
public XmlForm() {
}

//Create a form XML object from a DTO
public XmlForm(SurveyDto dto) {
surveyId = dto.getKeyId();
surveyGroupId = dto.getSurveyGroupId();
//TODO surveyGroupName = dto.getSurveyGroupName();
defaultLanguageCode = dto.getDefaultLanguageCode();
name = dto.getCode();
if (name == null){
name = dto.getName();
}
version = dto.getVersion();
//Now copy the tree of child objects
questionGroup = new XmlQuestionGroup[dto.getQuestionGroupList().size()];
int i = 0;
for (QuestionGroupDto g: dto.getQuestionGroupList()) {
questionGroup[i++] = new XmlQuestionGroup(g);
}
}

/**
* @return a Dto object with relevant fields copied
*/
public SurveyDto toDto() {
SurveyDto dto = new SurveyDto();
//TODO: need to test against many form files
dto.setKeyId(surveyId);
dto.setName(name);
dto.setCode(name);
dto.setVersion(version);
ArrayList<QuestionGroupDto> gList = new ArrayList<>();
int i = 1;
for (XmlQuestionGroup g : questionGroup) {
g.setOrder(i++);
gList.add(g.toDto());
if (questionGroup != null) {
ArrayList<QuestionGroupDto> gList = new ArrayList<>();
int i = 1;
for (XmlQuestionGroup g : questionGroup) {
g.setOrder(i++);
gList.add(g.toDto());
}
dto.setQuestionGroupList(gList);
}
dto.setQuestionGroupList(gList);
//TODO: fields not needed by the export process

return dto;
}
Expand Down Expand Up @@ -123,11 +143,11 @@ public void setApp(String app) {
this.app = app;
}

public String getSurveyGroupId() {
public Long getSurveyGroupId() {
return surveyGroupId;
}

public void setSurveyGroupId(String surveyGroupId) {
public void setSurveyGroupId(Long surveyGroupId) {
this.surveyGroupId = surveyGroupId;
}

Expand Down
22 changes: 13 additions & 9 deletions GAE/src/org/akvo/flow/xml/XmlHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class XmlHelp {

@JacksonXmlProperty(localName = "type", isAttribute = true)
private String type; //Should be "tip"
private String type; //Should always be "tip"
@JacksonXmlProperty(localName = "text", isAttribute = false)
private String text;
@JacksonXmlElementWrapper(localName = "altText", useWrapping = false)
Expand All @@ -41,6 +41,18 @@ public class XmlHelp {
public XmlHelp() {
}

public XmlHelp(String txt) {
type = "tip";
text = txt;
}

@Override public String toString() {
return "help{" +
"type='" + type +
"',text='" + text +
"'}";
}

public String getText() {
return text;
}
Expand All @@ -57,12 +69,4 @@ public void setAltText(XmlAltText[] altText) {
this.altText = altText;
}


@Override public String toString() {
return "help{" +
"type='" + type +
"',text='" + text +
"'}";
}

}
57 changes: 34 additions & 23 deletions GAE/src/org/akvo/flow/xml/XmlOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@

package org.akvo.flow.xml;

import java.util.ArrayList;

import org.waterforpeople.mapping.app.gwt.client.survey.QuestionOptionDto;
import org.waterforpeople.mapping.app.gwt.client.survey.TranslationDto;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

/*
* Class for working with form XML like this:
*
* <question order="1" type="option" mandatory="true" localeNameFlag="false" id="46843002">
* <options allowOther="false" allowMultiple="false" renderType="radio">
* <option value="Yes" code="Y"><text>Yes</text></option>
* <option value="No" code="N"><text>No</text></option>
* </options>
* <
* <option value="No" code="N"><text>No</text><altText type="translation" language="fr">Non</altText></option>
*/

public class XmlOption {
Expand All @@ -46,6 +45,36 @@ public class XmlOption {
public XmlOption() {
}

public XmlOption(QuestionOptionDto dto) {
setCode(dto.getCode());
setText(dto.getText());
setValue(dto.getText());
//Translations
ArrayList<XmlAltText> oList = new ArrayList<>();
for (TranslationDto t: dto.getTranslationMap().values()) {
oList.add(new XmlAltText(t));
}
setAltText((XmlAltText[])oList.toArray());
}

/**
* @return a Dto object with relevant fields copied
*/
public QuestionOptionDto toDto() {
QuestionOptionDto dto = new QuestionOptionDto();
//TODO translations
dto.setCode(code);
dto.setText(text);
return dto;
}

@Override public String toString() {
return "option{" +
"code='" + code +
"',value='" + value +
"',text='" + text +
"'}";
}

public String getCode() {
return code;
Expand Down Expand Up @@ -79,23 +108,5 @@ public void setAltText(XmlAltText[] altText) {
this.altText = altText;
}

/**
* @return a Dto object with relevant fields copied
*/
public QuestionOptionDto toDto() {
QuestionOptionDto dto = new QuestionOptionDto();
//TODO translations
dto.setCode(code);
dto.setText(text);
return dto;
}

@Override public String toString() {
return "option{" +
"code='" + code +
"',value='" + value +
"',text='" + text +
"'}";
}

}
62 changes: 36 additions & 26 deletions GAE/src/org/akvo/flow/xml/XmlOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ public class XmlOptions {
public XmlOptions() {
}

public XmlOptions(OptionContainerDto dto) {
setAllowOther(dto.getAllowOtherFlag());
setAllowMultiple(dto.getAllowMultipleFlag());
option = new XmlOption[dto.getOptionsList().size()];
int i = 0;
for (QuestionOptionDto o: dto.getOptionsList()) {
option[i++] = new XmlOption(o);
}
}

/**
* @return a Dto with relevant fields copied
*/
public OptionContainerDto toDto() {
OptionContainerDto dto = new OptionContainerDto();
dto.setAllowOtherFlag(allowOther);
dto.setAllowMultipleFlag(allowMultiple);
if (option != null) {
ArrayList<QuestionOptionDto> oList = new ArrayList<>();
for (XmlOption o : option) {
oList.add(o.toDto());
}
dto.setOptionsList(oList);
}

return dto;
}

@Override public String toString() {
return "options{" +
"allowOther='" + allowOther +
"',allowMultiple='" + allowMultiple +
"',options=" + option.toString() +
"}";
}

public XmlOption[] getOption() {
return option;
}
Expand Down Expand Up @@ -71,30 +107,4 @@ public void setRenderType(String renderType) {
this.renderType = renderType;
}

/**
* @return a Dto with relevant fields copied
*/
public OptionContainerDto toDto() {
OptionContainerDto dto = new OptionContainerDto();
dto.setAllowOtherFlag(allowOther);
dto.setAllowMultipleFlag(allowMultiple);
if (option != null) {
ArrayList<QuestionOptionDto> oList = new ArrayList<>();
for (XmlOption o : option) {
oList.add(o.toDto());
}
dto.setOptionsList(oList);
}

return dto;
}

@Override public String toString() {
return "options{" +
"allowOther='" + allowOther +
"',allowMultiple='" + allowMultiple +
"',options=" + option.toString() +
'}';
}

}
Loading

0 comments on commit 368f27b

Please # to comment.