2024-01-30 15:31:03 +01:00
package eu.old.eudat.migration ;
2024-03-12 15:28:07 +01:00
import com.fasterxml.jackson.annotation.JsonValue ;
2024-05-02 14:57:19 +02:00
import eu.old.eudat.models.data.components.commons.DefaultValue ;
2024-04-26 16:12:29 +02:00
import org.opencdmp.commons.XmlHandlingService ;
import org.opencdmp.commons.enums.* ;
import org.opencdmp.commons.types.descriptiontemplate.* ;
import org.opencdmp.commons.types.descriptiontemplate.fielddata.* ;
import org.opencdmp.commons.types.externalfetcher.* ;
import org.opencdmp.commons.types.referencetype.* ;
import org.opencdmp.convention.ConventionService ;
import org.opencdmp.data.DescriptionTemplateEntity ;
import org.opencdmp.data.ReferenceEntity ;
import org.opencdmp.data.ReferenceTypeEntity ;
import org.opencdmp.data.converters.enums.DatabaseEnum ;
import org.opencdmp.model.DescriptionTemplate ;
import org.opencdmp.query.DescriptionTemplateQuery ;
import org.opencdmp.query.ReferenceTypeQuery ;
2024-04-24 13:07:51 +02:00
import eu.old.eudat.logic.services.helpers.HelpersService ;
2024-01-30 15:31:03 +01:00
import eu.old.eudat.logic.utilities.builders.XmlBuilder ;
import eu.old.eudat.models.data.components.commons.Multiplicity ;
import eu.old.eudat.models.data.components.commons.Rule ;
import eu.old.eudat.models.data.components.commons.datafield.* ;
import eu.old.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.* ;
import gr.cite.tools.data.query.Ordering ;
import gr.cite.tools.data.query.Paging ;
import gr.cite.tools.data.query.QueryFactory ;
import gr.cite.tools.exception.MyApplicationException ;
import gr.cite.tools.logging.LoggerService ;
import jakarta.persistence.EntityManager ;
2024-04-05 10:35:37 +02:00
import jakarta.ws.rs.NotSupportedException ;
2024-01-30 15:31:03 +01:00
import jakarta.xml.bind.JAXBException ;
2024-04-25 12:13:44 +02:00
import org.apache.commons.lang3.StringUtils ;
2024-04-25 10:35:08 +02:00
import org.elasticsearch.common.inject.Guice ;
2024-01-30 15:31:03 +01:00
import org.jetbrains.annotations.NotNull ;
import org.slf4j.LoggerFactory ;
2024-02-21 17:38:28 +01:00
import org.springframework.http.MediaType ;
2024-01-30 15:31:03 +01:00
import org.springframework.stereotype.Service ;
import org.w3c.dom.Document ;
import org.xml.sax.SAXException ;
import javax.management.InvalidApplicationException ;
import javax.xml.parsers.ParserConfigurationException ;
import javax.xml.transform.TransformerException ;
import java.io.IOException ;
2024-02-21 17:38:28 +01:00
import java.net.URI ;
import java.net.URISyntaxException ;
import java.time.Instant ;
2024-03-21 14:36:48 +01:00
import java.time.LocalDate ;
import java.time.ZoneOffset ;
import java.time.format.DateTimeParseException ;
2024-02-21 17:38:28 +01:00
import java.util.* ;
import java.util.stream.Collectors ;
2024-01-30 15:31:03 +01:00
@Service
public class DescriptionTemplateXmlMigrationService {
private static final LoggerService logger = new LoggerService ( LoggerFactory . getLogger ( DescriptionTemplateXmlMigrationService . class ) ) ;
private final EntityManager entityManager ;
private final ConventionService conventionService ;
private final XmlHandlingService xmlHandlingService ;
private final QueryFactory queryFactory ;
2024-03-21 14:36:48 +01:00
private final MigrationTools migrationTools ;
2024-01-30 15:31:03 +01:00
private static final int PageSize = 500 ;
private static final boolean TestMode = false ;
2024-03-21 14:36:48 +01:00
public DescriptionTemplateXmlMigrationService ( EntityManager entityManager , ConventionService conventionService , XmlHandlingService xmlHandlingService , QueryFactory queryFactory , MigrationTools migrationTools ) {
2024-01-30 15:31:03 +01:00
this . entityManager = entityManager ;
this . conventionService = conventionService ;
this . xmlHandlingService = xmlHandlingService ;
this . queryFactory = queryFactory ;
2024-03-21 14:36:48 +01:00
this . migrationTools = migrationTools ;
2024-01-30 15:31:03 +01:00
}
2024-02-21 17:38:28 +01:00
public void migrate ( ) throws IOException , NoSuchFieldException , IllegalAccessException , JAXBException , ParserConfigurationException , InstantiationException , SAXException , InvalidApplicationException , TransformerException , URISyntaxException {
2024-01-30 15:31:03 +01:00
long total = this . queryFactory . query ( DescriptionTemplateQuery . class ) . count ( ) ;
logger . debug ( " Migrate DescriptionTemplate Total : " + total ) ;
int page = 0 ;
2024-02-21 17:38:28 +01:00
Map < String , ReferenceTypeEntity > referenceTypeEntityMap = new HashMap < > ( ) ;
2024-04-25 10:35:08 +02:00
Map < UUID , ReferenceTypeEntity > knownReferenceEntities = this . queryFactory . query ( ReferenceTypeQuery . class ) . ids ( ReferenceTypeIds . KnownReferenceTypeIds ) . collect ( ) . stream ( ) . collect ( Collectors . toMap ( ReferenceTypeEntity : : getId , x - > x ) ) ;
2024-01-30 15:31:03 +01:00
List < DescriptionTemplateEntity > items ;
do {
DescriptionTemplateQuery query = this . queryFactory . query ( DescriptionTemplateQuery . class ) ;
query . setOrder ( new Ordering ( ) . addDescending ( DescriptionTemplate . _createdAt ) ) ;
query . setPage ( new Paging ( page * PageSize , PageSize ) ) ;
items = query . collect ( ) ;
if ( items ! = null & & ! items . isEmpty ( ) ) {
logger . debug ( " Migrate DescriptionTemplate " + page * PageSize + " of " + total ) ;
for ( DescriptionTemplateEntity item : items ) {
if ( this . conventionService . isNullOrEmpty ( item . getDefinition ( ) ) ) continue ;
Document document = XmlBuilder . fromXml ( item . getDefinition ( ) ) ;
if ( document = = null ) {
logger . error ( " Migrate DescriptionTemplate " + item . getId ( ) + " failed read xml " ) ;
2024-02-28 17:48:46 +01:00
throw new MyApplicationException ( " Migrate DescriptionTemplate " + item . getId ( ) + " failed read xml " ) ;
2024-01-30 15:31:03 +01:00
}
2024-01-31 16:48:59 +01:00
ViewStyleModel viewStyleModel = null ;
2024-01-30 15:31:03 +01:00
try {
2024-01-31 16:48:59 +01:00
viewStyleModel = new ViewStyleModel ( ) . fromXml ( document . getDocumentElement ( ) ) ;
2024-01-30 15:31:03 +01:00
} catch ( Exception ex ) {
logger . error ( " Migrate DescriptionTemplate " + item . getId ( ) + " failed read xml " ) ;
2024-02-28 17:48:46 +01:00
throw new MyApplicationException ( " Migrate DescriptionTemplate " + item . getId ( ) + " failed read xml " ) ;
2024-01-30 15:31:03 +01:00
}
2024-04-25 10:35:08 +02:00
item . setDefinition ( this . xmlHandlingService . toXml ( this . buildDefinitionEntity ( viewStyleModel , referenceTypeEntityMap , knownReferenceEntities ) ) ) ;
2024-01-30 15:31:03 +01:00
this . entityManager . merge ( item ) ;
}
this . entityManager . flush ( ) ;
page + + ;
}
} while ( items ! = null & & ! items . isEmpty ( ) & & ! TestMode ) ;
2024-02-21 17:38:28 +01:00
logger . debug ( " Migrate DescriptionTemplate ReferenceTypes : " + referenceTypeEntityMap . size ( ) ) ;
for ( ReferenceTypeEntity item : referenceTypeEntityMap . values ( ) ) {
this . entityManager . persist ( item ) ;
}
this . entityManager . flush ( ) ;
2024-01-30 15:31:03 +01:00
}
2024-04-25 10:35:08 +02:00
private @NotNull DefinitionEntity buildDefinitionEntity ( ViewStyleModel persist , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-01-30 15:31:03 +01:00
DefinitionEntity data = new DefinitionEntity ( ) ;
if ( persist = = null )
return data ;
if ( ! this . conventionService . isListNullOrEmpty ( persist . getPages ( ) ) ) {
2024-03-26 15:30:13 +01:00
Map < String , List < Section > > sectionPerPage = mapSectionsToPage ( persist ) ;
2024-01-30 15:31:03 +01:00
data . setPages ( new ArrayList < > ( ) ) ;
for ( Page pagePersist : persist . getPages ( ) ) {
2024-04-25 10:35:08 +02:00
data . getPages ( ) . add ( this . buildPageEntity ( pagePersist , sectionPerPage . get ( pagePersist . getId ( ) ) , referenceTypeEntityMap , knownReferenceEntities ) ) ;
2024-01-30 15:31:03 +01:00
}
}
return data ;
}
2024-04-25 10:35:08 +02:00
private @NotNull SectionEntity buildSectionEntity ( Section persist , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-01-30 15:31:03 +01:00
SectionEntity data = new SectionEntity ( ) ;
if ( persist = = null )
return data ;
data . setId ( persist . getId ( ) ) ;
data . setDescription ( persist . getDescription ( ) ) ;
data . setExtendedDescription ( persist . getExtendedDescription ( ) ) ;
data . setNumbering ( persist . getNumbering ( ) ) ;
data . setOrdinal ( persist . getOrdinal ( ) ) ;
data . setDefaultVisibility ( persist . isDefaultVisibility ( ) ) ;
data . setTitle ( persist . getTitle ( ) ) ;
if ( ! this . conventionService . isListNullOrEmpty ( persist . getSections ( ) ) ) {
data . setSections ( new ArrayList < > ( ) ) ;
for ( Section sectionPersist : persist . getSections ( ) ) {
2024-04-25 10:35:08 +02:00
data . getSections ( ) . add ( this . buildSectionEntity ( sectionPersist , referenceTypeEntityMap , knownReferenceEntities ) ) ;
2024-01-30 15:31:03 +01:00
}
}
if ( ! this . conventionService . isListNullOrEmpty ( persist . getFieldSets ( ) ) ) {
data . setFieldSets ( new ArrayList < > ( ) ) ;
for ( FieldSet fieldSetPersist : persist . getFieldSets ( ) ) {
2024-04-25 10:35:08 +02:00
data . getFieldSets ( ) . add ( this . buildFieldSetEntity ( fieldSetPersist , referenceTypeEntityMap , knownReferenceEntities ) ) ;
2024-01-30 15:31:03 +01:00
}
}
return data ;
}
2024-04-25 10:35:08 +02:00
private @NotNull FieldSetEntity buildFieldSetEntity ( FieldSet persist , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-01-30 15:31:03 +01:00
FieldSetEntity data = new FieldSetEntity ( ) ;
if ( persist = = null )
return data ;
data . setId ( persist . getId ( ) ) ;
data . setOrdinal ( persist . getOrdinal ( ) ) ;
data . setNumbering ( persist . getNumbering ( ) ) ;
data . setTitle ( persist . getTitle ( ) ) ;
2024-03-27 16:09:10 +01:00
data . setDescription ( persist . getDescription ( ) ) ;
2024-01-30 15:31:03 +01:00
data . setExtendedDescription ( persist . getExtendedDescription ( ) ) ;
data . setAdditionalInformation ( persist . getAdditionalInformation ( ) ) ;
data . setHasCommentField ( persist . getHasCommentField ( ) ) ;
2024-04-24 13:07:51 +02:00
data . setHasMultiplicity ( persist . getMultiplicity ( ) ! = null & & ( persist . getMultiplicity ( ) . getMin ( ) ! = 0 | | persist . getMultiplicity ( ) . getMax ( ) ! = 0 ) ) ;
if ( persist . getMultiplicity ( ) ! = null & & data . getHasMultiplicity ( ) ) data . setMultiplicity ( this . buildMultiplicityEntity ( persist . getMultiplicity ( ) ) ) ;
2024-01-30 15:31:03 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( persist . getFields ( ) ) ) {
data . setFields ( new ArrayList < > ( ) ) ;
for ( Field fieldPersist : persist . getFields ( ) ) {
2024-04-25 10:35:08 +02:00
data . getFields ( ) . add ( this . buildFieldEntity ( fieldPersist , referenceTypeEntityMap , knownReferenceEntities ) ) ;
2024-01-30 15:31:03 +01:00
}
}
return data ;
}
2024-04-25 10:35:08 +02:00
private @NotNull FieldEntity buildFieldEntity ( Field persist , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-01-30 15:31:03 +01:00
FieldEntity data = new FieldEntity ( ) ;
if ( persist = = null )
return data ;
data . setId ( persist . getId ( ) ) ;
data . setOrdinal ( persist . getOrdinal ( ) ) ;
data . setSchematics ( persist . getSchematics ( ) ) ;
data . setNumbering ( persist . getNumbering ( ) ) ;
if ( persist . getValidations ( ) ! = null ) data . setValidations ( persist . getValidations ( ) . stream ( ) . map ( x - > {
switch ( x ) {
case NONE - > {
return FieldValidationType . None ;
}
case REQUIRED - > {
return FieldValidationType . Required ;
}
case URL - > {
return FieldValidationType . Url ;
}
default - > throw new MyApplicationException ( " Invalid type " + x ) ;
}
} ) . toList ( ) ) ;
data . setIncludeInExport ( persist . getExport ( ) ) ;
2024-02-23 17:55:35 +01:00
if ( persist . getData ( ) ! = null ) {
2024-04-25 10:35:08 +02:00
data . setData ( this . buildFieldDataEntity ( persist . getData ( ) , persist . getViewStyle ( ) . getRenderStyle ( ) , referenceTypeEntityMap , knownReferenceEntities ) ) ;
2024-02-23 17:55:35 +01:00
if ( persist . getViewStyle ( ) . getRenderStyle ( ) . equals ( " externalDatasets " ) ) {
FieldDataExternalDatasetType type = null ;
try {
type = FieldDataExternalDatasetType . of ( ( ( ExternalDatasetsData ) persist . getData ( ) ) . getType ( ) ) ;
} catch ( Exception e ) { type = FieldDataExternalDatasetType . Other ; }
if ( type = = null ) type = FieldDataExternalDatasetType . Other ;
if ( data . getSchematics ( ) = = null ) data . setSchematics ( new ArrayList < > ( ) ) ;
switch ( type ) {
case Other - > data . getSchematics ( ) . add ( " referencetype.externaldataset.type.other " ) ;
case ProducedDataset - > data . getSchematics ( ) . add ( " referencetype.externaldataset.type.produced " ) ;
case ReusedDataset - > data . getSchematics ( ) . add ( " referencetype.externaldataset.type.reused " ) ;
default - > throw new MyApplicationException ( " Invalid type " + type ) ;
}
}
}
2024-05-02 14:57:19 +02:00
if ( persist . getDefaultValue ( ) ! = null ) data . setDefaultValue ( this . buildDefaultValueEntity ( persist . getDefaultValue ( ) , data . getData ( ) . getFieldType ( ) ) ) ;
2024-01-30 15:31:03 +01:00
if ( persist . getVisible ( ) ! = null & & ! this . conventionService . isListNullOrEmpty ( persist . getVisible ( ) . getRules ( ) ) ) {
data . setVisibilityRules ( new ArrayList < > ( ) ) ;
for ( Rule fieldPersist : persist . getVisible ( ) . getRules ( ) ) {
2024-03-21 14:36:48 +01:00
data . getVisibilityRules ( ) . add ( this . buildRuleEntity ( fieldPersist , data . getData ( ) . getFieldType ( ) ) ) ;
2024-01-30 15:31:03 +01:00
}
}
return data ;
}
2024-04-25 10:35:08 +02:00
private BaseFieldDataEntity buildFieldDataEntity ( FieldData < ? > persist , String renderStyle , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-01-30 15:31:03 +01:00
if ( renderStyle . equals ( " checkBox " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . CHECK_BOX ) ;
}
if ( renderStyle . equals ( " validation " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . VALIDATION ) ;
}
if ( renderStyle . equals ( " textarea " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . TEXT_AREA ) ;
}
if ( renderStyle . equals ( " tags " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . TAGS ) ;
}
if ( renderStyle . equals ( " richTextarea " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . RICH_TEXT_AREA ) ;
}
if ( renderStyle . equals ( " freetext " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . FREE_TEXT ) ;
}
if ( renderStyle . equals ( " datePicker " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . DATE_PICKER ) ;
}
if ( renderStyle . equals ( " datasetIdentifier " ) ) {
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . DATASET_IDENTIFIER ) ;
}
if ( renderStyle . equals ( " currency " ) ) {
2024-04-09 17:14:16 +02:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , false , ReferenceTypeIds . Currency ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " taxonomies " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( TaxonomiesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Taxonomy ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " services " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( ServicesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Services ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " researchers " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( ResearcherData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Researcher ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " registries " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( RegistriesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Registries ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " pubRepositories " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( DataRepositoriesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . PubRepositories ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " publications " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( PublicationsData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Publication ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " organizations " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( OrganizationsData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Organizations ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " licenses " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( LicensesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . License ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " journalRepositories " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( DataRepositoriesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Journal ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " dataRepositories " ) ) {
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( DataRepositoriesData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . DataRepositories ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " booleanDecision " ) ) {
2024-01-30 16:23:18 +01:00
return this . buildLabelDataEntity ( persist . getLabel ( ) , FieldType . BOOLEAN_DECISION ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " internalDmpEntities " ) ) {
switch ( ( ( InternalDmpEntitiesData < ? > ) persist ) . getType ( ) ) {
case " dmps " :
2024-02-21 17:38:28 +01:00
return this . buildLabelAndMultiplicityDataEntity ( persist . getLabel ( ) , ( ( DMPsAutoCompleteData ) persist ) . getMultiAutoComplete ( ) , FieldType . INTERNAL_ENTRIES_DMPS ) ;
2024-01-30 15:31:03 +01:00
case " datasets " :
2024-02-21 17:38:28 +01:00
return this . buildLabelAndMultiplicityDataEntity ( persist . getLabel ( ) , ( ( DatasetsAutoCompleteData ) persist ) . getMultiAutoComplete ( ) , FieldType . INTERNAL_ENTRIES_DESCRIPTIONS ) ;
2024-01-30 15:31:03 +01:00
case " researchers " :
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( ResearchersAutoCompleteData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Researcher ) ;
2024-01-30 15:31:03 +01:00
}
}
if ( renderStyle . equals ( " externalDatasets " ) ) {
2024-02-23 17:55:35 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( ExternalDatasetsData ) persist ) . getMultiAutoComplete ( ) , ReferenceTypeIds . Datasets ) ;
2024-01-30 15:31:03 +01:00
}
if ( renderStyle . equals ( " upload " ) ) {
UploadDataEntity data = new UploadDataEntity ( ) ;
data . setLabel ( persist . getLabel ( ) ) ;
data . setMaxFileSizeInMB ( ( ( UploadData ) persist ) . getMaxFileSizeInMB ( ) ) ;
data . setTypes ( new ArrayList < > ( ) ) ;
if ( ( ( UploadData ) persist ) . getTypes ( ) ! = null ) {
for ( UploadData . Option option : ( ( UploadData ) persist ) . getTypes ( ) ) {
UploadDataEntity . UploadDataOptionEntity optionEntity = new UploadDataEntity . UploadDataOptionEntity ( ) ;
optionEntity . setValue ( option . getValue ( ) ) ;
optionEntity . setLabel ( option . getLabel ( ) ) ;
data . getTypes ( ) . add ( optionEntity ) ;
}
}
data . setFieldType ( FieldType . UPLOAD ) ;
return data ;
}
if ( renderStyle . equals ( " radiobox " ) ) {
RadioBoxDataEntity data = new RadioBoxDataEntity ( ) ;
data . setLabel ( persist . getLabel ( ) ) ;
data . setOptions ( new ArrayList < > ( ) ) ;
if ( ( ( RadioBoxData ) persist ) . getOptions ( ) ! = null ) {
for ( RadioBoxData . Option option : ( ( RadioBoxData ) persist ) . getOptions ( ) ) {
RadioBoxDataEntity . RadioBoxDataOptionEntity optionEntity = new RadioBoxDataEntity . RadioBoxDataOptionEntity ( ) ;
optionEntity . setValue ( option . getValue ( ) ) ;
optionEntity . setLabel ( option . getLabel ( ) ) ;
data . getOptions ( ) . add ( optionEntity ) ;
}
}
data . setFieldType ( FieldType . RADIO_BOX ) ;
return data ;
}
if ( renderStyle . equals ( " combobox " ) ) {
switch ( ( ( ComboBoxData < ? > ) persist ) . getType ( ) ) {
case " wordlist " : {
2024-01-31 16:48:59 +01:00
SelectDataEntity data = new SelectDataEntity ( ) ;
2024-01-30 15:31:03 +01:00
data . setLabel ( persist . getLabel ( ) ) ;
data . setOptions ( new ArrayList < > ( ) ) ;
2024-01-31 16:48:59 +01:00
data . setMultipleSelect ( ( ( WordListData ) persist ) . getMultiList ( ) ) ;
2024-01-30 15:31:03 +01:00
if ( ( ( WordListData ) persist ) . getOptions ( ) ! = null ) {
for ( ComboBoxData . Option option : ( ( WordListData ) persist ) . getOptions ( ) ) {
2024-01-31 16:48:59 +01:00
SelectDataEntity . OptionEntity optionEntity = new SelectDataEntity . OptionEntity ( ) ;
2024-01-30 15:31:03 +01:00
optionEntity . setValue ( option . getValue ( ) ) ;
optionEntity . setLabel ( option . getLabel ( ) ) ;
data . getOptions ( ) . add ( optionEntity ) ;
}
}
2024-01-31 16:48:59 +01:00
data . setFieldType ( FieldType . SELECT ) ;
2024-01-30 15:31:03 +01:00
return data ;
}
case " autocomplete " : {
2024-04-25 10:35:08 +02:00
ReferenceTypeEntity referenceType = this . resolveReferenceTypeEntityAutoCompleteData ( ( ( AutoCompleteData ) persist ) , referenceTypeEntityMap , knownReferenceEntities ) ;
2024-02-21 17:38:28 +01:00
return this . buildReferenceTypeDataEntity ( persist . getLabel ( ) , ( ( AutoCompleteData ) persist ) . getMultiAutoComplete ( ) , referenceType . getId ( ) ) ;
2024-01-30 15:31:03 +01:00
}
}
}
throw new MyApplicationException ( " Can transform field data " + renderStyle ) ;
}
2024-01-31 16:48:59 +01:00
private BaseFieldDataEntity buildLabelDataEntity ( String label , FieldType fieldType ) {
2024-01-30 15:31:03 +01:00
LabelDataEntity data = new LabelDataEntity ( ) ;
data . setLabel ( label ) ;
data . setFieldType ( fieldType ) ;
return data ;
}
2024-01-31 16:48:59 +01:00
private BaseFieldDataEntity buildLabelAndMultiplicityDataEntity ( String label , Boolean multiAutoComplete , FieldType fieldType ) {
2024-01-30 15:31:03 +01:00
LabelAndMultiplicityDataEntity data = new LabelAndMultiplicityDataEntity ( ) ;
data . setLabel ( label ) ;
data . setFieldType ( fieldType ) ;
2024-01-31 16:48:59 +01:00
data . setMultipleSelect ( multiAutoComplete ) ;
2024-01-30 15:31:03 +01:00
return data ;
}
2024-02-21 17:38:28 +01:00
private BaseFieldDataEntity buildReferenceTypeDataEntity ( String label , Boolean multiAutoComplete , UUID referenceTypeId ) {
ReferenceTypeDataEntity data = new ReferenceTypeDataEntity ( ) ;
data . setLabel ( label ) ;
data . setFieldType ( FieldType . REFERENCE_TYPES ) ;
data . setMultipleSelect ( multiAutoComplete ) ;
data . setReferenceTypeId ( referenceTypeId ) ;
return data ;
}
2024-05-02 14:57:19 +02:00
private @NotNull DefaultValueEntity buildDefaultValueEntity ( DefaultValue persist , FieldType fieldType ) {
DefaultValueEntity data = new DefaultValueEntity ( ) ;
if ( persist = = null )
return data ;
String textValue = persist . getValue ( ) ;
if ( textValue = = null | | textValue . isEmpty ( ) ) return data ;
switch ( fieldType ) {
case FREE_TEXT , TEXT_AREA , RICH_TEXT_AREA , RADIO_BOX - > data . setTextValue ( textValue . trim ( ) ) ;
case CHECK_BOX , BOOLEAN_DECISION - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
data . setBooleanValue ( " true " . equals ( textValue . trim ( ) . toLowerCase ( Locale . ROOT ) ) ) ;
}
}
case DATE_PICKER - > {
Instant instant = null ;
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
try {
instant = Instant . parse ( textValue ) ;
} catch ( DateTimeParseException ex ) {
instant = LocalDate . parse ( textValue ) . atStartOfDay ( ) . toInstant ( ZoneOffset . UTC ) ;
}
}
data . setDateValue ( instant ) ;
}
case SELECT - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
String [ ] valuesParsed = migrationTools . tryParseJsonAsObjectString ( String [ ] . class , textValue ) ;
2024-05-02 16:13:59 +02:00
data . setTextValue ( valuesParsed ! = null & & valuesParsed . length > 0 ? valuesParsed [ 0 ] : null ) ;
2024-05-02 14:57:19 +02:00
}
}
case DATASET_IDENTIFIER , VALIDATION - > {
throw new NotSupportedException ( " Upload validator not supported " ) ;
}
case UPLOAD - > throw new NotSupportedException ( " Upload validator not supported " ) ;
case TAGS - > throw new NotSupportedException ( " Tags validator not supported " ) ;
case INTERNAL_ENTRIES_DMPS - > throw new NotSupportedException ( " INTERNAL_ENTRIES_DMPS validator not supported " ) ;
case INTERNAL_ENTRIES_DESCRIPTIONS - > throw new NotSupportedException ( " INTERNAL_ENTRIES_DESCRIPTIONS validator not supported " ) ;
case REFERENCE_TYPES - > throw new NotSupportedException ( " REFERENCE_TYPES validator not supported " ) ;
default - > throw new MyApplicationException ( " unrecognized type " + fieldType ) ;
}
return data ;
}
2024-02-21 17:38:28 +01:00
2024-03-21 14:36:48 +01:00
private @NotNull RuleEntity buildRuleEntity ( Rule persist , FieldType fieldType ) {
2024-01-30 15:31:03 +01:00
RuleEntity data = new RuleEntity ( ) ;
if ( persist = = null )
return data ;
2024-03-21 14:36:48 +01:00
String textValue = persist . getValue ( ) ;
if ( textValue = = null | | textValue . isEmpty ( ) ) return data ;
2024-01-30 15:31:03 +01:00
data . setTarget ( persist . getTarget ( ) ) ;
2024-03-21 14:36:48 +01:00
switch ( fieldType ) {
case FREE_TEXT , TEXT_AREA , RICH_TEXT_AREA , RADIO_BOX - > data . setTextValue ( textValue . trim ( ) ) ;
2024-05-02 09:46:36 +02:00
case CHECK_BOX , BOOLEAN_DECISION - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
data . setBooleanValue ( " true " . equals ( textValue . trim ( ) . toLowerCase ( Locale . ROOT ) ) ) ;
}
}
2024-03-21 14:36:48 +01:00
case DATE_PICKER - > {
Instant instant = null ;
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
try {
instant = Instant . parse ( textValue ) ;
} catch ( DateTimeParseException ex ) {
instant = LocalDate . parse ( textValue ) . atStartOfDay ( ) . toInstant ( ZoneOffset . UTC ) ;
}
}
data . setDateValue ( instant ) ;
}
case SELECT - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
String [ ] valuesParsed = migrationTools . tryParseJsonAsObjectString ( String [ ] . class , textValue ) ;
2024-05-02 16:13:59 +02:00
data . setTextValue ( valuesParsed ! = null & & valuesParsed . length > 0 ? valuesParsed [ 0 ] : null ) ;
2024-03-21 14:36:48 +01:00
}
}
2024-05-02 15:29:50 +02:00
case DATASET_IDENTIFIER - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
throw new NotSupportedException ( " DATASET_IDENTIFIER validator not supported " ) ;
}
}
case VALIDATION - > {
2024-03-21 14:36:48 +01:00
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
2024-05-02 15:29:50 +02:00
throw new NotSupportedException ( " VALIDATION validator not supported " ) ;
2024-03-21 14:36:48 +01:00
}
}
case UPLOAD - > throw new NotSupportedException ( " Upload validator not supported " ) ;
case TAGS - > throw new NotSupportedException ( " Tags validator not supported " ) ;
case INTERNAL_ENTRIES_DMPS - > throw new NotSupportedException ( " INTERNAL_ENTRIES_DMPS validator not supported " ) ;
case INTERNAL_ENTRIES_DESCRIPTIONS - > throw new NotSupportedException ( " INTERNAL_ENTRIES_DESCRIPTIONS validator not supported " ) ;
case REFERENCE_TYPES - > throw new NotSupportedException ( " REFERENCE_TYPES validator not supported " ) ;
default - > throw new MyApplicationException ( " unrecognized type " + fieldType ) ;
}
2024-01-30 15:31:03 +01:00
return data ;
}
private @NotNull MultiplicityEntity buildMultiplicityEntity ( Multiplicity persist ) {
MultiplicityEntity data = new MultiplicityEntity ( ) ;
if ( persist = = null )
return data ;
data . setMax ( persist . getMax ( ) ) ;
data . setMin ( persist . getMin ( ) ) ;
data . setPlaceholder ( persist . getPlaceholder ( ) ) ;
data . setTableView ( persist . getTableView ( ) ) ;
return data ;
}
2024-04-25 10:35:08 +02:00
private @NotNull PageEntity buildPageEntity ( Page persist , List < Section > sections , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-01-30 15:31:03 +01:00
PageEntity data = new PageEntity ( ) ;
if ( persist = = null )
return data ;
data . setId ( persist . getId ( ) ) ;
data . setOrdinal ( persist . getOrdinal ( ) ) ;
data . setTitle ( persist . getTitle ( ) ) ;
2024-01-31 16:48:59 +01:00
if ( ! this . conventionService . isListNullOrEmpty ( sections ) ) {
data . setSections ( new ArrayList < > ( ) ) ;
for ( Section sectionPersist : sections ) {
2024-04-25 10:35:08 +02:00
data . getSections ( ) . add ( this . buildSectionEntity ( sectionPersist , referenceTypeEntityMap , knownReferenceEntities ) ) ;
2024-01-31 16:48:59 +01:00
}
}
2024-01-30 15:31:03 +01:00
return data ;
}
2024-01-31 16:48:59 +01:00
2024-03-26 15:30:13 +01:00
private Map < String , List < Section > > mapSectionsToPage ( ViewStyleModel persist ) {
List < String > mappedSectionsIds = new ArrayList < > ( ) ;
Map < String , List < Section > > sectionPerPage = new HashMap < > ( ) ;
if ( persist = = null | | persist . getPages ( ) = = null | | persist . getSections ( ) = = null ) return sectionPerPage ;
for ( Page page : persist . getPages ( ) ) {
sectionPerPage . put ( page . getId ( ) , new ArrayList < > ( ) ) ;
for ( Section section : persist . getSections ( ) ) {
if ( this . isInPage ( section , page ) ) {
if ( mappedSectionsIds . contains ( section . getId ( ) ) ) throw new MyApplicationException ( " Multiple page section found for page " + page . getId ( ) + " section " + section . getId ( ) ) ;
mappedSectionsIds . add ( section . getId ( ) ) ;
sectionPerPage . get ( page . getId ( ) ) . add ( section ) ;
}
}
}
for ( Section section : persist . getSections ( ) ) {
if ( ! mappedSectionsIds . contains ( section . getId ( ) ) ) throw new MyApplicationException ( " Orphan section found " + section . getId ( ) ) ;
}
return sectionPerPage ;
}
2024-01-31 16:48:59 +01:00
private boolean isInPage ( Section section , Page page ) {
try {
return UUID . fromString ( section . getPage ( ) ) . equals ( UUID . fromString ( page . getId ( ) ) ) ;
} catch ( Exception e ) {
2024-03-26 15:30:13 +01:00
return section . getPage ( ) . toLowerCase ( Locale . ROOT ) . equals ( page . getId ( ) . toLowerCase ( Locale . ROOT ) ) ;
2024-01-31 16:48:59 +01:00
}
}
2024-02-21 17:38:28 +01:00
2024-04-25 10:35:08 +02:00
public ReferenceTypeEntity resolveReferenceTypeEntityAutoCompleteData ( AutoCompleteData persist , Map < String , ReferenceTypeEntity > referenceTypeEntityMap , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) throws URISyntaxException {
2024-02-21 17:38:28 +01:00
ReferenceTypeDefinitionEntity definitionEntity = this . buildReferenceTypeDefinitionEntity ( persist ) ;
String hash = this . referenceTypeCalculatedHash ( definitionEntity ) ;
2024-04-25 10:35:08 +02:00
ReferenceTypeEntity data = this . getReferenceTypeEntityByUrl ( definitionEntity , knownReferenceEntities ) ;
if ( data = = null ) {
data = referenceTypeEntityMap . getOrDefault ( hash , null ) ;
}
2024-02-21 17:38:28 +01:00
if ( data = = null ) {
data = new ReferenceTypeEntity ( ) ;
data . setId ( UUID . randomUUID ( ) ) ;
data . setIsActive ( IsActive . Active ) ;
data . setCreatedAt ( Instant . now ( ) ) ;
data . setName ( persist . getLabel ( ) . substring ( 0 , Math . min ( persist . getLabel ( ) . length ( ) , 199 ) ) ) ;
data . setCode ( persist . getLabel ( ) . substring ( 0 , Math . min ( persist . getLabel ( ) . length ( ) , 99 ) ) ) ;
data . setDefinition ( this . xmlHandlingService . toXmlSafe ( definitionEntity ) ) ;
data . setUpdatedAt ( Instant . now ( ) ) ;
referenceTypeEntityMap . put ( hash , data ) ;
}
return data ;
}
2024-04-25 10:35:08 +02:00
private ReferenceTypeEntity getReferenceTypeEntityByUrl ( ReferenceTypeDefinitionEntity definitionEntity , Map < UUID , ReferenceTypeEntity > knownReferenceEntities ) {
if ( definitionEntity = = null | | this . conventionService . isListNullOrEmpty ( definitionEntity . getSources ( ) ) | | definitionEntity . getSources ( ) . size ( ) ! = 1 ) return null ;
ExternalFetcherApiSourceConfigurationEntity externalFetcherApiSourceConfigurationEntity = ( ExternalFetcherApiSourceConfigurationEntity ) definitionEntity . getSources ( ) . getFirst ( ) ;
if ( externalFetcherApiSourceConfigurationEntity = = null | | this . conventionService . isNullOrEmpty ( externalFetcherApiSourceConfigurationEntity . getUrl ( ) ) ) return null ;
2024-04-25 12:13:44 +02:00
String url = StringUtils . stripEnd ( externalFetcherApiSourceConfigurationEntity . getUrl ( ) . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) , " / " ) ;
if ( url . equals ( " https://eestore.paas2.uninett.no/api/license " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . License ) ;
else if ( url . equals ( " https://zenodo.org/api/licenses " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . License ) ;
else if ( url . equals ( " https://zenodo.org/api/licenses?q={like} " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . License ) ;
else if ( url . equals ( " http://api.openaire.eu/search/publications " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . Publication ) ;
else if ( url . equals ( " http://api.openaire.eu/search/publications?format=json&title={like} " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . Publication ) ;
else if ( url . equals ( " https://services.openaire.eu/openaire/ds/searchdetails/{page}/{pageSize}?requestSortBy=id&order=ASCENDING " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . Publication ) ;
else if ( url . equals ( " https://eestore.paas2.uninett.no/api/datarepo " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . DataRepositories ) ;
else if ( url . equals ( " https://services.openaire.eu/search/v2/api/datasources?q={like}&fq=datasourcetypeuiname exact \" Data Repository \" &format=json " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . DataRepositories ) ;
else if ( url . equals ( " https://services.openaire.eu/search/v2/api/datasources?q={like}&fq=datasourcetypeuiname " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . DataRepositories ) ;
else if ( url . equals ( " https://services.openaire.eu/search/v2/api/resources?query=oaftype exact datasource and {like} and ( datasourcetypeuiid = \" pubsrepository::journal \" or datasourcetypeuiid = \" aggregator::pubsrepository::journals \" )&page=0&size=60&format=json " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . Journal ) ;
else if ( url . equals ( " https://eestore.paas2.uninett.no/api/metadataschema " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . Registries ) ;
else if ( url . equals ( " https://eestore.paas2.uninett.no/api/taxonomy " . trim ( ) . toLowerCase ( Locale . ROOT ) . replace ( " " , " " ) ) ) return knownReferenceEntities . get ( ReferenceTypeIds . Taxonomy ) ;
2024-04-25 10:35:08 +02:00
return null ;
}
2024-02-21 17:38:28 +01:00
public String referenceTypeCalculatedHash ( ReferenceTypeDefinitionEntity definitionEntity ) {
List < String > sourceKeys = new ArrayList < > ( ) ;
if ( definitionEntity . getSources ( ) ! = null ) {
2024-02-23 17:55:35 +01:00
for ( ExternalFetcherApiSourceConfigurationEntity source : definitionEntity . getSources ( ) . stream ( ) . map ( x - > ( ExternalFetcherApiSourceConfigurationEntity ) x ) . toList ( ) ) {
2024-02-21 17:38:28 +01:00
StringBuilder sourceStringBuilder = new StringBuilder ( ) ;
2024-04-25 12:13:44 +02:00
sourceStringBuilder . append ( StringUtils . stripEnd ( source . getUrl ( ) . trim ( ) . toLowerCase ( Locale . ROOT ) , " / " ) ) ;
sourceStringBuilder . append ( source . getHttpMethod ( ) . getValue ( ) . toString ( ) . trim ( ) . toLowerCase ( Locale . ROOT ) ) ;
// sourceStringBuilder.append(source.getKey());
// if (source.getResults() != null){
// sourceStringBuilder.append(source.getResults().getResultsArrayPath());
// if (source.getResults().getFieldsMapping() != null) {
// sourceStringBuilder.append(source.getResults().getFieldsMapping().stream().filter(x-> x.getCode().equals(ReferenceEntity.KnownFields.ReferenceId)).map(ResultFieldsMappingConfigurationEntity::getResponsePath).findFirst().orElse(""));
// sourceStringBuilder.append(source.getResults().getFieldsMapping().stream().filter(x-> x.getCode().equals(ReferenceEntity.KnownFields.Label)).map(ResultFieldsMappingConfigurationEntity::getResponsePath).findFirst().orElse(""));
// }
// }
2024-02-21 17:38:28 +01:00
sourceKeys . add ( sourceStringBuilder . toString ( ) . toLowerCase ( Locale . ROOT ) ) ;
}
}
return sourceKeys . stream ( ) . sorted ( ) . collect ( Collectors . joining ( " " ) ) ;
}
private @NotNull ReferenceTypeDefinitionEntity buildReferenceTypeDefinitionEntity ( AutoCompleteData persist ) throws URISyntaxException {
ReferenceTypeDefinitionEntity data = new ReferenceTypeDefinitionEntity ( ) ;
if ( persist = = null ) return data ;
data . setFields ( new ArrayList < > ( ) ) ;
if ( ! this . conventionService . isListNullOrEmpty ( persist . getAutoCompleteSingleDataList ( ) ) ) {
data . setSources ( new ArrayList < > ( ) ) ;
int ordinal = 0 ;
for ( AutoCompleteData . AutoCompleteSingleData sourceBaseConfigPersist : persist . getAutoCompleteSingleDataList ( ) ) {
data . getSources ( ) . add ( this . buildSourceBaseConfigEntity ( sourceBaseConfigPersist , ordinal ) ) ;
ordinal + + ;
}
}
return data ;
}
2024-02-23 17:55:35 +01:00
private @NotNull ExternalFetcherApiSourceConfigurationEntity buildSourceBaseConfigEntity ( AutoCompleteData . AutoCompleteSingleData persist , int ordinal ) throws URISyntaxException {
if ( persist = = null ) return new ExternalFetcherApiSourceConfigurationEntity ( ) ;
2024-02-21 17:38:28 +01:00
2024-02-23 17:55:35 +01:00
ExternalFetcherApiSourceConfigurationEntity apiEntity = new ExternalFetcherApiSourceConfigurationEntity ( ) ;
2024-02-21 17:38:28 +01:00
URI uri ;
if ( persist . getUrl ( ) . contains ( " ? " ) ) {
uri = new URI ( persist . getUrl ( ) . substring ( 0 , persist . getUrl ( ) . trim ( ) . lastIndexOf ( " ? " ) ) ) ;
} else {
uri = new URI ( persist . getUrl ( ) . trim ( ) ) ;
}
String source = persist . getAutoCompleteOptions ( ) . getSource ( ) ;
source = source ! = null & & ! source . isEmpty ( ) ? source : uri . getHost ( ) ;
String parsedUrl = persist . getUrl ( ) . trim ( ) ;
parsedUrl = parsedUrl . replace ( " %20 " , " " ) ;
parsedUrl = parsedUrl . replace ( " %22 " , " \" " ) ;
while ( parsedUrl . contains ( " & " ) ) {
parsedUrl = parsedUrl . replace ( " & " , " & " ) ;
}
apiEntity . setUrl ( parsedUrl ) ;
apiEntity . setResults ( this . buildResultsConfigEntity ( persist ) ) ;
apiEntity . setPaginationPath ( null ) ;
apiEntity . setContentType ( MediaType . APPLICATION_JSON_VALUE ) ;
apiEntity . setFirstPage ( " 1 " ) ;
apiEntity . setHttpMethod ( this . toReferenceTypeExternalApiHTTPMethodType ( persist . getMethod ( ) ) ) ;
apiEntity . setRequestBody ( null ) ;
apiEntity . setFilterType ( null ) ;
if ( persist . getHasAuth ( ) & & persist . getAuth ( ) ! = null ) {
apiEntity . setAuth ( this . buildAuthConfigEntity ( persist . getAuth ( ) ) ) ;
}
apiEntity . setQueries ( new ArrayList < > ( ) ) ;
apiEntity . getQueries ( ) . add ( this . buildQueryConfigEntity ( parsedUrl ) ) ;
2024-02-28 17:51:16 +01:00
apiEntity . setType ( ExternalFetcherSourceType . API ) ;
2024-02-21 17:38:28 +01:00
apiEntity . setKey ( source ) ;
apiEntity . setLabel ( source ) ;
apiEntity . setOrdinal ( ordinal ) ;
apiEntity . setReferenceTypeDependencyIds ( null ) ;
return apiEntity ;
}
private @NotNull ResultsConfigurationEntity buildResultsConfigEntity ( AutoCompleteData . AutoCompleteSingleData persist ) {
ResultsConfigurationEntity data = new ResultsConfigurationEntity ( ) ;
data . setResultsArrayPath ( persist . getOptionsRoot ( ) ) ;
if ( persist . getAutoCompleteOptions ( ) = = null & & this . conventionService . isNullOrEmpty ( persist . getAutoCompleteOptions ( ) . getLabel ( ) ) ) {
data . setFieldsMapping ( new ArrayList < > ( ) ) ;
ResultFieldsMappingConfigurationEntity labelField = new ResultFieldsMappingConfigurationEntity ( ) ;
labelField . setCode ( ReferenceEntity . KnownFields . Label ) ;
labelField . setResponsePath ( persist . getAutoCompleteOptions ( ) . getLabel ( ) ) ;
data . getFieldsMapping ( ) . add ( labelField ) ;
}
if ( persist . getAutoCompleteOptions ( ) = = null & & this . conventionService . isNullOrEmpty ( persist . getAutoCompleteOptions ( ) . getValue ( ) ) ) {
ResultFieldsMappingConfigurationEntity idField = new ResultFieldsMappingConfigurationEntity ( ) ;
idField . setCode ( ReferenceEntity . KnownFields . ReferenceId ) ;
idField . setResponsePath ( persist . getAutoCompleteOptions ( ) . getValue ( ) ) ;
data . getFieldsMapping ( ) . add ( idField ) ;
}
return data ;
}
private @NotNull QueryConfigEntity buildQueryConfigEntity ( String path ) {
QueryConfigEntity data = new QueryConfigEntity ( ) ;
data . setName ( " like " ) ;
if ( ( path . toLowerCase ( Locale . ROOT ) . trim ( ) . contains ( " openaire " ) | | path . toLowerCase ( Locale . ROOT ) . trim ( ) . contains ( " orcid " )
| | path . toLowerCase ( Locale . ROOT ) . trim ( ) . contains ( " ror " ) | | path . toLowerCase ( Locale . ROOT ) . trim ( ) . contains ( " fairsharing " ) ) ) {
data . setDefaultValue ( " * " ) ;
} else {
data . setDefaultValue ( " " ) ;
}
data . setCases ( new ArrayList < > ( ) ) ;
QueryCaseConfigEntity caseConfig = new QueryCaseConfigEntity ( ) ;
caseConfig . setReferenceTypeId ( null ) ;
caseConfig . setReferenceTypeSourceKey ( null ) ;
caseConfig . setSeparator ( null ) ;
caseConfig . setValue ( " {like} " ) ;
caseConfig . setLikePattern ( null ) ;
data . getCases ( ) . add ( caseConfig ) ;
return data ;
}
private @NotNull AuthenticationConfigurationEntity buildAuthConfigEntity ( AutoCompleteData . AuthAutoCompleteData persist ) {
AuthenticationConfigurationEntity data = new AuthenticationConfigurationEntity ( ) ;
if ( persist = = null ) return data ;
data . setEnabled ( true ) ;
data . setAuthUrl ( persist . getUrl ( ) ) ;
data . setAuthMethod ( this . toReferenceTypeExternalApiHTTPMethodType ( persist . getMethod ( ) ) ) ;
data . setAuthRequestBody ( persist . getBody ( ) ) ;
data . setType ( persist . getType ( ) ) ;
data . setAuthTokenPath ( persist . getPath ( ) ) ;
return data ;
}
2024-02-28 17:51:16 +01:00
private ExternalFetcherApiHTTPMethodType toReferenceTypeExternalApiHTTPMethodType ( String method ) {
2024-02-21 17:38:28 +01:00
if ( this . conventionService . isNullOrEmpty ( method ) ) {
logger . error ( " Migrate DescriptionTemplate autocomplete method not set use default GET " ) ;
2024-02-28 17:51:16 +01:00
return ExternalFetcherApiHTTPMethodType . GET ;
2024-02-21 17:38:28 +01:00
} else if ( method . toLowerCase ( Locale . ROOT ) . trim ( ) . equals ( " get " ) ) {
2024-02-28 17:51:16 +01:00
return ExternalFetcherApiHTTPMethodType . GET ;
2024-02-21 17:38:28 +01:00
} else if ( method . toLowerCase ( Locale . ROOT ) . trim ( ) . equals ( " post " ) ) {
2024-02-28 17:51:16 +01:00
return ExternalFetcherApiHTTPMethodType . POST ;
2024-02-21 17:38:28 +01:00
} else {
logger . error ( " Migrate DescriptionTemplate autocomplete method is invalid " + method ) ;
throw new MyApplicationException ( " Migrate DescriptionTemplate autocomplete method is invalid " + method ) ;
}
}
2024-03-12 15:28:07 +01:00
public enum FieldDataExternalDatasetType implements DatabaseEnum < String > {
ReusedDataset ( " reused_dataset " ) ,
ProducedDataset ( " produced_dataset " ) ,
Other ( " other " ) ;
private final String value ;
FieldDataExternalDatasetType ( String value ) {
this . value = value ;
}
@JsonValue
public String getValue ( ) {
return value ;
}
private static final Map < String , FieldDataExternalDatasetType > map = EnumUtils . getEnumValueMap ( FieldDataExternalDatasetType . class ) ;
public static FieldDataExternalDatasetType of ( String i ) {
return map . get ( i ) ;
}
}
2024-01-30 15:31:03 +01:00
}