Prefilling changes

This commit is contained in:
Efstratios Giannopoulos 2024-02-29 17:51:20 +02:00
parent 96696f1a14
commit 46862e06cc
14 changed files with 152 additions and 1089 deletions

View File

@ -1,45 +0,0 @@
package eu.eudat.service.externalfetcher.config;
import eu.eudat.commons.enums.ExternalFetcherSourceType;
import eu.eudat.service.externalfetcher.config.entities.SourceBaseConfiguration;
public class UrlConfiguration implements SourceBaseConfiguration {
private String key;
private String label;
private Integer ordinal;
private ExternalFetcherSourceType type;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public Integer getOrdinal() {
return ordinal;
}
public void setOrdinal(Integer ordinal) {
this.ordinal = ordinal;
}
public ExternalFetcherSourceType getType() {
return type;
}
public void setType(ExternalFetcherSourceType type) {
this.type = type;
}
}

View File

@ -1,58 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "mapping")
public class DefaultPrefillingMapping implements PrefillingMapping{
private String source;
private String target;
private String semanticTarget;
private String subSource;
private String trimRegex;
public String getSource() {
return source;
}
@XmlAttribute(name = "source")
public void setSource(String source) {
this.source = source;
}
public String getTarget() {
return target;
}
@XmlAttribute(name = "target")
public void setTarget(String target) {
this.target = target;
}
public String getSemanticTarget() {
return semanticTarget;
}
@XmlAttribute(name = "semanticTarget")
public void setSemanticTarget(String semanticTarget) {
this.semanticTarget = semanticTarget;
}
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

@ -1,11 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
import java.util.List;
public abstract class GenericUrls {
public abstract List<SourceBaseConfiguration> getUrls();
}

View File

@ -1,41 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "config")
public class PrefillingConfig {
private PrefillingSearch prefillingSearch;
private PrefillingGet prefillingGet;
private String type;
public PrefillingSearch getPrefillingSearch() {
return prefillingSearch;
}
@XmlElement(name = "prefillingSearch")
public void setPrefillingSearch(PrefillingSearch prefillingSearch) {
this.prefillingSearch = prefillingSearch;
}
public PrefillingGet getPrefillingGet() {
return prefillingGet;
}
@XmlElement(name = "prefillingGet")
public void setPrefillingGet(PrefillingGet prefillingGet) {
this.prefillingGet = prefillingGet;
}
@XmlAttribute(name = "type")
public void setType(String type) {
this.type = type;
}
public String getType(){
return type;
}
}

View File

@ -1,48 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "fixedMapping")
public class PrefillingFixedMapping implements PrefillingMapping{
private String target;
private String semanticTarget;
private String value;
public String getTarget() {
return target;
}
@XmlAttribute(name = "target")
public void setTarget(String target) {
this.target = target;
}
public String getSemanticTarget() {
return semanticTarget;
}
@XmlAttribute(name = "semanticTarget")
public void setSemanticTarget(String semanticTarget) {
this.semanticTarget = semanticTarget;
}
@Override
public String getSubSource() {
return "";
}
@Override
public String getTrimRegex() {
return "";
}
public String getValue() {
return value;
}
@XmlAttribute(name = "value")
public void setValue(String value) {
this.value = value;
}
}

View File

@ -1,40 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import java.util.List;
public class PrefillingGet{
private String url;
private List<DefaultPrefillingMapping> mappings;
private List<PrefillingFixedMapping> fixedMappings;
public String getUrl() {
return url;
}
@XmlElement(name = "url")
public void setUrl(String url) {
this.url = url;
}
public List<DefaultPrefillingMapping> getMappings() {
return mappings;
}
@XmlElement(name = "mapping")
@XmlElementWrapper
public void setMappings(List<DefaultPrefillingMapping> mappings) {
this.mappings = mappings;
}
public List<PrefillingFixedMapping> getFixedMappings() {
return fixedMappings;
}
@XmlElement(name = "fixedMapping")
@XmlElementWrapper
public void setFixedMappings(List<PrefillingFixedMapping> fixedMappings) {
this.fixedMappings = fixedMappings;
}
}

View File

@ -1,15 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
public interface PrefillingMapping {
String getTarget();
void setTarget(String target);
String getSemanticTarget();
void setSemanticTarget(String semanticTarget);
String getSubSource();
String getTrimRegex();
}

View File

@ -1,27 +0,0 @@
package eu.eudat.service.externalfetcher.config.entities;
import eu.eudat.service.externalfetcher.config.UrlConfiguration;
import jakarta.xml.bind.annotation.XmlElement;
import java.util.ArrayList;
import java.util.List;
public class PrefillingSearch extends GenericUrls {
private UrlConfiguration urlConfig;
public UrlConfiguration getUrlConfig() {
return urlConfig;
}
@XmlElement(name = "urlConfig")
public void setUrlConfig(UrlConfiguration urlConfig) {
this.urlConfig = urlConfig;
}
@Override
public List<SourceBaseConfiguration> getUrls() {
List<SourceBaseConfiguration> urls = new ArrayList<>();
urls.add(urlConfig);
return urls;
}
}

View File

