502 lines
30 KiB
Java
502 lines
30 KiB
Java
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.JsonHandlingService;
|
|
import eu.eudat.commons.XmlHandlingService;
|
|
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
|
|
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
|
|
import eu.eudat.commons.types.prefilling.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.remotefetcher.ExternalUrlConfigProvider;
|
|
import eu.eudat.service.remotefetcher.RemoteFetcherService;
|
|
import eu.eudat.service.remotefetcher.config.entities.*;
|
|
import eu.eudat.service.remotefetcher.criteria.ExternalReferenceCriteria;
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
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 ExternalUrlConfigProvider externalUrlConfigProvider;
|
|
private final XmlHandlingService xmlHandlingService;
|
|
private final ValidatorFactory validatorFactory;
|
|
@Autowired
|
|
public PrefillingServiceImpl(
|
|
EntityManager entityManager,
|
|
BuilderFactory builderFactory,
|
|
ConventionService conventionService,
|
|
MessageSource messageSource,
|
|
ExternalUrlConfigProvider externalUrlConfigProvider,
|
|
XmlHandlingService xmlHandlingService,
|
|
ValidatorFactory validatorFactory) {
|
|
this.entityManager = entityManager;
|
|
this.builderFactory = builderFactory;
|
|
this.conventionService = conventionService;
|
|
this.messageSource = messageSource;
|
|
this.externalUrlConfigProvider = externalUrlConfigProvider;
|
|
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;
|
|
Map<String, PrefillingConfig> prefillingConfigs = this.externalUrlConfigProvider.getExternalUrls().getPrefillings();
|
|
// 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 = this.externalUrlConfigProvider.getExternalUrls().getPrefillings().get(model.getConfigId());
|
|
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 = this.externalUrlConfigProvider.getExternalUrls().getPrefillings().get(model.getConfigId());
|
|
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("&")) {
|
|
// parsedUrl = parsedUrl.replace("&", "&");
|
|
// }
|
|
// 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
|
|
}
|
|
|