Improve and generalize prefilling logic

This commit is contained in:
George Kalampokis 2022-02-18 17:51:32 +02:00
parent da5ba952e1
commit 11ff99fd77
14 changed files with 300 additions and 179 deletions

View File

@ -4,8 +4,8 @@ import eu.eudat.queryable.jpa.predicates.*;
import eu.eudat.queryable.queryableentity.DataEntity; import eu.eudat.queryable.queryableentity.DataEntity;
import eu.eudat.queryable.types.SelectionField; import eu.eudat.queryable.types.SelectionField;
import javax.persistence.TypedQuery; import javax.persistence.criteria.Join;
import javax.persistence.criteria.Expression; import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Subquery; import javax.persistence.criteria.Subquery;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -69,4 +69,8 @@ public interface QueryableList<T extends DataEntity> {
<U extends Comparable> Subquery<U> subQueryMax(SinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass); <U extends Comparable> Subquery<U> subQueryMax(SinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass);
<U extends Comparable> Subquery<U> subQueryMax(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass); <U extends Comparable> Subquery<U> subQueryMax(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass);
Join getJoin(String field, JoinType type);
} }

View File

@ -64,7 +64,7 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
} }
private QueryableList<T> selectFields() { private QueryableList<T> selectFields() {
List<Selection> rootFields = fields.stream().map(field -> this.convertFieldToPath(field)).filter(x -> x != null).collect(Collectors.toList()); List<Selection> rootFields = fields.stream().map(this::convertFieldToPath).filter(Objects::nonNull).collect(Collectors.toList());
this.query.select(this.manager.getCriteriaBuilder().tuple(rootFields.toArray(new Selection[rootFields.size()]))); this.query.select(this.manager.getCriteriaBuilder().tuple(rootFields.toArray(new Selection[rootFields.size()])));
return this; return this;
} }
@ -95,7 +95,7 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
return join; return join;
} }
private Join getJoin(String field, JoinType type) { public Join getJoin(String field, JoinType type) {
if (this.joinsMap.containsKey(field)) return this.joinsMap.get(field); if (this.joinsMap.containsKey(field)) return this.joinsMap.get(field);
Join join = this.root.join(field, type); Join join = this.root.join(field, type);
this.joinsMap.put(field, join); this.joinsMap.put(field, join);
@ -135,11 +135,11 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
} }
public <R> List<R> select(SelectPredicate<T, R> predicate) { public <R> List<R> select(SelectPredicate<T, R> predicate) {
return this.toList().stream().map(item -> predicate.applySelection(item)).collect(Collectors.toList()); return this.toList().stream().map(predicate::applySelection).collect(Collectors.toList());
} }
public <R> CompletableFuture<List<R>> selectAsync(SelectPredicate<T, R> predicate) { public <R> CompletableFuture<List<R>> selectAsync(SelectPredicate<T, R> predicate) {
return this.toListAsync().thenApplyAsync(items -> items.stream().map(item -> predicate.applySelection(item)).collect(Collectors.toList())); return this.toListAsync().thenApplyAsync(items -> items.stream().map(predicate::applySelection).collect(Collectors.toList()));
} }
public QueryableList<T> distinct() { public QueryableList<T> distinct() {

View File

@ -57,11 +57,12 @@ import java.util.stream.Collectors;
@Component @Component
public class DatasetProfileManager { public class DatasetProfileManager {
private static final Logger logger = LoggerFactory.getLogger(DatasetProfileManager.class); private static final Logger logger = LoggerFactory.getLogger(DatasetProfileManager.class);
private static final List<String> cache = new ArrayList<>();
private ApiContext apiContext; private ApiContext apiContext;
private DatabaseRepository databaseRepository; private DatabaseRepository databaseRepository;
private Environment environment; private Environment environment;
private List<String> cache;
private final MetricsManager metricsManager; private final MetricsManager metricsManager;
@Autowired @Autowired
@ -70,7 +71,6 @@ public class DatasetProfileManager {
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.environment = environment; this.environment = environment;
this.metricsManager = metricsManager; this.metricsManager = metricsManager;
this.cache = new ArrayList<>();
} }
@Transactional @Transactional
@ -133,7 +133,7 @@ public class DatasetProfileManager {
return field; return field;
} }
public List<ExternalAutocompleteFieldModel> getAutocomplete(AutoCompleteData data, String like) { public static List<ExternalAutocompleteFieldModel> getAutocomplete(AutoCompleteData data, String like) {
List<ExternalAutocompleteFieldModel> result = new LinkedList<>(); List<ExternalAutocompleteFieldModel> result = new LinkedList<>();
SimpleClientHttpRequestFactory simpleFactory = new SimpleClientHttpRequestFactory(); SimpleClientHttpRequestFactory simpleFactory = new SimpleClientHttpRequestFactory();
@ -182,9 +182,9 @@ public class DatasetProfileManager {
headers.setContentType(MediaType.TEXT_PLAIN); headers.setContentType(MediaType.TEXT_PLAIN);
entity = new HttpEntity<>("parameters", headers); entity = new HttpEntity<>("parameters", headers);
if (this.cache.size() <= i) { if (cache.size() <= i) {
response = restTemplate.exchange(singleData.getUrl(), HttpMethod.GET, entity, String.class); response = restTemplate.exchange(singleData.getUrl(), HttpMethod.GET, entity, String.class);
this.cache.add((String) response.getBody()); cache.add((String) response.getBody());
} }
jsonContext = JsonPath.parse(cache.get(i)); jsonContext = JsonPath.parse(cache.get(i));
jsonItems = jsonContext.read(singleData.getOptionsRoot() + "['" + singleData.getAutoCompleteOptions().getLabel() + "','" + singleData.getAutoCompleteOptions().getValue() + "','" + singleData.getAutoCompleteOptions().getSource() + "','" + "uri" + "']"); jsonItems = jsonContext.read(singleData.getOptionsRoot() + "['" + singleData.getAutoCompleteOptions().getLabel() + "','" + singleData.getAutoCompleteOptions().getValue() + "','" + singleData.getAutoCompleteOptions().getSource() + "','" + "uri" + "']");
@ -201,7 +201,7 @@ public class DatasetProfileManager {
//return result; //return result;
} }
private String parseItem(Object item) { private static String parseItem(Object item) {
if (item instanceof String) { if (item instanceof String) {
return (String) item; return (String) item;
} }

View File

@ -2,12 +2,12 @@ package eu.eudat.logic.managers;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.DatasetProfile; import eu.eudat.data.entities.DatasetProfile;
import eu.eudat.logic.mapper.prefilling.PrefillingMapper;
import eu.eudat.logic.proxy.config.ExternalUrlCriteria; import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader; import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.proxy.config.entities.PrefillingConfig; import eu.eudat.logic.proxy.config.entities.PrefillingConfig;
import eu.eudat.logic.proxy.config.entities.PrefillingGet; import eu.eudat.logic.proxy.config.entities.PrefillingGet;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;
import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.eudat.models.data.prefilling.Prefilling; import eu.eudat.models.data.prefilling.Prefilling;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,15 +28,13 @@ public class PrefillingManager {
private final ConfigLoader configLoader; private final ConfigLoader configLoader;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
private final DatasetManager datasetManager; private final DatasetManager datasetManager;
private final DatasetProfileManager datasetProfileManager;
@Autowired @Autowired
public PrefillingManager(ApiContext apiContext, ConfigLoader configLoader, DatasetManager datasetManager, DatasetProfileManager datasetProfileManager) { public PrefillingManager(ApiContext apiContext, ConfigLoader configLoader, DatasetManager datasetManager) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.configLoader = configLoader; this.configLoader = configLoader;
this.objectMapper = new ObjectMapper(); this.objectMapper = new ObjectMapper();
this.datasetManager = datasetManager; this.datasetManager = datasetManager;
this.datasetProfileManager = datasetProfileManager;
} }
public List<Prefilling> getPrefillings(String like, String configId) { public List<Prefilling> getPrefillings(String like, String configId) {
@ -52,7 +50,7 @@ public class PrefillingManager {
PrefillingGet prefillingGet = prefillingConfig.getPrefillingGet(); PrefillingGet prefillingGet = prefillingConfig.getPrefillingGet();
Map<String, Object> prefillingEntity = getSingle(prefillingGet.getUrl(), prefillId); Map<String, Object> prefillingEntity = getSingle(prefillingGet.getUrl(), prefillId);
DatasetProfile datasetProfile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(profileId); DatasetProfile datasetProfile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(profileId);
return DatasetWizardModel.fromPrefilledEntity(prefillingEntity, prefillingGet, datasetProfile, datasetManager); return PrefillingMapper.mapPrefilledEntityToDatasetWizard(prefillingEntity, prefillingGet, datasetProfile, datasetManager);
} }
private Map<String, Object> getSingle(String url, String id) { private Map<String, Object> getSingle(String url, String id) {

View File

@ -0,0 +1,195 @@
package eu.eudat.logic.mapper.prefilling;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.Dataset;
import eu.eudat.data.entities.DatasetProfile;
import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.managers.DatasetManager;
import eu.eudat.logic.managers.DatasetProfileManager;
import eu.eudat.logic.proxy.config.entities.DefaultPrefillingMapping;
import eu.eudat.logic.proxy.config.entities.PrefillingFixedMapping;
import eu.eudat.logic.proxy.config.entities.PrefillingGet;
import eu.eudat.logic.proxy.config.entities.PrefillingMapping;
import eu.eudat.logic.utilities.helpers.StreamDistinctBy;
import eu.eudat.logic.utilities.json.JsonSearcher;
import eu.eudat.models.data.components.commons.datafield.AutoCompleteData;
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;
import eu.eudat.models.data.datasetprofile.RenderStyle;
import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.eudat.models.data.externaldataset.ExternalAutocompleteFieldModel;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
public class PrefillingMapper {
private static final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
public static DatasetWizardModel mapPrefilledEntityToDatasetWizard(Map<String, Object> prefilledEntity, PrefillingGet prefillingGet,
DatasetProfile profile, DatasetManager datasetManager) throws Exception {
DatasetWizardModel datasetWizardModel = new DatasetWizardModel();
datasetWizardModel.setProfile(new DatasetProfileOverviewModel().fromDataModel(profile));
Dataset dataset = new Dataset();
dataset.setProfile(profile);
Map<String, Object> properties = new HashMap<>();
JsonNode parentNode = mapper.readTree(mapper.writeValueAsString(datasetManager.getPagedProfile(datasetWizardModel, dataset)));
for (DefaultPrefillingMapping prefillingMapping: prefillingGet.getMappings()) {
List<String> sourceKeys = Arrays.asList(prefillingMapping.getSource().split("\\."));
Object sourceValue = null;
for (String sourceKey: sourceKeys) {
if (sourceValue == null) {
sourceValue = prefilledEntity.get(sourceKey);
} else if (sourceValue instanceof Map) {
sourceValue = ((Map)sourceValue).get(sourceKey);
}
}
setValue(prefillingMapping, mapper.writeValueAsString(sourceValue), datasetWizardModel, parentNode, properties);
}
for (PrefillingFixedMapping fixedMapping: prefillingGet.getFixedMappings()) {
setValue(fixedMapping, fixedMapping.getValue(), datasetWizardModel, parentNode, properties);
}
dataset.setProperties(mapper.writeValueAsString(properties));
datasetWizardModel.setDatasetProfileDefinition(datasetManager.getPagedProfile(datasetWizardModel, dataset));
return datasetWizardModel;
}
private static void setValue(PrefillingMapping prefillingMapping, String value, DatasetWizardModel datasetWizardModel, JsonNode parentNode, Map<String, Object> properties) throws InvocationTargetException, IllegalAccessException, JsonProcessingException {
if (prefillingMapping.getTarget() != null) {
try {
String methodName = "set" + prefillingMapping.getTarget().substring(0, 1).toUpperCase(Locale.ROOT) + prefillingMapping.getTarget().substring(1);
Method setterMethod = Arrays.stream(DatasetWizardModel.class.getDeclaredMethods())
.filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()).get(0);
Class<?>[] params = setterMethod.getParameterTypes();
//GK: Tags Special logic
if (!value.equals("null") && prefillingMapping.getTarget().equals("tags")) {
value = mapper.valueToTree(parseTags(value)).toString();
}
setterMethod.invoke(datasetWizardModel, mapper.readValue(value, params[0]));
}catch (InvocationTargetException | IllegalAccessException | JsonProcessingException e) {
throw e;
}
} else {
List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", prefillingMapping.getMaDmpTarget());
/*if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.available_until") && !value.equals("null")){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(value.replace("\"", ""), formatter);
date = date.plusYears(20);
value = date.toString();
}*/
String trimRegex = prefillingMapping.getTrimRegex() != null ? prefillingMapping.getTrimRegex() : "";
if (!value.startsWith("\"") && !value.startsWith("[") && !value.equals("null")) {
value = "\"" + value + "\"";
}
JsonNode valueNode = mapper.readTree(value);
List<String> parsedValues = new ArrayList<>();
if (valueNode.isArray()) {
if (prefillingMapping.getSubSource() == null || prefillingMapping.getSubSource().isEmpty()) {
throw new IllegalArgumentException("Source value is an array but no subSource field have been set");
}
String parsedValue;
for(int i = 0; i < valueNode.size(); i++){
JsonNode jsonObj = valueNode.get(i);
String subSource = jsonObj.get(prefillingMapping.getSubSource()).asText();
parsedValue = subSource.replaceAll(trimRegex, "");
parsedValues.add(parsedValue);
}
parsedValues = parsedValues.stream().distinct().collect(Collectors.toList());
}
String parsedValue = null;
if (valueNode.isTextual()) {
parsedValue = valueNode.textValue().replace(trimRegex, "");
}
for (JsonNode node: nodes) {
String id = node.isArray() ? node.get(0).get("id").asText() : node.get("id").asText();
String renderStyle = node.isArray() ? node.get(0).get("viewStyle").get("renderStyle").asText() : node.get("viewStyle").get("renderStyle").asText();
switch (RenderStyle.fromValue(renderStyle)) {
case COMBO_BOX:
if (parsedValues.isEmpty())
parsedValues.add(parsedValue);
properties.put(id, parseComboBoxValues(node, parsedValues));
break;
case TAGS:
properties.put(id, parseTags(parsedValue));
break;
default:
if (!parsedValues.isEmpty())
properties.put(id, String.join(", ", parsedValues));
else
properties.put(id, parsedValue);
break;
}
/*if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){
value = value.replace("\"", "");
if(value.equals("open")){
properties.put(id, value);
}
else if(value.equals("restricted")){
properties.put(id, "shared");
}
else{
properties.put(id, "closed");
}
}*/
}
}
}
private static Object parseComboBoxValues(JsonNode node, List<String> parsedValues) throws JsonProcessingException {
List<Object> normalizedValues = new ArrayList<>();
boolean isMultiSelect;
String type = node.isArray() ? node.get(0).get("data").get("type").asText() : node.get("data").get("type").asText();
if(type.equals("autocomplete")) {
JsonNode dataNode = node.isArray() ? node.get(0).get("data") : node.get("data");
AutoCompleteData autoCompleteData = mapper.treeToValue(dataNode, AutoCompleteData.class);
isMultiSelect = autoCompleteData.getMultiAutoComplete();
for (String format : parsedValues) {
List<ExternalAutocompleteFieldModel> result = DatasetProfileManager.getAutocomplete(autoCompleteData, format);
result = result.stream().filter(StreamDistinctBy.distinctByKey(ExternalAutocompleteFieldModel::getId)).collect(Collectors.toList());
if(!result.isEmpty()){
List<String> tempValues = new LinkedList<>();
for (ExternalAutocompleteFieldModel f : result) {
if (format.equals(f.getId()) || f.getLabel().toUpperCase(Locale.ROOT).contains(format.toUpperCase(Locale.ROOT)))
tempValues.add(mapper.valueToTree(f).toString());
}
if (isMultiSelect)
normalizedValues.addAll(tempValues);
else if (!tempValues.isEmpty())
normalizedValues.add(tempValues.get(0));
}
}
return !normalizedValues.isEmpty() ? (isMultiSelect ? normalizedValues : normalizedValues.get(0)) : null;
} else {
JsonNode optionsNode = node.isArray() ? node.get(0).get("data").get("options") : node.get("data").get("options");
isMultiSelect = node.isArray() ? node.get(0).get("data").get("multiList").booleanValue() : node.get("data").get("multiList").booleanValue();
for (int i = 0; i < optionsNode.size(); i++) {
String value = optionsNode.get(i).get("value").textValue();
if (parsedValues.contains(value)) {
normalizedValues.add(value);
}
}
List<String> normalizedStringValues = normalizedValues.stream().map(Object::toString).collect(Collectors.toList());
return !normalizedValues.isEmpty() ? (isMultiSelect ? String.join(", ", normalizedStringValues) : normalizedValues.get(0)) : null;
}
}
private static List<Tag> parseTags(String value) throws JsonProcessingException {
JsonNode rawTags = mapper.readTree(value);
List<Tag> parsedTags = new LinkedList<>();
if (rawTags.isArray()) {
for (int i = 0; i < rawTags.size(); i++) {
parsedTags.add(new Tag(rawTags.get(i).textValue(), rawTags.get(i).textValue()));
}
} else if (rawTags.isTextual()){
List<String> tags = Arrays.asList(rawTags.textValue().split(", "));
parsedTags.addAll(tags.stream().map(s -> new Tag(s, s)).collect(Collectors.toList()));
}
return parsedTags;
}
}

View File

@ -8,6 +8,8 @@ public class DefaultPrefillingMapping implements PrefillingMapping{
private String source; private String source;
private String target; private String target;
private String maDmpTarget; private String maDmpTarget;
private String subSource;
private String trimRegex;
public String getSource() { public String getSource() {
return source; return source;
@ -35,4 +37,22 @@ public class DefaultPrefillingMapping implements PrefillingMapping{
public void setMaDmpTarget(String maDmpTarget) { public void setMaDmpTarget(String maDmpTarget) {
this.maDmpTarget = maDmpTarget; this.maDmpTarget = maDmpTarget;
} }
public String getSubSource() {
return subSource;
}
@XmlAttribute(name = "subSource")
public void setSubSource(String subSource) {
this.subSource = subSource;
}
public String getTrimRegex() {
return trimRegex;
}
@XmlAttribute(name = "trimRegex")
public void setTrimRegex(String trimRegex) {
this.trimRegex = trimRegex;
}
} }

View File

@ -27,6 +27,16 @@ public class PrefillingFixedMapping implements PrefillingMapping{
this.maDmpTarget = maDmpTarget; this.maDmpTarget = maDmpTarget;
} }
@Override
public String getSubSource() {
return "";
}
@Override
public String getTrimRegex() {
return "";
}
public String getValue() { public String getValue() {
return value; return value;
} }

View File

@ -10,4 +10,8 @@ public interface PrefillingMapping {
String getMaDmpTarget(); String getMaDmpTarget();
void setMaDmpTarget(String maDmpTarget); void setMaDmpTarget(String maDmpTarget);
String getSubSource();
String getTrimRegex();
} }

View File

@ -358,7 +358,7 @@ public class WordBuilder {
for (Map<String, Object> map: mapList) { for (Map<String, Object> map: mapList) {
for (Map.Entry<String, Object> entry : map.entrySet()) { for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != null && (entry.getKey().equals("label") || entry.getKey().equals("description") || entry.getKey().equals("name"))) { if (entry.getValue() != null && (entry.getKey().equals("label") || entry.getKey().equals("description") || entry.getKey().equals("name"))) {
sb.append(entry.getValue().toString()); sb.append(entry.getValue());
break; break;
} }
} }
@ -369,9 +369,9 @@ public class WordBuilder {
} else if (comboboxType.equals("wordlist")) { } else if (comboboxType.equals("wordlist")) {
WordListData wordListData = (WordListData) field.getData(); WordListData wordListData = (WordListData) field.getData();
if (field.getValue() != null){ if (field.getValue() != null){
ComboBoxData<WordListData>.Option selectedOption = null; ComboBoxData.Option selectedOption = null;
if (!wordListData.getOptions().isEmpty()) { if (!wordListData.getOptions().isEmpty()) {
for (ComboBoxData<WordListData>.Option option : wordListData.getOptions()) { for (ComboBoxData.Option option : wordListData.getOptions()) {
if (option.getValue().equals(field.getValue())) { if (option.getValue().equals(field.getValue())) {
selectedOption = option; selectedOption = option;
} }

View File

@ -10,10 +10,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class AutoCompleteData extends ComboBoxData<AutoCompleteData> { public class AutoCompleteData extends ComboBoxData<AutoCompleteData> {
public class AutoCompleteSingleData { public static class AutoCompleteSingleData {
private int autocompleteType; private int autocompleteType;
private String url; private String url;
private Option autoCompleteOptions; private ComboBoxData.Option autoCompleteOptions;
private String optionsRoot; private String optionsRoot;
public int getAutocompleteType() { public int getAutocompleteType() {
@ -38,10 +38,10 @@ public class AutoCompleteData extends ComboBoxData<AutoCompleteData> {
this.url = url; this.url = url;
} }
public Option getAutoCompleteOptions() { public ComboBoxData.Option getAutoCompleteOptions() {
return autoCompleteOptions; return autoCompleteOptions;
} }
public void setAutoCompleteOptions(Option autoCompleteOptions) { public void setAutoCompleteOptions(ComboBoxData.Option autoCompleteOptions) {
this.autoCompleteOptions = autoCompleteOptions; this.autoCompleteOptions = autoCompleteOptions;
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public abstract class ComboBoxData<T> extends FieldData<T> { public abstract class ComboBoxData<T> extends FieldData<T> {
public class Option implements XmlSerializable<Option> { public static class Option implements XmlSerializable<Option> {
private String label; private String label;
private String value; private String value;
private String source; private String source;
@ -54,7 +54,7 @@ public abstract class ComboBoxData<T> extends FieldData<T> {
} }
@Override @Override
public Option fromXml(Element item) { public ComboBoxData.Option fromXml(Element item) {
this.label = item.getAttribute("label"); this.label = item.getAttribute("label");
this.value = item.getAttribute("value"); this.value = item.getAttribute("value");
this.source = item.getAttribute("source"); this.source = item.getAttribute("source");

View File

@ -0,0 +1,42 @@
package eu.eudat.models.data.datasetprofile;
public enum RenderStyle {
COMBO_BOX("combobox"),
INTERNAL_DMP_ENTITIES("internalDmpEntities"),
BOOLEAN_DECISION("booleanDecision"),
RADIO_BOX("radiobox"),
CHECKBOX("checkBox"),
FREETEXT("freetext"),
TEXTAREA("textarea"),
RICH_TEXTAREA("richTextarea"),
DATE_PICKER("datePicker"),
EXTERNAL_DATASETS("externalDatasets"),
DATASET_REPOSITORIES("dataRepositories"),
REGISTRIES("registries"),
SERVICES("services"),
TAGS("tags"),
RESEARCHERS("researchers"),
ORGANIZATIONS("organizations"),
DATASET_IDENTIFIER("datasetIdentifier"),
CURRENCY("currency"),
VALIDATION("validation");
private final String name;
RenderStyle(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static RenderStyle fromValue(String value) {
for(RenderStyle renderStyle: RenderStyle.values()) {
if (renderStyle.getName().equals(value)) {
return renderStyle;
}
}
throw new IllegalArgumentException("RenderStyle [" + value + "] is not available");
}
}

View File

@ -1,17 +1,7 @@
package eu.eudat.models.data.datasetwizard; package eu.eudat.models.data.datasetwizard;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.*; import eu.eudat.data.entities.*;
import eu.eudat.elastic.entities.Tag; import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.managers.DataManagementProfileManager;
import eu.eudat.logic.managers.DatasetManager;
import eu.eudat.logic.proxy.config.entities.PrefillingFixedMapping;
import eu.eudat.logic.proxy.config.entities.PrefillingGet;
import eu.eudat.logic.proxy.config.entities.DefaultPrefillingMapping;
import eu.eudat.logic.proxy.config.entities.PrefillingMapping;
import eu.eudat.logic.utilities.json.JsonSearcher;
import eu.eudat.models.DataModel; import eu.eudat.models.DataModel;
import eu.eudat.models.data.dataset.DataRepository; import eu.eudat.models.data.dataset.DataRepository;
import eu.eudat.models.data.dataset.Registry; import eu.eudat.models.data.dataset.Registry;
@ -19,16 +9,9 @@ import eu.eudat.models.data.dataset.Service;
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel; import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;
import eu.eudat.models.data.dmp.DataManagementPlan; import eu.eudat.models.data.dmp.DataManagementPlan;
import eu.eudat.models.data.externaldataset.ExternalDatasetListingModel; import eu.eudat.models.data.externaldataset.ExternalDatasetListingModel;
import eu.eudat.models.data.helpermodels.Tuple;
import eu.eudat.models.data.user.composite.PagedDatasetProfile; import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import net.minidev.json.JSONValue; import net.minidev.json.JSONValue;
import org.json.JSONArray;
import org.json.JSONObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -314,141 +297,6 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
return entity; return entity;
} }
public static DatasetWizardModel fromPrefilledEntity(Map<String, Object> prefilledEntity, PrefillingGet prefillingGet,
DatasetProfile profile, DatasetManager datasetManager) throws Exception {
DatasetWizardModel datasetWizardModel = new DatasetWizardModel();
datasetWizardModel.setProfile(new DatasetProfileOverviewModel().fromDataModel(profile));
Dataset dataset = new Dataset();
dataset.setProfile(profile);
Map<String, Object> properties = new HashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode parentNode = objectMapper.readTree(objectMapper.writeValueAsString(datasetManager.getPagedProfile(datasetWizardModel, dataset)));
for (DefaultPrefillingMapping prefillingMapping: prefillingGet.getMappings()) {
List<String> sourceKeys = Arrays.asList(prefillingMapping.getSource().split("\\."));
Object sourceValue = null;
for (String sourceKey: sourceKeys) {
if (sourceValue == null) {
sourceValue = prefilledEntity.get(sourceKey);
} else if (sourceValue instanceof Map) {
sourceValue = ((Map)sourceValue).get(sourceKey);
}
}
setValue(prefillingMapping, objectMapper.writeValueAsString(sourceValue), datasetWizardModel, parentNode, properties);
}
for (PrefillingFixedMapping fixedMapping: prefillingGet.getFixedMappings()) {
setValue(fixedMapping, fixedMapping.getValue(), datasetWizardModel, parentNode, properties);
}
dataset.setProperties(objectMapper.writeValueAsString(properties));
datasetWizardModel.setDatasetProfileDefinition(datasetManager.getPagedProfile(datasetWizardModel, dataset));
return datasetWizardModel;
}
private static void setValue(PrefillingMapping prefillingMapping, String value, DatasetWizardModel datasetWizardModel, JsonNode parentNode, Map<String, Object> properties) throws InvocationTargetException, IllegalAccessException, JsonProcessingException {
if (prefillingMapping.getTarget() != null) {
try {
String methodName = "set" + prefillingMapping.getTarget().substring(0, 1).toUpperCase(Locale.ROOT) + prefillingMapping.getTarget().substring(1);
Method setterMethod = Arrays.stream(DatasetWizardModel.class.getDeclaredMethods())
.filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()).get(0);
Class<?>[] params = setterMethod.getParameterTypes();
ObjectMapper mapper = new ObjectMapper();
//GK: Tags Special logic
if (!value.equals("null") && prefillingMapping.getTarget().equals("tags")) {
List<Object> rawTags = (List<Object>) mapper.readValue(value, params[0]);
if (rawTags.get(0) instanceof String) {
List<Tag> parsedTags = rawTags.stream().map(rawTag -> new Tag((String) rawTag, (String) rawTag)).collect(Collectors.toList());
value = mapper.writeValueAsString(parsedTags);
List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", "dataset.keyword");
for (JsonNode node: nodes) {
String id = node.get(0) != null ? node.get(0).get("id").asText() : node.get("id").asText();
properties.put(id, value);
}
}
}
setterMethod.invoke(datasetWizardModel, mapper.readValue(value, params[0]));
}catch (InvocationTargetException | IllegalAccessException | JsonProcessingException e) {
throw e;
}
} else {
List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", prefillingMapping.getMaDmpTarget());
if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.available_until") && !value.equals("null")){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(value.replace("\"", ""), formatter);
date = date.plusYears(20);
value = date.toString();
}
StringBuilder freeTextFormat = new StringBuilder();
for (JsonNode node: nodes) {
String id = node.get(0) != null ? node.get(0).get("id").asText() : node.get("id").asText();
if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.format") && !value.equals("null")) {
JSONArray jsonArr = new JSONArray(value);
List<String> formats = new ArrayList<>();
String extension;
for(int i = 0; i < jsonArr.length(); i++){
JSONObject jsonObj = jsonArr.getJSONObject(i);
String filename = (String) jsonObj.get("filename");
int index = filename.lastIndexOf('.');
extension = (index > 0) ? filename.substring(index+1) : filename;
formats.add(extension);
}
formats = formats.stream().distinct().collect(Collectors.toList());
List<Object> standardFormats = new ArrayList<>();
String renderStyle = node.get(0) != null ? node.get(0).get("viewStyle").get("renderStyle").asText() : node.get("viewStyle").get("renderStyle").asText();
if(renderStyle.equals("combobox")){
String autocomplete = node.get(0) != null ? node.get(0).get("data").get("type").asText() : node.get("data").get("type").asText();
if(autocomplete.equals("autocomplete")) {
JsonNode urlNode = node.get(0) != null ? node.get(0).get("data").get("autoCompleteSingleDataList") : node.get("data").get("autoCompleteSingleDataList");
String url = urlNode.get(0).get("url").asText();
String optionsRoot = urlNode.get(0).get("optionsRoot").asText();
String label = urlNode.get(0).get("autoCompleteOptions").get("label").asText();
String val = urlNode.get(0).get("autoCompleteOptions").get("value").asText();
for (String format : formats) {
List<Tuple<String, String>> result = DataManagementProfileManager.externalAutocompleteRequest(url, optionsRoot, label, val, format);
result = result.stream().distinct().collect(Collectors.toList());
if(!result.isEmpty()){
for (Tuple<String, String> f : result) {
JSONObject cur = new JSONObject();
cur.put("label", f.getLabel());
standardFormats.add(cur.toString());
freeTextFormat.append(f.getLabel()).append(", ");
}
}
else{
freeTextFormat.append(format).append(", ");
}
}
properties.put(id, standardFormats);
}
}
else if(renderStyle.equals("freetext")){
if (freeTextFormat.length() == 0) {
for (String format : formats) {
freeTextFormat.append(format).append(", ");
}
}
freeTextFormat.setLength(freeTextFormat.length() - 2);
properties.put(id, freeTextFormat.toString());
}
}
else if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){
value = value.replace("\"", "");
if(value.equals("open")){
properties.put(id, value);
}
else if(value.equals("restricted")){
properties.put(id, "shared");
}
else{
properties.put(id, "closed");
}
}
else{
properties.put(id, value.replace("\"", ""));
}
}
}
}
@Override @Override
public String getHint() { public String getHint() {

View File

@ -1041,7 +1041,7 @@
<mapping source="metadata.publication_date" maDmpTarget="dataset.issued"/> <mapping source="metadata.publication_date" maDmpTarget="dataset.issued"/>
<mapping source="metadata.license.created" maDmpTarget="dataset.distribution.license.start_date"/> <mapping source="metadata.license.created" maDmpTarget="dataset.distribution.license.start_date"/>
<mapping source="metadata.embargo_date" maDmpTarget="dataset.distribution.license.start_date"/> <mapping source="metadata.embargo_date" maDmpTarget="dataset.distribution.license.start_date"/>
<mapping source="files" maDmpTarget="dataset.distribution.format"/> <mapping source="files" subSource="filename" trimRegex="^.*\." maDmpTarget="dataset.distribution.format"/>
</mappings> </mappings>
<fixedMappings> <fixedMappings>
<fixedMapping maDmpTarget="dataset.distribution.host.title" value="Zenodo" /> <fixedMapping maDmpTarget="dataset.distribution.host.title" value="Zenodo" />