@ -1,35 +0,0 @@
package eu.eudat.service.externalfetcher.config.prefilling;
import eu.eudat.service.externalfetcher.config.entities.PrefillingConfig;
import org.w3c.dom.Element;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
import java.util.HashMap;
import java.util.Map;
public class PrefillingConfigMapAdapter extends XmlAdapter<Object, Map<String, PrefillingConfig>> {
@Override
public Map<String, PrefillingConfig> unmarshal(Object v) throws Exception {
Map<String, PrefillingConfig> configMap = new HashMap<>();
Element element = (Element) v;
JAXBContext jaxbContext = JAXBContext.newInstance(PrefillingConfig.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
for (int i = 0; i < element.getChildNodes().getLength(); i++) {
if (element.getChildNodes().item(i).getAttributes() == null) {
continue;
}
String id = element.getChildNodes().item(i).getAttributes().getNamedItem("id").getNodeValue();
PrefillingConfig prefillingConfig = (PrefillingConfig) jaxbUnmarshaller.unmarshal(element.getChildNodes().item(i));
prefillingConfig = configMap.put(id, prefillingConfig);
System.out.println(prefillingConfig);
}
return configMap;
}
@Override
public Object marshal(Map<String, PrefillingConfig> v) throws Exception {
return null;
}
}

View File

@ -1,44 +0,0 @@
package eu.eudat.service.externalfetcher.models;
public class ExternalAutocompleteFieldResult {
private String id;
private String label;
private String source;
private String uri;
public ExternalAutocompleteFieldResult(String id, String label, String source, String uri) {
this.id = id;
this.label = label;
this.source = source;
this.uri = uri;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
}

View File

@ -1,24 +0,0 @@
package eu.eudat.service.prefilling;
import eu.eudat.model.Description;
import eu.eudat.model.Prefilling;
import eu.eudat.model.PrefillingLookup;
import eu.eudat.model.persist.DescriptionProfilingRequest;
import eu.eudat.model.persist.DescriptionProfilingWithDataRequest;
import jakarta.xml.bind.JAXBException;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public interface PrefillingService {
List<Prefilling> getPrefillings(PrefillingLookup like);
Description getPrefilledDescription(DescriptionProfilingRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException;
Description getPrefilledDescriptionUsingData(DescriptionProfilingWithDataRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException;
}

View File

@ -1,493 +0,0 @@
package eu.eudat.service.prefilling;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.XmlHandlingService;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.commons.types.prefillingsource.PrefillingEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionTemplateEntity;
import eu.eudat.model.*;
import eu.eudat.model.builder.DescriptionTemplateBuilder;
import eu.eudat.model.builder.PrefillingBuilder;
import eu.eudat.model.descriptionproperties.Field;
import eu.eudat.model.descriptionproperties.PropertyDefinition;
import eu.eudat.model.descriptionproperties.PropertyDefinitionFieldSet;
import eu.eudat.model.descriptionproperties.PropertyDefinitionFieldSetItem;
import eu.eudat.model.persist.DescriptionProfilingRequest;
import eu.eudat.model.persist.DescriptionProfilingWithDataRequest;
import eu.eudat.service.externalfetcher.config.entities.*;
import eu.eudat.service.externalfetcher.criteria.ExternalReferenceCriteria;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory;
import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class PrefillingServiceImpl implements PrefillingService {
private final static String Zenodo = "zenodo";
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PrefillingServiceImpl.class));
private final EntityManager entityManager;
private final BuilderFactory builderFactory;
private final ConventionService conventionService;
private final MessageSource messageSource;
private final XmlHandlingService xmlHandlingService;
private final ValidatorFactory validatorFactory;
@Autowired
public PrefillingServiceImpl(
EntityManager entityManager,
BuilderFactory builderFactory,
ConventionService conventionService,
MessageSource messageSource,
XmlHandlingService xmlHandlingService,
ValidatorFactory validatorFactory) {
this.entityManager = entityManager;
this.builderFactory = builderFactory;
this.conventionService = conventionService;
this.messageSource = messageSource;
this.xmlHandlingService = xmlHandlingService;
this.validatorFactory = validatorFactory;
}
@Override
public List<Prefilling> getPrefillings(PrefillingLookup lookup) {
logger.debug(new MapLogEntry("persisting data").And("lookup", lookup));
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria();
externalReferenceCriteria.setLike(lookup.getLike());
List<PrefillingEntity> prefillings = new ArrayList<>();
List<Map<String, String>> map;
// for (PrefillingConfig prefillingConfig: prefillingConfigs.values()) {//TODO new reference logic
// map = remoteFetcherService.getExternalData(prefillingConfig.getPrefillingSearch().getUrls(), externalReferenceCriteria, prefillingConfig.getPrefillingSearch().getFetchMode());
// prefillings.addAll(map.stream().map(submap -> PrefillingEntity.build(submap, this.jsonHandlingService)).toList());
// if (prefillingConfig.getPrefillingSearch().getUrlConfig().isDataInListing()) {
// List<Map<String, Object>> mapData = remoteFetcherService.getExternalGenericWithData(externalReferenceCriteria, prefillingConfig.getPrefillingSearch());
// for (int i = 0; i < mapData.size(); i++) {
// prefillings.get(i).setData(mapData.get(i));
// }
// prefillings = prefillings.stream().filter(prefilling -> prefilling.getData() != null).collect(Collectors.toList());
// }
// }
return this.builderFactory.builder(PrefillingBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(lookup.getProject(), Prefilling._pid), prefillings);
}
@Override
public Description getPrefilledDescription(DescriptionProfilingRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException {
PrefillingConfig prefillingConfig = null;
PrefillingGet prefillingGet = prefillingConfig.getPrefillingGet();
Map<String, Object> prefillingEntity = getSinglePrefillingData(prefillingGet.getUrl(), model.getPrefillId());
DescriptionProfilingWithDataRequest descriptionProfilingWithDataRequest = new DescriptionProfilingWithDataRequest();
descriptionProfilingWithDataRequest.setConfigId(model.getConfigId());
descriptionProfilingWithDataRequest.setProject(model.getProject());
descriptionProfilingWithDataRequest.setDescriptionTemplateId(model.getDescriptionTemplateId());
descriptionProfilingWithDataRequest.setData(prefillingEntity);
validatorFactory.validator(DescriptionProfilingWithDataRequest.DescriptionProfilingWithDataRequestValidator.ValidatorName).validateForce(descriptionProfilingWithDataRequest);
return this.getPrefilledDescriptionUsingData(descriptionProfilingWithDataRequest);
}
private Map<String, Object> getSinglePrefillingData(String url, String id) {
RestTemplate restTemplate = new RestTemplate();
String parsedUrl = url.replace("{id}", id);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity("", headers);
return restTemplate.exchange(parsedUrl, HttpMethod.GET, entity, LinkedHashMap.class).getBody();
}
@Override
public Description getPrefilledDescriptionUsingData(DescriptionProfilingWithDataRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException {
PrefillingConfig prefillingConfig = null;
PrefillingGet prefillingGet = prefillingConfig.getPrefillingGet();
DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getDescriptionTemplateId());
if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinition = this.xmlHandlingService.fromXml(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition());
Description description = new Description();
description.setDescriptionTemplate(this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(model.getProject(), descriptionTemplateEntity));
return mapPrefilledEntityToDescription(description, model.getData(), prefillingGet, prefillingConfig.getType(), descriptionTemplateDefinition);
}
//region Description Mapping
private Object getValueFromPath(Map<String, Object> data, String path){
if (path == null || this.conventionService.isNullOrEmpty(path) || data == null) return null;
if (path.contains(".")){
String[] paths = path.split("\\.");
if (paths.length == 0) return null;
else {
Object value = data.getOrDefault(paths[0], null);
if (value instanceof Map) return this.getValueFromPath((Map) value, path.substring(paths[0].length() + 1));
else return null;
}
}else {
return data.getOrDefault(path, null);
}
}
private Description mapPrefilledEntityToDescription(Description description, Map<String, Object> prefilledData, PrefillingGet prefillingGet, String type, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinition){
for (DefaultPrefillingMapping prefillingMapping: prefillingGet.getMappings()) {
Object sourceValue = this.getValueFromPath(prefilledData, prefillingMapping.getSource());
try {
setValueToDescription(description, prefillingMapping, sourceValue, descriptionTemplateDefinition, type);
}
catch (Exception e) {
logger.warn("Couldn't map " + (this.conventionService.isNullOrEmpty(prefillingMapping.getSemanticTarget()) ? prefillingMapping.getTarget() : prefillingMapping.getSemanticTarget()));
}
}
for (PrefillingFixedMapping fixedMapping: prefillingGet.getFixedMappings()) {
try {
setValueToDescription(description, fixedMapping, fixedMapping.getValue(), descriptionTemplateDefinition, type);
}
catch (Exception e) {
logger.warn("Couldn't map " + (this.conventionService.isNullOrEmpty(fixedMapping.getSemanticTarget()) ? fixedMapping.getTarget() : fixedMapping.getSemanticTarget()));
}
}
return description;
}
private void setValueToDescription(Description description, PrefillingMapping prefillingMapping, Object value, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition, String type) {
JsonNode valueNode = new ObjectMapper().valueToTree(value);
String parsedValue = this.getValueAsString(prefillingMapping, valueNode);
List<String> parsedValues = this.getValueAsStringArray(prefillingMapping, valueNode);
if (prefillingMapping.getTarget() != null) {
this.applyValueToDescriptionObject(description, prefillingMapping, parsedValue, parsedValues);
} else {
// zenodo prefilling customizations
if(type.equals(PrefillingServiceImpl.Zenodo)){
if(prefillingMapping.getSemanticTarget().equals("rda.dataset.distribution.data_access")){
if(parsedValue != null && parsedValue.equals("open")){
List<FieldEntity> issuedFieldEntities = definition.getAllField().stream().filter(x-> x.getSchematics() != null && x.getSchematics().contains("rda.dataset.issued")).toList();
if(this.conventionService.isListNullOrEmpty(issuedFieldEntities)){
String issuedIdNode = issuedFieldEntities.getFirst().getId();
String issuedValue = description.getProperties().getFieldSets().values().stream().map(PropertyDefinitionFieldSet::getItems).flatMap(List::stream)
.filter(x-> x.getFields() != null && x.getFields().containsKey(issuedIdNode)).map(x-> x.getFields().get(issuedIdNode).getTextValue()).findFirst().orElse(null); //TODO Update with the new logic of property definition
List<FieldSetEntity> licStartFieldSetsEntities = definition.getAllFieldSets().stream().filter(x-> x.getAllField() != null && x.getAllField().stream().anyMatch(y-> y.getSchematics() != null && y.getSchematics().contains("rda.dataset.distribution.license.start_date"))).toList();
for (FieldSetEntity licStartFieldSetEntity: licStartFieldSetsEntities) {
List<FieldEntity> licStartEntities = licStartFieldSetEntity.getAllField().stream().filter(x -> x.getSchematics() != null && x.getSchematics().contains("rda.dataset.distribution.license.start_date")).toList();
if (!this.conventionService.isListNullOrEmpty(licStartEntities)) {
this.ensureFieldSetEntity(description, licStartFieldSetEntity);
for (FieldEntity licStartDateNode : licStartEntities) {
description.getProperties().getFieldSets().get(licStartFieldSetEntity.getId()).getItems().add(buildPropertyDefinitionFieldSetItemValue(licStartDateNode, issuedValue, parsedValues, type));
}
}
}
}
}
}
if (prefillingMapping.getSemanticTarget().equals("rda.dataset.distribution.available_until") && parsedValue != null && !parsedValue.equals("null")) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(parsedValue, formatter);
date = date.plusYears(20);
parsedValue = date.toString();
}
}
List<FieldSetEntity> fieldSetsEntities = definition.getAllFieldSets().stream().filter(x-> x.getAllField() != null && x.getAllField().stream().anyMatch(y-> y.getSchematics() != null && y.getSchematics().contains(prefillingMapping.getSemanticTarget()))).toList();
for (FieldSetEntity fieldSetEntity: fieldSetsEntities) {
List<FieldEntity> fieldEntities = fieldSetEntity.getAllField().stream().filter(x-> x.getSchematics() != null && x.getSchematics().contains(prefillingMapping.getSemanticTarget())).toList();
if (!this.conventionService.isListNullOrEmpty(fieldEntities)) {
this.ensureFieldSetEntity(description, fieldSetEntity);
for (FieldEntity fieldEntity : fieldEntities){
description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().add(buildPropertyDefinitionFieldSetItemValue(fieldEntity, parsedValue, parsedValues, type));
}
}
}
}
}
private void ensureFieldSetEntity(Description description, FieldSetEntity fieldSetEntity){
if (description.getProperties() == null) description.setProperties(new PropertyDefinition());
if (description.getProperties().getFieldSets() == null) description.getProperties().setFieldSets(new HashMap<>());
if (!description.getProperties().getFieldSets().containsKey(fieldSetEntity.getId())) description.getProperties().getFieldSets().put(fieldSetEntity.getId(), new PropertyDefinitionFieldSet());
if (description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems() == null) description.getProperties().getFieldSets().get(fieldSetEntity.getId()).setItems(new ArrayList<>());
}
private void applyValueToDescriptionObject(Description description, PrefillingMapping prefillingMapping, String parsedValue, List<String> parsedValues){
switch (prefillingMapping.getTarget()){
case Description._description:{
description.setDescription(parsedValue);
}
case Description._label:{
description.setLabel(parsedValue);
}
case Description._descriptionTags:{
if (!parsedValues.isEmpty()){
for (String tagString : parsedValues){
if (description.getDescriptionTags() == null) description.setDescriptionTags(new ArrayList<>());
if(description.getDescriptionTags().stream().anyMatch(x-> x.getTag() !=null && x.getTag().getLabel().equals(tagString))) continue;
DescriptionTag descriptionTag = new DescriptionTag();
Tag tag = new Tag();
tag.setLabel(tagString.trim());
descriptionTag.setTag(tag);
description.getDescriptionTags().add(descriptionTag);
}
}
}
}
}
private PropertyDefinitionFieldSetItem buildPropertyDefinitionFieldSetItemValue(FieldEntity fieldEntity, String parsedValue, List<String> parsedValues, String type){
String id = fieldEntity.getId();
PropertyDefinitionFieldSetItem fieldSetItem = new PropertyDefinitionFieldSetItem();
fieldSetItem.setFields(new HashMap<>());
Field field = new Field();
//TODO Update with the new logic of property definition
// switch (fieldEntity.getData().getFieldType()) {
// case EXTERNAL_SELECT:
// case SELECT: {
// if (!parsedValues.stream().allMatch(Objects::isNull)) {
// field.setValue(this.jsonHandlingService.toJsonSafe(parseComboBoxValues(fieldEntity, parsedValues)));
// }
// break;
// }
// case TAGS: {
// if (!parsedValues.isEmpty()) {
// field.setValue(jsonHandlingService.toJsonSafe(parseTags(String.join(", ", parsedValues))));
// }
// break;
// }
// case DATASET_IDENTIFIER: {
// JSONObject datasetID = new JSONObject();
// datasetID.put("identifier", parsedValue);
// if (type.equals(PrefillingServiceImpl.Zenodo)) {
// datasetID.put("type", "doi");
// }
// field.setValue(datasetID.toString());
// break;
// }
// case LICENSES: {
// List<ReferenceEntity> licenses = this.queryFactory.query(ReferenceQuery.class).references(parsedValues).collect();
// LabelAndMultiplicityDataEntity licenseDataEntity = (LabelAndMultiplicityDataEntity) fieldEntity.getData();
// if (licenses != null && !licenses.isEmpty() && licenseDataEntity != null) {
// boolean isMultiAutocomplete = licenseDataEntity.getMultipleSelect();
// List<Reference> licenseModels = this.builderFactory.builder(ReferenceBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(new BaseFieldSet(), licenses);
// if (isMultiAutocomplete) {
// field.setValue(jsonHandlingService.toJsonSafe(licenseModels));
// } else {
// field.setValue(jsonHandlingService.toJsonSafe(licenseModels.getFirst()));
// }
// }
//
// break;
// }
// default:
// field.setValue(parsedValue);
// break;
// }
fieldSetItem.getFields().put(fieldEntity.getId(), field);
return fieldSetItem;
}
private List<String> getValueAsStringArray(PrefillingMapping prefillingMapping, Object value){
String trimRegex = !this.conventionService.isNullOrEmpty(prefillingMapping.getTrimRegex()) ? prefillingMapping.getTrimRegex() : "";
List<String> parsedValues = new ArrayList<>();
if (value instanceof String){
parsedValues.add(((String) value).replace(trimRegex, ""));
} else if (value.getClass().isArray()){
if (value instanceof String[]){
List<String> values = new LinkedList<>();
for (String val : (String[]) value) {
parsedValues.add(val.replace(trimRegex, ""));
}
} else {
if (prefillingMapping.getSubSource() == null || prefillingMapping.getSubSource().isEmpty()) {
throw new MyApplicationException("Source value is an array but no subSource field have been set");
}
for (Object obj : (Object[]) value) {
if (obj instanceof Map) {
Object property = ((Map)obj).getOrDefault(prefillingMapping.getSubSource(), null);
if (property instanceof String) {
parsedValues.add(((String) property).replaceAll(trimRegex, ""));
}
}
}
}
}
parsedValues = parsedValues.stream().distinct().collect(Collectors.toList());
return parsedValues;
}
private String getValueAsString(PrefillingMapping prefillingMapping, Object value){
List<String> parsedValues = this.getValueAsStringArray(prefillingMapping, value);
return parsedValues.isEmpty() ? null : String.join(", ", parsedValues);
}
// private Object parseComboBoxValues(FieldEntity fieldEntity, List<String> parsedValues) {
// List<String> normalizedValues = new ArrayList<>();
// boolean isMultiSelect;
// if(fieldEntity.getData().getFieldType().equals(FieldType.EXTERNAL_SELECT)) {
// ExternalSelectDataEntity autoCompleteData = (ExternalSelectDataEntity)fieldEntity.getData();
// isMultiSelect = autoCompleteData.getMultipleSelect();
// for (String format : parsedValues) {
// List<ExternalAutocompleteFieldResult> result = new ArrayList<>();
// try {
// result = this.getAutocomplete(autoCompleteData, format);
// }
// catch (Exception e) {
// logger.error(e.getMessage(), e);
// }
// result = result.stream().filter(StreamDistinctBy.distinctByKey(ExternalAutocompleteFieldResult::getId)).collect(Collectors.toList());
// if(!result.isEmpty()){
// List<String> tempValues = new LinkedList<>();
// for (ExternalAutocompleteFieldResult f : result) {
// if (format.equals(f.getId()) || f.getLabel().toUpperCase(Locale.ROOT).contains(format.toUpperCase(Locale.ROOT)))
// tempValues.add(this.jsonHandlingService.toJsonSafe(f));
// }
// if (isMultiSelect)
// normalizedValues.addAll(tempValues);
// else if (!tempValues.isEmpty())
// normalizedValues.add(tempValues.get(0));
// }
// }
// return !normalizedValues.isEmpty() ? (isMultiSelect ? normalizedValues : normalizedValues.getFirst()) : null;
// } else {
// SelectDataEntity selectDataEntity = (SelectDataEntity)fieldEntity.getData();
// isMultiSelect = selectDataEntity.getMultipleSelect();
// if (selectDataEntity.getOptions() != null) {
// for (SelectDataEntity.OptionEntity option : selectDataEntity.getOptions()) {
// if (parsedValues.contains(option.getValue())) {
// normalizedValues.add(option.getValue());
// }
// }
// }
// List<String> normalizedStringValues = normalizedValues.stream().map(Object::toString).collect(Collectors.toList());
//
// return !normalizedValues.isEmpty() ? (isMultiSelect ? String.join(", ", normalizedStringValues) : normalizedValues.getFirst()) : null;
// }
// }
private static List<Tag> parseTags(String value) {
if (value == null || value.isEmpty())
return new LinkedList<>();
String[] rawTags = value.split(",");
List<Tag> parsedTags = new LinkedList<>();
for (String rawTag : rawTags) {
Tag tag = new Tag();
tag.setLabel(rawTag.trim());
parsedTags.add(tag);
}
return parsedTags;
}
// public List<ExternalAutocompleteFieldResult> getAutocomplete(ExternalSelectDataEntity data, String like) throws URISyntaxException {
//
// List<ExternalAutocompleteFieldResult> result = new LinkedList<>();
// ExternalReferenceCriteria urlCriteria = new ExternalReferenceCriteria();
// GeneralUrls genericUrls = new GeneralUrls();
// int ordinal = 1;
// List<Map<String, String>> rawResults = new ArrayList<>();
// genericUrls.setFetchMode(FetchStrategy.FIRST);
// urlCriteria.setLike(like);
// for (ExternalSelectDataEntity.ExternalSelectSourceEntity singleData : data.getSources()) {
// UrlConfiguration urlConfiguration = new UrlConfiguration();
// try {
// URI uri;
// if (singleData.getUrl().contains("?")) {
// uri = new URI(singleData.getUrl().substring(0, singleData.getUrl().lastIndexOf("?")));
// } else {
// uri = new URI(singleData.getUrl());
// }
// String source = singleData.getSourceBinding().getSource();
// source = source != null && !source.isEmpty() ? source : uri.getHost();
//// String uriString = singleData.getSourceBinding().getUrl();
//// uriString = uriString != null && !uriString.isEmpty() ? uriString : "uri";
// String uriString = "uri";
// String parsedUrl = singleData.getUrl();
// parsedUrl = parsedUrl.replace("%20", " ");
// parsedUrl = parsedUrl.replace("%22", "\"");
// while (parsedUrl.contains("&amp;")) {
// parsedUrl = parsedUrl.replace("&amp;", "&");
// }
// urlConfiguration.setUrl(parsedUrl);
// urlConfiguration.setOrdinal(ordinal);
// urlConfiguration.setType("External");
// urlConfiguration.setContentType(MediaType.APPLICATION_JSON_VALUE);
// urlConfiguration.setFirstpage("1");
// urlConfiguration.setRequestType(singleData.getMethod() != null ? singleData.getMethod() : "GET");
// DataUrlConfiguration dataUrlConfiguration = new DataUrlConfiguration();
// dataUrlConfiguration.setPath(singleData.getOptionsRoot());
// DataFieldsUrlConfiguration fieldsUrlConfiguration = new DataFieldsUrlConfiguration();
// fieldsUrlConfiguration.setId(singleData.getSourceBinding().getValue());
// fieldsUrlConfiguration.setName(singleData.getSourceBinding().getLabel());
// fieldsUrlConfiguration.setSource(singleData.getSourceBinding().getSource().isEmpty()? null : singleData.getSourceBinding().getSource());
// fieldsUrlConfiguration.setUri(uriString);
// dataUrlConfiguration.setFieldsUrlConfiguration(fieldsUrlConfiguration);
// urlConfiguration.setKey(source);
// urlConfiguration.setLabel(source);
// urlConfiguration.setData(dataUrlConfiguration);
// if (singleData.getHasAuth()) {
// AuthenticationConfiguration authenticationConfiguration = new AuthenticationConfiguration();
// authenticationConfiguration.setAuthUrl(singleData.getAuth().getUrl());
// authenticationConfiguration.setAuthMethod(singleData.getAuth().getMethod());
// authenticationConfiguration.setAuthTokenPath(singleData.getAuth().getPath());
// authenticationConfiguration.setAuthRequestBody(singleData.getAuth().getBody());
// authenticationConfiguration.setType(singleData.getAuth().getType());
// urlConfiguration.setAuth(authenticationConfiguration);
// }
// genericUrls.getUrls().add(urlConfiguration);
// List<Map<String, String>> singleResults = this.remoteFetcherService.getExternalGeneric(urlCriteria, genericUrls);
// if (!singleResults.isEmpty() && !singleResults.get(0).containsKey("source") && !singleData.getSourceBinding().getSource().isEmpty()) {
// singleResults.forEach(singleResult -> singleResult.put("source", singleData.getSourceBinding().getSource()));
// }
// rawResults.addAll(singleResults);
// genericUrls.getUrls().clear();
// } catch (URISyntaxException e) {
// logger.error(e.getMessage(), e);
// }
// }
// rawResults.forEach(item -> result.add(new ExternalAutocompleteFieldResult(parseItem(item.get("pid")), parseItem(item.get("name")), parseItem(item.get("source")), parseItem(item.get("uri")))));
// return result;
// }
private static String parseItem(Object item) {
if (item instanceof String) {
return (String) item;
}
if (item instanceof List) {
List listedItems = (List) item;
return parseItem(listedItems.get(0));
}
if (item instanceof Map) {
return String.valueOf(((Map)item).get("$"));
}
return item != null ? item.toString() : null;
}
//endregion Description Mapping
}

View File

@ -1,14 +1,15 @@
package eu.eudat.service.prefillingsource; package eu.eudat.service.prefillingsource;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.authorization.Permission; import eu.eudat.authorization.Permission;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.XmlHandlingService; import eu.eudat.commons.XmlHandlingService;
import eu.eudat.commons.enums.IsActive; import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.types.description.ExternalIdentifierEntity;
import eu.eudat.commons.types.descriptiontemplate.DefinitionEntity; import eu.eudat.commons.types.descriptiontemplate.DefinitionEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity; import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity; import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.commons.types.descriptiontemplate.fielddata.SelectDataEntity;
import eu.eudat.commons.types.externalfetcher.*; import eu.eudat.commons.types.externalfetcher.*;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionEntity; import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionEntity;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionFieldEntity; import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionFieldEntity;
@ -21,18 +22,16 @@ import eu.eudat.model.*;
import eu.eudat.model.builder.DescriptionTemplateBuilder; import eu.eudat.model.builder.DescriptionTemplateBuilder;
import eu.eudat.model.builder.PrefillingSourceBuilder; import eu.eudat.model.builder.PrefillingSourceBuilder;
import eu.eudat.model.deleter.PrefillingSourceDeleter; import eu.eudat.model.deleter.PrefillingSourceDeleter;
import eu.eudat.model.descriptionproperties.Field; import eu.eudat.model.descriptionproperties.*;
import eu.eudat.model.descriptionproperties.PropertyDefinition;
import eu.eudat.model.descriptionproperties.PropertyDefinitionFieldSet;
import eu.eudat.model.descriptionproperties.PropertyDefinitionFieldSetItem;
import eu.eudat.model.externalfetcher.ExternalFetcherApiSourceConfiguration;
import eu.eudat.model.persist.DescriptionProfilingRequest; import eu.eudat.model.persist.DescriptionProfilingRequest;
import eu.eudat.model.persist.DescriptionProfilingWithDataRequest; import eu.eudat.model.persist.DescriptionProfilingWithDataRequest;
import eu.eudat.model.persist.PrefillingSourcePersist; import eu.eudat.model.persist.PrefillingSourcePersist;
import eu.eudat.model.persist.dmpblueprintdefinition.FieldPersist;
import eu.eudat.model.persist.externalfetcher.*; import eu.eudat.model.persist.externalfetcher.*;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFieldPersist; import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFieldPersist;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFixedValueFieldPersist; import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFixedValueFieldPersist;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionPersist; import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionPersist;
import eu.eudat.model.prefillingsourcedefinition.PrefillingSourceDefinition;
import eu.eudat.query.PrefillingSourceQuery; import eu.eudat.query.PrefillingSourceQuery;
import eu.eudat.service.externalfetcher.ExternalFetcherService; import eu.eudat.service.externalfetcher.ExternalFetcherService;
import eu.eudat.service.externalfetcher.models.ExternalDataResult; import eu.eudat.service.externalfetcher.models.ExternalDataResult;
@ -51,6 +50,8 @@ import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.ValidatorFactory;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
@ -63,7 +64,9 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*; import java.util.*;
@Service @Service
@ -81,12 +84,14 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
private final ExternalFetcherService externalFetcherService; private final ExternalFetcherService externalFetcherService;
private final ErrorThesaurusProperties errors; private final ErrorThesaurusProperties errors;
private final ValidatorFactory validatorFactory; private final ValidatorFactory validatorFactory;
private final JsonHandlingService jsonHandlingService;
private static final String Zenodo = "zenodo";
public PrefillingSourceServiceImpl( public PrefillingSourceServiceImpl(
EntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory, EntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory,
QueryFactory queryFactory, ConventionService conventionService, MessageSource messageSource, QueryFactory queryFactory, ConventionService conventionService, MessageSource messageSource,
XmlHandlingService xmlHandlingService, ExternalFetcherService externalFetcherService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory) { XmlHandlingService xmlHandlingService, ExternalFetcherService externalFetcherService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, JsonHandlingService jsonHandlingService) {
this.entityManager = entityManager; this.entityManager = entityManager;
this.authorizationService = authorizationService; this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory; this.deleterFactory = deleterFactory;
@ -98,6 +103,7 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
this.externalFetcherService = externalFetcherService; this.externalFetcherService = externalFetcherService;
this.errors = errors; this.errors = errors;
this.validatorFactory = validatorFactory; this.validatorFactory = validatorFactory;
this.jsonHandlingService = jsonHandlingService;
} }
@ -297,16 +303,13 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
} }
public Description getPrefilledDescription(DescriptionProfilingRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException { public Description getPrefilledDescription(DescriptionProfilingRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException {
PrefillingSourceEntity prefillingSourceEntity = this.queryFactory.query(PrefillingSourceQuery.class).ids(UUID.fromString(model.getPrefillId())).first(); PrefillingSourceEntity prefillingSourceEntity = this.queryFactory.query(PrefillingSourceQuery.class).ids(UUID.fromString(model.getPrefillId())).isActive(IsActive.Active).first();
if (prefillingSourceEntity == null){ if (prefillingSourceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getPrefillId(), PrefillingSource.class.getSimpleName()}, LocaleContextHolder.getLocale()));
return null;
}
DescriptionProfilingWithDataRequest descriptionProfilingWithDataRequest = new DescriptionProfilingWithDataRequest(); DescriptionProfilingWithDataRequest descriptionProfilingWithDataRequest = new DescriptionProfilingWithDataRequest();
descriptionProfilingWithDataRequest.setConfigId(model.getConfigId()); descriptionProfilingWithDataRequest.setConfigId(model.getConfigId());
descriptionProfilingWithDataRequest.setProject(model.getProject()); descriptionProfilingWithDataRequest.setProject(model.getProject());
descriptionProfilingWithDataRequest.setDescriptionTemplateId(model.getDescriptionTemplateId()); descriptionProfilingWithDataRequest.setDescriptionTemplateId(model.getDescriptionTemplateId());
// descriptionProfilingWithDataRequest.setData(prefillingEntity);
validatorFactory.validator(DescriptionProfilingWithDataRequest.DescriptionProfilingWithDataRequestValidator.ValidatorName).validateForce(descriptionProfilingWithDataRequest); validatorFactory.validator(DescriptionProfilingWithDataRequest.DescriptionProfilingWithDataRequestValidator.ValidatorName).validateForce(descriptionProfilingWithDataRequest);
return this.getPrefilledDescriptionUsingData(descriptionProfilingWithDataRequest); return this.getPrefilledDescriptionUsingData(descriptionProfilingWithDataRequest);
} }
@ -314,98 +317,75 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
public Description getPrefilledDescriptionUsingData(DescriptionProfilingWithDataRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException { public Description getPrefilledDescriptionUsingData(DescriptionProfilingWithDataRequest model) throws JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException {
PrefillingSourceEntity prefillingSourceEntity = this.queryFactory.query(PrefillingSourceQuery.class).ids(UUID.fromString(model.getConfigId())).first(); PrefillingSourceEntity prefillingSourceEntity = this.queryFactory.query(PrefillingSourceQuery.class).ids(UUID.fromString(model.getConfigId())).first();
if (prefillingSourceEntity == null){ if (prefillingSourceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getConfigId(), PrefillingSource.class.getSimpleName()}, LocaleContextHolder.getLocale()));
PrefillingSourceDefinitionEntity prefillingSourceDefinition = this.xmlHandlingService.fromXmlSafe(PrefillingSourceDefinitionEntity.class, prefillingSourceEntity.getDefinition());
if (prefillingSourceDefinition == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getConfigId(), PrefillingSourceDefinition.class.getSimpleName()}, LocaleContextHolder.getLocale()));
ExternalDataResult externalData = this.externalFetcherService.getExternalData(List.of(prefillingSourceDefinition.getGetConfiguration()), null, null);
if (externalData == null || externalData.getResults() == null) {
return null; return null;
} }
PrefillingSourceDefinitionEntity prefillingSourceDefinition = this.xmlHandlingService.fromXmlSafe(PrefillingSourceDefinitionEntity.class, prefillingSourceEntity.getDefinition());
ExternalDataResult data = this.externalFetcherService.getExternalData(List.of(prefillingSourceDefinition.getGetConfiguration()), null, null);
if (data == null || data.getResults() == null){
return null;
}
DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getDescriptionTemplateId()); DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getDescriptionTemplateId());
if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinition = this.xmlHandlingService.fromXml(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition()); eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinition = this.xmlHandlingService.fromXml(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition());
Description description = new Description(); Description description = new Description();
description.setDescriptionTemplate(this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(model.getProject(), descriptionTemplateEntity)); description.setDescriptionTemplate(this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(model.getProject(), descriptionTemplateEntity));
return mapPrefilledEntityToDescription(description, descriptionTemplateDefinition, prefillingSourceDefinition, prefillingSourceEntity.getLabel(), data.getResults()); return mapPrefilledEntityToDescription(description, descriptionTemplateDefinition, prefillingSourceDefinition, prefillingSourceEntity.getLabel(), externalData.getResults().getFirst());
} }
private Description mapPrefilledEntityToDescription(Description description, DefinitionEntity descriptionTemplateDefinition, PrefillingSourceDefinitionEntity prefillingSourceDefinition, String type, List<Map<String, String>> data){ //TODO
private Description mapPrefilledEntityToDescription(Description description, DefinitionEntity descriptionTemplateDefinition, PrefillingSourceDefinitionEntity prefillingSourceDefinition, String type, Map<String, String> externalData){
for (PrefillingSourceDefinitionFieldEntity field: prefillingSourceDefinition.getFields()) { for (PrefillingSourceDefinitionFieldEntity field: prefillingSourceDefinition.getFields()) {
String sourceValue = data.get(0).get(field.getCode()); String sourceValue = externalData.get(field.getCode());
try { this.prefillSystemValueToDescription(description, field.getSystemFieldTarget(), sourceValue);
this.setValueToDescription(description, field, sourceValue, descriptionTemplateDefinition, type); this.prefillSemanticValueToDescription(description, field.getSemanticTarget(), sourceValue, descriptionTemplateDefinition, type);
}
catch (Exception e) {
logger.warn("Couldn't map " + (this.conventionService.isNullOrEmpty(field.getSemanticTarget()) ? field.getSystemFieldTarget() : field.getSemanticTarget()));
}
} }
for (PrefillingSourceDefinitionFixedValueFieldEntity field: prefillingSourceDefinition.getFixedValueFields()) { for (PrefillingSourceDefinitionFixedValueFieldEntity field: prefillingSourceDefinition.getFixedValueFields()) {
try { this.prefillSystemValueToDescription(description, field.getSystemFieldTarget(), field.getFixedValue());
// this.setValueToDescription(description, field, field.getFixedValue(), descriptionTemplateDefinition, type); this.prefillSemanticValueToDescription(description, field.getSemanticTarget(), field.getFixedValue(), descriptionTemplateDefinition, type);
} }
catch (Exception e) {
logger.warn("Couldn't map " + (this.conventionService.isNullOrEmpty(field.getSemanticTarget()) ? field.getSystemFieldTarget() : field.getSemanticTarget())); for (PrefillingSourceDefinitionFieldEntity field: prefillingSourceDefinition.getFields()) {
String sourceValue = externalData.get(field.getCode());
this.ensureZenodoFields(description, field.getSemanticTarget(), sourceValue, descriptionTemplateDefinition, type);
} }
for (PrefillingSourceDefinitionFixedValueFieldEntity field: prefillingSourceDefinition.getFixedValueFields()) {
this.ensureZenodoFields(description, field.getSemanticTarget(), field.getFixedValue(), descriptionTemplateDefinition, type);
} }
return description; return description;
} }
private void setValueToDescription(Description description, PrefillingSourceDefinitionFieldEntity field, String value, DefinitionEntity definition, String type) { private void ensureZenodoFields(Description description, String semanticTarget, String value, DefinitionEntity definition, String type) {
// JsonNode valueNode = new ObjectMapper().valueToTree(value); if (!this.conventionService.isNullOrEmpty(type) && !this.conventionService.isNullOrEmpty(semanticTarget) && !this.conventionService.isNullOrEmpty(value) && type.equals(Zenodo)) {
// String parsedValue = this.getValueAsString(prefillingMapping, valueNode); if (semanticTarget.equals("rda.dataset.distribution.data_access")) {
// List<String> parsedValues = this.getValueAsStringArray(prefillingMapping, valueNode); if (value.equals("open")) {
if (field.getSemanticTarget() != null) { List<FieldEntity> issuedFieldEntities = definition.getAllField().stream().filter(x -> x.getSchematics() != null && x.getSchematics().contains("rda.dataset.issued")).toList();
this.applyValueToDescriptionObject(description, field, value, null); if (this.conventionService.isListNullOrEmpty(issuedFieldEntities)) {
} else {
// zenodo prefilling customizations
if(type.equals("Zenodo")){
if(field.getSemanticTarget().equals("rda.dataset.distribution.data_access")){
if(value != null && value.equals("open")){
List<FieldEntity> issuedFieldEntities = definition.getAllField().stream().filter(x-> x.getSchematics() != null && x.getSchematics().contains("rda.dataset.issued")).toList();
if(this.conventionService.isListNullOrEmpty(issuedFieldEntities)){
String issuedIdNode = issuedFieldEntities.getFirst().getId(); String issuedIdNode = issuedFieldEntities.getFirst().getId();
String issuedValue = description.getProperties().getFieldSets().values().stream().map(PropertyDefinitionFieldSet::getItems).flatMap(List::stream) String issuedValue = this.conventionService.isNullOrEmpty(issuedIdNode) ? null : description.getProperties().getFieldSets().values().stream().map(PropertyDefinitionFieldSet::getItems).flatMap(List::stream)
.filter(x-> x.getFields() != null && x.getFields().containsKey(issuedIdNode)).map(x-> x.getFields().get(issuedIdNode).getTextValue()).findFirst().orElse(null); //TODO Update with the new logic of property definition .filter(x -> x.getFields() != null && x.getFields().containsKey(issuedIdNode)).map(x -> x.getFields().get(issuedIdNode).getTextValue()).findFirst().orElse(null);
if (!this.conventionService.isNullOrEmpty(issuedValue)) {
List<FieldSetEntity> licStartFieldSetsEntities = definition.getAllFieldSets().stream().filter(x-> x.getAllField() != null && x.getAllField().stream().anyMatch(y-> y.getSchematics() != null && y.getSchematics().contains("rda.dataset.distribution.license.start_date"))).toList(); List<FieldSetEntity> licStartFieldSetsEntities = definition.getAllFieldSets().stream().filter(x -> x.getAllField() != null && x.getAllField().stream().anyMatch(y -> y.getSchematics() != null && y.getSchematics().contains("rda.dataset.distribution.license.start_date"))).toList();
for (FieldSetEntity licStartFieldSetEntity: licStartFieldSetsEntities) { for (FieldSetEntity licStartFieldSetEntity : licStartFieldSetsEntities) {
List<FieldEntity> licStartEntities = licStartFieldSetEntity.getAllField().stream().filter(x -> x.getSchematics() != null && x.getSchematics().contains("rda.dataset.distribution.license.start_date")).toList(); List<FieldEntity> licStartEntities = licStartFieldSetEntity.getAllField().stream().filter(x -> x.getSchematics() != null && x.getSchematics().contains("rda.dataset.distribution.license.start_date")).toList();
if (!this.conventionService.isListNullOrEmpty(licStartEntities)) { if (!this.conventionService.isListNullOrEmpty(licStartEntities)) {
this.ensureFieldSetEntity(description, licStartFieldSetEntity); this.ensureFieldSetEntity(description, licStartFieldSetEntity);
for (FieldEntity licStartDateNode : licStartEntities) { for (FieldEntity licStartDateNode : licStartEntities) {
description.getProperties().getFieldSets().get(licStartFieldSetEntity.getId()).getItems().add(buildPropertyDefinitionFieldSetItemValue(licStartDateNode, issuedValue, null, type)); description.getProperties().getFieldSets().get(licStartFieldSetEntity.getId()).getItems().getFirst().getFields().put(licStartDateNode.getId(), buildPropertyDefinitionFieldItemValue(licStartDateNode, semanticTarget, issuedValue, type));
} }
} }
} }
} }
} }
} }
if (field.getSemanticTarget().equals("rda.dataset.distribution.available_until") && value != null && !value.equals("null")) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(value, formatter);
date = date.plusYears(20);
value = date.toString();
}
}
List<FieldSetEntity> fieldSetsEntities = definition.getAllFieldSets().stream().filter(x-> x.getAllField() != null && x.getAllField().stream().anyMatch(y-> y.getSchematics() != null && y.getSchematics().contains(field.getSemanticTarget()))).toList();
for (FieldSetEntity fieldSetEntity: fieldSetsEntities) {
List<FieldEntity> fieldEntities = fieldSetEntity.getAllField().stream().filter(x-> x.getSchematics() != null && x.getSchematics().contains(field.getSemanticTarget())).toList();
if (!this.conventionService.isListNullOrEmpty(fieldEntities)) {
this.ensureFieldSetEntity(description, fieldSetEntity);
for (FieldEntity fieldEntity : fieldEntities){
description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().add(buildPropertyDefinitionFieldSetItemValue(fieldEntity, value, null, type));
}
}
} }
} }
} }
@ -415,22 +395,37 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
if (description.getProperties().getFieldSets() == null) description.getProperties().setFieldSets(new HashMap<>()); if (description.getProperties().getFieldSets() == null) description.getProperties().setFieldSets(new HashMap<>());
if (!description.getProperties().getFieldSets().containsKey(fieldSetEntity.getId())) description.getProperties().getFieldSets().put(fieldSetEntity.getId(), new PropertyDefinitionFieldSet()); if (!description.getProperties().getFieldSets().containsKey(fieldSetEntity.getId())) description.getProperties().getFieldSets().put(fieldSetEntity.getId(), new PropertyDefinitionFieldSet());
if (description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems() == null) description.getProperties().getFieldSets().get(fieldSetEntity.getId()).setItems(new ArrayList<>()); if (description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems() == null) description.getProperties().getFieldSets().get(fieldSetEntity.getId()).setItems(new ArrayList<>());
if (this.conventionService.isListNullOrEmpty(description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems())) description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().add(new PropertyDefinitionFieldSetItem());
if (description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().getFirst().getFields() == null) description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().getFirst().setFields(new HashMap<>());
if (description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().getFirst().getOrdinal() == null) description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().getFirst().setOrdinal(0);
} }
private void applyValueToDescriptionObject(Description description, PrefillingSourceDefinitionFieldEntity field, String parsedValue, List<String> parsedValues) { private void prefillSemanticValueToDescription(Description description, String semanticTarget, String parsedValue, DefinitionEntity definition, String type) {
switch (field.getSemanticTarget()) { if (this.conventionService.isNullOrEmpty(semanticTarget) || this.conventionService.isNullOrEmpty(parsedValue)) return;
case Description._description: { List<FieldSetEntity> fieldSetsEntities = definition.getAllFieldSets().stream().filter(x-> x.getAllField() != null && x.getAllField().stream().anyMatch(y-> !this.conventionService.isListNullOrEmpty(y.getSchematics()) && y.getSchematics().contains(semanticTarget))).toList();
description.setDescription(parsedValue); for (FieldSetEntity fieldSetEntity: fieldSetsEntities) {
List<FieldEntity> fieldEntities = fieldSetEntity.getAllField().stream().filter(x-> !this.conventionService.isListNullOrEmpty(x.getSchematics()) && x.getSchematics().contains(semanticTarget)).toList();
if (!this.conventionService.isListNullOrEmpty(fieldEntities)) {
this.ensureFieldSetEntity(description, fieldSetEntity);
for (FieldEntity fieldEntity : fieldEntities){
description.getProperties().getFieldSets().get(fieldSetEntity.getId()).getItems().getFirst().getFields().put(fieldEntity.getId() , buildPropertyDefinitionFieldItemValue(fieldEntity, semanticTarget, parsedValue, type));
} }
case Description._label: {
description.setLabel(parsedValue);
} }
case Description._descriptionTags: { }
if (!parsedValues.isEmpty()) { }
for (String tagString : parsedValues) {
private void prefillSystemValueToDescription(Description description, String systemFieldTarget, String parsedValue) {
if (this.conventionService.isNullOrEmpty(systemFieldTarget) || this.conventionService.isNullOrEmpty(parsedValue)) return;
switch (systemFieldTarget) {
case Description._description -> description.setDescription(parsedValue);
case Description._label -> description.setLabel(parsedValue);
case Description._descriptionTags -> {
String[] valuesParsed = this.tryParseJsonAsObjectString(String[].class, parsedValue);
List<String> finalValue = valuesParsed == null ? List.of(parsedValue) : Arrays.stream(valuesParsed).toList();
for (String tagString : finalValue) {
if (description.getDescriptionTags() == null) description.setDescriptionTags(new ArrayList<>()); if (description.getDescriptionTags() == null) description.setDescriptionTags(new ArrayList<>());
if (description.getDescriptionTags().stream().anyMatch(x -> x.getTag() != null && x.getTag().getLabel().equals(tagString))) if (description.getDescriptionTags().stream().anyMatch(x -> x.getTag() != null && x.getTag().getLabel().equals(tagString))) continue;
continue;
DescriptionTag descriptionTag = new DescriptionTag(); DescriptionTag descriptionTag = new DescriptionTag();
Tag tag = new Tag(); Tag tag = new Tag();
@ -441,16 +436,70 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
} }
} }
} }
private Field buildPropertyDefinitionFieldItemValue(FieldEntity fieldEntity, String semanticTarget, String value, String type) {
Field field = new Field();
if (fieldEntity == null || fieldEntity.getData() == null || fieldEntity.getData().getFieldType() == null || this.conventionService.isNullOrEmpty(value)) return field;
try{
switch (fieldEntity.getData().getFieldType()){
case FREE_TEXT, TEXT_AREA, RICH_TEXT_AREA, RADIO_BOX -> field.setTextValue(value.toLowerCase(Locale.ROOT));
case CHECK_BOX, BOOLEAN_DECISION -> field.setTextValue(value.trim().toLowerCase(Locale.ROOT));
case DATE_PICKER ->{
Instant instant = null;
try {
if (!this.conventionService.isNullOrEmpty(type) && type.equals(Zenodo) && semanticTarget.equals("rda.dataset.distribution.available_until") ) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(value, formatter);
date = date.plusYears(20);
instant = date.atStartOfDay().toInstant(ZoneOffset.UTC);
} else {
instant = Instant.parse(value);
}
} catch (DateTimeParseException ex) {
instant = LocalDate.parse(value).atStartOfDay().toInstant(ZoneOffset.UTC);
}
field.setDateValue(instant);
}
case DATASET_IDENTIFIER -> {
ExternalIdentifier externalIdentifier = new ExternalIdentifier();
externalIdentifier.setIdentifier(value);
if(!this.conventionService.isNullOrEmpty(type) && type.equals(Zenodo)) externalIdentifier.setType("doi");
field.setExternalIdentifier(externalIdentifier);
}
case SELECT -> {
String[] valuesParsed = this.tryParseJsonAsObjectString(String[].class, value);
List<String> finalValue = valuesParsed == null ? List.of(value) : Arrays.stream(valuesParsed).toList();
SelectDataEntity selectDataEntity = (SelectDataEntity) fieldEntity.getData();
if (selectDataEntity == null || selectDataEntity.getOptions() == null) throw new MyApplicationException("Can not cast fieldEntity data");
field.setTextListValue(new ArrayList<>());
for (SelectDataEntity.OptionEntity entity : selectDataEntity.getOptions()){
if (finalValue.contains(entity.getValue()) || finalValue.contains(entity.getLabel())){
field.getTextListValue().add(entity.getValue());
}
}
}
case TAGS, CURRENCY -> {
String[] valuesParsed = this.tryParseJsonAsObjectString(String[].class, value);
field.setTextListValue(valuesParsed == null ? List.of(value) : Arrays.stream(valuesParsed).toList()); //TODO Tags, Currency is ids
}
case REFERENCE_TYPES -> {
throw new NotImplementedException("");
}
case VALIDATION, UPLOAD, INTERNAL_ENTRIES_DMPS, INTERNAL_ENTRIES_DESCRIPTIONS -> throw new MyApplicationException("invalid type " + fieldEntity.getData().getFieldType());
default -> throw new MyApplicationException("invalid type " + fieldEntity.getData().getFieldType());
}
} catch (Exception e){
logger.error("Could not parse value " + value + " of field " + fieldEntity.getId() + " with type " + fieldEntity.getData().getFieldType(), e);
} }
private PropertyDefinitionFieldSetItem buildPropertyDefinitionFieldSetItemValue(FieldEntity fieldEntity, String value, List<String> parsedValues, String type) { return field;
String id = fieldEntity.getId(); }
PropertyDefinitionFieldSetItem fieldSetItem = new PropertyDefinitionFieldSetItem();
fieldSetItem.setFields(new HashMap<>());
Field field = new Field();
fieldSetItem.getFields().put(fieldEntity.getId(), field); private <T> T tryParseJsonAsObjectString(Class<T> type, String value){
return fieldSetItem; T item = this.jsonHandlingService.fromJsonSafe(type, value);
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJava(value));
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJson(value));
return item;
} }
} }

