2024-02-22 18:01:23 +01:00
package eu.eudat.service.prefillingsource ;
import eu.eudat.authorization.AuthorizationFlags ;
import eu.eudat.authorization.Permission ;
2024-02-29 16:51:20 +01:00
import eu.eudat.commons.JsonHandlingService ;
2024-02-22 18:01:23 +01:00
import eu.eudat.commons.XmlHandlingService ;
import eu.eudat.commons.enums.IsActive ;
2024-02-29 10:46:59 +01:00
import eu.eudat.commons.types.descriptiontemplate.DefinitionEntity ;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity ;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity ;
2024-03-01 09:54:52 +01:00
import eu.eudat.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity ;
2024-02-29 16:51:20 +01:00
import eu.eudat.commons.types.descriptiontemplate.fielddata.SelectDataEntity ;
2024-02-23 13:30:05 +01:00
import eu.eudat.commons.types.externalfetcher.* ;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionEntity ;
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionFieldEntity ;
2024-02-29 10:46:59 +01:00
import eu.eudat.commons.types.prefillingsource.PrefillingSourceDefinitionFixedValueFieldEntity ;
2024-02-22 18:01:23 +01:00
import eu.eudat.convention.ConventionService ;
2024-02-29 10:46:59 +01:00
import eu.eudat.data.DescriptionTemplateEntity ;
2024-02-22 18:01:23 +01:00
import eu.eudat.data.PrefillingSourceEntity ;
2024-04-01 17:36:03 +02:00
import eu.eudat.data.TenantEntityManager ;
2024-02-22 18:01:23 +01:00
import eu.eudat.errorcode.ErrorThesaurusProperties ;
2024-02-29 10:46:59 +01:00
import eu.eudat.model.* ;
import eu.eudat.model.builder.DescriptionTemplateBuilder ;
2024-02-22 18:01:23 +01:00
import eu.eudat.model.builder.PrefillingSourceBuilder ;
import eu.eudat.model.deleter.PrefillingSourceDeleter ;
2024-02-29 16:51:20 +01:00
import eu.eudat.model.descriptionproperties.* ;
2024-04-18 16:12:41 +02:00
import eu.eudat.model.descriptionreference.DescriptionReferenceData ;
2024-03-05 12:41:58 +01:00
import eu.eudat.model.persist.PrefillingSearchRequest ;
2024-04-18 13:15:03 +02:00
import eu.eudat.model.persist.DescriptionPrefillingRequest ;
2024-02-22 18:01:23 +01:00
import eu.eudat.model.persist.PrefillingSourcePersist ;
2024-02-23 13:30:05 +01:00
import eu.eudat.model.persist.externalfetcher.* ;
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFieldPersist ;
2024-02-29 10:46:59 +01:00
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionFixedValueFieldPersist ;
2024-02-23 13:30:05 +01:00
import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionPersist ;
2024-02-29 16:51:20 +01:00
import eu.eudat.model.prefillingsourcedefinition.PrefillingSourceDefinition ;
2024-03-01 09:54:52 +01:00
import eu.eudat.model.referencedefinition.Definition ;
2024-02-29 10:46:59 +01:00
import eu.eudat.query.PrefillingSourceQuery ;
2024-03-01 09:54:52 +01:00
import eu.eudat.query.lookup.ReferenceSearchLookup ;
2024-02-29 10:46:59 +01:00
import eu.eudat.service.externalfetcher.ExternalFetcherService ;
2024-03-01 17:45:32 +01:00
import eu.eudat.service.externalfetcher.criteria.ExternalReferenceCriteria ;
2024-02-29 10:46:59 +01:00
import eu.eudat.service.externalfetcher.models.ExternalDataResult ;
2024-03-01 09:54:52 +01:00
import eu.eudat.service.reference.ReferenceService ;
2024-02-22 18:01:23 +01:00
import gr.cite.commons.web.authz.service.AuthorizationService ;
import gr.cite.tools.data.builder.BuilderFactory ;
import gr.cite.tools.data.deleter.DeleterFactory ;
2024-02-29 10:46:59 +01:00
import gr.cite.tools.data.query.QueryFactory ;
2024-02-22 18:01:23 +01:00
import gr.cite.tools.exception.MyApplicationException ;
import gr.cite.tools.exception.MyForbiddenException ;
import gr.cite.tools.exception.MyNotFoundException ;
import gr.cite.tools.exception.MyValidationException ;
import gr.cite.tools.fieldset.BaseFieldSet ;
import gr.cite.tools.fieldset.FieldSet ;
import gr.cite.tools.logging.LoggerService ;
import gr.cite.tools.logging.MapLogEntry ;
2024-02-23 13:30:05 +01:00
import jakarta.xml.bind.JAXBException ;
2024-02-29 16:51:20 +01:00
import org.apache.commons.lang3.StringEscapeUtils ;
2024-02-23 13:30:05 +01:00
import org.jetbrains.annotations.NotNull ;
2024-02-22 18:01:23 +01:00
import org.slf4j.LoggerFactory ;
import org.springframework.context.MessageSource ;
import org.springframework.context.i18n.LocaleContextHolder ;
import org.springframework.stereotype.Service ;
2024-02-29 10:46:59 +01:00
import org.xml.sax.SAXException ;
2024-02-22 18:01:23 +01:00
import javax.management.InvalidApplicationException ;
2024-02-29 10:46:59 +01:00
import javax.xml.parsers.ParserConfigurationException ;
import java.io.IOException ;
2024-02-22 18:01:23 +01:00
import java.time.Instant ;
2024-02-29 10:46:59 +01:00
import java.time.LocalDate ;
2024-02-29 16:51:20 +01:00
import java.time.ZoneOffset ;
2024-02-29 10:46:59 +01:00
import java.time.format.DateTimeFormatter ;
2024-02-29 16:51:20 +01:00
import java.time.format.DateTimeParseException ;
2024-03-01 09:54:52 +01:00
import java.time.temporal.ChronoUnit ;
2024-02-29 10:46:59 +01:00
import java.util.* ;
2024-03-01 09:54:52 +01:00
import java.util.stream.Collectors ;
import java.util.stream.Stream ;
2024-02-22 18:01:23 +01:00
@Service
public class PrefillingSourceServiceImpl implements PrefillingSourceService {
private static final LoggerService logger = new LoggerService ( LoggerFactory . getLogger ( PrefillingSourceServiceImpl . class ) ) ;
2024-04-01 17:36:03 +02:00
private final TenantEntityManager entityManager ;
2024-02-22 18:01:23 +01:00
private final AuthorizationService authorizationService ;
private final DeleterFactory deleterFactory ;
private final BuilderFactory builderFactory ;
2024-02-29 10:46:59 +01:00
private final QueryFactory queryFactory ;
2024-02-22 18:01:23 +01:00
private final ConventionService conventionService ;
private final MessageSource messageSource ;
private final XmlHandlingService xmlHandlingService ;
2024-02-29 10:46:59 +01:00
private final ExternalFetcherService externalFetcherService ;
2024-02-22 18:01:23 +01:00
private final ErrorThesaurusProperties errors ;
2024-02-29 16:51:20 +01:00
private final JsonHandlingService jsonHandlingService ;
2024-03-01 09:54:52 +01:00
private final ReferenceService referenceService ;
2024-03-01 17:45:32 +01:00
private static final String Zenodo = " Zenodo " ;
2024-02-22 18:01:23 +01:00
public PrefillingSourceServiceImpl (
2024-04-01 17:36:03 +02:00
TenantEntityManager entityManager , AuthorizationService authorizationService , DeleterFactory deleterFactory , BuilderFactory builderFactory ,
2024-02-29 16:51:20 +01:00
QueryFactory queryFactory , ConventionService conventionService , MessageSource messageSource ,
2024-04-01 17:36:03 +02:00
XmlHandlingService xmlHandlingService , ExternalFetcherService externalFetcherService , ErrorThesaurusProperties errors , JsonHandlingService jsonHandlingService , ReferenceService referenceService ) {
2024-02-22 18:01:23 +01:00
this . entityManager = entityManager ;
this . authorizationService = authorizationService ;
this . deleterFactory = deleterFactory ;
this . builderFactory = builderFactory ;
2024-02-29 10:46:59 +01:00
this . queryFactory = queryFactory ;
2024-02-22 18:01:23 +01:00
this . conventionService = conventionService ;
this . messageSource = messageSource ;
this . xmlHandlingService = xmlHandlingService ;
2024-02-29 10:46:59 +01:00
this . externalFetcherService = externalFetcherService ;
2024-02-22 18:01:23 +01:00
this . errors = errors ;
2024-02-29 16:51:20 +01:00
this . jsonHandlingService = jsonHandlingService ;
2024-03-01 09:54:52 +01:00
this . referenceService = referenceService ;
2024-02-22 18:01:23 +01:00
}
2024-02-23 13:30:05 +01:00
public PrefillingSource persist ( PrefillingSourcePersist model , FieldSet fields ) throws MyForbiddenException , MyValidationException , MyApplicationException , MyNotFoundException , InvalidApplicationException , JAXBException {
2024-02-22 18:01:23 +01:00
logger . debug ( new MapLogEntry ( " persisting data " ) . And ( " model " , model ) . And ( " fields " , fields ) ) ;
this . authorizationService . authorizeForce ( Permission . EditPrefillingSource ) ;
Boolean isUpdate = this . conventionService . isValidGuid ( model . getId ( ) ) ;
PrefillingSourceEntity data ;
if ( isUpdate ) {
data = this . entityManager . find ( PrefillingSourceEntity . class , model . getId ( ) ) ;
if ( data = = null )
throw new MyNotFoundException ( messageSource . getMessage ( " General_ItemNotFound " , new Object [ ] { model . getId ( ) , PrefillingSource . class . getSimpleName ( ) } , LocaleContextHolder . getLocale ( ) ) ) ;
if ( ! this . conventionService . hashValue ( data . getUpdatedAt ( ) ) . equals ( model . getHash ( ) ) ) throw new MyValidationException ( this . errors . getHashConflict ( ) . getCode ( ) , this . errors . getHashConflict ( ) . getMessage ( ) ) ;
} else {
data = new PrefillingSourceEntity ( ) ;
data . setId ( UUID . randomUUID ( ) ) ;
data . setIsActive ( IsActive . Active ) ;
data . setCreatedAt ( Instant . now ( ) ) ;
}
data . setLabel ( model . getLabel ( ) ) ;
2024-02-23 13:30:05 +01:00
data . setDefinition ( this . xmlHandlingService . toXml ( this . buildDefinitionEntity ( model . getDefinition ( ) ) ) ) ;
2024-02-22 18:01:23 +01:00
data . setUpdatedAt ( Instant . now ( ) ) ;
if ( isUpdate ) this . entityManager . merge ( data ) ;
else this . entityManager . persist ( data ) ;
this . entityManager . flush ( ) ;
2024-03-12 17:27:16 +01:00
return this . builderFactory . builder ( PrefillingSourceBuilder . class ) . authorize ( AuthorizationFlags . OwnerOrDmpAssociatedOrPermission ) . build ( BaseFieldSet . build ( fields , PrefillingSource . _id ) , data ) ;
2024-02-22 18:01:23 +01:00
}
2024-02-23 13:30:05 +01:00
private @NotNull PrefillingSourceDefinitionEntity buildDefinitionEntity ( PrefillingSourceDefinitionPersist persist ) {
PrefillingSourceDefinitionEntity data = new PrefillingSourceDefinitionEntity ( ) ;
if ( persist = = null )
return data ;
if ( ! this . conventionService . isListNullOrEmpty ( persist . getFields ( ) ) ) {
data . setFields ( new ArrayList < > ( ) ) ;
for ( PrefillingSourceDefinitionFieldPersist fieldPersist : persist . getFields ( ) ) {
data . getFields ( ) . add ( this . buildFieldEntity ( fieldPersist ) ) ;
}
}
2024-02-29 10:46:59 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( persist . getFixedValueFields ( ) ) ) {
data . setFixedValueFields ( new ArrayList < > ( ) ) ;
for ( PrefillingSourceDefinitionFixedValueFieldPersist fieldPersist : persist . getFixedValueFields ( ) ) {
data . getFixedValueFields ( ) . add ( this . buildFixedValueFieldEntity ( fieldPersist ) ) ;
}
}
2024-02-26 18:47:20 +01:00
if ( persist . getSearchConfiguration ( ) ! = null ) {
data . setSearchConfiguration ( this . buildExternalFetcherApiConfigEntity ( persist . getSearchConfiguration ( ) ) ) ;
}
2024-02-27 15:27:26 +01:00
if ( persist . getGetConfiguration ( ) ! = null & & persist . getGetEnabled ( ) ) {
2024-02-26 18:47:20 +01:00
data . setGetConfiguration ( this . buildExternalFetcherApiConfigEntity ( persist . getGetConfiguration ( ) ) ) ;
}
2024-02-23 13:30:05 +01:00
return data ;
}
private @NotNull PrefillingSourceDefinitionFieldEntity buildFieldEntity ( PrefillingSourceDefinitionFieldPersist persist ) {
PrefillingSourceDefinitionFieldEntity data = new PrefillingSourceDefinitionFieldEntity ( ) ;
if ( persist = = null )
return data ;
data . setCode ( persist . getCode ( ) ) ;
2024-02-29 10:46:59 +01:00
data . setSemanticTarget ( persist . getSemanticTarget ( ) ) ;
data . setSystemFieldTarget ( persist . getSystemFieldTarget ( ) ) ;
data . setTrimRegex ( persist . getTrimRegex ( ) ) ;
return data ;
}
private @NotNull PrefillingSourceDefinitionFixedValueFieldEntity buildFixedValueFieldEntity ( PrefillingSourceDefinitionFixedValueFieldPersist persist ) {
PrefillingSourceDefinitionFixedValueFieldEntity data = new PrefillingSourceDefinitionFixedValueFieldEntity ( ) ;
if ( persist = = null )
return data ;
2024-02-23 13:30:05 +01:00
data . setSemanticTarget ( persist . getSemanticTarget ( ) ) ;
data . setSystemFieldTarget ( persist . getSystemFieldTarget ( ) ) ;
data . setFixedValue ( persist . getFixedValue ( ) ) ;
data . setTrimRegex ( persist . getTrimRegex ( ) ) ;
return data ;
}
private @NotNull ExternalFetcherApiSourceConfigurationEntity buildExternalFetcherApiConfigEntity ( ExternalFetcherApiSourceConfigurationPersist persist ) {
ExternalFetcherApiSourceConfigurationEntity data = new ExternalFetcherApiSourceConfigurationEntity ( ) ;
if ( persist = = null )
return data ;
data . setUrl ( persist . getUrl ( ) ) ;
if ( persist . getResults ( ) ! = null ) {
data . setResults ( this . buildResultsConfigEntity ( persist . getResults ( ) ) ) ;
}
2024-02-26 18:47:20 +01:00
data . setKey ( persist . getKey ( ) ) ;
data . setLabel ( persist . getLabel ( ) ) ;
data . setOrdinal ( persist . getOrdinal ( ) ) ;
data . setType ( persist . getType ( ) ) ;
data . setReferenceTypeDependencyIds ( persist . getReferenceTypeDependencyIds ( ) ) ;
2024-02-23 13:30:05 +01:00
data . setPaginationPath ( persist . getPaginationPath ( ) ) ;
data . setContentType ( persist . getContentType ( ) ) ;
data . setFirstPage ( persist . getFirstPage ( ) ) ;
data . setHttpMethod ( persist . getHttpMethod ( ) ) ;
data . setRequestBody ( persist . getRequestBody ( ) ) ;
data . setFilterType ( persist . getFilterType ( ) ) ;
if ( persist . getAuth ( ) ! = null ) {
data . setAuth ( this . buildAuthConfigEntity ( persist . getAuth ( ) ) ) ;
}
if ( ! this . conventionService . isListNullOrEmpty ( persist . getQueries ( ) ) ) {
data . setQueries ( new ArrayList < > ( ) ) ;
for ( QueryConfigPersist queryConfigPersist : ( persist . getQueries ( ) ) ) {
data . getQueries ( ) . add ( this . buildQueryConfigEntity ( queryConfigPersist ) ) ;
}
}
return data ;
}
private @NotNull ResultsConfigurationEntity buildResultsConfigEntity ( ResultsConfigurationPersist persist ) {
ResultsConfigurationEntity data = new ResultsConfigurationEntity ( ) ;
if ( persist = = null ) return data ;
data . setResultsArrayPath ( persist . getResultsArrayPath ( ) ) ;
if ( ! this . conventionService . isListNullOrEmpty ( persist . getFieldsMapping ( ) ) ) {
data . setFieldsMapping ( new ArrayList < > ( ) ) ;
for ( ResultFieldsMappingConfigurationPersist fieldsMappingPersist : persist . getFieldsMapping ( ) ) {
data . getFieldsMapping ( ) . add ( this . buildResultFieldsMappingConfigEntity ( fieldsMappingPersist ) ) ;
}
}
return data ;
}
private @NotNull ResultFieldsMappingConfigurationEntity buildResultFieldsMappingConfigEntity ( ResultFieldsMappingConfigurationPersist persist ) {
ResultFieldsMappingConfigurationEntity data = new ResultFieldsMappingConfigurationEntity ( ) ;
if ( persist = = null ) return data ;
data . setCode ( persist . getCode ( ) ) ;
data . setResponsePath ( persist . getResponsePath ( ) ) ;
return data ;
}
private @NotNull AuthenticationConfigurationEntity buildAuthConfigEntity ( AuthenticationConfigurationPersist persist ) {
AuthenticationConfigurationEntity data = new AuthenticationConfigurationEntity ( ) ;
if ( persist = = null ) return data ;
data . setEnabled ( persist . getEnabled ( ) ) ;
data . setAuthUrl ( persist . getAuthUrl ( ) ) ;
data . setAuthMethod ( persist . getAuthMethod ( ) ) ;
data . setAuthRequestBody ( persist . getAuthRequestBody ( ) ) ;
data . setType ( persist . getType ( ) ) ;
data . setAuthTokenPath ( persist . getAuthTokenPath ( ) ) ;
return data ;
}
private @NotNull QueryConfigEntity buildQueryConfigEntity ( QueryConfigPersist persist ) {
QueryConfigEntity data = new QueryConfigEntity ( ) ;
if ( persist = = null ) return data ;
data . setName ( persist . getName ( ) ) ;
data . setDefaultValue ( persist . getDefaultValue ( ) ) ;
if ( ! this . conventionService . isListNullOrEmpty ( persist . getCases ( ) ) ) {
data . setCases ( new ArrayList < > ( ) ) ;
for ( QueryCaseConfigPersist queryCaseConfigPersist : persist . getCases ( ) ) {
data . getCases ( ) . add ( this . buildQueryCaseConfigEntity ( queryCaseConfigPersist ) ) ;
}
}
return data ;
}
private @NotNull QueryCaseConfigEntity buildQueryCaseConfigEntity ( QueryCaseConfigPersist persist ) {
QueryCaseConfigEntity data = new QueryCaseConfigEntity ( ) ;
if ( persist = = null ) return data ;
data . setReferenceTypeId ( persist . getReferenceTypeId ( ) ) ;
data . setReferenceTypeSourceKey ( persist . getReferenceTypeSourceKey ( ) ) ;
data . setSeparator ( persist . getSeparator ( ) ) ;
data . setValue ( persist . getValue ( ) ) ;
data . setLikePattern ( persist . getLikePattern ( ) ) ;
return data ;
}
2024-02-22 18:01:23 +01:00
public void deleteAndSave ( UUID id ) throws MyForbiddenException , InvalidApplicationException {
logger . debug ( " deleting : {} " , id ) ;
this . authorizationService . authorizeForce ( Permission . DeletePrefillingSource ) ;
this . deleterFactory . deleter ( PrefillingSourceDeleter . class ) . deleteAndSaveByIds ( List . of ( id ) ) ;
}
2024-02-29 10:46:59 +01:00
2024-03-05 12:41:58 +01:00
public List < Prefilling > searchPrefillings ( PrefillingSearchRequest model ) {
2024-03-01 10:14:27 +01:00
PrefillingSourceEntity prefillingSourceEntity = this . queryFactory . query ( PrefillingSourceQuery . class ) . ids ( model . getPrefillingSourceId ( ) ) . isActive ( IsActive . Active ) . first ( ) ;
2024-03-05 12:41:58 +01:00
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 . getPrefillingSourceId ( ) , PrefillingSourceDefinition . class . getSimpleName ( ) } , LocaleContextHolder . getLocale ( ) ) ) ;
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria ( ) ;
externalReferenceCriteria . setLike ( model . getLike ( ) ) ;
ExternalDataResult externalData = this . externalFetcherService . getExternalData ( Stream . of ( prefillingSourceDefinition . getSearchConfiguration ( ) ) . collect ( Collectors . toList ( ) ) , externalReferenceCriteria , null ) ;
if ( externalData = = null | | this . conventionService . isListNullOrEmpty ( externalData . getResults ( ) ) ) {
return null ;
}
List < Prefilling > prefillings = new ArrayList < > ( ) ;
for ( Map < String , String > result : externalData . getResults ( ) ) {
Prefilling prefilling = new Prefilling ( ) ;
prefilling . setId ( result . getOrDefault ( Prefilling . _id , null ) ) ;
prefilling . setLabel ( result . getOrDefault ( Prefilling . _label , null ) ) ;
prefilling . setKey ( result . getOrDefault ( Prefilling . _key , null ) ) ;
prefilling . setTag ( result . getOrDefault ( Prefilling . _tag , null ) ) ;
2024-04-18 13:15:03 +02:00
if ( prefillingSourceDefinition . getGetConfiguration ( ) = = null ) prefilling . setData ( result ) ;
2024-02-29 10:46:59 +01:00
2024-03-05 12:41:58 +01:00
prefillings . add ( prefilling ) ;
}
2024-04-17 10:44:25 +02:00
prefillings = prefillings . stream ( ) . sorted ( Comparator . comparing ( Prefilling : : getLabel , Comparator . nullsFirst ( Comparator . naturalOrder ( ) ) ) ) . collect ( Collectors . toList ( ) ) ;
2024-03-05 12:41:58 +01:00
return prefillings ;
2024-02-29 10:46:59 +01:00
}
2024-04-18 13:15:03 +02:00
public Description getPrefilledDescription ( DescriptionPrefillingRequest model , FieldSet fieldSet ) throws JAXBException , ParserConfigurationException , IOException , InstantiationException , IllegalAccessException , SAXException , InvalidApplicationException {
2024-02-29 10:46:59 +01:00
2024-03-01 10:14:27 +01:00
PrefillingSourceEntity prefillingSourceEntity = this . queryFactory . query ( PrefillingSourceQuery . class ) . ids ( model . getPrefillingSourceId ( ) ) . first ( ) ;
2024-03-01 09:54:52 +01:00
if ( prefillingSourceEntity = = null ) throw new MyNotFoundException ( messageSource . getMessage ( " General_ItemNotFound " , new Object [ ] { model . getPrefillingSourceId ( ) , PrefillingSource . class . getSimpleName ( ) } , LocaleContextHolder . getLocale ( ) ) ) ;
2024-02-29 10:46:59 +01:00
PrefillingSourceDefinitionEntity prefillingSourceDefinition = this . xmlHandlingService . fromXmlSafe ( PrefillingSourceDefinitionEntity . class , prefillingSourceEntity . getDefinition ( ) ) ;
2024-03-01 09:54:52 +01:00
if ( prefillingSourceDefinition = = null ) throw new MyNotFoundException ( messageSource . getMessage ( " General_ItemNotFound " , new Object [ ] { model . getPrefillingSourceId ( ) , PrefillingSourceDefinition . class . getSimpleName ( ) } , LocaleContextHolder . getLocale ( ) ) ) ;
2024-03-01 17:45:32 +01:00
2024-04-18 11:34:46 +02:00
Map < String , String > data = new HashMap < > ( ) ;
if ( prefillingSourceDefinition . getGetConfiguration ( ) ! = null ) {
2024-04-18 13:15:03 +02:00
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria ( ) ;
externalReferenceCriteria . setLike ( model . getData ( ) . getId ( ) ) ;
2024-04-18 11:34:46 +02:00
ExternalDataResult externalData = this . externalFetcherService . getExternalData ( Stream . of ( prefillingSourceDefinition . getGetConfiguration ( ) ) . collect ( Collectors . toList ( ) ) , externalReferenceCriteria , null ) ;
if ( externalData ! = null & & ! this . conventionService . isListNullOrEmpty ( externalData . getResults ( ) ) ) {
data = externalData . getResults ( ) . getFirst ( ) ;
}
} else {
2024-04-18 13:15:03 +02:00
data = model . getData ( ) = = null ? new HashMap < > ( ) : model . getData ( ) . getData ( ) ;
2024-02-29 10:46:59 +01:00
}
2024-02-29 16:51:20 +01:00
2024-02-29 10:46:59 +01:00
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 ( ) ;
2024-04-03 16:50:47 +02:00
FieldSet descriptionTemplateFields = fieldSet . extractPrefixed ( this . conventionService . asPrefix ( Description . _descriptionTemplate ) ) ;
description . setDescriptionTemplate ( this . builderFactory . builder ( DescriptionTemplateBuilder . class ) . authorize ( AuthorizationFlags . OwnerOrDmpAssociatedOrPermission ) . build ( descriptionTemplateFields , descriptionTemplateEntity ) ) ;
2024-04-18 11:34:46 +02:00
return mapPrefilledEntityToDescription ( description , descriptionTemplateDefinition , prefillingSourceDefinition , prefillingSourceEntity . getLabel ( ) , data ) ;
2024-02-29 10:46:59 +01:00
}
2024-02-29 16:51:20 +01:00
private Description mapPrefilledEntityToDescription ( Description description , DefinitionEntity descriptionTemplateDefinition , PrefillingSourceDefinitionEntity prefillingSourceDefinition , String type , Map < String , String > externalData ) {
2024-04-18 16:12:41 +02:00
List < DescriptionReference > descriptionReferences = new ArrayList < > ( ) ;
2024-03-01 17:45:32 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( prefillingSourceDefinition . getFields ( ) ) ) {
for ( PrefillingSourceDefinitionFieldEntity field : prefillingSourceDefinition . getFields ( ) ) {
String sourceValue = externalData . get ( field . getCode ( ) ) ;
this . prefillSystemValueToDescription ( description , field . getSystemFieldTarget ( ) , sourceValue ) ;
2024-04-18 16:12:41 +02:00
this . prefillSemanticValueToDescription ( description , descriptionReferences , field . getSemanticTarget ( ) , sourceValue , descriptionTemplateDefinition , type ) ;
2024-03-01 17:45:32 +01:00
}
2024-02-29 10:46:59 +01:00
}
2024-03-01 17:45:32 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( prefillingSourceDefinition . getFixedValueFields ( ) ) ) {
for ( PrefillingSourceDefinitionFixedValueFieldEntity field : prefillingSourceDefinition . getFixedValueFields ( ) ) {
this . prefillSystemValueToDescription ( description , field . getSystemFieldTarget ( ) , field . getFixedValue ( ) ) ;
2024-04-18 16:12:41 +02:00
this . prefillSemanticValueToDescription ( description , descriptionReferences , field . getSemanticTarget ( ) , field . getFixedValue ( ) , descriptionTemplateDefinition , type ) ;
2024-03-01 17:45:32 +01:00
}
2024-02-29 16:51:20 +01:00
}
2024-03-01 17:45:32 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( prefillingSourceDefinition . getFields ( ) ) ) {
for ( PrefillingSourceDefinitionFieldEntity field : prefillingSourceDefinition . getFields ( ) ) {
String sourceValue = externalData . get ( field . getCode ( ) ) ;
2024-04-18 16:12:41 +02:00
this . ensureZenodoFields ( description , descriptionReferences , field . getSemanticTarget ( ) , sourceValue , descriptionTemplateDefinition , type ) ;
2024-03-01 17:45:32 +01:00
}
2024-02-29 16:51:20 +01:00
}
2024-03-01 17:45:32 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( prefillingSourceDefinition . getFixedValueFields ( ) ) ) {
for ( PrefillingSourceDefinitionFixedValueFieldEntity field : prefillingSourceDefinition . getFixedValueFields ( ) ) {
2024-04-18 16:12:41 +02:00
this . ensureZenodoFields ( description , descriptionReferences , field . getSemanticTarget ( ) , field . getFixedValue ( ) , descriptionTemplateDefinition , type ) ;
2024-03-01 17:45:32 +01:00
}
2024-02-29 10:46:59 +01:00
}
2024-04-18 16:12:41 +02:00
description . setDescriptionReferences ( descriptionReferences ) ;
2024-02-29 10:46:59 +01:00
return description ;
}
2024-02-29 16:51:20 +01:00
2024-04-18 16:12:41 +02:00
private void ensureZenodoFields ( Description description , List < DescriptionReference > descriptionReferences , String semanticTarget , String value , DefinitionEntity definition , String type ) {
2024-02-29 16:51:20 +01:00
if ( ! this . conventionService . isNullOrEmpty ( type ) & & ! this . conventionService . isNullOrEmpty ( semanticTarget ) & & ! this . conventionService . isNullOrEmpty ( value ) & & type . equals ( Zenodo ) ) {
if ( semanticTarget . equals ( " rda.dataset.distribution.data_access " ) ) {
if ( value . equals ( " open " ) ) {
List < FieldEntity > issuedFieldEntities = definition . getAllField ( ) . stream ( ) . filter ( x - > x . getSchematics ( ) ! = null & & x . getSchematics ( ) . contains ( " rda.dataset.issued " ) ) . toList ( ) ;
2024-03-01 17:45:32 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( issuedFieldEntities ) ) {
2024-02-29 16:51:20 +01:00
String issuedIdNode = issuedFieldEntities . getFirst ( ) . getId ( ) ;
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 ) ;
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 ( ) ;
for ( FieldSetEntity licStartFieldSetEntity : licStartFieldSetsEntities ) {
2024-02-29 10:46:59 +01:00
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 ) {
2024-04-18 16:12:41 +02:00
description . getProperties ( ) . getFieldSets ( ) . get ( licStartFieldSetEntity . getId ( ) ) . getItems ( ) . getFirst ( ) . getFields ( ) . put ( licStartDateNode . getId ( ) , buildPropertyDefinitionFieldItemValue ( descriptionReferences , licStartDateNode , semanticTarget , issuedValue , type ) ) ;
2024-02-29 10:46:59 +01:00
}
}
}
}
}
}
}
}
}
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 < > ( ) ) ;
2024-02-29 16:51:20 +01:00
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 ) ;
2024-02-29 10:46:59 +01:00
}
2024-04-18 16:12:41 +02:00
private void prefillSemanticValueToDescription ( Description description , List < DescriptionReference > descriptionReferences , String semanticTarget , String parsedValue , DefinitionEntity definition , String type ) {
2024-02-29 16:51:20 +01:00
if ( this . conventionService . isNullOrEmpty ( semanticTarget ) | | this . conventionService . isNullOrEmpty ( parsedValue ) ) return ;
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 ( ) ;
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 ) {
2024-04-18 16:12:41 +02:00
description . getProperties ( ) . getFieldSets ( ) . get ( fieldSetEntity . getId ( ) ) . getItems ( ) . getFirst ( ) . getFields ( ) . put ( fieldEntity . getId ( ) , buildPropertyDefinitionFieldItemValue ( descriptionReferences , fieldEntity , semanticTarget , parsedValue , type ) ) ;
2024-02-29 16:51:20 +01:00
}
2024-02-29 10:46:59 +01:00
}
2024-02-29 16:51:20 +01:00
}
}
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 ( ) . 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 ) ;
2024-02-29 10:46:59 +01:00
}
}
}
}
2024-04-18 16:12:41 +02:00
private Field buildPropertyDefinitionFieldItemValue ( List < DescriptionReference > descriptionReferences , FieldEntity fieldEntity , String semanticTarget , String value , String type ) {
2024-02-29 10:46:59 +01:00
Field field = new Field ( ) ;
2024-02-29 16:51:20 +01:00
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 ) ;
2024-03-01 09:54:52 +01:00
if ( ! this . conventionService . isNullOrEmpty ( type ) & & type . equals ( Zenodo ) & & semanticTarget . equals ( " rda.dataset.distribution.available_until " ) ) {
instant . plus ( 20 , ChronoUnit . YEARS ) ;
}
2024-02-29 16:51:20 +01:00
}
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 < > ( ) ) ;
2024-04-18 16:12:41 +02:00
2024-02-29 16:51:20 +01:00
for ( SelectDataEntity . OptionEntity entity : selectDataEntity . getOptions ( ) ) {
if ( finalValue . contains ( entity . getValue ( ) ) | | finalValue . contains ( entity . getLabel ( ) ) ) {
2024-04-18 16:12:41 +02:00
if ( selectDataEntity . getMultipleSelect ( ) ) field . getTextListValue ( ) . add ( entity . getValue ( ) ) ;
else field . setTextValue ( entity . getValue ( ) ) ;
2024-02-29 16:51:20 +01:00
}
}
}
2024-04-09 17:28:07 +02:00
case TAGS - > {
2024-02-29 16:51:20 +01:00
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 - > {
2024-03-01 09:54:52 +01:00
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 )
2024-04-18 16:12:41 +02:00
. ensure ( this . conventionService . asIndexer ( Reference . _type , ReferenceType . _id ) )
2024-03-01 09:54:52 +01:00
. 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 )
2024-04-18 16:12:41 +02:00
. ensure ( Reference . _sourceType )
2024-03-01 09:54:52 +01:00
. 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 ) ;
2024-04-18 16:12:41 +02:00
DescriptionReference descriptionReference = new DescriptionReference ( ) ;
descriptionReference . setReference ( reference ) ;
DescriptionReferenceData descriptionReferenceData = new DescriptionReferenceData ( ) ;
descriptionReferenceData . setFieldId ( fieldEntity . getId ( ) ) ;
descriptionReference . setData ( descriptionReferenceData ) ;
descriptionReference . setIsActive ( IsActive . Active ) ;
descriptionReferences . add ( descriptionReference ) ;
2024-03-01 09:54:52 +01:00
}
}
}
2024-02-29 16:51:20 +01:00
}
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 ) ;
}
return field ;
}
2024-02-29 10:46:59 +01:00
2024-02-29 16:51:20 +01:00
private < T > T tryParseJsonAsObjectString ( Class < T > type , String value ) {
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 ;
2024-02-29 10:46:59 +01:00
}
2024-02-22 18:01:23 +01:00
}