prefiling changes

This commit is contained in:
Efstratios Giannopoulos 2024-03-01 10:54:52 +02:00
parent 46862e06cc
commit 25adaf23e2
2 changed files with 69 additions and 22 deletions

View File

@ -21,9 +21,9 @@ public class DescriptionProfilingWithDataRequest {
private Map<String, Object> data;
public static final String _data = "data";
private String configId;
private String prefillingSourceId;
public static final String _configId = "configId";
public static final String _prefillingSourceId = "prefillingSourceId";
private UUID descriptionTemplateId;
@ -40,12 +40,12 @@ public class DescriptionProfilingWithDataRequest {
this.data = data;
}
public String getConfigId() {
return configId;
public String getPrefillingSourceId() {
return prefillingSourceId;
}
public void setConfigId(String configId) {
this.configId = configId;
public void setPrefillingSourceId(String prefillingSourceId) {
this.prefillingSourceId = prefillingSourceId;
}
public UUID getDescriptionTemplateId() {
@ -90,8 +90,8 @@ public class DescriptionProfilingWithDataRequest {
.must(() -> this.isValidGuid(item.getDescriptionTemplateId()))
.failOn(DescriptionProfilingWithDataRequest._descriptionTemplateId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionProfilingWithDataRequest._descriptionTemplateId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getConfigId()))
.failOn(DescriptionProfilingWithDataRequest._configId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionProfilingWithDataRequest._configId}, LocaleContextHolder.getLocale())),
.must(() -> !this.isEmpty(item.getPrefillingSourceId()))
.failOn(DescriptionProfilingWithDataRequest._prefillingSourceId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionProfilingWithDataRequest._prefillingSourceId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> item.getData() != null)
.failOn(DescriptionProfilingWithDataRequest._data).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionProfilingWithDataRequest._data}, LocaleContextHolder.getLocale())),

View File

