2020-03-20 17:20:22 +01:00
package eu.eudat.models.rda.mapper ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.ObjectMapper ;
2023-07-14 12:56:08 +02:00
import com.fasterxml.jackson.databind.node.ArrayNode ;
2023-07-25 14:04:39 +02:00
import eu.eudat.data.entities.DescriptionTemplate ;
2022-01-28 13:55:52 +01:00
import eu.eudat.elastic.criteria.DatasetCriteria ;
2020-05-28 17:38:08 +02:00
import eu.eudat.elastic.entities.Tag ;
2020-03-20 17:20:22 +01:00
import eu.eudat.logic.managers.DatasetManager ;
2020-03-26 17:39:25 +01:00
import eu.eudat.logic.services.ApiContext ;
2022-01-28 13:55:52 +01:00
import eu.eudat.logic.utilities.helpers.StreamDistinctBy ;
2020-03-20 17:20:22 +01:00
import eu.eudat.logic.utilities.json.JsonSearcher ;
2020-09-04 16:09:24 +02:00
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel ;
2020-03-20 17:20:22 +01:00
import eu.eudat.models.data.datasetwizard.DatasetWizardModel ;
2021-12-29 14:24:46 +01:00
import eu.eudat.models.rda.* ;
2020-03-26 17:39:25 +01:00
import org.json.JSONObject ;
2020-03-20 17:20:22 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Component ;
import javax.transaction.Transactional ;
2021-11-17 10:12:34 +01:00
import java.time.Instant ;
import java.time.ZoneId ;
import java.time.format.DateTimeFormatter ;
import java.time.format.DateTimeParseException ;
2020-03-26 17:39:25 +01:00
import java.util.* ;
2020-03-20 17:20:22 +01:00
import java.util.stream.Collectors ;
2020-03-26 17:39:25 +01:00
import java.util.stream.Stream ;
2020-06-08 17:31:34 +02:00
import java.util.stream.StreamSupport ;
2020-03-20 17:20:22 +01:00
@Component
public class DatasetRDAMapper {
private static final Logger logger = LoggerFactory . getLogger ( DatasetRDAMapper . class ) ;
private DatasetManager datasetManager ;
2020-03-26 17:39:25 +01:00
private ApiContext apiContext ;
2020-03-20 17:20:22 +01:00
@Autowired
2020-03-26 17:39:25 +01:00
public DatasetRDAMapper ( DatasetManager datasetManager , ApiContext apiContext ) {
2020-03-20 17:20:22 +01:00
this . datasetManager = datasetManager ;
2020-03-26 17:39:25 +01:00
this . apiContext = apiContext ;
2020-03-20 17:20:22 +01:00
}
@Transactional
2022-01-12 13:10:00 +01:00
public Dataset toRDA ( eu . eudat . data . entities . Dataset dataset , eu . eudat . models . rda . Dmp dmp ) {
2020-03-20 17:20:22 +01:00
Dataset rda = new Dataset ( ) ;
2020-05-28 17:38:08 +02:00
// rda.setDatasetId(DatasetIdRDAMapper.toRDA(dataset.getId()));
2020-11-13 17:26:09 +01:00
if ( dataset . getLabel ( ) = = null ) {
2020-11-13 17:29:16 +01:00
throw new IllegalArgumentException ( " Dataset Label is missing " ) ;
2020-11-13 17:26:09 +01:00
}
2020-03-20 17:20:22 +01:00
rda . setTitle ( dataset . getLabel ( ) ) ;
rda . setDescription ( dataset . getDescription ( ) ) ;
2020-03-26 17:39:25 +01:00
rda . setAdditionalProperty ( " template " , dataset . getProfile ( ) . getId ( ) ) ;
2020-03-20 17:20:22 +01:00
try {
2020-03-26 17:39:25 +01:00
JSONObject jObject = new JSONObject ( dataset . getProperties ( ) ) ;
Map < String , Object > templateIdsToValues = jObject . toMap ( ) ;
2020-03-20 17:20:22 +01:00
DatasetWizardModel datasetWizardModel = new DatasetWizardModel ( ) . fromDataModel ( dataset ) ;
datasetWizardModel . setDatasetProfileDefinition ( datasetManager . getPagedProfile ( datasetWizardModel , dataset ) ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
String datasetDescriptionJson = mapper . writeValueAsString ( datasetWizardModel . getDatasetProfileDefinition ( ) ) ;
JsonNode datasetDescriptionObj = mapper . readTree ( datasetDescriptionJson ) ;
2023-02-21 10:57:34 +01:00
List < JsonNode > idNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.dataset_id " ) ;
2020-05-28 17:38:08 +02:00
if ( ! idNodes . isEmpty ( ) ) {
rda . setDatasetId ( DatasetIdRDAMapper . toRDA ( idNodes ) ) ;
}
2020-09-15 12:21:22 +02:00
if ( rda . getDatasetId ( ) = = null ) {
rda . setDatasetId ( new DatasetId ( dataset . getId ( ) . toString ( ) , DatasetId . Type . OTHER ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > typeNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.type " ) ;
2021-12-29 14:24:46 +01:00
if ( ! typeNodes . isEmpty ( ) & & ! typeNodes . get ( 0 ) . get ( " value " ) . asText ( ) . isEmpty ( ) ) {
2020-03-23 17:09:31 +01:00
rda . setType ( typeNodes . get ( 0 ) . get ( " value " ) . asText ( ) ) ;
2020-05-28 17:38:08 +02:00
} else {
2020-09-15 12:21:22 +02:00
rda . setType ( " DMP Dataset " ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > languageNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.language " ) ;
2021-12-29 14:24:46 +01:00
if ( ! languageNodes . isEmpty ( ) & & ! languageNodes . get ( 0 ) . get ( " value " ) . asText ( ) . isEmpty ( ) ) {
2023-07-14 12:56:08 +02:00
String lang = languageNodes . get ( 0 ) . get ( " value " ) . asText ( ) ;
try {
rda . setLanguage ( Language . fromValue ( lang ) ) ;
}
catch ( IllegalArgumentException e ) {
logger . warn ( " Language " + lang + " from semantic rda.dataset.language was not found. Setting ' " + dataset . getProfile ( ) . getLanguage ( ) + " ' as language from the dataset profile. " ) ;
rda . setLanguage ( LanguageRDAMapper . mapLanguageIsoToRDAIso ( dataset . getProfile ( ) . getLanguage ( ) ) ) ;
}
2020-06-03 11:13:31 +02:00
} else {
rda . setLanguage ( LanguageRDAMapper . mapLanguageIsoToRDAIso ( dataset . getProfile ( ) . getLanguage ( ) ) ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > metadataNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.metadata " ) ;
2020-03-20 17:20:22 +01:00
if ( ! metadataNodes . isEmpty ( ) ) {
2020-03-23 17:09:31 +01:00
rda . setMetadata ( MetadataRDAMapper . toRDAList ( metadataNodes ) ) ;
2021-12-17 11:59:10 +01:00
} else {
2021-12-29 14:24:46 +01:00
rda . setMetadata ( new ArrayList < > ( ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > qaNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.data_quality_assurance " ) ;
2020-03-20 17:20:22 +01:00
if ( ! qaNodes . isEmpty ( ) ) {
2020-05-28 17:38:08 +02:00
/ * rda . setDataQualityAssurance ( qaNodes . stream ( ) . map ( qaNode - > qaNode . get ( " value " ) . asText ( ) ) . collect ( Collectors . toList ( ) ) ) ;
2020-03-26 17:39:25 +01:00
for ( int i = 0 ; i < qaNodes . size ( ) ; i + + ) {
2020-03-27 09:50:37 +01:00
rda . setAdditionalProperty ( " qaId " + ( i + 1 ) , qaNodes . get ( i ) . get ( " id " ) . asText ( ) ) ;
2020-05-28 17:38:08 +02:00
} * /
2022-01-18 10:38:24 +01:00
List < String > qaList = new ArrayList < > ( ) ;
String qa ;
for ( JsonNode node : qaNodes ) {
if ( node . get ( " value " ) . isArray ( ) ) {
Iterator < JsonNode > iter = node . get ( " value " ) . elements ( ) ;
while ( iter . hasNext ( ) ) {
qa = iter . next ( ) . asText ( ) ;
qaList . add ( qa ) ;
}
}
}
String data_quality ;
for ( JsonNode dqa : qaNodes ) {
data_quality = dqa . get ( " value " ) . asText ( ) ;
if ( ! data_quality . isEmpty ( ) ) {
qaList . add ( data_quality ) ;
rda . setAdditionalProperty ( " otherDQAID " , dqa . get ( " id " ) ) ;
rda . setAdditionalProperty ( " otherDQA " , data_quality ) ;
break ;
}
}
2021-12-16 10:44:34 +01:00
rda . setDataQualityAssurance ( qaList ) ;
2021-12-17 11:59:10 +01:00
} else {
2021-12-29 14:24:46 +01:00
rda . setDataQualityAssurance ( new ArrayList < > ( ) ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > preservationNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.preservation_statement " ) ;
2021-12-29 14:24:46 +01:00
if ( ! preservationNodes . isEmpty ( ) & & ! preservationNodes . get ( 0 ) . get ( " value " ) . asText ( ) . isEmpty ( ) ) {
2020-03-24 10:26:17 +01:00
rda . setPreservationStatement ( preservationNodes . get ( 0 ) . get ( " value " ) . asText ( ) ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > distributionNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.distribution " ) ;
2020-03-20 17:20:22 +01:00
if ( ! distributionNodes . isEmpty ( ) ) {
2021-12-29 14:24:46 +01:00
rda . setDistribution ( DistributionRDAMapper . toRDAList ( distributionNodes ) ) ;
2021-12-17 11:59:10 +01:00
} else {
2021-12-29 14:24:46 +01:00
rda . setDistribution ( new ArrayList < > ( ) ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > keywordNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.keyword " ) ;
2020-03-20 17:20:22 +01:00
if ( ! keywordNodes . isEmpty ( ) ) {
2020-06-08 17:31:34 +02:00
rda . setKeyword ( keywordNodes . stream ( ) . map ( keywordNode - > {
JsonNode value = keywordNode . get ( " value " ) ;
if ( value . isArray ( ) ) {
2022-01-28 13:55:52 +01:00
return StreamSupport . stream ( value . spliterator ( ) , false ) . map ( node - > KeywordRDAMapper . toRDA ( node . toString ( ) ) ) . flatMap ( Collection : : stream ) . collect ( Collectors . toList ( ) ) ;
2020-06-08 17:31:34 +02:00
} else {
2020-09-15 12:21:22 +02:00
return KeywordRDAMapper . toRDA ( keywordNode . get ( " value " ) . asText ( ) ) ;
2020-06-08 17:31:34 +02:00
}
2020-06-09 15:25:50 +02:00
} ) . flatMap ( Collection : : stream ) . collect ( Collectors . toList ( ) ) ) ;
2020-03-26 17:39:25 +01:00
for ( int i = 0 ; i < keywordNodes . size ( ) ; i + + ) {
rda . setAdditionalProperty ( " keyword " + ( i + 1 ) , keywordNodes . get ( i ) . get ( " id " ) . asText ( ) ) ;
}
2021-07-06 11:58:38 +02:00
} else if ( apiContext . getOperationsContext ( ) . getElasticRepository ( ) . getDatasetRepository ( ) . exists ( ) ) {
2020-05-28 17:38:08 +02:00
List < String > tags = apiContext . getOperationsContext ( ) . getElasticRepository ( ) . getDatasetRepository ( ) . findDocument ( dataset . getId ( ) . toString ( ) ) . getTags ( ) . stream ( ) . map ( Tag : : getName ) . collect ( Collectors . toList ( ) ) ;
rda . setKeyword ( tags ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > personalDataNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.personal_data " ) ;
2020-03-20 17:20:22 +01:00
if ( ! personalDataNodes . isEmpty ( ) ) {
2022-01-12 13:10:00 +01:00
try {
rda . setPersonalData ( personalDataNodes . stream ( ) . map ( personalDataNode - > Dataset . PersonalData . fromValue ( personalDataNode . get ( " value " ) . asText ( ) ) ) . findFirst ( ) . get ( ) ) ;
} catch ( IllegalArgumentException e ) {
rda . setPersonalData ( Dataset . PersonalData . UNKNOWN ) ;
}
2020-05-28 17:38:08 +02:00
} else {
rda . setPersonalData ( Dataset . PersonalData . UNKNOWN ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > securityAndPrivacyNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.security_and_privacy " ) ;
2020-03-20 17:20:22 +01:00
if ( ! securityAndPrivacyNodes . isEmpty ( ) ) {
2020-03-23 17:09:31 +01:00
rda . setSecurityAndPrivacy ( SecurityAndPrivacyRDAMapper . toRDAList ( securityAndPrivacyNodes ) ) ;
2021-12-17 11:59:10 +01:00
} else {
2021-12-29 14:24:46 +01:00
rda . setSecurityAndPrivacy ( new ArrayList < > ( ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > sensitiveDataNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.sensitive_data " ) ;
2020-03-20 17:20:22 +01:00
if ( ! sensitiveDataNodes . isEmpty ( ) ) {
2022-01-12 13:10:00 +01:00
try {
rda . setSensitiveData ( sensitiveDataNodes . stream ( ) . map ( sensitiveDataNode - > Dataset . SensitiveData . fromValue ( sensitiveDataNode . get ( " value " ) . asText ( ) ) ) . findFirst ( ) . get ( ) ) ;
} catch ( IllegalArgumentException e ) {
rda . setSensitiveData ( Dataset . SensitiveData . UNKNOWN ) ;
}
2020-09-15 12:21:22 +02:00
} else {
rda . setSensitiveData ( Dataset . SensitiveData . UNKNOWN ) ;
2020-03-20 17:20:22 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > technicalResourceNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.technical_resource " ) ;
2020-03-20 17:20:22 +01:00
if ( ! technicalResourceNodes . isEmpty ( ) ) {
2020-03-23 17:09:31 +01:00
rda . setTechnicalResource ( TechnicalResourceRDAMapper . toRDAList ( technicalResourceNodes ) ) ;
2021-12-17 11:59:10 +01:00
} else {
2021-12-29 14:24:46 +01:00
rda . setTechnicalResource ( new ArrayList < > ( ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > issuedNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.issued " ) ;
2021-12-29 14:24:46 +01:00
if ( ! issuedNodes . isEmpty ( ) & & ! issuedNodes . get ( 0 ) . get ( " value " ) . asText ( ) . isEmpty ( ) ) {
2020-05-28 17:38:08 +02:00
rda . setIssued ( issuedNodes . get ( 0 ) . get ( " value " ) . asText ( ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > contributorNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dmp.contributor " ) ;
2020-06-09 15:25:50 +02:00
if ( ! contributorNodes . isEmpty ( ) ) {
2022-01-12 13:10:00 +01:00
dmp . getContributor ( ) . addAll ( contributorNodes . stream ( ) . map ( contributorNode - > {
2020-06-09 15:25:50 +02:00
JsonNode value = contributorNode . get ( " value " ) ;
if ( value . isArray ( ) ) {
return StreamSupport . stream ( value . spliterator ( ) , false ) . map ( node - > ContributorRDAMapper . toRDA ( node . asText ( ) ) ) . collect ( Collectors . toList ( ) ) ;
} else {
2023-02-21 10:57:34 +01:00
return Collections . singletonList ( new Contributor ( ) ) ;
2020-06-09 15:25:50 +02:00
}
} ) . flatMap ( Collection : : stream ) . collect ( Collectors . toList ( ) ) ) ;
2023-07-14 12:56:08 +02:00
dmp . setContributor ( dmp . getContributor ( ) . stream ( ) . filter ( contributor - > contributor . getContributorId ( ) ! = null & & contributor . getName ( ) ! = null ) . collect ( Collectors . toList ( ) ) ) ;
2020-06-09 15:25:50 +02:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > costNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dmp.cost " ) ;
2021-12-29 14:24:46 +01:00
if ( ! costNodes . isEmpty ( ) ) {
2022-01-12 13:10:00 +01:00
dmp . getCost ( ) . addAll ( CostRDAMapper . toRDAList ( costNodes ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > ethicsNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dmp.ethical_issues " ) ;
2022-01-12 13:10:00 +01:00
if ( ! ethicsNodes . isEmpty ( ) ) {
for ( JsonNode node : ethicsNodes ) {
2023-02-21 10:57:34 +01:00
String rdaProperty = " " ;
JsonNode schematics = node . get ( " schematics " ) ;
if ( schematics . isArray ( ) ) {
2023-07-14 12:56:08 +02:00
int index = 0 ;
2023-02-21 10:57:34 +01:00
for ( JsonNode schematic : schematics ) {
if ( schematic . asText ( ) . startsWith ( " rda.dmp.ethical_issues " ) ) {
rdaProperty = schematic . asText ( ) ;
2023-07-14 12:56:08 +02:00
( ( ArrayNode ) schematics ) . remove ( index ) ;
2023-02-21 10:57:34 +01:00
break ;
}
2023-07-14 12:56:08 +02:00
index + + ;
2023-02-21 10:57:34 +01:00
}
}
else {
continue ;
}
2022-01-12 13:10:00 +01:00
String rdaValue = node . get ( " value " ) . asText ( ) ;
if ( rdaValue = = null | | rdaValue . isEmpty ( ) ) {
continue ;
}
if ( rdaProperty . contains ( " exist " ) ) {
try {
Dmp . EthicalIssuesExist exists = dmp . getEthicalIssuesExist ( ) ;
if ( exists = = null
| | ( ( exists = = Dmp . EthicalIssuesExist . NO | | exists = = Dmp . EthicalIssuesExist . UNKNOWN ) & & rdaValue . equals ( " yes " ) )
| | ( exists = = Dmp . EthicalIssuesExist . YES & & ! ( rdaValue . equals ( " no " ) | | rdaValue . equals ( " unknown " ) ) )
| | ( exists = = Dmp . EthicalIssuesExist . UNKNOWN & & rdaValue . equals ( " no " ) ) ) {
dmp . setEthicalIssuesExist ( Dmp . EthicalIssuesExist . fromValue ( rdaValue ) ) ;
}
} catch ( IllegalArgumentException e ) {
logger . warn ( e . getLocalizedMessage ( ) + " . Setting ethical_issues_exist to unknown " ) ;
dmp . setEthicalIssuesExist ( Dmp . EthicalIssuesExist . UNKNOWN ) ;
}
}
2022-01-18 10:38:24 +01:00
// else if(rdaProperty.contains("description")){
// if(dmp.getEthicalIssuesDescription() == null){
// dmp.setEthicalIssuesDescription(rdaValue);
// }
// else{
// dmp.setEthicalIssuesDescription(dmp.getEthicalIssuesDescription() + ", " + rdaValue);
// }
// }
// else if(rdaProperty.contains("report")){
// try {
// dmp.setEthicalIssuesReport(URI.create(rdaValue));
// } catch (IllegalArgumentException e) {
// logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
// }
// }
2022-01-12 13:10:00 +01:00
}
2021-12-29 14:24:46 +01:00
}
2020-03-26 17:39:25 +01:00
List < JsonNode > foundNodes = Stream . of ( typeNodes , languageNodes , metadataNodes , qaNodes , preservationNodes , distributionNodes ,
keywordNodes , personalDataNodes , securityAndPrivacyNodes , sensitiveDataNodes , technicalResourceNodes ) . flatMap ( Collection : : stream ) . collect ( Collectors . toList ( ) ) ;
templateIdsToValues . entrySet ( ) . forEach ( entry - > {
boolean isFound = foundNodes . stream ( ) . anyMatch ( node - > node . get ( " id " ) . asText ( ) . equals ( entry . getKey ( ) ) ) ;
if ( ! isFound & & entry . getValue ( ) ! = null & & ! entry . getValue ( ) . toString ( ) . isEmpty ( ) ) {
2021-11-17 10:12:34 +01:00
try {
Instant time = Instant . parse ( entry . getValue ( ) . toString ( ) ) ;
rda . setAdditionalProperty ( entry . getKey ( ) , DateTimeFormatter . ofPattern ( " yyyy-MM-dd " ) . withZone ( ZoneId . systemDefault ( ) ) . format ( time ) ) ;
} catch ( DateTimeParseException e ) {
rda . setAdditionalProperty ( entry . getKey ( ) , entry . getValue ( ) ) ;
}
2020-03-26 17:39:25 +01:00
}
} ) ;
2020-03-20 17:20:22 +01:00
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
return rda ;
}
2020-03-26 17:39:25 +01:00
2023-07-25 14:04:39 +02:00
public eu . eudat . data . entities . Dataset toEntity ( Dataset rda , DescriptionTemplate defaultProfile ) {
2020-03-26 17:39:25 +01:00
eu . eudat . data . entities . Dataset entity = new eu . eudat . data . entities . Dataset ( ) ;
entity . setLabel ( rda . getTitle ( ) ) ;
entity . setDescription ( rda . getDescription ( ) ) ;
try {
2023-07-25 14:04:39 +02:00
DescriptionTemplate profile = apiContext . getOperationsContext ( ) . getDatabaseRepository ( ) . getDatasetProfileDao ( ) . find ( UUID . fromString ( rda . getAdditionalProperties ( ) . get ( " template " ) . toString ( ) ) ) ;
2020-03-26 17:39:25 +01:00
entity . setProfile ( profile ) ;
} catch ( Exception e ) {
logger . warn ( e . getMessage ( ) , e ) ;
2020-05-29 12:39:18 +02:00
entity . setProfile ( defaultProfile ) ;
2020-03-26 17:39:25 +01:00
}
try {
Map < String , Object > properties = new HashMap < > ( ) ;
DatasetWizardModel datasetWizardModel = new DatasetWizardModel ( ) ;
2020-09-04 16:09:24 +02:00
datasetWizardModel . setProfile ( new DatasetProfileOverviewModel ( ) . fromDataModel ( entity . getProfile ( ) ) ) ;
2020-03-26 17:39:25 +01:00
datasetWizardModel . setDatasetProfileDefinition ( datasetManager . getPagedProfile ( datasetWizardModel , entity ) ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
String datasetDescriptionJson = mapper . writeValueAsString ( datasetWizardModel . getDatasetProfileDefinition ( ) ) ;
JsonNode datasetDescriptionObj = mapper . readTree ( datasetDescriptionJson ) ;
2023-02-21 10:57:34 +01:00
List < JsonNode > typeNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.type " ) ;
2020-03-26 17:39:25 +01:00
if ( ! typeNodes . isEmpty ( ) ) {
properties . put ( typeNodes . get ( 0 ) . get ( " id " ) . asText ( ) , rda . getType ( ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > languageNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.language " ) ;
2020-05-29 13:42:21 +02:00
if ( ! languageNodes . isEmpty ( ) & & rda . getLanguage ( ) ! = null ) {
2020-03-26 17:39:25 +01:00
properties . put ( languageNodes . get ( 0 ) . get ( " id " ) . asText ( ) , rda . getLanguage ( ) . value ( ) ) ;
}
if ( rda . getMetadata ( ) ! = null ) {
properties . putAll ( MetadataRDAMapper . toProperties ( rda . getMetadata ( ) ) ) ;
}
2020-05-28 17:38:08 +02:00
if ( rda . getDatasetId ( ) ! = null ) {
properties . putAll ( DatasetIdRDAMapper . toProperties ( rda . getDatasetId ( ) , datasetDescriptionObj ) ) ;
}
/ * List < String > qaIds = rda . getAdditionalProperties ( ) . entrySet ( ) . stream ( ) . filter ( entry - > entry . getKey ( ) . startsWith ( " qaId " ) ) . map ( entry - > entry . getValue ( ) . toString ( ) ) . collect ( Collectors . toList ( ) ) ;
2020-03-26 17:39:25 +01:00
for ( int i = 0 ; i < qaIds . size ( ) ; i + + ) {
properties . put ( qaIds . get ( i ) , rda . getDataQualityAssurance ( ) . get ( i ) ) ;
2020-05-28 17:38:08 +02:00
} * /
2023-02-21 10:57:34 +01:00
List < JsonNode > qaNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.data_quality_assurance " ) ;
2020-05-29 12:39:18 +02:00
if ( ! qaNodes . isEmpty ( ) & & rda . getDataQualityAssurance ( ) ! = null & & ! rda . getDataQualityAssurance ( ) . isEmpty ( ) ) {
2022-01-18 10:38:24 +01:00
ObjectMapper m = new ObjectMapper ( ) ;
List < String > qas = new ArrayList < > ( rda . getDataQualityAssurance ( ) ) ;
if ( ! qas . isEmpty ( ) ) {
properties . put ( qaNodes . get ( 0 ) . get ( " id " ) . asText ( ) , m . writeValueAsString ( qas ) ) ;
if ( rda . getAdditionalProperties ( ) . containsKey ( " otherDQAID " ) ) {
properties . put ( ( String ) rda . getAdditionalProperties ( ) . get ( " otherDQAID " ) , rda . getAdditionalProperties ( ) . get ( " otherDQA " ) ) ;
}
}
2020-03-26 17:39:25 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > preservationNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.preservation_statement " ) ;
2020-03-26 17:39:25 +01:00
if ( ! preservationNodes . isEmpty ( ) ) {
properties . put ( preservationNodes . get ( 0 ) . get ( " id " ) . asText ( ) , rda . getPreservationStatement ( ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > issuedNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.issued " ) ;
2020-05-28 17:38:08 +02:00
if ( ! issuedNodes . isEmpty ( ) ) {
properties . put ( issuedNodes . get ( 0 ) . get ( " id " ) . asText ( ) , rda . getIssued ( ) ) ;
}
2022-01-18 10:38:24 +01:00
if ( rda . getDistribution ( ) ! = null & & ! rda . getDistribution ( ) . isEmpty ( ) ) {
2020-05-28 17:38:08 +02:00
properties . putAll ( DistributionRDAMapper . toProperties ( rda . getDistribution ( ) . get ( 0 ) , datasetDescriptionObj ) ) ;
2020-03-26 17:39:25 +01:00
}
2020-06-25 12:30:47 +02:00
if ( rda . getKeyword ( ) ! = null ) {
List < String > keywordIds = rda . getAdditionalProperties ( ) . entrySet ( ) . stream ( ) . filter ( entry - > entry . getKey ( ) . startsWith ( " keyword " ) ) . map ( entry - > entry . getValue ( ) . toString ( ) ) . collect ( Collectors . toList ( ) ) ;
2022-01-18 10:38:24 +01:00
// boolean takeAll = false;
// if (keywordIds.size() < rda.getKeyword().size()) {
// takeAll = true;
// }
2022-01-28 13:55:52 +01:00
DatasetCriteria criteria = new DatasetCriteria ( ) ;
criteria . setHasTags ( true ) ;
List < Tag > tags = this . apiContext . getOperationsContext ( ) . getElasticRepository ( ) . getDatasetRepository ( ) . query ( criteria ) . stream ( ) . map ( eu . eudat . elastic . entities . Dataset : : getTags ) . flatMap ( Collection : : stream ) . filter ( StreamDistinctBy . distinctByKey ( Tag : : getId ) ) . collect ( Collectors . toList ( ) ) ;
2022-01-18 10:38:24 +01:00
if ( ! rda . getKeyword ( ) . isEmpty ( ) ) {
2022-01-28 13:55:52 +01:00
List < Tag > templateTags = tags . stream ( ) . filter ( tag - > rda . getKeyword ( ) . contains ( tag . getName ( ) ) ) . collect ( Collectors . toList ( ) ) ;
2023-06-21 11:17:22 +02:00
if ( ! templateTags . isEmpty ( ) ) {
properties . put ( keywordIds . get ( 0 ) , mapper . writeValueAsString ( templateTags ) ) ;
}
2022-01-28 13:55:52 +01:00
// for (int i = 0; i < keywordIds.size(); i++) {
// //if (takeAll) {
2022-01-18 10:38:24 +01:00
// List<String> tags = new ArrayList<>();
// for (String keyword : rda.getKeyword()) {
// tags.add(mapper.writeValueAsString(toTagEntity(keyword)));
// }
// properties.put(keywordIds.get(i), tags);
// } else {
// properties.put(keywordIds.get(i), mapper.writeValueAsString(toTagEntity(rda.getKeyword().get(i))));
// }
2022-01-28 13:55:52 +01:00
// properties.put(keywordIds.get(i), rda.getKeyword().get(i));
// }
2020-06-09 15:25:50 +02:00
}
2020-03-26 17:39:25 +01:00
}
2023-02-21 10:57:34 +01:00
List < JsonNode > personalDataNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.personal_data " ) ;
2020-03-26 17:39:25 +01:00
if ( ! personalDataNodes . isEmpty ( ) ) {
properties . put ( personalDataNodes . get ( 0 ) . get ( " id " ) . asText ( ) , rda . getPersonalData ( ) . value ( ) ) ;
}
if ( rda . getSecurityAndPrivacy ( ) ! = null ) {
properties . putAll ( SecurityAndPrivacyRDAMapper . toProperties ( rda . getSecurityAndPrivacy ( ) ) ) ;
}
2023-02-21 10:57:34 +01:00
List < JsonNode > sensitiveDataNodes = JsonSearcher . findNodes ( datasetDescriptionObj , " schematics " , " rda.dataset.sensitive_data " ) ;
2020-03-26 17:39:25 +01:00
if ( ! sensitiveDataNodes . isEmpty ( ) ) {
properties . put ( sensitiveDataNodes . get ( 0 ) . get ( " id " ) . asText ( ) , rda . getSensitiveData ( ) . value ( ) ) ;
}
if ( rda . getTechnicalResource ( ) ! = null ) {
properties . putAll ( TechnicalResourceRDAMapper . toProperties ( rda . getTechnicalResource ( ) ) ) ;
}
rda . getAdditionalProperties ( ) . entrySet ( ) . stream ( )
2020-03-27 09:50:37 +01:00
. filter ( entry - > ! entry . getKey ( ) . equals ( " template " ) & & ! entry . getKey ( ) . startsWith ( " qaId " ) & & ! entry . getKey ( ) . startsWith ( " keyword " ) )
2020-03-26 17:39:25 +01:00
. forEach ( entry - > properties . put ( entry . getKey ( ) , entry . getValue ( ) ) ) ;
entity . setProperties ( new ObjectMapper ( ) . writeValueAsString ( properties ) ) ;
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
return entity ;
}
2020-06-09 15:25:50 +02:00
private static Tag toTagEntity ( String name ) {
Tag tag = new Tag ( ) ;
tag . setId ( " " ) ;
tag . setName ( name ) ;
return tag ;
}
2020-03-20 17:20:22 +01:00
}