Refactor fieldData mapping

This commit is contained in:
George Kalampokis 2022-04-18 18:38:58 +03:00
parent b012f415a7
commit 6b6d63c128
4 changed files with 117 additions and 45 deletions

View File

@ -1,5 +1,6 @@
package eu.eudat.logic.utilities.builders;
import eu.eudat.logic.utilities.helpers.FieldFactory;
import eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.DatabaseViewStyleDefinition;
import eu.eudat.models.data.components.commons.datafield.*;
import eu.eudat.models.data.entities.xmlmodels.modeldefinition.DatabaseModelDefinition;
@ -20,9 +21,7 @@ public class ModelBuilder {
for (U item : items) {
try {
list.add(item.toDatabaseDefinition(clazz.newInstance()));
} catch (InstantiationException e) {
logger.error(e.getMessage(), e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
logger.error(e.getMessage(), e);
}
}
@ -34,9 +33,7 @@ public class ModelBuilder {
for (U item : items) {
try {
list.add(item.toDatabaseDefinition(clazz.newInstance()));
} catch (InstantiationException e) {
logger.error(e.getMessage(), e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
logger.error(e.getMessage(), e);
}
}
@ -50,64 +47,71 @@ public class ModelBuilder {
U modelItem = clazz.newInstance();
modelItem.fromDatabaseDefinition(item);
list.add(modelItem);
} catch (InstantiationException e) {
logger.error(e.getMessage(), e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
logger.error(e.getMessage(), e);
}
}
return list;
}
public <U> FieldData<U> toFieldData(Object data, String type, Element dataElement) {
if (type.equals("combobox")) {
public FieldData toFieldData(Object data, String type, Element dataElement) {
if (dataElement != null && dataElement.hasAttribute("type")) {
logger.info("Type " + type + " SubType " + dataElement.getAttribute("type"));
return FieldFactory.fromData(type, data, dataElement.getAttribute("type"));
}
return FieldFactory.fromData(type, data);
/*if (type.equals("combobox")) {
if (dataElement != null) {
if (dataElement.getAttribute("type").equals("autocomplete")) {
return (FieldData<U>) new AutoCompleteData().fromData(data);
return new AutoCompleteData().fromData(data);
} else if (dataElement.getAttribute("type").equals("wordlist"))
return (FieldData<U>) new WordListData().fromData(data);
return new WordListData().fromData(data);
}
}
if (type.equals("internalDmpEntities")) {
if (dataElement != null) {
if (dataElement.getAttribute("type").equals("researchers")) {
return (FieldData<U>) new ResearchersAutoCompleteData().fromData(data);
return new ResearchersAutoCompleteData().fromData(data);
}
else if (dataElement.getAttribute("type").equals("datasets"))
return (FieldData<U>) new DatasetsAutoCompleteData().fromData(data);
return new DatasetsAutoCompleteData().fromData(data);
else if (dataElement.getAttribute("type").equals("dmps"))
return (FieldData<U>) new DMPsAutoCompleteData().fromData(data);
return new DMPsAutoCompleteData().fromData(data);
}
}
if (type.equals("booleanDecision")) return (FieldData<U>) new BooleanDecisionData().fromData(data);
if (type.equals("radiobox")) return (FieldData<U>) new RadioBoxData().fromData(data);
if (type.equals("checkBox")) return (FieldData<U>) new CheckBoxData().fromData(data);
if (type.equals("freetext")) return (FieldData<U>) new FreeTextData().fromData(data);
if (type.equals("textarea")) return (FieldData<U>) new TextAreaData().fromData(data);
if (type.equals("richTextarea")) return (FieldData<U>) new RichTextAreaData().fromData(data);
if (type.equals("upload")) return (FieldData<U>) new UploadData().fromData(data);
if (type.equals("booleanDecision")) return new BooleanDecisionData().fromData(data);
if (type.equals("radiobox")) return new RadioBoxData().fromData(data);
if (type.equals("checkBox")) return new CheckBoxData().fromData(data);
if (type.equals("freetext")) return new FreeTextData().fromData(data);
if (type.equals("textarea")) return new TextAreaData().fromData(data);
if (type.equals("richTextarea")) return new RichTextAreaData().fromData(data);
if (type.equals("upload")) return new UploadData().fromData(data);
// if (type.equals("table")) return (FieldData<U>) new TableData().fromData(data);
if (type.equals("datePicker")) return (FieldData<U>) new DatePickerData().fromData(data);
if (type.equals("externalDatasets")) return (FieldData<U>) new ExternalDatasetsData().fromData(data);
if (type.equals("dataRepositories")) return (FieldData<U>) new DataRepositoriesData().fromData(data);
if (type.equals("pubRepositories")) return (FieldData<U>) new DataRepositoriesData().fromData(data);
if (type.equals("journalRepositories")) return (FieldData<U>) new DataRepositoriesData().fromData(data);
if (type.equals("taxonomies")) return (FieldData<U>) new TaxonomiesData().fromData(data);
if (type.equals("licenses")) return (FieldData<U>) new LicensesData().fromData(data);
if (type.equals("publications")) return (FieldData<U>) new PublicationsData().fromData(data);
if (type.equals("registries")) return (FieldData<U>) new RegistriesData().fromData(data);
if (type.equals("services")) return (FieldData<U>) new ServicesData().fromData(data);
if (type.equals("tags")) return (FieldData<U>) new TagsData().fromData(data);
if (type.equals("researchers")) return (FieldData<U>) new ResearcherData().fromData(data);
if (type.equals("organizations")) return (FieldData<U>) new OrganizationsData().fromData(data);
if (type.equals("datasetIdentifier")) return (FieldData<U>) new DatasetIdentifierData().fromData(data);
if (type.equals("currency")) return (FieldData<U>) new CurrencyData().fromData(data);
if (type.equals("validation")) return (FieldData<U>) new ValidationData().fromData(data);
return null;
if (type.equals("datePicker")) return new DatePickerData().fromData(data);
if (type.equals("externalDatasets")) return new ExternalDatasetsData().fromData(data);
if (type.equals("dataRepositories")) return new DataRepositoriesData().fromData(data);
if (type.equals("pubRepositories")) return new DataRepositoriesData().fromData(data);
if (type.equals("journalRepositories")) return new DataRepositoriesData().fromData(data);
if (type.equals("taxonomies")) return new TaxonomiesData().fromData(data);
if (type.equals("licenses")) return new LicensesData().fromData(data);
if (type.equals("publications")) return new PublicationsData().fromData(data);
if (type.equals("registries")) return new RegistriesData().fromData(data);
if (type.equals("services")) return new ServicesData().fromData(data);
if (type.equals("tags")) return new TagsData().fromData(data);
if (type.equals("researchers")) return new ResearcherData().fromData(data);
if (type.equals("organizations")) return new OrganizationsData().fromData(data);
if (type.equals("datasetIdentifier")) return new DatasetIdentifierData().fromData(data);
if (type.equals("currency")) return new CurrencyData().fromData(data);
if (type.equals("validation")) return new ValidationData().fromData(data);*/
//return null;
}
public <U> FieldData<U> toFieldData(Object data, String type) {
if (type.equals("combobox")) {
public FieldData toFieldData(Object data, String type) {
if (data != null && ((Map<String, Object>) data).containsKey("type")) {
return FieldFactory.fromData(type, data, (String) ((Map<String, Object>) data).get("type"));
}
return FieldFactory.fromData(type, data);
/*if (type.equals("combobox")) {
String comboboxType = (String) ((Map<String, Object>) data).get("type");
if (comboboxType.equals("autocomplete")) {
return (FieldData<U>) new AutoCompleteData().fromData(data);
@ -150,6 +154,6 @@ public class ModelBuilder {
if (type.equals("datasetIdentifier")) return (FieldData<U>) new DatasetIdentifierData().fromData(data);
if (type.equals("currency")) return (FieldData<U>) new CurrencyData().fromData(data);
if (type.equals("validation")) return (FieldData<U>) new ValidationData().fromData(data);
return null;
return null;*/
}
}

View File

@ -0,0 +1,63 @@
package eu.eudat.logic.utilities.helpers;
import eu.eudat.models.data.components.commons.datafield.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FieldFactory {
private static final Logger logger = LoggerFactory.getLogger(FieldFactory.class);
private static final Map<String, Class<? extends FieldData>> fieldMap = Stream.of(new Object[][]{
{"combobox", ComboBoxData.class},
{"autocomplete", AutoCompleteData.class},
{"wordlist", WordListData.class},
{"internalDmpEntities", InternalDmpEntitiesData.class},
{"researchers", ResearcherData.class},
{"datasets", DatasetsAutoCompleteData.class},
{"dmps", DMPsAutoCompleteData.class},
{"booleanDecision", BooleanDecisionData.class},
{"radiobox", RadioBoxData.class},
{"checkBox", CheckBoxData.class},
{"freetext", FreeTextData.class},
{"textarea", TextAreaData.class},
{"richTextarea", RichTextAreaData.class},
{"upload", UploadData.class},
{"datePicker", DatePickerData.class},
{"externalDatasets", ExternalDatasetsData.class},
{"dataRepositories", DataRepositoriesData.class},
{"pubRepositories", DataRepositoriesData.class},
{"journalRepositories", DataRepositoriesData.class},
{"taxonomies", TaxonomiesData.class},
{"licenses", LicensesData.class},
{"publications", PublicationsData.class},
{"registries", RegistriesData.class},
{"services", ServicesData.class},
{"tags", TagsData.class},
{"organizations", OrganizationsData.class},
{"datasetIdentifier", DatasetIdentifierData.class},
{"currency", CurrencyData.class},
{"validation", ValidationData.class}
}).collect(Collectors.toMap(data -> (String) data[0], data -> (Class<? extends FieldData>) data[1]));
public static FieldData fromData(String type, Object data, String subType) {
if (Modifier.isAbstract(fieldMap.get(type).getModifiers())) {
return fromData(subType, data);
} else {
return fromData(type, data);
}
}
public static FieldData fromData(String type, Object data) {
try {
return (FieldData) fieldMap.get(type).newInstance().fromData(data);
} catch (InstantiationException | IllegalAccessException e) {
logger.error(e.getMessage(), e);
}
return null;
}
}

View File

@ -1,5 +1,6 @@
package eu.eudat.models.data.components.commons.datafield;
import eu.eudat.logic.utilities.helpers.FieldFactory;
import eu.eudat.logic.utilities.interfaces.XmlSerializable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@ -21,6 +22,10 @@ public abstract class FieldData<T> implements XmlSerializable<T> {
return null;
}
public T fromData(Object data, String subType) {
return (T) FieldFactory.fromData(subType, data);
}
public Object toData() {
return null;
}

View File

@ -175,12 +175,12 @@ public class FieldSet implements Comparable, PropertiesModelBuilder, ViewStyleDe
private FieldSet CloneForMultiplicity2(List<String> key, Map<String, Object> properties,String[] ids, int index){
FieldSet newFieldSet = new FieldSet();
newFieldSet.id = ids[0]+"_"+ids[1]+"_"+ids[2] + (ids.length > 4 ? "_" + ids[3] : "");
newFieldSet.id = ids[0] + "_" + ids[1] + "_" + ids[2] + (ids.length > 3 && !ids[ids.length - 1].isEmpty() ? "_" + ids[ids.length - 1] : "");
newFieldSet.description = this.description;
newFieldSet.extendedDescription = this.extendedDescription;
newFieldSet.additionalInformation=this.additionalInformation;
newFieldSet.title = this.title;
newFieldSet.ordinal = ids.length > 4 ? Integer.valueOf(ids[3]) : this.ordinal;
newFieldSet.ordinal = ids.length > 3 && !ids[ids.length - 1].isEmpty() && ids[ids.length - 1].matches("[0-9]+") ? Integer.valueOf(ids[ids.length - 1]) : this.ordinal;
newFieldSet.fields = new LinkedList();
for (Field field: this.fields) {