argos/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/datasetProfileXml/ExportXmlBuilderDatasetProf...

372 lines
21 KiB
Java

package eu.eudat.logic.utilities.documents.xml.datasetProfileXml;
import eu.eudat.commons.types.descriptiontemplate.fielddata.*;
import eu.eudat.models.data.admin.components.datasetprofile.Page;
import eu.eudat.models.data.user.components.datasetprofile.Field;
import eu.eudat.models.data.user.components.datasetprofile.FieldSet;
import eu.eudat.models.data.user.components.datasetprofile.Section;
import eu.eudat.logic.xml.XmlBuilder;
import org.springframework.core.env.Environment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
public class ExportXmlBuilderDatasetProfile {
public File build(eu.eudat.models.data.user.composite.DatasetProfile datasetProfile, Environment environment) throws IOException {
File xmlFile = new File(environment.getProperty("temp.temp") + UUID.randomUUID() + ".xml");
BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true));
Document xmlDoc = XmlBuilder.getDocument();
// Element root = xmlDoc.createElement("root");
// root.appendChild(createPages(datasetProfile.getPages(), datasetProfile.getSections(), xmlDoc));
xmlDoc.appendChild(createPages(datasetProfile.getPages(), datasetProfile.getSections(), xmlDoc));
Element pages = (Element)xmlDoc.getFirstChild();
pages.setAttribute("description", datasetProfile.getDescription());
pages.setAttribute("language", datasetProfile.getLanguage());
pages.setAttribute("type", datasetProfile.getType());
String xml = XmlBuilder.generateXml(xmlDoc);
writer.write(xml);
writer.close();
return xmlFile;
}
public Element createPages(List<Page> datasetProfilePages, List<Section> sections, Document element) {
Element pages = element.createElement("pages");
datasetProfilePages.forEach(item -> {
Element page = element.createElement("page");
page.setAttribute("id", "" + item.getId());
page.setAttribute("ordinal", "" + item.getOrdinal());
page.setAttribute("title", "" + item.getTitle());
sections.forEach(sectionFromLis -> {
if (sectionFromLis.getPage().equals(item.getId())) {
Element elementSections = element.createElement("sections");
page.appendChild(createSections(sectionFromLis, element, elementSections));
}
});
pages.appendChild(page);
});
return pages;
}
public Element createSections(Section sections, Document element, Element elementSections) {
// if (sections.getSections() != null) {
sections.getSections().forEach(sectionFor -> {
Element elementSectionsChild = element.createElement("section");
elementSections.appendChild(createSections(sectionFor, element, elementSectionsChild));
});
// }
elementSections.setAttribute("defaultVisibility", "" + sections.getDefaultVisibility());
elementSections.setAttribute("id", "" + sections.getId());
elementSections.setAttribute("ordinal", "" + sections.getOrdinal());
elementSections.setAttribute("page", "" + sections.getPage());
elementSections.setAttribute("page", "" + sections.getPage());
if (sections.getCompositeFields() != null) {
elementSections.appendChild(createFieldSet(sections.getCompositeFields(), element));
}
if (sections.getNumbering() != null) {
Element numbering = element.createElement("numbering");
numbering.setTextContent(sections.getNumbering());
elementSections.appendChild(numbering);
}
if (sections.getDescription() != null) {
Element description = element.createElement("description");
description.setTextContent(sections.getDescription());
elementSections.appendChild(description);
}
if (sections.getTitle() != null) {
Element title = element.createElement("title");
title.setTextContent(sections.getTitle());
elementSections.appendChild(title);
}
return elementSections;
}
public Element createFieldSet(List<FieldSet> fieldSet, Document element) {
Element elementFieldSets = element.createElement("field-Sets");
fieldSet.forEach(field -> {
Element composite = element.createElement("field-Set");
composite.setAttribute("id", field.getId());
composite.setAttribute("ordinal", "" + field.getOrdinal());
if (field.getNumbering() != null) {
Element numbering = element.createElement("numbering");
numbering.setTextContent(field.getNumbering());
composite.appendChild(numbering);
}
Element commentField = element.createElement("CommentField");
commentField.setTextContent("" + field.getHasCommentField());
composite.appendChild(commentField);
composite.appendChild(createFields(field.getFields(), element));
if (field.getMultiplicity() != null) {
Element multiplicity = element.createElement("multiplicity");
multiplicity.setAttribute("max", "" + field.getMultiplicity().getMax());
multiplicity.setAttribute("min", "" + field.getMultiplicity().getMin());
multiplicity.setAttribute("placeholder", field.getMultiplicity().getPlaceholder());
multiplicity.setAttribute("tableView", String.valueOf(field.getMultiplicity().getTableView()));
composite.appendChild(multiplicity);
}
if (field.getTitle() != null && !field.getTitle().isEmpty()) {
Element title = element.createElement("title");
title.setTextContent(field.getTitle());
composite.appendChild(title);
}
if (field.getDescription() != null && !field.getDescription().isEmpty()) {
Element title = element.createElement("description");
title.setTextContent(field.getDescription());
composite.appendChild(title);
}
if (field.getExtendedDescription() != null && !field.getExtendedDescription().isEmpty()) {
Element extendedDescription = element.createElement("extendedDescription");
extendedDescription.setTextContent(field.getExtendedDescription());
composite.appendChild(extendedDescription);
}
if (field.getAdditionalInformation() != null && !field.getAdditionalInformation().isEmpty()) {
Element additionalInformation = element.createElement("additionalInformation");
additionalInformation.setTextContent(field.getAdditionalInformation());
composite.appendChild(additionalInformation);
}
elementFieldSets.appendChild(composite);
});
return elementFieldSets;
}
public Element createFields(List<Field> fields, Document element) {
Element elementFields = element.createElement("fields");
fields.forEach(field -> {
Element elementField = element.createElement("field");
elementField.setAttribute("id", field.getId());
elementField.setAttribute("ordinal", "" + field.getOrdinal());
if (field.getNumbering() != null) {
Element numbering = element.createElement("numbering");
numbering.setTextContent(field.getNumbering());
elementField.appendChild(numbering);
}
if (field.getSchematics() != null) {
Element schematics = element.createElement("schematics");
field.getSchematics().forEach(schematic -> {
Element schematicChild = element.createElement("schematic");
schematicChild.setTextContent(schematic);
schematics.appendChild(schematicChild);
});
elementField.appendChild(schematics);
}
if (field.getValidations() != null) {
Element validations = element.createElement("validations");
field.getValidations().forEach(validation -> {
Element validationChild = element.createElement("validation");
validationChild.setAttribute("type", "" + validation);
validations.appendChild(validationChild);
});
elementField.appendChild(validations);
}
if (field.getDefaultValue() != null) {
Element defaultValue = element.createElement("defaultValue");
defaultValue.setAttribute("type", field.getDefaultValue().getType());
defaultValue.setAttribute("value", field.getDefaultValue().getValue());
elementField.appendChild(defaultValue);
}
if (field.getVisible() != null) {
Element visible = element.createElement("visible");
visible.setAttribute("style", "" + field.getVisible().getStyle());
field.getVisible().getRules().forEach(rule -> {
Element ruleChild = element.createElement("rule");
ruleChild.setAttribute("target", "" + rule.getTarget());
Element ruleChildValue = element.createElement("value");
ruleChildValue.setTextContent(rule.getValue());
ruleChild.appendChild(ruleChildValue);
visible.appendChild(ruleChild);
});
elementField.appendChild(visible);
}
if (field.getViewStyle() != null) {
Element viewStyle = element.createElement("viewStyle");
viewStyle.setAttribute("cssClass", field.getViewStyle().getCssClass());
viewStyle.setAttribute("renderStyle", field.getViewStyle().getFieldType().getValue());
elementField.appendChild(viewStyle);
}
if (field.getData() != null) {
Element dataOut = element.createElement("data");
switch (field.getViewStyle().getFieldType()) {
case WORD_LIST:
case AUTO_COMPLETE:
ComboBoxDataEntity comboBoxDataEntityObject = (ComboBoxDataEntity) field.getData();
if (comboBoxDataEntityObject.getFieldType().equals("wordlist")) {
WordListDataEntity wordListDataEntityObject = (WordListDataEntity) field.getData();
dataOut.setAttribute("label", wordListDataEntityObject.getLabel());
dataOut.setAttribute("type", wordListDataEntityObject.getFieldType().getValue());
dataOut.setAttribute("multiList", wordListDataEntityObject.getMultiList().toString());
Element options = element.createElement("options");
wordListDataEntityObject.getOptions().forEach(optionChildFor -> {
Element optionChild = element.createElement("option");
optionChild.setAttribute("label", optionChildFor.getLabel());
optionChild.setAttribute("value", optionChildFor.getValue());
options.appendChild(optionChild);
});
dataOut.appendChild(options);
} else if (comboBoxDataEntityObject.getFieldType().equals("autocomplete")) {
AutoCompleteDataEntity autoCompleteDataEntityObject = (AutoCompleteDataEntity) field.getData();
dataOut.setAttribute("label", autoCompleteDataEntityObject.getLabel());
dataOut.setAttribute("type", autoCompleteDataEntityObject.getFieldType().getValue());
dataOut.setAttribute("multiAutoComplete", autoCompleteDataEntityObject.getMultiAutoComplete().toString());
for (AutoCompleteDataEntity.AutoCompleteSingleData singleData: autoCompleteDataEntityObject.getAutoCompleteSingleDataList()) {
Element singleItem = element.createElement("autocompleteSingle");
singleItem.setAttribute("optionsRoot", singleData.getOptionsRoot());
singleItem.setAttribute("url", singleData.getUrl());
singleItem.setAttribute("autoCompleteType", Integer.toString(singleData.getAutocompleteType().getValue()));
if (singleData.getAutoCompleteOptions() != null) {
Element optionChild = element.createElement("option");
optionChild.setAttribute("label", singleData.getAutoCompleteOptions().getLabel());
optionChild.setAttribute("value", singleData.getAutoCompleteOptions().getValue());
singleItem.appendChild(optionChild);
}
dataOut.appendChild(singleItem);
}
}
break;
case UPLOAD:
UploadDataEntity uploadEntityObject = (UploadDataEntity) field.getData();
dataOut.setAttribute("label", uploadEntityObject.getLabel());
dataOut.setAttribute("maxFileSizeInMB", String.valueOf(uploadEntityObject.getMaxFileSizeInMB()));
Element types = element.createElement("types");
uploadEntityObject.getTypes().forEach(type -> {
Element optionChild = element.createElement("option");
optionChild.setAttribute("label", type.getLabel());
optionChild.setAttribute("value", type.getValue());
types.appendChild(optionChild);
});
dataOut.appendChild(types);
break;
case BOOLEAN_DECISION:
LabelAndMultiplicityDataEntity booleanDecisionDataEntityObject = (LabelAndMultiplicityDataEntity) field.getData();
dataOut.setAttribute("label", booleanDecisionDataEntityObject.getLabel());
break;
case RADIO_BOX:
RadioBoxDataEntity radioBoxDataEntityObject = (RadioBoxDataEntity) field.getData();
dataOut.setAttribute("label", radioBoxDataEntityObject.getLabel());
Element options = element.createElement("options");
radioBoxDataEntityObject.getOptions().forEach(optionChildFor -> {
Element optionChild = element.createElement("option");
optionChild.setAttribute("label", optionChildFor.getLabel());
optionChild.setAttribute("value", optionChildFor.getValue());
options.appendChild(optionChild);
});
dataOut.appendChild(options);
break;
case CHECK_BOX:
case FREE_TEXT:
case TEXT_AREA:
case RICH_TEXT_AREA:
case DATE_PICKER:
case DATASET_IDENTIFIER:
case CURRENCY:
case TAGS:
BaseFieldDataEntity baseFieldDataObject = (BaseFieldDataEntity) field.getData();
dataOut.setAttribute("label", baseFieldDataObject.getLabel());
break;
case INTERNAL_DMP_ENTRIES_RESEARCHERS:
case INTERNAL_DMP_ENTRIES_DATASETS:
case INTERNAL_DMP_ENTRIES_DMPS:
InternalDmpBaseDataEntity internalDmpEntitiesData = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", internalDmpEntitiesData.getLabel());
dataOut.setAttribute("type", internalDmpEntitiesData.getFieldType().getValue());
switch (internalDmpEntitiesData.getFieldType()) {
case INTERNAL_DMP_ENTRIES_RESEARCHERS:
InternalDmpBaseDataEntity researchersAutoCompleteData = (InternalDmpBaseDataEntity) internalDmpEntitiesData;
dataOut.setAttribute("multiAutocomplete", researchersAutoCompleteData.getMultiAutoComplete().toString());
break;
case INTERNAL_DMP_ENTRIES_DATASETS:
InternalDmpBaseDataEntity datasetAutoCompleteDataEntity = (InternalDmpBaseDataEntity) internalDmpEntitiesData;
dataOut.setAttribute("multiAutocomplete", datasetAutoCompleteDataEntity.getMultiAutoComplete().toString());
break;
case INTERNAL_DMP_ENTRIES_DMPS:
InternalDmpBaseDataEntity dmpAutoCompleteDataEntity = (InternalDmpBaseDataEntity) internalDmpEntitiesData;
dataOut.setAttribute("multiAutocomplete", dmpAutoCompleteDataEntity.getMultiAutoComplete().toString());
break;
}
break;
case EXTERNAL_DATASETS:
ExternalDatasetDataEntity externalDatasetDataEntity = (ExternalDatasetDataEntity) field.getData();
dataOut.setAttribute("label", externalDatasetDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", externalDatasetDataEntity.getMultiAutoComplete().toString());
dataOut.setAttribute("type", externalDatasetDataEntity.getType().getValue());
break;
case DATA_REPOSITORIES:
case JOURNAL_REPOSITORIES:
case PUB_REPOSITORIES:
InternalDmpBaseDataEntity dataRepositoryDataEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", dataRepositoryDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", dataRepositoryDataEntity.getMultiAutoComplete().toString());
break;
case TAXONOMIES:
InternalDmpBaseDataEntity taxonomyDataEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", taxonomyDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", taxonomyDataEntity.getMultiAutoComplete().toString());
break;
case LICENSES:
InternalDmpBaseDataEntity licensesData = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", licensesData.getLabel());
dataOut.setAttribute("multiAutocomplete", licensesData.getMultiAutoComplete().toString());
break;
case PUBLICATIONS:
InternalDmpBaseDataEntity publicationDataEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", publicationDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", publicationDataEntity.getMultiAutoComplete().toString());
break;
case ORGANIZATIONS:
InternalDmpBaseDataEntity organizationDataEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", organizationDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", organizationDataEntity.getMultiAutoComplete().toString());
break;
case RESEARCHERS:
InternalDmpBaseDataEntity researcherDataEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", researcherDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", researcherDataEntity.getMultiAutoComplete().toString());
break;
case REGISTRIES:
InternalDmpBaseDataEntity registriesEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", registriesEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", registriesEntity.getMultiAutoComplete().toString());
break;
case SERVICES:
InternalDmpBaseDataEntity serviceDataEntity = (InternalDmpBaseDataEntity) field.getData();
dataOut.setAttribute("label", serviceDataEntity.getLabel());
dataOut.setAttribute("multiAutocomplete", serviceDataEntity.getMultiAutoComplete().toString());
break;
}
elementField.appendChild(dataOut);
}
elementFields.appendChild(elementField);
});
return elementFields;
}
}