View File

@ -1,105 +0,0 @@
package eu.eudat.controllers;
import eu.eudat.audit.AuditableAction;
import gr.cite.tools.validation.ValidationFilterAnnotation;
import eu.eudat.model.Description;
import eu.eudat.model.Prefilling;
import eu.eudat.model.PrefillingLookup;
import eu.eudat.model.censorship.DescriptionCensor;
import eu.eudat.model.censorship.PrefillingCensor;
import eu.eudat.model.persist.DescriptionProfilingRequest;
import eu.eudat.model.persist.DescriptionProfilingWithDataRequest;
import eu.eudat.service.prefilling.PrefillingService;
import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.censor.CensorFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(path = "api/prefilling")
public class PrefillingController {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PrefillingController.class));
@Autowired
private final AuditService auditService;
private final PrefillingService prefillingService;
private final CensorFactory censorFactory;
public PrefillingController(
AuditService auditService,
PrefillingService prefillingService,
CensorFactory censorFactory) {
this.auditService = auditService;
this.prefillingService = prefillingService;
this.censorFactory = censorFactory;
}
@PostMapping("query")
public List<Prefilling> query(@RequestBody PrefillingLookup model) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
this.censorFactory.censor(PrefillingCensor.class).censor(model.getProject(), null);
logger.debug(new MapLogEntry("persisting" + Prefilling.class.getSimpleName()).And("model", model));
List<Prefilling> items = this.prefillingService.getPrefillings(model);
this.auditService.track(AuditableAction.Prefilling_Query, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
return items;
}
@PostMapping("generate")
@ValidationFilterAnnotation(validator = DescriptionProfilingRequest.DescriptionProfilingRequestValidator.ValidatorName, argumentName = "model")
public Description generate(@RequestBody DescriptionProfilingRequest model) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException {
logger.debug(new MapLogEntry("persisting" + Prefilling.class.getSimpleName()).And("model", model));
this.censorFactory.censor(DescriptionCensor.class).censor(model.getProject(), null);
Description item = this.prefillingService.getPrefilledDescription(model);
this.auditService.track(AuditableAction.Prefilling_Generate, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
return item;
}
@PostMapping("generate-with-data")
@ValidationFilterAnnotation(validator = DescriptionProfilingWithDataRequest.DescriptionProfilingWithDataRequestValidator.ValidatorName, argumentName = "model")
public Description generateWithData(@RequestBody DescriptionProfilingWithDataRequest model) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException {
logger.debug(new MapLogEntry("persisting" + Prefilling.class.getSimpleName()).And("model", model));
this.censorFactory.censor(DescriptionCensor.class).censor(model.getProject(), null);
Description item = this.prefillingService.getPrefilledDescriptionUsingData(model);
this.auditService.track(AuditableAction.Prefilling_GenerateWithData, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
return item;
}
}