2019-11-05 18:39:35 +01:00
package org.gcube.informationsystem.resourceregistry.instances.context ;
2019-10-30 09:58:18 +01:00
2018-04-12 15:04:25 +02:00
import java.io.IOException ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.UUID ;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.context.ContextElaborator ;
2019-10-25 17:45:16 +02:00
import org.gcube.informationsystem.context.impl.entities.ContextImpl ;
import org.gcube.informationsystem.context.reference.entities.Context ;
2018-04-12 15:04:25 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException ;
2018-09-06 14:48:30 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException ;
2019-11-05 18:39:35 +01:00
import org.gcube.informationsystem.resourceregistry.instances.context.entities.ContextManagement ;
2019-02-06 10:14:05 +01:00
import org.gcube.informationsystem.utils.ISMapper ;
2019-11-15 11:56:53 +01:00
import org.gcube.resourcemanagement.support.shared.types.datamodel.D4SEnvironment ;
2018-04-12 15:04:25 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2019-10-30 09:58:18 +01:00
public class ContextCreator extends ContextElaborator {
2018-04-12 15:04:25 +02:00
private static Logger logger = LoggerFactory . getLogger ( ContextCreator . class ) ;
protected Map < String , UUID > contexts ;
protected ContextManagement contextManagement ;
public ContextCreator ( ) {
super ( ) ;
contexts = new HashMap < > ( ) ;
contextManagement = new ContextManagement ( ) ;
}
protected Context create ( Context context ) throws ResourceRegistryException , IOException {
ContextManagement contextManagement = new ContextManagement ( ) ;
2019-02-13 12:25:56 +01:00
contextManagement . setJson ( ISMapper . marshal ( context ) ) ;
2018-04-12 15:04:25 +02:00
String contextString = contextManagement . create ( ) ;
logger . info ( " Created {} " , contextString ) ;
Context c = ISMapper . unmarshal ( Context . class , contextString ) ;
return c ;
}
protected UUID getContextUUID ( ScopeBean scopeBean ) throws ResourceRegistryException , IOException {
if ( scopeBean ! = null ) {
try {
UUID uuid = contexts . get ( scopeBean . toString ( ) ) ;
if ( uuid = = null ) {
uuid = ContextUtility . getInstance ( ) . getContextUUIDFromFullName ( scopeBean . toString ( ) ) ;
if ( uuid ! = null ) {
contexts . put ( scopeBean . toString ( ) , uuid ) ;
}
}
return uuid ;
} catch ( ContextException e ) {
logger . info ( " {} does not exists " , scopeBean ) ;
}
}
return null ;
}
2019-11-15 11:56:53 +01:00
protected void elaborateContext ( D4SEnvironment d4sEnvironment ) throws Exception {
UUID uuid = null ;
ScopeBean scopeBean = d4sEnvironment . getContext ( ) ;
UUID oldISResourceUUID = null ;
try {
oldISResourceUUID = UUID . fromString ( d4sEnvironment . getUuid ( ) ) ;
} catch ( Exception e ) {
}
try {
uuid = getContextUUID ( scopeBean ) ;
} catch ( ContextNotFoundException e ) {
// The context has to be created
logger . info ( " {} not found " , scopeBean . toString ( ) ) ;
}
if ( uuid = = null ) {
logger . info ( " Parent Context of {} is {} " , scopeBean , scopeBean . enclosingScope ( ) ) ;
UUID parentUUID = getContextUUID ( scopeBean . enclosingScope ( ) ) ;
Context context ;
if ( oldISResourceUUID = = null ) {
context = new ContextImpl ( scopeBean . name ( ) ) ;
} else {
context = new ContextImpl ( scopeBean . name ( ) , oldISResourceUUID ) ;
}
if ( parentUUID ! = null ) {
context . setParent ( parentUUID ) ;
}
context = create ( context ) ;
contexts . put ( scopeBean . toString ( ) , context . getHeader ( ) . getUUID ( ) ) ;
} else {
logger . info ( " Context {} is already present with UUID {} " , scopeBean , uuid . toString ( ) ) ;
if ( oldISResourceUUID = = null | | uuid . compareTo ( oldISResourceUUID ) ! = 0 ) {
String warn = " Context " + scopeBean . toString ( ) + " has in Resource Registry UUID " + uuid . toString ( ) + " which does not match with UUID " + oldISResourceUUID + " of the resource in the OLD Information System " ;
logger . warn ( warn ) ;
}
}
}
2018-04-12 15:04:25 +02:00
2019-11-15 12:04:12 +01:00
/ *
2018-04-12 15:04:25 +02:00
protected void elaborateContext ( ScopeBean scopeBean ) throws Exception {
2018-09-06 14:48:30 +02:00
UUID uuid = null ;
try {
uuid = getContextUUID ( scopeBean ) ;
} catch ( ContextNotFoundException e ) {
// The context has to be created
2018-09-06 14:51:00 +02:00
logger . info ( " {} not found " , scopeBean . toString ( ) ) ;
2018-09-06 14:48:30 +02:00
}
2018-04-12 15:04:25 +02:00
if ( uuid = = null ) {
logger . info ( " Parent Context of {} is {} " , scopeBean , scopeBean . enclosingScope ( ) ) ;
UUID parentUUID = getContextUUID ( scopeBean . enclosingScope ( ) ) ;
Context context = new ContextImpl ( scopeBean . name ( ) ) ;
if ( parentUUID ! = null ) {
context . setParent ( parentUUID ) ;
}
context = create ( context ) ;
contexts . put ( scopeBean . toString ( ) , context . getHeader ( ) . getUUID ( ) ) ;
} else {
logger . info ( " Context {} is already present with UUID {} " , scopeBean , uuid . toString ( ) ) ;
}
}
2019-11-15 12:04:12 +01:00
* /
2018-04-12 15:04:25 +02:00
}
2019-10-30 09:58:18 +01:00