2023-11-23 09:26:40 +01:00
package eu.old.eudat.migration ;
2024-04-26 16:12:29 +02:00
import org.opencdmp.commons.JsonHandlingService ;
import org.opencdmp.commons.XmlHandlingService ;
import org.opencdmp.commons.enums.* ;
import org.opencdmp.commons.types.description.* ;
import org.opencdmp.commons.types.descriptionreference.DescriptionReferenceDataEntity ;
import org.opencdmp.commons.types.descriptiontemplate.FieldSetEntity ;
import org.opencdmp.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity ;
import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity ;
import org.opencdmp.commons.types.externalfetcher.ExternalFetcherApiSourceConfigurationEntity ;
import org.opencdmp.commons.types.referencetype.ReferenceTypeDefinitionEntity ;
import org.opencdmp.convention.ConventionService ;
import org.opencdmp.data.* ;
import org.opencdmp.model.Dmp ;
import org.opencdmp.model.Reference ;
import org.opencdmp.model.ReferenceType ;
import org.opencdmp.model.persist.descriptionreference.DescriptionReferenceDataPersist ;
import org.opencdmp.query.* ;
2023-11-23 14:23:49 +01:00
import eu.old.eudat.data.dao.entities.DatasetDao ;
2023-11-23 09:26:40 +01:00
import eu.old.eudat.data.entities.Dataset ;
import eu.old.eudat.logic.services.operations.DatabaseRepository ;
2023-11-23 14:23:49 +01:00
import gr.cite.tools.data.query.QueryFactory ;
import gr.cite.tools.exception.MyApplicationException ;
2023-11-23 17:09:17 +01:00
import gr.cite.tools.fieldset.BaseFieldSet ;
2023-11-23 14:23:49 +01:00
import gr.cite.tools.logging.LoggerService ;
2023-11-23 09:26:40 +01:00
import jakarta.persistence.EntityManager ;
2023-11-23 14:23:49 +01:00
import jakarta.xml.bind.JAXBException ;
2024-04-24 17:28:13 +02:00
import org.jetbrains.annotations.NotNull ;
2023-11-23 17:09:17 +01:00
import org.json.JSONObject ;
2023-11-23 14:23:49 +01:00
import org.slf4j.LoggerFactory ;
2023-11-23 09:26:40 +01:00
import org.springframework.stereotype.Service ;
2023-11-23 14:23:49 +01:00
import org.xml.sax.SAXException ;
2023-11-23 09:26:40 +01:00
2023-11-23 14:23:49 +01:00
import javax.xml.parsers.ParserConfigurationException ;
import java.io.IOException ;
import java.time.Instant ;
2024-02-22 17:33:52 +01:00
import java.time.LocalDate ;
import java.time.ZoneOffset ;
import java.time.format.DateTimeParseException ;
2023-11-23 14:23:49 +01:00
import java.util.* ;
2024-02-28 17:48:46 +01:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2023-11-23 17:09:17 +01:00
import java.util.stream.Collectors ;
2023-11-23 09:26:40 +01:00
@Service
public class DatasetMigrationService {
2023-11-23 14:23:49 +01:00
private static final LoggerService logger = new LoggerService ( LoggerFactory . getLogger ( DmpDatasetProfileMigrationService . class ) ) ;
2023-11-23 09:26:40 +01:00
private final DatabaseRepository databaseRepository ;
private final JsonHandlingService jsonHandlingService ;
2023-11-23 14:23:49 +01:00
private final QueryFactory queryFactory ;
private final XmlHandlingService xmlHandlingService ;
private static final int PageSize = 500 ;
private static final boolean TestMode = false ;
2023-11-23 09:26:40 +01:00
private final EntityManager entityManager ;
2024-02-22 17:33:52 +01:00
private final ConventionService conventionService ;
2024-03-21 14:36:48 +01:00
private final MigrationTools migrationTools ;
2023-11-23 09:26:40 +01:00
2024-03-21 14:36:48 +01:00
public DatasetMigrationService ( DatabaseRepository databaseRepository , JsonHandlingService jsonHandlingService , QueryFactory queryFactory , XmlHandlingService xmlHandlingService , EntityManager entityManager , ConventionService conventionService , MigrationTools migrationTools ) {
2023-11-23 09:26:40 +01:00
this . databaseRepository = databaseRepository ;
this . jsonHandlingService = jsonHandlingService ;
2023-11-23 14:23:49 +01:00
this . queryFactory = queryFactory ;
this . xmlHandlingService = xmlHandlingService ;
2023-11-23 09:26:40 +01:00
this . entityManager = entityManager ;
2024-02-22 17:33:52 +01:00
this . conventionService = conventionService ;
2024-03-21 14:36:48 +01:00
this . migrationTools = migrationTools ;
2023-11-23 09:26:40 +01:00
}
2023-11-23 14:23:49 +01:00
public void migrate ( ) throws IOException , JAXBException , ParserConfigurationException , InstantiationException , IllegalAccessException , SAXException {
DatasetDao datasetDao = databaseRepository . getDatasetDao ( ) ;
long total = datasetDao . asQueryable ( ) . count ( ) ;
logger . debug ( " Migrate Dataset Total : " + total ) ;
int page = 0 ;
2024-02-23 17:55:35 +01:00
Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap = new HashMap < > ( ) ;
List < ReferenceTypeEntity > referenceTypes = this . queryFactory . query ( ReferenceTypeQuery . class ) . collectAs ( new BaseFieldSet ( ) . ensure ( ReferenceType . _id ) . ensure ( ReferenceType . _definition ) ) ;
for ( ReferenceTypeEntity referenceType : referenceTypes ) {
referenceTypeDefinitionEntityMap . put ( referenceType . getId ( ) , this . xmlHandlingService . fromXml ( ReferenceTypeDefinitionEntity . class , referenceType . getDefinition ( ) ) ) ;
}
Map < ReferenceKey , ReferenceEntity > referenceMap = new HashMap < > ( ) ;
List < ReferenceEntity > references = this . queryFactory . query ( ReferenceQuery . class ) . collectAs ( new BaseFieldSet ( ) . ensure ( Reference . _id ) . ensure ( Reference . _source ) . ensure ( Reference . _type ) . ensure ( Reference . _reference ) ) ;
for ( ReferenceEntity reference : references ) {
if ( reference . getCreatedById ( ) = = null ) referenceMap . put ( new ReferenceKey ( reference ) , reference ) ;
}
2023-11-23 14:23:49 +01:00
List < Dataset > items ;
do {
2023-11-23 17:09:17 +01:00
items = datasetDao . asQueryable ( ) . orderBy ( ( builder , root ) - > builder . asc ( root . get ( " created " ) ) ) . orderBy ( ( builder , root ) - > builder . asc ( root . get ( " ID " ) ) ) . skip ( page * PageSize ) . take ( PageSize ) . toList ( ) ;
2023-11-23 14:23:49 +01:00
if ( items ! = null & & ! items . isEmpty ( ) ) {
logger . debug ( " Migrate Dataset " + page * PageSize + " of " + total ) ;
2023-11-23 17:09:17 +01:00
List < DmpBlueprintEntity > dmpBlueprints = this . queryFactory . query ( DmpBlueprintQuery . class ) . ids ( items . stream ( ) . map ( x - > x . getDmp ( ) . getProfile ( ) . getId ( ) ) . distinct ( ) . toList ( ) ) . collect ( ) ;
Map < UUID , DefinitionEntity > dmpBlueprintsMap = new HashMap < > ( ) ;
for ( DmpBlueprintEntity dmpBlueprint : dmpBlueprints ) {
DefinitionEntity definitionEntity = this . xmlHandlingService . fromXml ( DefinitionEntity . class , dmpBlueprint . getDefinition ( ) ) ;
dmpBlueprintsMap . put ( dmpBlueprint . getId ( ) , definitionEntity ) ;
}
2024-02-21 17:38:28 +01:00
List < DmpDescriptionTemplateEntity > dmpDescriptionTemplateEntities = this . queryFactory . query ( DmpDescriptionTemplateQuery . class ) . dmpIds ( items . stream ( ) . map ( x - > x . getDmp ( ) . getId ( ) ) . distinct ( ) . toList ( ) ) . collect ( ) ;
List < DescriptionTemplateEntity > descriptionTemplates = this . queryFactory . query ( DescriptionTemplateQuery . class ) . ids ( items . stream ( ) . map ( x - > x . getProfile ( ) . getId ( ) ) . distinct ( ) . toList ( ) ) . collect ( ) ;
2024-02-28 17:48:46 +01:00
Map < UUID , UUID > creatorsByDmp = this . queryFactory . query ( DmpQuery . class ) . ids ( items . stream ( ) . map ( x - > x . getDmp ( ) . getId ( ) ) . distinct ( ) . toList ( ) ) . collectAs ( new BaseFieldSet ( ) . ensure ( Dmp . _id ) . ensure ( Dmp . _creator ) ) . stream ( ) . collect ( Collectors . toMap ( DmpEntity : : getId , DmpEntity : : getCreatorId ) ) ;
2023-11-23 17:09:17 +01:00
2024-04-26 16:12:29 +02:00
Map < UUID , org . opencdmp . commons . types . descriptiontemplate . DefinitionEntity > descriptionTemplateDefinitionMap = new HashMap < > ( ) ;
2024-02-21 17:38:28 +01:00
for ( DescriptionTemplateEntity descriptionTemplateEntity : descriptionTemplates ) {
2024-04-26 16:12:29 +02:00
descriptionTemplateDefinitionMap . put ( descriptionTemplateEntity . getId ( ) , this . xmlHandlingService . fromXml ( org . opencdmp . commons . types . descriptiontemplate . DefinitionEntity . class , descriptionTemplateEntity . getDefinition ( ) ) ) ;
2024-02-21 17:38:28 +01:00
}
2023-11-23 14:23:49 +01:00
for ( Dataset item : items ) {
2023-11-23 17:09:17 +01:00
DefinitionEntity definition = dmpBlueprintsMap . getOrDefault ( item . getDmp ( ) . getProfile ( ) . getId ( ) , null ) ;
2023-11-29 15:56:25 +01:00
if ( definition = = null | | definition . getSections ( ) = = null | | definition . getSections ( ) . size ( ) < = item . getDmpSectionIndex ( ) ) {
2023-11-23 17:09:17 +01:00
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " cannot found section id for section " + item . getDmpSectionIndex ( ) ) ;
}
UUID sectionId = definition . getSections ( ) . get ( item . getDmpSectionIndex ( ) ) . getId ( ) ;
if ( sectionId = = null ) {
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " cannot found section id for section " + item . getDmpSectionIndex ( ) ) ;
}
2024-02-21 17:38:28 +01:00
List < DmpDescriptionTemplateEntity > itemDescriptionTemplates = this . getOrCreateDmpDescriptionTemplateEntity ( item , sectionId , dmpDescriptionTemplateEntities ) ;
2023-11-23 17:09:17 +01:00
if ( itemDescriptionTemplates . size ( ) > 1 ) {
2024-02-28 17:48:46 +01:00
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " multiple DmpDescriptionTemplateEntity for section " + item . getDmpSectionIndex ( ) ) ;
2023-11-23 17:09:17 +01:00
}
2023-11-23 14:23:49 +01:00
DescriptionEntity data = new DescriptionEntity ( ) ;
2023-11-23 17:09:17 +01:00
data . setId ( item . getId ( ) ) ;
data . setDescription ( item . getDescription ( ) ) ;
if ( item . getCreator ( ) ! = null ) {
data . setCreatedById ( item . getCreator ( ) . getId ( ) ) ;
} else {
data . setCreatedById ( creatorsByDmp . getOrDefault ( item . getDmp ( ) . getId ( ) , null ) ) ;
}
data . setDmpId ( item . getDmp ( ) . getId ( ) ) ;
data . setLabel ( item . getLabel ( ) ) ;
2024-02-21 17:38:28 +01:00
data . setDmpDescriptionTemplateId ( itemDescriptionTemplates . getFirst ( ) . getId ( ) ) ;
2023-11-24 16:18:28 +01:00
data . setDescriptionTemplateId ( item . getProfile ( ) . getId ( ) ) ;
2023-11-23 17:09:17 +01:00
data . setCreatedAt ( item . getCreated ( ) ! = null ? item . getCreated ( ) . toInstant ( ) : Instant . now ( ) ) ;
data . setUpdatedAt ( item . getModified ( ) ! = null ? item . getModified ( ) . toInstant ( ) : Instant . now ( ) ) ;
if ( item . getFinalizedAt ( ) ! = null )
data . setFinalizedAt ( item . getFinalizedAt ( ) . toInstant ( ) ) ;
if ( item . getStatus ( ) = = 99 ) {
2023-11-23 14:23:49 +01:00
data . setIsActive ( IsActive . Inactive ) ;
data . setStatus ( DescriptionStatus . Canceled ) ;
} else {
data . setIsActive ( IsActive . Active ) ;
2023-11-23 17:09:17 +01:00
data . setStatus ( DescriptionStatus . of ( item . getStatus ( ) ) ) ;
}
2023-11-24 16:18:28 +01:00
if ( data . getCreatedById ( ) = = null ) {
2023-11-23 17:09:17 +01:00
throw new MyApplicationException ( " Migration skipped creator not found " + item . getId ( ) ) ;
2023-11-23 14:23:49 +01:00
}
2024-04-26 16:12:29 +02:00
org . opencdmp . commons . types . descriptiontemplate . DefinitionEntity descriptionTemplateDefinitionEntity = descriptionTemplateDefinitionMap . getOrDefault ( item . getProfile ( ) . getId ( ) , null ) ;
2023-11-23 14:23:49 +01:00
this . entityManager . persist ( data ) ;
this . entityManager . flush ( ) ;
2024-04-24 17:28:13 +02:00
data . setProperties ( this . jsonHandlingService . toJson ( this . buildPropertyDefinitionEntity ( item , descriptionTemplateDefinitionEntity , referenceTypeDefinitionEntityMap , referenceMap ) ) ) ;
this . entityManager . merge ( data ) ;
this . entityManager . flush ( ) ;
2023-11-23 14:23:49 +01:00
}
page + + ;
2023-11-23 09:26:40 +01:00
}
2023-11-23 14:23:49 +01:00
} while ( items ! = null & & ! items . isEmpty ( ) & & ! TestMode ) ;
2024-02-22 17:33:52 +01:00
}
2024-02-21 17:38:28 +01:00
private List < DmpDescriptionTemplateEntity > getOrCreateDmpDescriptionTemplateEntity ( Dataset item , UUID sectionId , List < DmpDescriptionTemplateEntity > dmpDescriptionTemplateEntities ) {
List < DmpDescriptionTemplateEntity > itemDescriptionTemplates = dmpDescriptionTemplateEntities . stream ( ) . filter ( x - > x . getDescriptionTemplateGroupId ( ) . equals ( item . getProfile ( ) . getGroupId ( ) ) & & x . getDmpId ( ) . equals ( item . getDmp ( ) . getId ( ) ) & & x . getSectionId ( ) . equals ( sectionId ) ) . toList ( ) ;
if ( itemDescriptionTemplates . isEmpty ( ) ) {
2024-02-28 17:48:46 +01:00
logger . warn ( " Migrate Dataset " + item . getId ( ) + " cannot found DmpDescriptionTemplateEntity for section " + item . getDmpSectionIndex ( ) ) ;
2024-02-22 17:33:52 +01:00
if ( dmpDescriptionTemplateEntities . stream ( ) . anyMatch ( x - > x . getDmpId ( ) . equals ( item . getDmp ( ) . getId ( ) ) & & x . getSectionId ( ) . equals ( sectionId ) ) ) {
2024-02-21 17:38:28 +01:00
DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity = new DmpDescriptionTemplateEntity ( ) ;
dmpDescriptionTemplateEntity . setId ( UUID . randomUUID ( ) ) ;
dmpDescriptionTemplateEntity . setDescriptionTemplateGroupId ( item . getProfile ( ) . getGroupId ( ) ) ;
dmpDescriptionTemplateEntity . setDmpId ( item . getDmp ( ) . getId ( ) ) ;
dmpDescriptionTemplateEntity . setCreatedAt ( item . getCreated ( ) ! = null ? item . getCreated ( ) . toInstant ( ) : Instant . now ( ) ) ;
dmpDescriptionTemplateEntity . setUpdatedAt ( item . getModified ( ) ! = null ? item . getModified ( ) . toInstant ( ) : Instant . now ( ) ) ;
dmpDescriptionTemplateEntity . setSectionId ( sectionId ) ;
dmpDescriptionTemplateEntity . setIsActive ( IsActive . Active ) ;
this . entityManager . persist ( dmpDescriptionTemplateEntity ) ;
itemDescriptionTemplates = List . of ( dmpDescriptionTemplateEntity ) ;
} else {
2024-02-28 17:48:46 +01:00
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " " + item . getLabel ( ) + " cannot found DmpDescriptionTemplateEntity for section " + item . getDmpSectionIndex ( ) ) ;
2024-02-21 17:38:28 +01:00
}
}
return itemDescriptionTemplates ;
}
2024-04-26 16:12:29 +02:00
private PropertyDefinitionEntity buildPropertyDefinitionEntity ( Dataset item , org . opencdmp . commons . types . descriptiontemplate . DefinitionEntity descriptionTemplateDefinitionEntity , Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap , Map < ReferenceKey , ReferenceEntity > referenceMap ) {
2024-02-22 17:33:52 +01:00
if ( this . conventionService . isNullOrEmpty ( item . getProperties ( ) ) ) return null ;
2024-02-21 17:38:28 +01:00
JSONObject jObject = new JSONObject ( item . getProperties ( ) ) ;
Map < String , Object > properties = jObject . toMap ( ) ;
PropertyDefinitionEntity propertyDefinitionEntity = new PropertyDefinitionEntity ( ) ;
2024-02-22 17:33:52 +01:00
propertyDefinitionEntity . setFieldSets ( new HashMap < > ( ) ) ;
2024-02-21 17:38:28 +01:00
List < FieldSetEntity > fieldSetEntities = descriptionTemplateDefinitionEntity . getAllFieldSets ( ) ;
2024-02-22 17:33:52 +01:00
for ( String key : properties . keySet ( ) ) {
if ( key . toLowerCase ( Locale . ROOT ) . trim ( ) . startsWith ( " commentfieldvalue " ) ) {
if ( properties . get ( key ) ! = null & & this . conventionService . isNullOrEmpty ( properties . get ( key ) . toString ( ) ) ) { continue ; }
FieldSetEntity currentFieldSet = fieldSetEntities . stream ( ) . filter ( x - > x . getId ( ) . equals ( key . trim ( ) . substring ( " commentfieldvalue " . length ( ) ) ) ) . findFirst ( ) . orElse ( null ) ;
if ( currentFieldSet = = null ) {
2024-02-28 17:48:46 +01:00
logger . error ( " Migrate Dataset " + item . getId ( ) + " " + item . getLabel ( ) + " Field set " + key + " not found for comment " + properties . get ( key ) . toString ( ) ) ;
2024-02-22 17:33:52 +01:00
continue ;
}
this . addSimpleCommentField ( propertyDefinitionEntity , currentFieldSet , properties . get ( key ) . toString ( ) ) ;
} else {
if ( ! key . toLowerCase ( Locale . ROOT ) . trim ( ) . startsWith ( " multiple_ " ) ) {
FieldSetEntity currentFieldSet = null ;
2024-04-26 16:12:29 +02:00
org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField = null ;
2024-02-22 17:33:52 +01:00
for ( FieldSetEntity fieldSetEntity : fieldSetEntities ) {
2024-04-26 16:12:29 +02:00
List < org . opencdmp . commons . types . descriptiontemplate . FieldEntity > fieldEntities = fieldSetEntity . getFieldById ( key . trim ( ) ) ;
2024-02-22 17:33:52 +01:00
if ( ! fieldEntities . isEmpty ( ) ) {
currentFieldSet = fieldSetEntity ;
currentField = fieldEntities . getFirst ( ) ;
}
}
if ( currentField = = null ) {
2024-02-28 17:48:46 +01:00
logger . error ( " Migrate Dataset " + item . getId ( ) + " " + item . getLabel ( ) + " Field set not found for field " + key + " " + properties . get ( key ) . toString ( ) ) ;
2024-02-22 17:33:52 +01:00
continue ;
}
2024-02-28 17:48:46 +01:00
this . addSimpleField ( item , propertyDefinitionEntity , currentFieldSet , currentField , properties , referenceTypeDefinitionEntityMap , referenceMap ) ;
2024-02-22 17:33:52 +01:00
}
}
}
2024-02-26 09:53:11 +01:00
2024-02-28 17:48:46 +01:00
this . addMultipleField ( item , properties , descriptionTemplateDefinitionEntity , propertyDefinitionEntity , referenceTypeDefinitionEntityMap , referenceMap ) ;
2024-02-22 17:33:52 +01:00
2024-02-26 09:53:11 +01:00
for ( PropertyDefinitionFieldSetEntity fieldSetEntity : propertyDefinitionEntity . getFieldSets ( ) . values ( ) ) {
int newOrdinal = 0 ;
for ( PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity : fieldSetEntity . getItems ( ) . stream ( ) . filter ( x - > x . getOrdinal ( ) > = 0 ) . sorted ( Comparator . comparingInt ( PropertyDefinitionFieldSetItemEntity : : getOrdinal ) ) . toList ( ) ) {
propertyDefinitionFieldSetItemEntity . setOrdinal ( newOrdinal ) ;
newOrdinal + + ;
}
for ( PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity : fieldSetEntity . getItems ( ) . stream ( ) . filter ( x - > x . getOrdinal ( ) < 0 ) . sorted ( Comparator . comparingInt ( PropertyDefinitionFieldSetItemEntity : : getOrdinal ) ) . toList ( ) ) {
propertyDefinitionFieldSetItemEntity . setOrdinal ( newOrdinal ) ;
newOrdinal + + ;
}
}
return propertyDefinitionEntity ;
}
2024-04-26 16:12:29 +02:00
private void addMultipleField ( Dataset item , Map < String , Object > properties , org . opencdmp . commons . types . descriptiontemplate . DefinitionEntity descriptionTemplateDefinitionEntity , PropertyDefinitionEntity propertyDefinitionEntity , Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap , Map < ReferenceKey , ReferenceEntity > referenceMap ) {
2024-02-26 09:53:11 +01:00
Map < MultipleFieldKey , Integer > groupOrdinalMapping = new HashMap < > ( ) ;
2024-02-22 17:33:52 +01:00
for ( String key : properties . keySet ( ) ) {
if ( key . trim ( ) . toLowerCase ( Locale . ROOT ) . startsWith ( " multiple_ " ) ) {
String newKey = key . trim ( ) . substring ( " multiple_ " . length ( ) ) ;
2024-02-28 17:48:46 +01:00
String [ ] keyParts = newKey . split ( " _ " ) ;
if ( keyParts . length > 4 & & newKey . contains ( " _multiple_ " ) ) {
String [ ] multiParts = newKey . split ( " _multiple_ " ) ;
if ( multiParts . length = = 2 ) {
newKey = multiParts [ 0 ] + " _ " + multiParts [ 1 ] . split ( " _ " ) [ multiParts [ 1 ] . split ( " _ " ) . length - 1 ] ;
keyParts = newKey . split ( " _ " ) ;
}
}
2024-02-22 17:33:52 +01:00
String fieldSetId = " " ;
String groupId = " " ;
2024-02-26 09:53:11 +01:00
int ordinal = Integer . MIN_VALUE ;
2024-02-22 17:33:52 +01:00
String fieldId = " " ;
2024-02-26 09:53:11 +01:00
2024-02-28 17:48:46 +01:00
if ( keyParts . length = = 4 ) {
fieldSetId = keyParts [ 0 ] . trim ( ) ;
groupId = keyParts [ 1 ] . trim ( ) ;
fieldId = keyParts [ 3 ] . trim ( ) ;
} else if ( keyParts . length = = 3 ) {
fieldSetId = keyParts [ 0 ] . trim ( ) ;
groupId = keyParts [ 1 ] . trim ( ) ;
fieldId = keyParts [ 2 ] . trim ( ) ;
} else {
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " " + item . getLabel ( ) + " Field group key has invalid format " + key + " " + properties . get ( key ) . toString ( ) ) ;
}
2024-02-26 09:53:11 +01:00
try {
if ( keyParts . length = = 4 ) ordinal = Integer . parseInt ( keyParts [ 2 ] . trim ( ) ) ;
2024-02-22 17:33:52 +01:00
} catch ( Exception e ) {
2024-02-28 17:48:46 +01:00
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " " + item . getLabel ( ) + " Field group key has invalid format " + key + " " + properties . get ( key ) . toString ( ) ) ;
2024-02-22 17:33:52 +01:00
}
2024-02-26 09:53:11 +01:00
2024-04-26 16:12:29 +02:00
org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField = descriptionTemplateDefinitionEntity . getFieldById ( fieldId ) . stream ( ) . findFirst ( ) . orElse ( null ) ;
2024-02-22 17:33:52 +01:00
if ( currentField = = null ) {
2024-02-28 17:48:46 +01:00
logger . warn ( " Migrate Dataset " + item . getId ( ) + " " + item . getLabel ( ) + " Field set not found for field " + key + " " + properties . get ( key ) . toString ( ) ) ;
2024-02-22 17:33:52 +01:00
continue ;
}
2024-02-26 09:53:11 +01:00
2024-02-22 17:33:52 +01:00
PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity . getFieldSets ( ) . getOrDefault ( fieldSetId , null ) ;
2024-02-26 09:53:11 +01:00
if ( propertyDefinitionFieldSetEntity = = null ) {
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity ( ) ;
propertyDefinitionFieldSetEntity . setItems ( new ArrayList < > ( ) ) ;
propertyDefinitionEntity . getFieldSets ( ) . put ( fieldId , propertyDefinitionFieldSetEntity ) ;
}
if ( ordinal = = Integer . MIN_VALUE ) {
if ( groupOrdinalMapping . containsKey ( new MultipleFieldKey ( fieldSetId , groupId ) ) ) {
ordinal = groupOrdinalMapping . get ( new MultipleFieldKey ( fieldSetId , groupId ) ) ;
2024-02-22 17:33:52 +01:00
} else {
2024-02-26 09:53:11 +01:00
ordinal = propertyDefinitionFieldSetEntity . getItems ( ) . stream ( ) . filter ( x - > ! x . getFields ( ) . isEmpty ( ) ) . map ( PropertyDefinitionFieldSetItemEntity : : getOrdinal ) . min ( Comparator . comparing ( x - > x ) ) . orElse ( - 1 ) - 1 ;
ordinal = Math . min ( ordinal , - 1 ) ;
2024-02-22 17:33:52 +01:00
}
2024-02-26 09:53:11 +01:00
}
if ( ! groupOrdinalMapping . containsKey ( new MultipleFieldKey ( fieldSetId , groupId ) ) ) {
groupOrdinalMapping . put ( new MultipleFieldKey ( fieldSetId , groupId ) , ordinal ) ;
2024-02-22 17:33:52 +01:00
} else {
2024-02-26 09:53:11 +01:00
if ( groupOrdinalMapping . get ( new MultipleFieldKey ( fieldSetId , groupId ) ) ! = ordinal ) throw new MyApplicationException ( " Invalid multiple key ordinal " + key ) ;
2024-02-22 17:33:52 +01:00
}
2024-02-28 17:48:46 +01:00
this . addMultipleField ( item , propertyDefinitionFieldSetEntity , ordinal , currentField , properties , referenceTypeDefinitionEntityMap , referenceMap ) ;
2024-02-26 09:53:11 +01:00
2024-02-22 17:33:52 +01:00
}
}
}
2024-04-26 16:12:29 +02:00
private void addMultipleField ( Dataset item , PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity , int ordinal , org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField , Map < String , Object > properties , Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap , Map < ReferenceKey , ReferenceEntity > referenceMap ) {
2024-02-22 17:33:52 +01:00
PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity = propertyDefinitionFieldSetEntity . getItems ( ) . stream ( ) . filter ( x - > x . getOrdinal ( ) = = ordinal ) . findFirst ( ) . orElse ( null ) ;
if ( propertyDefinitionFieldSetItemEntity = = null ) {
propertyDefinitionFieldSetItemEntity = new PropertyDefinitionFieldSetItemEntity ( ) ;
propertyDefinitionFieldSetItemEntity . setFields ( new HashMap < > ( ) ) ;
propertyDefinitionFieldSetItemEntity . setOrdinal ( ordinal ) ;
propertyDefinitionFieldSetEntity . getItems ( ) . add ( propertyDefinitionFieldSetItemEntity ) ;
}
2024-02-28 17:48:46 +01:00
propertyDefinitionFieldSetItemEntity . getFields ( ) . put ( currentField . getId ( ) , this . buildField ( item , currentField , properties , referenceTypeDefinitionEntityMap , referenceMap ) ) ;
2024-02-22 17:33:52 +01:00
}
2024-04-26 16:12:29 +02:00
private void addSimpleField ( Dataset item , PropertyDefinitionEntity propertyDefinitionEntity , FieldSetEntity currentFieldSet , org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField , Map < String , Object > properties , Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap , Map < ReferenceKey , ReferenceEntity > referenceMap ) {
2024-02-22 17:33:52 +01:00
PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity . getFieldSets ( ) . getOrDefault ( currentFieldSet . getId ( ) , null ) ;
if ( propertyDefinitionFieldSetEntity = = null ) {
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity ( ) ;
propertyDefinitionFieldSetEntity . setItems ( new ArrayList < > ( ) ) ;
propertyDefinitionEntity . getFieldSets ( ) . put ( currentFieldSet . getId ( ) , propertyDefinitionFieldSetEntity ) ;
}
PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity = null ;
if ( this . conventionService . isListNullOrEmpty ( propertyDefinitionFieldSetEntity . getItems ( ) ) ) {
propertyDefinitionFieldSetItemEntity = new PropertyDefinitionFieldSetItemEntity ( ) ;
propertyDefinitionFieldSetItemEntity . setFields ( new HashMap < > ( ) ) ;
propertyDefinitionFieldSetItemEntity . setOrdinal ( 0 ) ;
propertyDefinitionFieldSetEntity . getItems ( ) . add ( propertyDefinitionFieldSetItemEntity ) ;
} else {
propertyDefinitionFieldSetItemEntity = propertyDefinitionFieldSetEntity . getItems ( ) . getFirst ( ) ;
}
2024-02-28 17:48:46 +01:00
propertyDefinitionFieldSetItemEntity . getFields ( ) . put ( currentField . getId ( ) , this . buildField ( item , currentField , properties , referenceTypeDefinitionEntityMap , referenceMap ) ) ;
2024-02-22 17:33:52 +01:00
}
2024-02-21 17:38:28 +01:00
2024-02-22 17:33:52 +01:00
private void addSimpleCommentField ( PropertyDefinitionEntity propertyDefinitionEntity , FieldSetEntity currentFieldSet , String comment ) {
PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity . getFieldSets ( ) . getOrDefault ( currentFieldSet . getId ( ) , null ) ;
if ( propertyDefinitionFieldSetEntity = = null ) {
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity ( ) ;
propertyDefinitionFieldSetEntity . setItems ( new ArrayList < > ( ) ) ;
propertyDefinitionEntity . getFieldSets ( ) . put ( currentFieldSet . getId ( ) , propertyDefinitionFieldSetEntity ) ;
}
PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity = null ;
if ( this . conventionService . isListNullOrEmpty ( propertyDefinitionFieldSetEntity . getItems ( ) ) ) {
propertyDefinitionFieldSetItemEntity = new PropertyDefinitionFieldSetItemEntity ( ) ;
propertyDefinitionFieldSetItemEntity . setFields ( new HashMap < > ( ) ) ;
propertyDefinitionFieldSetItemEntity . setOrdinal ( 0 ) ;
propertyDefinitionFieldSetEntity . getItems ( ) . add ( propertyDefinitionFieldSetItemEntity ) ;
} else {
propertyDefinitionFieldSetItemEntity = propertyDefinitionFieldSetEntity . getItems ( ) . getFirst ( ) ;
}
propertyDefinitionFieldSetItemEntity . setComment ( comment ) ;
2024-02-21 17:38:28 +01:00
}
2024-02-22 17:33:52 +01:00
2024-04-26 16:12:29 +02:00
private FieldEntity buildField ( Dataset item , org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField , Map < String , Object > properties , Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap , Map < ReferenceKey , ReferenceEntity > referenceMap ) {
2024-02-22 17:33:52 +01:00
FieldEntity fieldEntity = new FieldEntity ( ) ;
String textValue = properties . get ( currentField . getId ( ) ) ! = null ? properties . get ( currentField . getId ( ) ) . toString ( ) : null ;
if ( textValue = = null | | textValue . isEmpty ( ) ) return fieldEntity ;
switch ( currentField . getData ( ) . getFieldType ( ) ) {
case FREE_TEXT , TEXT_AREA , RICH_TEXT_AREA , RADIO_BOX - > fieldEntity . setTextValue ( textValue . trim ( ) ) ;
2024-05-02 09:46:36 +02:00
case CHECK_BOX , BOOLEAN_DECISION - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
fieldEntity . setBooleanValue ( " true " . equals ( textValue . trim ( ) . toLowerCase ( Locale . ROOT ) ) ) ;
}
}
2024-02-22 17:33:52 +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 ) ;
}
}
fieldEntity . setDateValue ( instant ) ;
}
case SELECT - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
2024-03-21 14:36:48 +01:00
String [ ] valuesParsed = migrationTools . tryParseJsonAsObjectString ( String [ ] . class , textValue ) ;
2024-02-22 17:33:52 +01:00
fieldEntity . setTextListValue ( valuesParsed = = null ? List . of ( textValue ) : Arrays . stream ( valuesParsed ) . toList ( ) ) ;
}
}
2024-04-10 11:10:35 +02:00
case DATASET_IDENTIFIER - > {
2024-02-22 17:33:52 +01:00
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
2024-03-21 14:36:48 +01:00
ExternalIdentifierEntity externalIdentifierEntity = migrationTools . tryParseJsonAsObjectString ( ExternalIdentifierEntity . class , textValue ) ;
2024-02-22 17:33:52 +01:00
if ( externalIdentifierEntity = = null ) throw new MyApplicationException ( " Could not parse dataset External Identifier : " + textValue ) ;
fieldEntity . setExternalIdentifier ( externalIdentifierEntity ) ;
}
}
case UPLOAD - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
2024-03-21 14:36:48 +01:00
Map < String , String > valuesParsed = migrationTools . tryParseJsonAsObjectString ( Map . class , textValue ) ;
2024-02-22 17:33:52 +01:00
if ( valuesParsed = = null ) {
valuesParsed = ( Map < String , String > ) properties . get ( currentField . getId ( ) ) ;
}
if ( valuesParsed = = null ) throw new MyApplicationException ( " Could not parse upload : " + textValue ) ;
String id = valuesParsed . getOrDefault ( " id " , null ) ;
if ( id = = null ) throw new MyApplicationException ( " Could not parse upload : " + textValue ) ;
fieldEntity . setTextValue ( UUID . fromString ( id ) . toString ( ) ) ;
}
}
case TAGS - > {
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) {
2024-03-21 14:36:48 +01:00
Tag [ ] tags = migrationTools . tryParseJsonAsObjectString ( Tag [ ] . class , textValue ) ;
2024-02-22 17:33:52 +01:00
if ( tags = = null ) {
2024-03-21 14:36:48 +01:00
Tag tag = migrationTools . tryParseJsonAsObjectString ( Tag . class , textValue ) ;
2024-02-22 17:33:52 +01:00
if ( tag ! = null ) tags = List . of ( tag ) . toArray ( Tag [ ] : : new ) ;
}
2024-03-21 14:36:48 +01:00
if ( tags = = null ) tags = this . jsonHandlingService . fromJsonSafe ( Tag [ ] . class , migrationTools . cleanTagString ( textValue ) ) ;
2024-02-28 17:48:46 +01:00
if ( tags = = null ) throw new MyApplicationException ( " Could not parse tag : " + textValue ) ;
2024-02-23 17:55:35 +01:00
fieldEntity . setTextListValue ( Arrays . stream ( tags ) . toList ( ) . stream ( ) . map ( Tag : : getName ) . toList ( ) ) ;
2024-02-22 17:33:52 +01:00
}
}
2024-02-23 17:55:35 +01:00
case INTERNAL_ENTRIES_DMPS - > throw new MyApplicationException ( " Found INTERNAL_ENTRIES_DMPS into description " ) ;
case INTERNAL_ENTRIES_DESCRIPTIONS - > throw new MyApplicationException ( " Found INTERNAL_ENTRIES_DMPS into description " ) ;
2024-04-10 11:10:35 +02:00
case VALIDATION - > throw new MyApplicationException ( " Found VALIDATION into description " ) ;
2024-02-23 17:55:35 +01:00
case REFERENCE_TYPES - > {
2024-02-26 09:53:11 +01:00
if ( ! this . conventionService . isNullOrEmpty ( textValue ) ) { //TODO
2024-02-28 17:48:46 +01:00
ReferenceTypeDataEntity referenceTypeDataEntity = ( ReferenceTypeDataEntity ) currentField . getData ( ) ;
if ( referenceTypeDataEntity = = null ) throw new MyApplicationException ( " Could not parse description template reference : " + textValue ) ;
2024-04-09 17:14:16 +02:00
Map < String , Object > [ ] references = migrationTools . tryParseJsonAsObjectString ( Map [ ] . class , textValue ) ;
if ( referenceTypeDataEntity . getReferenceTypeId ( ) . equals ( ReferenceTypeIds . Currency ) ) {
Currency currency = migrationTools . tryParseJsonAsObjectString ( Currency . class , textValue ) ;
if ( currency = = null & & textValue . toUpperCase ( Locale . ROOT ) . contains ( " EUR " ) ) {
currency = new Currency ( ) ;
currency . setName ( " Euro " ) ;
currency . setValue ( " EUR " ) ;
}
if ( currency = = null & & textValue . toUpperCase ( Locale . ROOT ) . contains ( " US DOLLAR " ) ) {
currency = new Currency ( ) ;
currency . setName ( " US DOLLAR " ) ;
currency . setValue ( " USD " ) ;
}
if ( currency = = null ) throw new MyApplicationException ( " Could not parse Currency : " + textValue ) ;
//TODO: {"name":"Euro","value":"EUR"} what we want to keep ?
Map < String , Object > reference = new HashMap < > ( ) ;
2024-04-10 11:10:35 +02:00
reference . put ( " pid " , currency . getValue ( ) ) ;
reference . put ( " name " , currency . getName ( ) ) ;
reference . put ( " key " , " currencies " ) ;
2024-04-09 17:14:16 +02:00
references = List . of ( reference ) . toArray ( Map [ ] : : new ) ;
}
2024-02-23 17:55:35 +01:00
if ( references = = null ) {
2024-03-21 14:36:48 +01:00
Map < String , Object > reference = migrationTools . tryParseJsonAsObjectString ( Map . class , textValue ) ;
2024-02-23 17:55:35 +01:00
if ( reference ! = null ) references = List . of ( reference ) . toArray ( Map [ ] : : new ) ;
}
2024-02-28 17:48:46 +01:00
if ( references = = null & & ! textValue . contains ( " { " ) & & ! textValue . contains ( " [ " ) ) {
if ( ! textValue . contains ( " , " ) ) {
Map < String , Object > ref = new HashMap < > ( ) ;
ref . put ( " name " , textValue ) ;
if ( ! ReferenceTypeIds . KnownReferenceTypeIds . contains ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ReferenceTypeDefinitionEntity referenceTypeDefinition = referenceTypeDefinitionEntityMap . getOrDefault ( referenceTypeDataEntity . getReferenceTypeId ( ) , null ) ;
if ( referenceTypeDefinition = = null ) throw new MyApplicationException ( " Could not find reference type for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
if ( this . conventionService . isListNullOrEmpty ( referenceTypeDefinition . getSources ( ) ) ) throw new MyApplicationException ( " Could not find reference source for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
if ( referenceTypeDefinition . getSources ( ) . size ( ) > 1 ) throw new MyApplicationException ( " Multiple reference source for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
ref . put ( " source " , referenceTypeDefinition . getSources ( ) . getFirst ( ) . getKey ( ) ) ;
} else if ( ReferenceTypeIds . DataRepositories . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " openaire " ) ;
} else if ( ReferenceTypeIds . Services . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " eosc " ) ;
} else if ( ReferenceTypeIds . Registries . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " rda-metadata-schemes " ) ;
} else if ( ReferenceTypeIds . Organizations . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " openaire " ) ;
} else if ( ReferenceTypeIds . Datasets . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " openaire " ) ;
} else if ( ReferenceTypeIds . Journal . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " openaire " ) ;
} else if ( ReferenceTypeIds . License . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " opendefinition " ) ;
} else if ( ReferenceTypeIds . PubRepositories . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " opendefinition " ) ;
2024-04-25 10:35:08 +02:00
} else if ( ReferenceTypeIds . Grants . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " openaire " ) ;
} else if ( ReferenceTypeIds . Project . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " project " ) ;
} else if ( ReferenceTypeIds . Taxonomy . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " taxonomy " ) ;
} else if ( ReferenceTypeIds . Publication . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " openaire " ) ;
} else if ( ReferenceTypeIds . Currency . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
ref . put ( " source " , " currencies " ) ;
2024-02-28 17:48:46 +01:00
} else if ( ReferenceTypeIds . Researcher . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
Matcher pattern = Pattern . compile ( " \\ ((.*?) \\ ) " ) . matcher ( textValue ) ;
if ( pattern . find ( ) ) {
String matchedString = pattern . group ( 0 ) ;
ref . put ( " name " , textValue . replace ( matchedString , " " ) . trim ( ) ) ;
matchedString = matchedString . substring ( 1 , matchedString . length ( ) - 1 ) ;
ref . put ( " source " , matchedString . split ( " : " ) [ 0 ] ) ;
ref . put ( " id " , matchedString . split ( " : " ) [ 1 ] ) ;
} else {
ref . put ( " name " , textValue ) ;
ref . put ( " source " , " orcid " ) ;
}
}
if ( ref . containsKey ( " source " ) ) references = List . of ( ref ) . toArray ( new Map [ 1 ] ) ;
} else {
2024-04-25 10:35:08 +02:00
ReferenceTypeDefinitionEntity referenceTypeDefinition = referenceTypeDefinitionEntityMap . getOrDefault ( referenceTypeDataEntity . getReferenceTypeId ( ) , null ) ;
if ( referenceTypeDefinition = = null ) throw new MyApplicationException ( " Could not find reference type for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
if ( this . conventionService . isListNullOrEmpty ( referenceTypeDefinition . getSources ( ) ) ) throw new MyApplicationException ( " Could not find reference source for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
//if (referenceTypeDefinition.getSources().size() > 1) throw new MyApplicationException("Multiple reference source for reference : " + this.jsonHandlingService.toJsonSafe(textValue));
String source = referenceTypeDefinition . getSources ( ) . getFirst ( ) . getKey ( ) ;
String [ ] parts = null ;
if ( textValue . startsWith ( " [ \" " ) ) {
parts = migrationTools . tryParseJsonAsObjectString ( String [ ] . class , textValue ) ;
} else {
if ( textValue . startsWith ( " [ " ) ) textValue = textValue . substring ( 1 ) . trim ( ) ;
if ( textValue . endsWith ( " ] " ) ) textValue = textValue . substring ( 0 , textValue . length ( ) - 1 ) . trim ( ) ;
parts = textValue . split ( " , " ) ;
}
if ( parts ! = null ) {
List < Map < String , Object > > items = new ArrayList < > ( ) ;
for ( String part : parts ) {
if ( this . conventionService . isNullOrEmpty ( part ) ) continue ;
part = part . trim ( ) . replace ( " , " , " " ) . trim ( ) ;
if ( this . conventionService . isNullOrEmpty ( part ) | | part . equals ( " null " ) ) continue ;
Map < String , Object > ref = new HashMap < > ( ) ;
ref . put ( " name " , part ) ;
ref . put ( " source " , source ) ;
items . add ( ref ) ;
2024-02-28 17:48:46 +01:00
}
2024-04-25 10:35:08 +02:00
if ( items . isEmpty ( ) ) break ;
references = items . toArray ( new Map [ items . size ( ) ] ) ;
}
2024-02-28 17:48:46 +01:00
}
}
2024-04-25 10:35:08 +02:00
if ( references = = null ) {
2024-02-28 17:48:46 +01:00
if ( textValue . equalsIgnoreCase ( " [ \" dublin core \" , \" { \\ \" id \\ \" : \\ \" metadataschema:rd-alliance/dublin-core \\ \" , \\ \" label \\ \" : \\ \" Dublin Core \\ \" , \\ \" source \\ \" : \\ \" \\ \" , \\ \" uri \\ \" : \\ \" http://dublincore.org \\ \" } \" ] " ) ) {
Map < String , Object > ref = new HashMap < > ( ) ;
ref . put ( " name " , " Dublin Core " ) ;
ref . put ( " source " , " metadataschema " ) ;
ref . put ( " id " , " rd-alliance/dublin-core " ) ;
ref . put ( " uri " , " http://dublincore.org " ) ;
references = List . of ( ref ) . toArray ( new Map [ 1 ] ) ;
} else if ( textValue . equalsIgnoreCase ( " [ \" Datacite \" , \" { \\ \" id \\ \" : \\ \" metadataschema:rd-alliance/datacite-metadata-schema \\ \" , \\ \" label \\ \" : \\ \" DataCite Metadata Schema \\ \" , \\ \" source \\ \" : \\ \" \\ \" , \\ \" uri \\ \" : \\ \" http://schema.datacite.org \\ \" } \" ] " ) ) {
Map < String , Object > ref = new HashMap < > ( ) ;
ref . put ( " name " , " DataCite Metadata Schema " ) ;
ref . put ( " source " , " metadataschema " ) ;
ref . put ( " id " , " rd-alliance/datacite-metadata-schema " ) ;
ref . put ( " uri " , " http://schema.datacite.org " ) ;
references = List . of ( ref ) . toArray ( new Map [ 1 ] ) ;
} else if ( textValue . equalsIgnoreCase ( " [ \" . \" ] " ) ) {
break ;
} else if ( textValue . equalsIgnoreCase ( " [INT] " ) ) {
ReferenceTypeDefinitionEntity referenceTypeDefinition = referenceTypeDefinitionEntityMap . getOrDefault ( referenceTypeDataEntity . getReferenceTypeId ( ) , null ) ;
if ( referenceTypeDefinition = = null ) throw new MyApplicationException ( " Could not find reference type for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
if ( this . conventionService . isListNullOrEmpty ( referenceTypeDefinition . getSources ( ) ) ) throw new MyApplicationException ( " Could not find reference source for reference : " + this . jsonHandlingService . toJsonSafe ( textValue ) ) ;
//if (referenceTypeDefinition.getSources().size() > 1) throw new MyApplicationException("Multiple reference source for reference : " + this.jsonHandlingService.toJsonSafe(textValue));
Map < String , Object > ref = new HashMap < > ( ) ;
ref . put ( " name " , " INT " ) ;
ref . put ( " source " , referenceTypeDefinition . getSources ( ) . getFirst ( ) . getKey ( ) ) ;
references = List . of ( ref ) . toArray ( new Map [ 1 ] ) ;
}
}
if ( references = = null & & ReferenceTypeIds . Researcher . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) {
if ( textValue . equals ( " [ \" - \" ] " ) ) {
break ;
} else if ( textValue . startsWith ( " [ \" " ) & & ! textValue . startsWith ( " [ \" { " ) & & textValue . contains ( " , " ) ) {
2024-03-21 14:36:48 +01:00
references = migrationTools . tryParseJsonAsObjectString ( Map [ ] . class , textValue . replace ( textValue . substring ( 1 , textValue . indexOf ( " , " ) + 1 ) , " " ) ) ;
2024-02-28 17:48:46 +01:00
} else if ( textValue . contains ( " , " ) & & ! textValue . contains ( " { " ) & & ! textValue . contains ( " [ " ) ) {
String [ ] parts = textValue . split ( " , " ) ;
List < Map < String , Object > > items = new ArrayList < > ( ) ;
for ( String part : parts ) {
if ( this . conventionService . isNullOrEmpty ( part ) ) continue ;
part = part . trim ( ) . replace ( " , " , " " ) . trim ( ) ;
if ( this . conventionService . isNullOrEmpty ( part ) | | part . equals ( " null " ) ) continue ;
Matcher pattern = Pattern . compile ( " \\ ((.*?) \\ ) " ) . matcher ( part ) ;
Map < String , Object > ref = new HashMap < > ( ) ;
if ( pattern . find ( ) ) {
String matchedString = pattern . group ( 0 ) ;
ref . put ( " name " , part . replace ( matchedString , " " ) . trim ( ) ) ;
matchedString = matchedString . substring ( 1 , matchedString . length ( ) - 1 ) ;
ref . put ( " source " , matchedString . split ( " : " ) [ 0 ] ) ;
ref . put ( " id " , matchedString . split ( " : " ) [ 1 ] ) ;
} else {
ref . put ( " name " , part ) ;
ref . put ( " source " , " orcid " ) ;
}
items . add ( ref ) ;
}
if ( items . isEmpty ( ) ) break ;
references = items . toArray ( new Map [ items . size ( ) ] ) ;
}
}
if ( references = = null & & ( ReferenceTypeIds . Datasets . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) )
| | ReferenceTypeIds . DataRepositories . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) )
| | ReferenceTypeIds . Services . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) )
| | ReferenceTypeIds . License . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) ) {
textValue = textValue . trim ( ) ;
if ( textValue . equals ( " [ \" . \" ] " ) ) {
break ;
} else if ( ! textValue . contains ( " { " ) ) {
String [ ] parts = null ;
if ( textValue . startsWith ( " [ \" " ) ) {
2024-03-21 14:36:48 +01:00
parts = migrationTools . tryParseJsonAsObjectString ( String [ ] . class , textValue ) ;
2024-02-28 17:48:46 +01:00
} else {
if ( textValue . startsWith ( " [ " ) ) textValue = textValue . substring ( 1 ) . trim ( ) ;
if ( textValue . endsWith ( " ] " ) ) textValue = textValue . substring ( 0 , textValue . length ( ) - 1 ) . trim ( ) ;
parts = textValue . split ( " , " ) ;
}
if ( parts ! = null ) {
List < Map < String , Object > > items = new ArrayList < > ( ) ;
for ( String part : parts ) {
if ( this . conventionService . isNullOrEmpty ( part ) ) continue ;
part = part . trim ( ) . replace ( " , " , " " ) . trim ( ) ;
if ( this . conventionService . isNullOrEmpty ( part ) | | part . equals ( " null " ) ) continue ;
Map < String , Object > ref = new HashMap < > ( ) ;
ref . put ( " name " , part ) ;
if ( ReferenceTypeIds . Services . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) ref . put ( " source " , " eosc " ) ;
else if ( ReferenceTypeIds . License . equals ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ) ref . put ( " source " , " opendefinition " ) ;
else ref . put ( " source " , " openaire " ) ;
items . add ( ref ) ;
}
if ( items . isEmpty ( ) ) break ;
references = items . toArray ( new Map [ items . size ( ) ] ) ;
}
}
}
2024-02-23 17:55:35 +01:00
if ( references = = null ) {
2024-02-28 17:48:46 +01:00
throw new MyApplicationException ( " Migrate Dataset " + item . getId ( ) + " " + item . getCreated ( ) + " " + item . getLabel ( ) + " Could not parse reference : " + referenceTypeDataEntity . getReferenceTypeId ( ) + " with value " + textValue ) ;
2024-02-23 17:55:35 +01:00
}
fieldEntity . setTextListValue ( new ArrayList < > ( ) ) ;
2024-02-28 17:48:46 +01:00
for ( Map < String , Object > reference : references ) {
ReferenceEntity referenceEntity = this . geReferenceEntity ( item , reference , referenceTypeDefinitionEntityMap , referenceTypeDataEntity , referenceMap ) ;
2024-04-24 17:28:13 +02:00
if ( referenceEntity ! = null ) {
this . createDescriptionReferenceEntity ( referenceEntity . getId ( ) , item . getId ( ) , currentField ) ;
fieldEntity . getTextListValue ( ) . add ( referenceEntity . getId ( ) . toString ( ) ) ;
}
2024-02-23 17:55:35 +01:00
}
}
}
2024-02-22 17:33:52 +01:00
}
return fieldEntity ;
}
2024-02-28 17:48:46 +01:00
private ReferenceEntity geReferenceEntity ( Dataset dataset , Map < String , Object > item , Map < UUID , ReferenceTypeDefinitionEntity > referenceTypeDefinitionEntityMap , ReferenceTypeDataEntity referenceTypeDataEntity , Map < ReferenceKey , ReferenceEntity > referenceMap ) {
2024-02-23 17:55:35 +01:00
ReferenceTypeDefinitionEntity referenceTypeDefinition = referenceTypeDefinitionEntityMap . getOrDefault ( referenceTypeDataEntity . getReferenceTypeId ( ) , null ) ;
2024-02-28 17:48:46 +01:00
if ( referenceTypeDefinition = = null ) throw new MyApplicationException ( " Could not find reference type for reference : " + this . jsonHandlingService . toJsonSafe ( item ) ) ;
if ( this . conventionService . isListNullOrEmpty ( referenceTypeDefinition . getSources ( ) ) ) throw new MyApplicationException ( " Could not find reference source for reference : " + this . jsonHandlingService . toJsonSafe ( item ) ) ;
2024-02-23 17:55:35 +01:00
ReferenceEntity data = new ReferenceEntity ( ) ;
if ( item . containsKey ( " name " ) ) data . setLabel ( ( String ) item . get ( " name " ) ) ;
if ( this . conventionService . isNullOrEmpty ( data . getLabel ( ) ) & & item . containsKey ( " label " ) ) data . setLabel ( ( String ) item . get ( " label " ) ) ;
if ( item . containsKey ( " abbreviation " ) ) data . setAbbreviation ( ( String ) item . get ( " abbreviation " ) ) ;
2024-02-28 17:48:46 +01:00
if ( item . containsKey ( " pid " ) ) data . setReference ( ( String ) item . get ( " pid " ) ) ;
2024-02-23 17:55:35 +01:00
2024-02-28 17:48:46 +01:00
if ( item . containsKey ( " key " ) ) data . setSource ( ( String ) item . get ( " key " ) ) ;
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) & & item . containsKey ( " source " ) ) data . setSource ( ( String ) item . get ( " source " ) ) ;
if ( item . containsKey ( " reference " ) & & ! this . conventionService . isNullOrEmpty ( ( ( String ) item . get ( " reference " ) ) ) ) {
if ( ( ( String ) item . get ( " reference " ) ) . contains ( " : " ) ) {
String [ ] referenceParts = ( ( String ) item . get ( " reference " ) ) . split ( " : " , 2 ) ;
if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) data . setReference ( referenceParts [ 1 ] ) ;
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( referenceParts [ 0 ] ) ;
}
else if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) data . setReference ( ( ( String ) item . get ( " reference " ) ) ) ;
}
if ( item . containsKey ( " tag " ) & & ! this . conventionService . isNullOrEmpty ( ( ( String ) item . get ( " tag " ) ) ) ) {
if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " openaire " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " Open Definition " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " opendefinition " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " RDA Metadata Schemes " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " rda-metadata-schemes " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " OpenAIRE publication repositories 1 " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " OpenAIRE publication repositories 2 " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " OpenAIRE publication repositories 3 " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " OpenAIRE Journals " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " OpenAIRE Alternative " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " Taxonomies " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " taxonomy " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " OpenAIRE Publications " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " openaire " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " EOSC Providers " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " eosc " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " ORCID " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " orcid " ) ;
} else if ( ( ( String ) item . get ( " tag " ) ) . equalsIgnoreCase ( " Zenodo " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " zenodo " ) ;
}
}
if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) & & item . containsKey ( " id " ) & & ! this . conventionService . isNullOrEmpty ( ( ( String ) item . get ( " id " ) ) ) ) {
if ( ( ( String ) item . get ( " id " ) ) . startsWith ( " datarepo:re3data/ " ) ) {
if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) data . setReference ( ( ( String ) item . get ( " id " ) ) . substring ( " datarepo:re3data/ " . length ( ) ) ) ;
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) data . setSource ( " re3data " ) ;
}
else if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) data . setReference ( ( ( String ) item . get ( " id " ) ) ) ;
data . setReference ( ( String ) item . get ( " id " ) ) ;
}
2024-02-23 17:55:35 +01:00
data . setId ( UUID . randomUUID ( ) ) ;
data . setCreatedAt ( Instant . now ( ) ) ;
data . setUpdatedAt ( Instant . now ( ) ) ;
data . setSourceType ( ReferenceSourceType . External ) ;
data . setTypeId ( referenceTypeDataEntity . getReferenceTypeId ( ) ) ;
data . setIsActive ( IsActive . Active ) ;
2024-04-26 16:12:29 +02:00
org . opencdmp . commons . types . reference . DefinitionEntity definitionEntity = new org . opencdmp . commons . types . reference . DefinitionEntity ( ) ;
2024-02-23 17:55:35 +01:00
definitionEntity . setFields ( new ArrayList < > ( ) ) ;
for ( Map . Entry < String , Object > entries : item . entrySet ( ) ) {
if ( entries . getValue ( ) ! = null & & ! entries . getKey ( ) . equals ( " name " ) & & ! entries . getKey ( ) . equals ( " label " ) & & ! entries . getKey ( ) . equals ( " abbreviation " )
2024-02-28 17:48:46 +01:00
& & ! entries . getKey ( ) . equals ( " id " ) & & ! entries . getKey ( ) . equals ( " key " ) & & ! entries . getKey ( ) . equals ( " pid " ) & & ! entries . getKey ( ) . equals ( " source " ) ) {
2024-04-26 16:12:29 +02:00
org . opencdmp . commons . types . reference . FieldEntity field = new org . opencdmp . commons . types . reference . FieldEntity ( ) ;
2024-02-23 17:55:35 +01:00
field . setCode ( entries . getKey ( ) ) ;
field . setDataType ( ReferenceFieldDataType . Text ) ;
field . setValue ( entries . getValue ( ) . toString ( ) ) ;
definitionEntity . getFields ( ) . add ( field ) ;
}
}
if ( ! this . conventionService . isListNullOrEmpty ( definitionEntity . getFields ( ) ) ) data . setDefinition ( this . xmlHandlingService . toXmlSafe ( definitionEntity ) ) ;
if ( this . conventionService . isNullOrEmpty ( data . getLabel ( ) ) ) data . setLabel ( data . getReference ( ) ) ;
2024-02-28 17:48:46 +01:00
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) & &
2024-04-25 10:35:08 +02:00
referenceTypeDefinition . getSources ( ) . size ( ) = = 1 ) {
2024-02-28 17:48:46 +01:00
data . setSource ( referenceTypeDefinition . getSources ( ) . getFirst ( ) . getKey ( ) ) ;
2024-02-23 17:55:35 +01:00
}
2024-02-28 17:48:46 +01:00
if ( this . conventionService . isNullOrEmpty ( data . getLabel ( ) ) | | data . getLabel ( ) . equals ( " null " ) ) {
logger . warn ( " Migrate Dataset " + dataset . getId ( ) + " " + dataset . getLabel ( ) + " no label for the reference : " + referenceTypeDataEntity . getReferenceTypeId ( ) + " " + this . jsonHandlingService . toJsonSafe ( item ) ) ; //TODO
return null ;
}
2024-04-25 10:35:08 +02:00
if ( this . conventionService . isNullOrEmpty ( data . getSource ( ) ) ) throw new MyApplicationException ( " Migrate Dataset " + dataset . getId ( ) + " " + dataset . getLabel ( ) + " Source not found source for reference : " + referenceTypeDataEntity . getReferenceTypeId ( ) + " " + this . jsonHandlingService . toJsonSafe ( item ) ) ; //TODO
2024-02-28 17:48:46 +01:00
2024-02-23 17:55:35 +01:00
if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) {
2024-02-28 17:48:46 +01:00
data . setReference ( this . queryFactory . query ( ReferenceQuery . class ) . sources ( data . getSource ( ) ) . like ( data . getLabel ( ) ) . typeIds ( data . getTypeId ( ) ) . sourceTypes ( ReferenceSourceType . External ) . collectAs ( new BaseFieldSet ( ) . ensure ( Reference . _reference , Reference . _label ) )
2024-02-23 17:55:35 +01:00
. stream ( ) . filter ( x - > x . getLabel ( ) . equals ( data . getLabel ( ) ) ) . map ( ReferenceEntity : : getReference ) . findFirst ( ) . orElse ( null ) ) ; //TODO
}
2024-02-28 17:48:46 +01:00
if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) {
2024-02-28 17:51:16 +01:00
if ( referenceTypeDefinition . getSources ( ) . stream ( ) . filter ( x - > x . getType ( ) . equals ( ExternalFetcherSourceType . API ) ) . map ( x - > ( ExternalFetcherApiSourceConfigurationEntity ) x ) . anyMatch ( x - > x . getUrl ( ) . toLowerCase ( Locale . ROOT ) . contains ( " eestore.paas2.uninett.no " ) ) ) {
2024-02-28 17:48:46 +01:00
if ( data . getLabel ( ) . equalsIgnoreCase ( " Attribution 4.0 International " ) ) {
data . setReference ( " CC-BY-4.0 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Attribution-NonCommercial 4.0 International " ) ) {
data . setReference ( " CC-BY-NC-4.0 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Attribution-NonCommercial-NoDerivatives 4.0 International " ) ) {
data . setReference ( " CC-BY-NC-ND-4.0 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Attribution-NoDerivatives 4.0 International " ) ) {
data . setReference ( " CC-BY-ND-4.0 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Attribution-NonCommercial-ShareAlike 4.0 International " ) ) {
data . setReference ( " CC-BY-NC-SA-4.0 " ) ;
}
} if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . License ) & & data . getSource ( ) . equalsIgnoreCase ( " OpenAIRE " ) ) {
if ( data . getLabel ( ) . equalsIgnoreCase ( " Academic Free License 3.0 " ) ) {
data . setReference ( " AFL-3.0 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Attribution Assurance Licenses " ) ) {
data . setReference ( " AFL-3.0 " ) ;
}
} else if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . Datasets ) & & data . getSource ( ) . equalsIgnoreCase ( " OpenAIRE " ) ) {
if ( data . getLabel ( ) . equalsIgnoreCase ( " Bulk RNA-seq U2OS cells treated with small molecules " ) & & data . getSource ( ) . equalsIgnoreCase ( " OpenAIRE " ) ) {
data . setReference ( " 50|_____OmicsDI::7fc1bcd133082463ec1948b82f1ad0ee " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Genome-wide effect of the microenvironment over the Galectin-7 regulation network in HeLa and SiHa tumors in nude mice. " ) ) {
data . setReference ( " 50|_____OmicsDI::4e0de948bf961b96dbdef82797b5a8dc " ) ;
}
// else if (data.getLabel().equalsIgnoreCase("other")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("none")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("to be clarified later")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("articles")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Freistilringen")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// }
else if ( data . getLabel ( ) . equalsIgnoreCase ( " Randomized Phase 3 Trial of ALIMTA (Pemetrexed) and Carboplatin versus Etoposide and Carboplatin in Extensive-Stage Small Cell Lung Cancer " ) ) {
data . setReference ( " 50|datacite____::1df6f72ab95712d5f26ecb43b2d1d303 " ) ;
}
} else if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . DataRepositories ) & & data . getSource ( ) . equalsIgnoreCase ( " OpenAIRE " ) ) {
if ( data . getLabel ( ) . equalsIgnoreCase ( " INPTDAT " ) ) {
data . setReference ( " re3data_____::r3d100013120 " ) ;
} if ( data . getLabel ( ) . equalsIgnoreCase ( " OpenAPC Global Initiative " ) ) {
data . setReference ( " apc_________::openapc " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Human Gene and Protein Database " ) ) {
data . setReference ( " fairsharing_::1592 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Bacterial protein tYrosine Kinase database " ) ) {
data . setReference ( " fairsharing_::1557 " ) ;
}
// else if (data.getLabel().equalsIgnoreCase("other")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Zenodo")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Zenado")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("none")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("articles")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("https://github.com/sisyphe-re/riot_rpl_udp_scenarios")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else else if (data.getLabel().equalsIgnoreCase("Cool data")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Raw data")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Repository hosted by Zenodo")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("ICPSR - Interuniversity Consortium for Political and Social Research")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("XC7 pharmaceutical development IPF")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("nct")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("UK Data Service")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("figshare")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("University of Strathclyde")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// }
} else if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . Services ) & & data . getSource ( ) . equalsIgnoreCase ( " eosc " ) ) {
// if (data.getLabel().equalsIgnoreCase("website")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("integration1")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("SeaDataNet Common Data Index (CDI)")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Europeana APIs")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Imaging at Plentzia Marine Station-UPV/EHU")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Spectroscopy")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Library services at Stazione Zoologica Anton Dohrn")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("PRACE Training Portal")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("DARIAH Science Gateway")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Finding Anisotropy Tensor")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Imaging at University of Bergen-SLRC")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Training facilities at Toralla Marine Science Station - Vigo University Marine Research Centre")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Zenodo")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Digital Commons")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// }
} else if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . Registries ) & & data . getSource ( ) . equalsIgnoreCase ( " rda-metadata-schemes " ) ) {
// if (data.getLabel().equalsIgnoreCase("DataCite Metadata Schema")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("AgMES (Agricultural Metadata Element Set)")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// }
} else if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . Organizations ) & & data . getSource ( ) . equalsIgnoreCase ( " OpenAIRE " ) ) {
if ( data . getLabel ( ) . equalsIgnoreCase ( " Programa de Pós-Graduação em Ciência da Informação Ibict/UFRJ " ) ) {
data . setReference ( " pending_org_::f20d38c291e7d4a7a11d08481eebac44 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Applied Decision Science (United States) " ) ) {
data . setReference ( " openorgs____::0000093299 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " BIODOL THERAPEUTICS " ) ) {
data . setReference ( " pending_org_::4c550b2917a6fc395bdb79eccdafe950 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " OKKO " ) ) {
data . setReference ( " pending_org_::37ddf98c5fd2d6373fd315b0371fd4ac " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Health Research Board " ) ) {
data . setReference ( " openorgs____::0000006166 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Vidzeme University of Applied Sciences " ) ) {
data . setReference ( " openorgs____::0000029719 " ) ;
}
// else if (data.getLabel().equalsIgnoreCase("Other Lab")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("ATS APPLIED TECH SYSTEMS LTD")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// } else if (data.getLabel().equalsIgnoreCase("Universidade Nova de Lisboa Centro de Linguística")){
// data.setReference(data.getId().toString().replace("-", "").toLowerCase(Locale.ROOT));
// data.setSourceType(ReferenceSourceType.Internal);
// if (dataset.getCreator() != null) data.setCreatedById(dataset.getCreator().getId());
// }
} else if ( data . getTypeId ( ) . equals ( ReferenceTypeIds . Researcher ) ) {
if ( data . getLabel ( ) . equalsIgnoreCase ( " Radosław Cichocki " ) ) {
data . setReference ( " 0000-0002-4667-8445 " ) ;
} else if ( data . getLabel ( ) . equalsIgnoreCase ( " Kiranmai Uppuluri " ) ) {
data . setReference ( data . getId ( ) . toString ( ) . replace ( " - " , " " ) . toLowerCase ( Locale . ROOT ) ) ;
data . setSourceType ( ReferenceSourceType . Internal ) ;
if ( dataset . getCreator ( ) ! = null ) data . setCreatedById ( dataset . getCreator ( ) . getId ( ) ) ;
}
}
}
2024-02-23 17:55:35 +01:00
if ( this . conventionService . isNullOrEmpty ( data . getReference ( ) ) ) {
2024-02-28 17:48:46 +01:00
data . setReference ( data . getId ( ) . toString ( ) . replace ( " - " , " " ) . toLowerCase ( Locale . ROOT ) ) ;
data . setSourceType ( ReferenceSourceType . Internal ) ;
2024-02-29 11:38:57 +01:00
data . setSource ( " Internal " ) ;
2024-02-28 17:48:46 +01:00
if ( dataset . getCreator ( ) ! = null ) data . setCreatedById ( dataset . getCreator ( ) . getId ( ) ) ;
logger . warn ( " Migrate Dataset " + dataset . getId ( ) + " " + dataset . getLabel ( ) + " reference auto created as internal " + referenceTypeDataEntity . getReferenceTypeId ( ) + " for value " + this . jsonHandlingService . toJsonSafe ( item ) ) ;
2024-02-23 17:55:35 +01:00
}
ReferenceEntity existingEntity = referenceMap . getOrDefault ( new ReferenceKey ( data ) , null ) ;
if ( existingEntity = = null ) {
2024-04-10 11:10:35 +02:00
if ( data . getCreatedById ( ) = = null ) referenceMap . put ( new ReferenceKey ( data ) , data ) ;
2024-02-23 17:55:35 +01:00
this . entityManager . persist ( data ) ;
2024-04-24 17:28:13 +02:00
this . entityManager . flush ( ) ;
2024-02-23 17:55:35 +01:00
return data ;
} else {
return existingEntity ;
}
}
2024-04-26 16:12:29 +02:00
private void createDescriptionReferenceEntity ( UUID referenceId , UUID descriptionId , org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField ) {
2024-04-24 17:28:13 +02:00
DescriptionReferenceEntity data = new DescriptionReferenceEntity ( ) ;
data . setId ( UUID . randomUUID ( ) ) ;
data . setReferenceId ( referenceId ) ;
data . setDescriptionId ( descriptionId ) ;
data . setCreatedAt ( Instant . now ( ) ) ;
data . setUpdatedAt ( Instant . now ( ) ) ;
data . setIsActive ( IsActive . Active ) ;
data . setData ( this . jsonHandlingService . toJsonSafe ( this . buildDescriptionReferenceDataEntity ( currentField ) ) ) ;
this . entityManager . persist ( data ) ;
}
2024-04-26 16:12:29 +02:00
private @NotNull DescriptionReferenceDataEntity buildDescriptionReferenceDataEntity ( org . opencdmp . commons . types . descriptiontemplate . FieldEntity currentField ) {
2024-04-24 17:28:13 +02:00
DescriptionReferenceDataEntity data = new DescriptionReferenceDataEntity ( ) ;
data . setFieldId ( currentField . getId ( ) ) ;
return data ;
}
2024-02-22 17:33:52 +01:00
public static class Currency {
private String name ;
private String value ;
public String getName ( ) {
return name ;
}
public void setName ( String name ) {
this . name = name ;
}
public String getValue ( ) {
return value ;
}
2023-11-23 09:26:40 +01:00
2024-02-22 17:33:52 +01:00
public void setValue ( String value ) {
this . value = value ;
}
}
public static class Tag {
private String id ;
private String name ;
public String getId ( ) {
return id ;
}
public void setId ( String id ) {
this . id = id ;
}
public String getName ( ) {
return name ;
}
public void setName ( String name ) {
this . name = name ;
}
}
2024-02-26 09:53:11 +01:00
public static class MultipleFieldKey {
private final String fieldSetId ;
private final String groupKey ;
private final int hashCode ;
public MultipleFieldKey ( String fieldSetId , String groupKey ) {
this . fieldSetId = fieldSetId ;
this . groupKey = groupKey ;
this . hashCode = Objects . hash ( this . fieldSetId , this . groupKey ) ;
}
public String getFieldSetId ( ) {
return fieldSetId ;
}
public String getGroupKey ( ) {
return groupKey ;
}
@Override
public boolean equals ( Object o ) {
if ( this = = o )
return true ;
if ( o = = null | | getClass ( ) ! = o . getClass ( ) )
return false ;
MultipleFieldKey that = ( MultipleFieldKey ) o ;
return Objects . equals ( fieldSetId , that . getFieldSetId ( ) ) & & Objects . equals ( groupKey , that . getGroupKey ( ) ) ;
}
@Override
public int hashCode ( ) {
return this . hashCode ;
}
}
2024-02-23 17:55:35 +01:00
2024-02-26 09:53:11 +01:00
public static class ReferenceKey {
2024-02-23 17:55:35 +01:00
private final UUID type ;
private final String source ;
private final String reference ;
private final int hashCode ;
public ReferenceKey ( ReferenceEntity reference ) {
this . type = reference . getTypeId ( ) ;
this . source = reference . getSource ( ) . trim ( ) ;
this . reference = reference . getReference ( ) . trim ( ) ;
2024-04-10 11:10:35 +02:00
hashCode = Objects . hash ( this . type , this . source . hashCode ( ) , this . reference ) ;
2024-02-23 17:55:35 +01:00
}
public UUID getType ( ) {
return type ;
}
public String getSource ( ) {
return source ;
}
public String getReference ( ) {
return reference ;
}
@Override
public boolean equals ( Object o ) {
if ( this = = o )
return true ;
if ( o = = null | | getClass ( ) ! = o . getClass ( ) )
return false ;
ReferenceKey that = ( ReferenceKey ) o ;
return Objects . equals ( type , that . getType ( ) ) & & Objects . equals ( source , that . getSource ( ) ) & & Objects . equals ( reference , that . getReference ( ) ) ;
}
@Override
public int hashCode ( ) {
return this . hashCode ;
}
}
2023-11-23 09:26:40 +01:00
}