@ -5,18 +5,21 @@ import eu.eudat.authorization.Permission;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.XmlHandlingService;
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.FieldEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity;
import eu.eudat.commons.types.descriptiontemplate.fielddata.SelectDataEntity;
import eu.eudat.commons.types.externalfetcher.*;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionEntity;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionFieldEntity;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionFixedValueFieldEntity;
import eu.eudat.commons.types.referencetype.ReferenceTypeDefinitionEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionTemplateEntity;
import eu.eudat.data.PrefillingSourceEntity;
import eu.eudat.data.ReferenceEntity;
import eu.eudat.data.ReferenceTypeEntity;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import eu.eudat.model.*;
import eu.eudat.model.builder.DescriptionTemplateBuilder;
@ -26,15 +29,20 @@ import eu.eudat.model.descriptionproperties.*;
import eu.eudat.model.persist.DescriptionProfilingRequest;
import eu.eudat.model.persist.DescriptionProfilingWithDataRequest;
import eu.eudat.model.persist.PrefillingSourcePersist;
import eu.eudat.model.persist.dmpblueprintdefinition.FieldPersist;
import eu.eudat.model.persist.externalfetcher.*;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFieldPersist;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFixedValueFieldPersist;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionPersist;
import eu.eudat.model.prefillingsourcedefinition.PrefillingSourceDefinition;
import eu.eudat.model.referencedefinition.Definition;
import eu.eudat.query.PrefillingSourceQuery;
import eu.eudat.query.ReferenceTypeQuery;
import eu.eudat.query.lookup.ReferenceSearchLookup;
import eu.eudat.service.externalfetcher.ExternalFetcherService;
import eu.eudat.service.externalfetcher.config.entities.SourceBaseConfiguration;
import eu.eudat.service.externalfetcher.criteria.ExternalReferenceCriteria;
import eu.eudat.service.externalfetcher.models.ExternalDataResult;
import eu.eudat.service.reference.ReferenceService;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
@ -50,7 +58,6 @@ import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory;
import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
@ -67,7 +74,10 @@ import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class PrefillingSourceServiceImpl implements PrefillingSourceService {
@ -85,13 +95,14 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
private final ErrorThesaurusProperties errors;
private final ValidatorFactory validatorFactory;
private final JsonHandlingService jsonHandlingService;
private final ReferenceService referenceService;
private static final String Zenodo = "zenodo";
public PrefillingSourceServiceImpl(
EntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory,
QueryFactory queryFactory, ConventionService conventionService, MessageSource messageSource,
XmlHandlingService xmlHandlingService, ExternalFetcherService externalFetcherService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, JsonHandlingService jsonHandlingService) {
XmlHandlingService xmlHandlingService, ExternalFetcherService externalFetcherService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, JsonHandlingService jsonHandlingService, ReferenceService referenceService) {
this.entityManager = entityManager;
this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory;
@ -104,6 +115,7 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
this.errors = errors;
this.validatorFactory = validatorFactory;
this.jsonHandlingService = jsonHandlingService;
this.referenceService = referenceService;
}
@ -307,7 +319,7 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
if (prefillingSourceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getPrefillId(), PrefillingSource.class.getSimpleName()}, LocaleContextHolder.getLocale()));
DescriptionProfilingWithDataRequest descriptionProfilingWithDataRequest = new DescriptionProfilingWithDataRequest();
descriptionProfilingWithDataRequest.setConfigId(model.getConfigId());
descriptionProfilingWithDataRequest.setPrefillingSourceId(model.getConfigId());
descriptionProfilingWithDataRequest.setProject(model.getProject());
descriptionProfilingWithDataRequest.setDescriptionTemplateId(model.getDescriptionTemplateId());
validatorFactory.validator(DescriptionProfilingWithDataRequest.DescriptionProfilingWithDataRequestValidator.ValidatorName).validateForce(descriptionProfilingWithDataRequest);
@ -316,14 +328,14 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
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();
if (prefillingSourceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getConfigId(), PrefillingSource.class.getSimpleName()}, LocaleContextHolder.getLocale()));
PrefillingSourceEntity prefillingSourceEntity = this.queryFactory.query(PrefillingSourceQuery.class).ids(UUID.fromString(model.getPrefillingSourceId())).first();
if (prefillingSourceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getPrefillingSourceId(), 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()));
if (prefillingSourceDefinition == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getPrefillingSourceId(), PrefillingSourceDefinition.class.getSimpleName()}, LocaleContextHolder.getLocale()));
ExternalDataResult externalData = this.externalFetcherService.getExternalData(List.of(prefillingSourceDefinition.getGetConfiguration()), null, null);
if (externalData == null || externalData.getResults() == null) {
ExternalDataResult externalData = this.externalFetcherService.getExternalData(Stream.of(prefillingSourceDefinition.getGetConfiguration()).collect(Collectors.toList()), null, null);
if (externalData == null || this.conventionService.isListNullOrEmpty(externalData.getResults())) {
return null;
}
@ -333,10 +345,9 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
Description description = new Description();
description.setDescriptionTemplate(this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(model.getProject(), descriptionTemplateEntity));
return mapPrefilledEntityToDescription(description, descriptionTemplateDefinition, prefillingSourceDefinition, prefillingSourceEntity.getLabel(), externalData.getResults().getFirst());
return mapPrefilledEntityToDescription(description, descriptionTemplateDefinition, prefillingSourceDefinition, prefillingSourceEntity.getLabel(), externalData.getResults().getFirst());//TODO
}
//TODO
private Description mapPrefilledEntityToDescription(Description description, DefinitionEntity descriptionTemplateDefinition, PrefillingSourceDefinitionEntity prefillingSourceDefinition, String type, Map<String, String> externalData){
for (PrefillingSourceDefinitionFieldEntity field: prefillingSourceDefinition.getFields()) {
String sourceValue = externalData.get(field.getCode());
@ -457,6 +468,9 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
}
} catch (DateTimeParseException ex) {
instant = LocalDate.parse(value).atStartOfDay().toInstant(ZoneOffset.UTC);
if (!this.conventionService.isNullOrEmpty(type) && type.equals(Zenodo) && semanticTarget.equals("rda.dataset.distribution.available_until") ) {
instant.plus(20, ChronoUnit.YEARS);
}
}
field.setDateValue(instant);
}
@ -483,7 +497,40 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService {
field.setTextListValue(valuesParsed == null ? List.of(value) : Arrays.stream(valuesParsed).toList()); //TODO Tags, Currency is ids
}
case REFERENCE_TYPES -> {
throw new NotImplementedException("");
String[] valuesParsed = this.tryParseJsonAsObjectString(String[].class, value);
List<String> finalValue = valuesParsed == null ? List.of(value) : Arrays.stream(valuesParsed).toList();
ReferenceTypeDataEntity selectDataEntity = (ReferenceTypeDataEntity) fieldEntity.getData();
if (selectDataEntity == null) throw new MyApplicationException("Can not cast fieldEntity data");
field.setReferences(new ArrayList<>());
for (String like : finalValue){
ReferenceSearchLookup externalReferenceCriteria = new ReferenceSearchLookup();
externalReferenceCriteria.setLike(like);
externalReferenceCriteria.setTypeId(selectDataEntity.getReferenceTypeId());
externalReferenceCriteria.setProject((BaseFieldSet) new BaseFieldSet()
.ensure(Reference._id)
.ensure(Reference._label)
.ensure(Reference._type)
.ensure(Reference._description)
.ensure(this.conventionService.asIndexer(Reference._description, Definition._fields, eu.eudat.model.referencedefinition.Field._code))
.ensure(this.conventionService.asIndexer(Reference._description, Definition._fields, eu.eudat.model.referencedefinition.Field._dataType))
.ensure(this.conventionService.asIndexer(Reference._description, Definition._fields, eu.eudat.model.referencedefinition.Field._value))
.ensure(Reference._reference)
.ensure(Reference._abbreviation)
.ensure(Reference._source)
.ensure(Reference._isActive)
.ensure(Reference._createdAt)
.ensure(Reference._updatedAt)
.ensure(Reference._hash)
);
List<Reference> references = this.referenceService.searchReferenceData(externalReferenceCriteria);
for (Reference reference : references){
if (reference.getReference().equals(like) || reference.getLabel().toUpperCase(Locale.ROOT).contains(like.toUpperCase(Locale.ROOT))) {
field.getReferences().add(reference);
}
}
}
}
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());