Refactors Dataset "Field" from String to Object.

This commit is contained in:
gkolokythas 2019-12-17 13:08:01 +02:00
parent 816ed4dd65
commit 6cedc40339
5 changed files with 71 additions and 46 deletions

View File

@ -25,7 +25,6 @@ import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class WordBuilder {
@ -132,13 +131,13 @@ public class WordBuilder {
return document;
}
public void createPages(List<DatasetProfilePage> datasetProfilePages, XWPFDocument mainDocumentPart, Boolean createListing, VisibilityRuleService visibilityRuleService) {
private void createPages(List<DatasetProfilePage> datasetProfilePages, XWPFDocument mainDocumentPart, Boolean createListing, VisibilityRuleService visibilityRuleService) {
datasetProfilePages.forEach(item -> {
createSections(item.getSections(), mainDocumentPart, ParagraphStyle.HEADER4, 0, createListing, visibilityRuleService);
});
}
public void createSections(List<Section> sections, XWPFDocument mainDocumentPart, ParagraphStyle style, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
private void createSections(List<Section> sections, XWPFDocument mainDocumentPart, ParagraphStyle style, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
if (createListing) this.addListing(mainDocumentPart, indent, false, true);
sections.forEach(section -> {
if (visibilityRuleService.isElementVisible(section.getId())) {
@ -153,7 +152,7 @@ public class WordBuilder {
});
}
public void createCompositeFields(List<FieldSet> compositeFields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
private void createCompositeFields(List<FieldSet> compositeFields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
if (createListing) this.addListing(mainDocumentPart, indent, true, true);
compositeFields.forEach(compositeField -> {
if (visibilityRuleService.isElementVisible(compositeField.getId()) && hasVisibleFields(compositeField, visibilityRuleService)) {
@ -177,7 +176,7 @@ public class WordBuilder {
});
}
public void createFields(List<Field> fields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
private void createFields(List<Field> fields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
if (createListing) this.addListing(mainDocumentPart, indent, false, false);
fields.forEach(field -> {
if (visibilityRuleService.isElementVisible(field.getId())) {
@ -202,7 +201,7 @@ public class WordBuilder {
return paragraph;
}
public void addListing(XWPFDocument document, int indent, Boolean question, Boolean hasIndication) {
private void addListing(XWPFDocument document, int indent, Boolean question, Boolean hasIndication) {
CTLvl cTLvl = this.cTAbstractNum.addNewLvl();
String textLevel = "";
@ -222,7 +221,7 @@ public class WordBuilder {
}
}
public String formatter(Field field) throws IOException {
private String formatter(Field field) throws IOException {
switch (field.getViewStyle().getRenderStyle()) {
case "combobox": {
String comboboxType = ((ComboBoxData) field.getData()).getType();
@ -232,7 +231,7 @@ public class WordBuilder {
Map<String, String> map = new HashMap<>();
if (!field.getValue().equals("")) {
try {
JSONArray jsonarray = new JSONArray(field.getValue());
JSONArray jsonarray = new JSONArray(field.getValue().toString());
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("id");
@ -241,37 +240,39 @@ public class WordBuilder {
map.put(id, label);
}
}
} catch (Exception e){
Map<String, String> exMap = mapper.readValue(field.getValue(), new TypeReference<Map<String, String>>() {
} catch (Exception e) {
Map<String, String> exMap = mapper.readValue(field.getValue().toString(), new TypeReference<Map<String, String>>() {
});
return exMap.get("label");
}
}
StringBuilder sb = new StringBuilder();
int index = 0;
for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append("\n");
sb.append(entry.getValue());
if (index != map.size() - 1) sb.append(", ");
index++;
}
return sb.toString();
} else if (comboboxType.equals("wordlist")) {
return field.getValue();
return field.getValue().toString();
}
}
case "booleanDecision":
if (field.getValue() != null && field.getValue().equals("true")) return "Yes";
else return "No";
case "radiobox":
return field.getValue();
return field.getValue().toString();
case "checkBox":
CheckBoxData data = (CheckBoxData) field.getData();
if (field.getValue() == null || field.getValue().equals("false")) return null;
return data.getLabel();
case "freetext":
return field.getValue();
return field.getValue().toString();
case "textarea":
return field.getValue();
return field.getValue().toString();
case "datepicker":
return field.getValue();
return field.getValue().toString();
}
return null;
}

View File

@ -7,6 +7,8 @@ import eu.eudat.models.data.user.components.datasetprofile.FieldSet;
import eu.eudat.models.data.user.components.datasetprofile.Section;
import eu.eudat.models.data.user.composite.DatasetProfilePage;
import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import org.json.JSONArray;
import org.json.JSONException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@ -17,9 +19,6 @@ import java.io.IOException;
import java.util.List;
import java.util.UUID;
/**
* Created by ikalyvas on 3/5/2018.
*/
public class ExportXmlBuilder {
public File build(PagedDatasetProfile pagedDatasetProfile, UUID datasetProfileId, VisibilityRuleService visibilityRuleService) throws IOException {
@ -49,7 +48,7 @@ public class ExportXmlBuilder {
return pages;
}
public Element createSections(List<Section> sections, VisibilityRuleService visibilityRuleService, Document element) {
private Element createSections(List<Section> sections, VisibilityRuleService visibilityRuleService, Document element) {
Element elementSections = element.createElement("sections");
sections.forEach(section -> {
Element elementSection = element.createElement("section");
@ -62,7 +61,7 @@ public class ExportXmlBuilder {
return elementSections;
}
public Element createCompositeFields(List<FieldSet> compositeFields, VisibilityRuleService visibilityRuleService, Document element) {
private Element createCompositeFields(List<FieldSet> compositeFields, VisibilityRuleService visibilityRuleService, Document element) {
Element elementComposites = element.createElement("composite-fields");
compositeFields.forEach(compositeField -> {
if (visibilityRuleService.isElementVisible(compositeField.getId()) && hasVisibleFields(compositeField, visibilityRuleService)) {
@ -86,15 +85,25 @@ public class ExportXmlBuilder {
return elementComposites;
}
public Element createFields(List<Field> fields, VisibilityRuleService visibilityRuleService, Document element) {
private Element createFields(List<Field> fields, VisibilityRuleService visibilityRuleService, Document element) {
Element elementFields = element.createElement("fields");
fields.forEach(field -> {
if (visibilityRuleService.isElementVisible(field.getId())) {
Element elementField = element.createElement("field");
elementField.setAttribute("id", field.getId());
if (field.getValue() != null && !field.getValue().isEmpty()) {
if (field.getValue() != null) {
Element valueField = element.createElement("value");
valueField.setTextContent(field.getValue());
try {
JSONArray jsonArray = new JSONArray(field.getValue().toString());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < jsonArray.length(); i++) {
sb.append(jsonArray.getJSONObject(i).get("label").toString());
if (i != jsonArray.length() - 1) sb.append(", ");
}
valueField.setTextContent(sb.toString());
} catch (JSONException ex) {
valueField.setTextContent(field.getValue().toString());
}
elementField.appendChild(valueField);
}
elementFields.appendChild(elementField);

View File

@ -9,6 +9,9 @@ import eu.eudat.models.data.user.composite.PropertiesModelBuilder;
import eu.eudat.logic.utilities.interfaces.ViewStyleDefinition;
import eu.eudat.logic.utilities.builders.ModelBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -17,7 +20,7 @@ import java.util.stream.Collectors;
public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefinition<eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Field>, PropertiesGenerator {
private String id;
private Integer ordinal;
private String value;
private Object value;
private ViewStyle viewStyle;
private String datatype;
private String numbering;
@ -58,11 +61,11 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin
this.ordinal = ordinal;
}
public String getValue() {
public Object getValue() {
return value;
}
public void setValue(String value) {
public void setValue(Object value) {
this.value = value;
}
@ -146,7 +149,7 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin
this.rdaProperty = rdaProperty;
}
public Field cloneForMultiplicity(String key, Map<String, Object> properties) {
Field cloneForMultiplicity(String key, Map<String, Object> properties) {
Field newField = new Field();
newField.id = key;
newField.ordinal = this.ordinal;
@ -189,13 +192,21 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin
@Override
public void fromJsonObject(Map<String, Object> properties) {
this.value = (String) properties.get(this.id);
this.multiplicityItems = new LinkedList<Field>();
try {
JSONArray jsonArray = new JSONArray(properties.get(this.id).toString());
List<String> stringList = new LinkedList<>();
for (int i = 0; i < jsonArray.length(); i++) {
stringList.add(jsonArray.getJSONObject(i).toString());
}
this.value = stringList;
} catch (JSONException e) {
this.value = (String) properties.get(this.id);
}
this.multiplicityItems = new LinkedList<>();
List<String> compositeKeys = properties.keySet().stream().filter(keys -> keys.startsWith("multiple_" + this.getId())).collect(Collectors.toList());
for (String key : compositeKeys) {
this.multiplicityItems.add(this.cloneForMultiplicity(key, properties));
}
}
@Override
@ -210,7 +221,11 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin
@Override
public void toMap(Map<String, Object> fieldValues) {
fieldValues.put(this.id, this.value);
if (this.value != null) {
fieldValues.put(this.id, this.value.toString());
} else {
fieldValues.put(this.id, "");
}
}
@Override

View File

@ -19,7 +19,7 @@
<mat-form-field class="col-md-12" *ngIf="form.get('data').value.type === datasetProfileComboBoxTypeEnum.Autocomplete">
<div *ngIf="form.get('data').value.multiAutoComplete">
<app-multiple-auto-complete placeholder="{{ form.get('data').value.label | translate }}" [formControl]="form.get('value')"
[configuration]="multipleAutoCompleteConfiguration" (optionRemoved)="_optionRemove($event)">
[configuration]="multipleAutoCompleteConfiguration">
</app-multiple-auto-complete>
</div>
<div *ngIf="!(form.get('data').value.multiAutoComplete)">
@ -47,7 +47,7 @@
<mat-form-field class="col-md-12" *ngIf="form.get('data').value.type === this.datasetProfileInternalDmpEntitiesTypeEnum.Researchers">
<div *ngIf="form.get('data').value.multiAutoComplete">
<app-multiple-auto-complete placeholder="{{ form.get('data').value.label | translate }}" [formControl]="form.get('value')"
[configuration]="multipleAutoCompleteConfiguration" (optionRemoved)="_optionRemove($event)">
[configuration]="multipleAutoCompleteConfiguration">
</app-multiple-auto-complete>
</div>
<div *ngIf="!(form.get('data').value.multiAutoComplete)">
@ -62,7 +62,7 @@
<mat-form-field class="col-md-12" *ngIf="form.get('data').value.type === this.datasetProfileInternalDmpEntitiesTypeEnum.Datasets">
<div *ngIf="form.get('data').value.multiAutoComplete">
<app-multiple-auto-complete placeholder="{{ form.get('data').value.label | translate }}" [formControl]="form.get('value')"
[configuration]="multipleAutoCompleteConfiguration" (optionRemoved)="_optionRemove($event)">
[configuration]="multipleAutoCompleteConfiguration">
</app-multiple-auto-complete>
</div>
<div *ngIf="!(form.get('data').value.multiAutoComplete)">
@ -77,7 +77,7 @@
<mat-form-field class="col-md-12" *ngIf="form.get('data').value.type === this.datasetProfileInternalDmpEntitiesTypeEnum.Dmps">
<div *ngIf="form.get('data').value.multiAutoComplete">
<app-multiple-auto-complete placeholder="{{ form.get('data').value.label | translate }}" [formControl]="form.get('value')"
[configuration]="multipleAutoCompleteConfiguration" (optionRemoved)="_optionRemove($event)">
[configuration]="multipleAutoCompleteConfiguration">
</app-multiple-auto-complete>
</div>
<div *ngIf="!(form.get('data').value.multiAutoComplete)">

View File

@ -100,16 +100,16 @@ export class FormFieldComponent extends BaseComponent implements OnInit {
});
}
_optionRemove(event) {
const array = JSON.parse(this.form.get('value').value);
if (array) {
const index = array.map(x => x.id).indexOf(event.id);
if (index >= 0) {
array.splice(index, 1);
}
this.form.get('value').patchValue(JSON.stringify(array));
}
}
// _optionRemove(event) {
// const array = JSON.parse(this.form.get('value').value);
// if (array) {
// const index = array.map(x => x.id).indexOf(event.id);
// if (index >= 0) {
// array.splice(index, 1);
// }
// this.form.get('value').patchValue(JSON.stringify(array));
// }
// }
_transformValue(item: any) {
if (!item) return [];