2018-06-27 12:29:21 +02:00
|
|
|
package eu.eudat.logic.utilities.builders;
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-06-27 12:29:21 +02:00
|
|
|
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;
|
|
|
|
import eu.eudat.logic.utilities.interfaces.ModelDefinition;
|
|
|
|
import eu.eudat.logic.utilities.interfaces.ViewStyleDefinition;
|
2020-01-16 16:46:24 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-02-16 11:34:02 +01:00
|
|
|
import org.w3c.dom.Element;
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
public class ModelBuilder {
|
2020-01-16 16:46:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ModelBuilder.class);
|
2018-02-16 11:34:02 +01:00
|
|
|
public <U extends ModelDefinition<T>, T extends DatabaseModelDefinition> List<T> toModelDefinition(List<U> items, Class<T> clazz) {
|
|
|
|
List<T> list = new LinkedList<T>();
|
|
|
|
for (U item : items) {
|
|
|
|
try {
|
|
|
|
list.add(item.toDatabaseDefinition(clazz.newInstance()));
|
|
|
|
} catch (InstantiationException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-02-16 11:34:02 +01:00
|
|
|
} catch (IllegalAccessException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-02-16 11:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
public <U extends ViewStyleDefinition<T>, T extends DatabaseViewStyleDefinition> List<T> toViewStyleDefinition(List<U> items, Class<T> clazz) {
|
|
|
|
List<T> list = new LinkedList<T>();
|
|
|
|
for (U item : items) {
|
|
|
|
try {
|
|
|
|
list.add(item.toDatabaseDefinition(clazz.newInstance()));
|
|
|
|
} catch (InstantiationException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-02-16 11:34:02 +01:00
|
|
|
} catch (IllegalAccessException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-02-16 11:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
public <U extends ViewStyleDefinition<T>, T extends DatabaseViewStyleDefinition> List<U> fromViewStyleDefinition(List<T> items, Class<U> clazz) {
|
|
|
|
List<U> list = new LinkedList<U>();
|
|
|
|
for (T item : items) {
|
|
|
|
try {
|
|
|
|
U modelItem = clazz.newInstance();
|
|
|
|
modelItem.fromDatabaseDefinition(item);
|
|
|
|
list.add(modelItem);
|
|
|
|
} catch (InstantiationException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-02-16 11:34:02 +01:00
|
|
|
} catch (IllegalAccessException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-02-16 11:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
public <U> FieldData<U> toFieldData(Object data, String type, Element dataElement) {
|
|
|
|
if (type.equals("combobox")) {
|
|
|
|
if (dataElement != null) {
|
|
|
|
if (dataElement.getAttribute("type").equals("autocomplete")) {
|
|
|
|
return (FieldData<U>) new AutoCompleteData().fromData(data);
|
|
|
|
} else if (dataElement.getAttribute("type").equals("wordlist"))
|
|
|
|
return (FieldData<U>) new WordListData().fromData(data);
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 15:41:12 +02:00
|
|
|
if (type.equals("internalDmpEntities")) {
|
|
|
|
if (dataElement != null) {
|
|
|
|
if (dataElement.getAttribute("type").equals("researchers")) {
|
|
|
|
return (FieldData<U>) new ResearchersAutoCompleteData().fromData(data);
|
|
|
|
}
|
2019-07-30 12:35:06 +02:00
|
|
|
else if (dataElement.getAttribute("type").equals("datasets"))
|
|
|
|
return (FieldData<U>) new DatasetsAutoCompleteData().fromData(data);
|
2019-07-30 13:31:41 +02:00
|
|
|
else if (dataElement.getAttribute("type").equals("dmps"))
|
|
|
|
return (FieldData<U>) new DMPsAutoCompleteData().fromData(data);
|
2019-07-16 15:41:12 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-16 11:34:02 +01:00
|
|
|
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);
|
2019-02-25 17:46:16 +01:00
|
|
|
if (type.equals("datePicker")) return (FieldData<U>) new DatePickerData().fromData(data);
|
2020-06-04 17:37:30 +02:00
|
|
|
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("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);
|
2020-06-25 12:00:29 +02:00
|
|
|
if (type.equals("currency")) return (FieldData<U>) new CurrencyData().fromData(data);
|
2018-02-16 11:34:02 +01:00
|
|
|
return null;
|
|
|
|
}
|
2017-12-15 11:05:51 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
public <U> FieldData<U> toFieldData(Object data, String type) {
|
|
|
|
if (type.equals("combobox")) {
|
|
|
|
String comboboxType = (String) ((Map<String, Object>) data).get("type");
|
|
|
|
if (comboboxType.equals("autocomplete")) {
|
|
|
|
return (FieldData<U>) new AutoCompleteData().fromData(data);
|
|
|
|
} else if (comboboxType.equals("wordlist"))
|
|
|
|
return (FieldData<U>) new WordListData().fromData(data);
|
|
|
|
}
|
2019-07-16 15:41:12 +02:00
|
|
|
if (type.equals("internalDmpEntities")) {
|
|
|
|
String internalDmpEntitiesType = (String) ((Map<String, Object>) data).get("type");
|
|
|
|
if (internalDmpEntitiesType.equals("researchers")) {
|
|
|
|
return (FieldData<U>) new ResearchersAutoCompleteData().fromData(data);
|
|
|
|
}
|
2019-07-30 12:35:06 +02:00
|
|
|
else if (internalDmpEntitiesType.equals("datasets")) {
|
|
|
|
return (FieldData<U>) new DatasetsAutoCompleteData().fromData(data);
|
|
|
|
}
|
2019-07-30 13:31:41 +02:00
|
|
|
else if (internalDmpEntitiesType.equals("dmps")) {
|
|
|
|
return (FieldData<U>) new DMPsAutoCompleteData().fromData(data);
|
|
|
|
}
|
2019-07-16 15:41:12 +02:00
|
|
|
}
|
2018-02-16 11:34:02 +01:00
|
|
|
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);
|
2019-02-25 17:46:16 +01:00
|
|
|
if (type.equals("datePicker")) return (FieldData<U>) new DatePickerData().fromData(data);
|
2020-06-04 17:37:30 +02:00
|
|
|
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("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);
|
2020-06-25 12:00:29 +02:00
|
|
|
if (type.equals("currency")) return (FieldData<U>) new CurrencyData().fromData(data);
|
2018-02-16 11:34:02 +01:00
|
|
|
return null;
|
|
|
|
}
|
2017-12-15 11:05:51 +01:00
|
|
|
}
|