2020-03-20 17:20:22 +01:00
package eu.eudat.models.rda.mapper ;
2021-12-20 13:19:17 +01:00
import com.fasterxml.jackson.core.JsonProcessingException ;
2020-03-20 17:20:22 +01:00
import com.fasterxml.jackson.databind.JsonNode ;
2021-12-20 13:19:17 +01:00
import com.fasterxml.jackson.databind.ObjectMapper ;
2023-07-14 12:56:08 +02:00
import com.fasterxml.jackson.databind.node.ArrayNode ;
2020-03-23 17:09:31 +01:00
import eu.eudat.logic.utilities.helpers.MyStringUtils ;
2020-03-20 17:20:22 +01:00
import eu.eudat.models.rda.Host ;
import eu.eudat.models.rda.PidSystem ;
2020-03-27 13:31:06 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2020-03-20 17:20:22 +01:00
import java.net.URI ;
2021-12-29 14:24:46 +01:00
import java.util.* ;
import java.util.stream.Collectors ;
2020-03-20 17:20:22 +01:00
public class HostRDAMapper {
2020-03-27 13:31:06 +01:00
private static final Logger logger = LoggerFactory . getLogger ( HostRDAMapper . class ) ;
2020-03-20 17:20:22 +01:00
2020-03-23 17:09:31 +01:00
public static Host toRDA ( List < JsonNode > nodes , String numbering ) {
2020-03-20 17:20:22 +01:00
Host rda = new Host ( ) ;
2020-03-23 17:09:31 +01:00
for ( JsonNode node : nodes ) {
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.dataset.distribution.host " ) ) {
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 ;
}
2020-03-23 17:09:31 +01:00
if ( rdaProperty . contains ( " host " ) ) {
int firstDiff = MyStringUtils . getFirstDifference ( numbering , node . get ( " numbering " ) . asText ( ) ) ;
2021-12-29 14:24:46 +01:00
if ( firstDiff = = - 1 | | firstDiff > = 2 ) {
2020-03-23 17:09:31 +01:00
String rdaValue = node . get ( " value " ) . asText ( ) ;
2021-12-29 14:24:46 +01:00
if ( rdaValue = = null | | ( rdaValue . isEmpty ( ) & & ! node . get ( " value " ) . isArray ( ) ) ) {
continue ;
}
2020-03-27 13:31:06 +01:00
for ( ExportPropertyName propertyName : ExportPropertyName . values ( ) ) {
2020-03-23 17:09:31 +01:00
if ( rdaProperty . contains ( propertyName . getName ( ) ) ) {
switch ( propertyName ) {
case AVAILABILITY :
rda . setAvailability ( rdaValue ) ;
2020-03-27 13:31:06 +01:00
rda . setAdditionalProperty ( ImportPropertyName . AVAILABILITY . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
2020-03-23 17:09:31 +01:00
break ;
case BACKUP_FREQUENCY :
rda . setBackupFrequency ( rdaValue ) ;
2020-03-27 13:31:06 +01:00
rda . setAdditionalProperty ( ImportPropertyName . BACKUP_FREQUENCY . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
2020-03-23 17:09:31 +01:00
break ;
case BACKUP_TYPE :
rda . setBackupType ( rdaValue ) ;
2022-01-18 10:38:24 +01:00
rda . setAdditionalProperty ( ImportPropertyName . BACKUP_TYPE . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
2020-03-23 17:09:31 +01:00
break ;
case CERTIFIED_WITH :
2023-07-14 12:56:08 +02:00
try {
rda . setCertifiedWith ( Host . CertifiedWith . fromValue ( rdaValue ) ) ;
rda . setAdditionalProperty ( ImportPropertyName . CERTIFIED_WITH . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
}
catch ( IllegalArgumentException e ) {
logger . warn ( " Distribution host certified with " + rdaValue + " from semantic distribution.host.certified_with is not valid. Certified_with will not be set set. " ) ;
}
2020-03-23 17:09:31 +01:00
break ;
case DESCRIPTION :
rda . setDescription ( rdaValue ) ;
2020-03-27 13:31:06 +01:00
rda . setAdditionalProperty ( ImportPropertyName . DESCRIPTION . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
2020-03-23 17:09:31 +01:00
break ;
case GEO_LOCATION :
2021-12-20 13:19:17 +01:00
if ( rdaValue . startsWith ( " { " ) ) {
try {
rdaValue = new ObjectMapper ( ) . readValue ( rdaValue , Map . class ) . get ( " id " ) . toString ( ) ;
} catch ( JsonProcessingException e ) {
logger . warn ( e . getLocalizedMessage ( ) + " . Try to pass value as is " ) ;
}
}
2023-07-14 12:56:08 +02:00
try {
rda . setGeoLocation ( Host . GeoLocation . fromValue ( rdaValue ) ) ;
rda . setAdditionalProperty ( ImportPropertyName . GEO_LOCATION . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
}
catch ( IllegalArgumentException e ) {
logger . warn ( " Distribution host geo location " + rdaValue + " from semantic distribution.host.geo_location is not valid. Geo location will not be set set. " ) ;
}
2020-03-23 17:09:31 +01:00
break ;
case PID_SYSTEM :
2021-12-29 14:24:46 +01:00
try {
Iterator < JsonNode > iter = node . get ( " value " ) . elements ( ) ;
List < String > pList = new ArrayList < > ( ) ;
while ( iter . hasNext ( ) ) {
pList . add ( iter . next ( ) . asText ( ) ) ;
}
2022-01-12 13:10:00 +01:00
List < PidSystem > pidList ;
if ( pList . size ( ) = = 0 ) {
pidList = Arrays . stream ( rdaValue . replaceAll ( " [ \\ [ \" \\ ]] " , " " ) . split ( " , " ) )
. map ( PidSystem : : fromValue ) . collect ( Collectors . toList ( ) ) ;
}
else {
pidList = pList . stream ( ) . map ( PidSystem : : fromValue ) . collect ( Collectors . toList ( ) ) ;
}
2021-12-29 14:24:46 +01:00
rda . setPidSystem ( pidList ) ;
rda . setAdditionalProperty ( ImportPropertyName . PID_SYSTEM . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
}
catch ( IllegalArgumentException e ) {
rda . setPidSystem ( new ArrayList < > ( ) ) ;
break ;
}
2020-03-23 17:09:31 +01:00
break ;
case STORAGE_TYPE :
rda . setStorageType ( rdaValue ) ;
2020-03-27 13:31:06 +01:00
rda . setAdditionalProperty ( ImportPropertyName . STORAGE_TYPE . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
2020-03-23 17:09:31 +01:00
break ;
case SUPPORT_VERSIONING :
2023-07-14 12:56:08 +02:00
try {
rda . setSupportVersioning ( Host . SupportVersioning . fromValue ( rdaValue ) ) ;
rda . setAdditionalProperty ( ImportPropertyName . SUPPORT_VERSIONING . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
}
catch ( IllegalArgumentException e ) {
logger . warn ( " Distribution host support versioning " + rdaValue + " from semantic distribution.host.support_versioning is not valid. Support versioning will not be set set. " ) ;
}
2020-03-23 17:09:31 +01:00
break ;
case TITLE :
rda . setTitle ( rdaValue ) ;
2020-03-27 13:31:06 +01:00
rda . setAdditionalProperty ( ImportPropertyName . TITLE . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
2020-03-23 17:09:31 +01:00
break ;
case URL :
2021-12-29 14:24:46 +01:00
try {
rda . setUrl ( URI . create ( rdaValue ) ) ;
rda . setAdditionalProperty ( ImportPropertyName . URL . getName ( ) , node . get ( " id " ) . asText ( ) ) ;
} catch ( IllegalArgumentException e ) {
logger . warn ( e . getLocalizedMessage ( ) + " . Skipping url parsing " ) ;
}
2020-03-23 17:09:31 +01:00
break ;
}
}
}
}
}
2020-03-20 17:20:22 +01:00
}
2021-12-29 14:24:46 +01:00
if ( rda . getTitle ( ) = = null | | rda . getUrl ( ) = = null ) {
return null ;
2020-11-13 17:26:09 +01:00
}
2020-03-20 17:20:22 +01:00
return rda ;
}
2020-03-23 17:09:31 +01:00
2020-03-27 13:31:06 +01:00
public static Map < String , String > toProperties ( Host rda ) {
Map < String , String > properties = new HashMap < > ( ) ;
rda . getAdditionalProperties ( ) . entrySet ( ) . forEach ( entry - > {
try {
ImportPropertyName importPropertyName = ImportPropertyName . fromString ( entry . getKey ( ) ) ;
switch ( importPropertyName ) {
case AVAILABILITY :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getAvailability ( ) ) ;
break ;
case TITLE :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getTitle ( ) ) ;
break ;
case DESCRIPTION :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getDescription ( ) ) ;
break ;
case BACKUP_FREQUENCY :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getBackupFrequency ( ) ) ;
break ;
case BACKUP_TYPE :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getBackupType ( ) ) ;
break ;
case CERTIFIED_WITH :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getCertifiedWith ( ) . value ( ) ) ;
break ;
case GEO_LOCATION :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getGeoLocation ( ) . value ( ) ) ;
break ;
case PID_SYSTEM :
2022-01-18 10:38:24 +01:00
List < Object > pids = new ArrayList < > ( ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
for ( PidSystem pid : rda . getPidSystem ( ) ) {
pids . add ( pid . value ( ) ) ;
}
if ( ! pids . isEmpty ( ) ) {
properties . put ( entry . getValue ( ) . toString ( ) , mapper . writeValueAsString ( pids ) ) ;
}
2020-03-27 13:31:06 +01:00
break ;
case STORAGE_TYPE :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getStorageType ( ) ) ;
break ;
case SUPPORT_VERSIONING :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getSupportVersioning ( ) . value ( ) ) ;
break ;
case URL :
properties . put ( entry . getValue ( ) . toString ( ) , rda . getUrl ( ) . toString ( ) ) ;
break ;
}
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
} ) ;
return properties ;
}
private enum ExportPropertyName {
2020-03-23 17:09:31 +01:00
AVAILABILITY ( " availability " ) ,
BACKUP_FREQUENCY ( " backup_frequency " ) ,
BACKUP_TYPE ( " backup_type " ) ,
CERTIFIED_WITH ( " certified_with " ) ,
DESCRIPTION ( " description " ) ,
GEO_LOCATION ( " geo_location " ) ,
PID_SYSTEM ( " pid_system " ) ,
STORAGE_TYPE ( " storage_type " ) ,
SUPPORT_VERSIONING ( " support_versioning " ) ,
TITLE ( " title " ) ,
URL ( " url " ) ;
private final String name ;
2020-03-27 13:31:06 +01:00
ExportPropertyName ( String name ) {
2020-03-23 17:09:31 +01:00
this . name = name ;
}
public String getName ( ) {
return name ;
}
}
2020-03-27 13:31:06 +01:00
private enum ImportPropertyName {
AVAILABILITY ( " availabilityId " ) ,
BACKUP_FREQUENCY ( " backup_frequencyId " ) ,
BACKUP_TYPE ( " backup_typeId " ) ,
CERTIFIED_WITH ( " certified_withId " ) ,
DESCRIPTION ( " descriptionId " ) ,
GEO_LOCATION ( " geo_locationId " ) ,
PID_SYSTEM ( " pid_systemId " ) ,
STORAGE_TYPE ( " storage_typeId " ) ,
SUPPORT_VERSIONING ( " support_versioningId " ) ,
TITLE ( " titleId " ) ,
URL ( " urlId " ) ;
private final String name ;
ImportPropertyName ( String name ) {
this . name = name ;
}
public String getName ( ) {
return name ;
}
public static ImportPropertyName fromString ( String name ) throws Exception {
for ( ImportPropertyName importPropertyName : ImportPropertyName . values ( ) ) {
if ( importPropertyName . getName ( ) . equals ( name ) ) {
return importPropertyName ;
}
}
throw new Exception ( " No name available " ) ;
}
}
2020-03-20 17:20:22 +01:00
}