refactor description template xml

This commit is contained in:
Efstratios Giannopoulos 2024-01-31 17:48:59 +02:00
parent 7e63abca50
commit bcd42d66b7
1 changed files with 45 additions and 33 deletions

View File

@ -34,6 +34,8 @@ import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
@Service
public class DescriptionTemplateXmlMigrationService {
@ -77,13 +79,14 @@ public class DescriptionTemplateXmlMigrationService {
logger.error("Migrate DescriptionTemplate " + item.getId() + " failed read xml");
continue;
}
ViewStyleModel viewStyleModel = null;
try {
ViewStyleModel viewStyleModel = new ViewStyleModel().fromXml(document.getDocumentElement());
item.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(viewStyleModel)));
viewStyleModel = new ViewStyleModel().fromXml(document.getDocumentElement());
} catch (Exception ex){
logger.error("Migrate DescriptionTemplate " + item.getId() + " failed read xml");
continue;
}
item.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(viewStyleModel)));
this.entityManager.merge(item);
}
@ -97,16 +100,10 @@ public class DescriptionTemplateXmlMigrationService {
DefinitionEntity data = new DefinitionEntity();
if (persist == null)
return data;
if (!this.conventionService.isListNullOrEmpty(persist.getSections())) {
data.setSections(new ArrayList<>());
for (Section sectionPersist : persist.getSections()) {
data.getSections().add(this.buildSectionEntity(sectionPersist));
}
}
if (!this.conventionService.isListNullOrEmpty(persist.getPages())) {
data.setPages(new ArrayList<>());
for (Page pagePersist : persist.getPages()) {
data.getPages().add(this.buildPageEntity(pagePersist));
data.getPages().add(this.buildPageEntity(pagePersist, persist.getSections()));
}
}
@ -125,7 +122,6 @@ public class DescriptionTemplateXmlMigrationService {
data.setOrdinal(persist.getOrdinal());
data.setDefaultVisibility(persist.isDefaultVisibility());
data.setMultiplicity(persist.getMultiplicity());
data.setPage(persist.getPage());
data.setTitle(persist.getTitle());
if (!this.conventionService.isListNullOrEmpty(persist.getSections())) {
@ -206,7 +202,7 @@ public class DescriptionTemplateXmlMigrationService {
return data;
}
private BaseFieldDataEntity<?> buildFieldDataEntity(FieldData persist, String renderStyle) {
private BaseFieldDataEntity buildFieldDataEntity(FieldData persist, String renderStyle) {
if (renderStyle.equals("checkBox")){
return this.buildLabelDataEntity(persist.getLabel(), FieldType.CHECK_BOX);
}
@ -281,7 +277,7 @@ public class DescriptionTemplateXmlMigrationService {
if (renderStyle.equals("externalDatasets")){
ExternalDatasetDataEntity data = new ExternalDatasetDataEntity();
data.setLabel(persist.getLabel());
data.setMultiAutoComplete(((ExternalDatasetsData)persist).getMultiAutoComplete());
data.setMultipleSelect(((ExternalDatasetsData)persist).getMultiAutoComplete());
data.setType(FieldDataExternalDatasetType.of(((ExternalDatasetsData)persist).getType()));
data.setFieldType(FieldType.EXTERNAL_DATASETS);
return data;
@ -325,37 +321,35 @@ public class DescriptionTemplateXmlMigrationService {
if (renderStyle.equals("combobox")){
switch (((ComboBoxData<?>)persist).getType()){
case "wordlist":{
WordListDataEntity data = new WordListDataEntity();
SelectDataEntity data = new SelectDataEntity();
data.setLabel(persist.getLabel());
data.setOptions(new ArrayList<>());
data.setMultiList(((WordListData)persist).getMultiList());
data.setMultipleSelect(((WordListData)persist).getMultiList());
if (((WordListData)persist).getOptions() != null){
for (ComboBoxData.Option option : ((WordListData)persist).getOptions()){
ComboBoxDataEntity.Option optionEntity = new ComboBoxDataEntity.Option();
SelectDataEntity.OptionEntity optionEntity = new SelectDataEntity.OptionEntity();
optionEntity.setValue(option.getValue());
optionEntity.setLabel(option.getLabel());
optionEntity.setSource(option.getSource());
optionEntity.setUri(option.getUri());
data.getOptions().add(optionEntity);
}
}
data.setFieldType(FieldType.WORD_LIST);
data.setFieldType(FieldType.SELECT);
return data;
}
case "autocomplete":{
AutoCompleteDataEntity data = new AutoCompleteDataEntity();
ExternalSelectDataEntity data = new ExternalSelectDataEntity();
data.setLabel(persist.getLabel());
data.setAutoCompleteSingleDataList(new ArrayList<>());
data.setMultiAutoComplete(((AutoCompleteData)persist).getMultiAutoComplete());
data.setSources(new ArrayList<>());
data.setMultipleSelect(((AutoCompleteData)persist).getMultiAutoComplete());
if (((AutoCompleteData)persist).getAutoCompleteSingleDataList() != null){
for (AutoCompleteData.AutoCompleteSingleData autoCompleteSingleData : ((AutoCompleteData)persist).getAutoCompleteSingleDataList()){
AutoCompleteDataEntity.AutoCompleteSingleData autoCompleteSingleDataEntity = new AutoCompleteDataEntity.AutoCompleteSingleData();
ExternalSelectDataEntity.ExternalSelectSourceEntity autoCompleteSingleDataEntity = new ExternalSelectDataEntity.ExternalSelectSourceEntity();
autoCompleteSingleDataEntity.setOptionsRoot(autoCompleteSingleData.getOptionsRoot());
autoCompleteSingleDataEntity.setUrl(autoCompleteSingleData.getUrl());
autoCompleteSingleDataEntity.setMethod(autoCompleteSingleData.getMethod());
autoCompleteSingleDataEntity.setHasAuth(autoCompleteSingleData.getHasAuth());
if (autoCompleteSingleData.getHasAuth() && autoCompleteSingleData.getAuth()!= null) {
AutoCompleteDataEntity.AuthAutoCompleteData optionEntity = new AutoCompleteDataEntity.AuthAutoCompleteData();
ExternalSelectDataEntity.ExternalSelectAuthDataEntity optionEntity = new ExternalSelectDataEntity.ExternalSelectAuthDataEntity();
optionEntity.setBody(autoCompleteSingleData.getAuth().getBody());
optionEntity.setMethod(autoCompleteSingleData.getAuth().getMethod());
optionEntity.setUrl(autoCompleteSingleData.getAuth().getUrl());
@ -363,19 +357,17 @@ public class DescriptionTemplateXmlMigrationService {
optionEntity.setBody(autoCompleteSingleData.getAuth().getBody());
autoCompleteSingleDataEntity.setAuth(optionEntity);
}
autoCompleteSingleDataEntity.setAutocompleteType(AutoCompleteDataEntity.AutocompleteType.of(autoCompleteSingleData.getAutocompleteType()));
if (autoCompleteSingleData.getAutoCompleteOptions() != null) {
ComboBoxDataEntity.Option optionEntity = new ComboBoxDataEntity.Option();
ExternalSelectDataEntity.ExternalSelectSourceBindingEntity optionEntity = new ExternalSelectDataEntity.ExternalSelectSourceBindingEntity();
optionEntity.setValue(autoCompleteSingleData.getAutoCompleteOptions().getValue());
optionEntity.setLabel(autoCompleteSingleData.getAutoCompleteOptions().getLabel());
optionEntity.setSource(autoCompleteSingleData.getAutoCompleteOptions().getSource());
optionEntity.setUri(autoCompleteSingleData.getAutoCompleteOptions().getUri());
autoCompleteSingleDataEntity.setAutoCompleteOptions(optionEntity);
autoCompleteSingleDataEntity.setSourceBinding(optionEntity);
}
data.getAutoCompleteSingleDataList().add(autoCompleteSingleDataEntity);
data.getSources().add(autoCompleteSingleDataEntity);
}
}
data.setFieldType(FieldType.AUTO_COMPLETE);
data.setFieldType(FieldType.EXTERNAL_SELECT);
return data;
}
}
@ -383,18 +375,18 @@ public class DescriptionTemplateXmlMigrationService {
throw new MyApplicationException("Can transform field data" + renderStyle);
}
private BaseFieldDataEntity<?> buildLabelDataEntity(String label, FieldType fieldType) {
private BaseFieldDataEntity buildLabelDataEntity(String label, FieldType fieldType) {
LabelDataEntity data = new LabelDataEntity();
data.setLabel(label);
data.setFieldType(fieldType);
return data;
}
private BaseFieldDataEntity<?> buildLabelAndMultiplicityDataEntity(String label, Boolean multiAutoComplete, FieldType fieldType) {
private BaseFieldDataEntity buildLabelAndMultiplicityDataEntity(String label, Boolean multiAutoComplete, FieldType fieldType) {
LabelAndMultiplicityDataEntity data = new LabelAndMultiplicityDataEntity();
data.setLabel(label);
data.setFieldType(fieldType);
data.setMultiAutoComplete(multiAutoComplete);
data.setMultipleSelect(multiAutoComplete);
return data;
}
@ -421,7 +413,7 @@ public class DescriptionTemplateXmlMigrationService {
private @NotNull PageEntity buildPageEntity(Page persist) {
private @NotNull PageEntity buildPageEntity(Page persist, List<Section> allSections) {
PageEntity data = new PageEntity();
if (persist == null)
return data;
@ -429,6 +421,26 @@ public class DescriptionTemplateXmlMigrationService {
data.setId(persist.getId());
data.setOrdinal(persist.getOrdinal());
data.setTitle(persist.getTitle());
List<Section> sections = allSections.stream().filter(x-> this.isInPage(x, persist)).toList();
if (!this.conventionService.isListNullOrEmpty(sections)) {
data.setSections(new ArrayList<>());
for (Section sectionPersist : sections) {
data.getSections().add(this.buildSectionEntity(sectionPersist));
}
}
return data;
}
private boolean isInPage(Section section, Page page){
try {
return UUID.fromString(section.getPage()).equals(UUID.fromString(page.getId()));
} catch (Exception e){
try {
return Integer.parseInt(section.getPage().replace("page_", "")) == page.getOrdinal();
} catch (Exception e1){
return section.getPage().toLowerCase(Locale.ROOT).equals(page.getId().toLowerCase(Locale.ROOT));
}
}
}
}