2019-11-06 12:13:19 +01:00
package org.gcube.informationsystem.resourceregistry.instances.model.entities ;
2016-12-19 14:59:27 +01:00
2021-03-04 21:57:01 +01:00
import java.util.Map ;
import java.util.UUID ;
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode ;
2019-10-25 17:45:59 +02:00
import org.gcube.informationsystem.base.reference.AccessType ;
2019-02-13 12:33:29 +01:00
import org.gcube.informationsystem.model.reference.entities.Facet ;
2021-03-02 11:54:59 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException ;
2018-06-04 15:31:13 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException ;
2016-12-19 14:59:27 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException ;
2021-03-04 21:57:01 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException ;
2016-12-19 14:59:27 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException ;
2017-11-17 11:42:37 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException ;
2016-12-19 14:59:27 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException ;
2021-03-02 10:25:58 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException ;
2021-03-02 10:58:23 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode ;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation ;
2021-02-22 16:36:19 +01:00
import org.gcube.informationsystem.types.reference.entities.FacetType ;
2016-12-19 14:59:27 +01:00
2021-03-02 10:58:23 +01:00
import com.orientechnologies.orient.core.db.document.ODatabaseDocument ;
import com.orientechnologies.orient.core.record.ODirection ;
2019-11-05 18:36:44 +01:00
import com.orientechnologies.orient.core.record.OVertex ;
2016-12-19 14:59:27 +01:00
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
2021-02-22 16:36:19 +01:00
public class FacetManagement extends EntityManagement < Facet , FacetType > {
2017-11-30 18:06:08 +01:00
2021-03-04 21:57:01 +01:00
protected ResourceManagement resourceManagement ;
2016-12-19 14:59:27 +01:00
public FacetManagement ( ) {
2017-03-29 16:53:37 +02:00
super ( AccessType . FACET ) ;
2016-12-19 14:59:27 +01:00
}
2017-11-17 11:42:37 +01:00
@Override
2021-03-05 10:46:59 +01:00
protected FacetNotFoundException getSpecificNotFoundException ( NotFoundException e ) {
2017-11-17 11:42:37 +01:00
return new FacetNotFoundException ( e . getMessage ( ) , e . getCause ( ) ) ;
}
@Override
2021-03-05 10:46:59 +01:00
public FacetAvailableInAnotherContextException getSpecificAvailableInAnotherContextException ( String message ) {
2017-11-17 11:42:37 +01:00
return new FacetAvailableInAnotherContextException ( message ) ;
}
@Override
2021-03-05 10:46:59 +01:00
protected FacetAlreadyPresentException getSpecificAlreadyPresentException ( String message ) {
2017-11-17 11:42:37 +01:00
return new FacetAlreadyPresentException ( message ) ;
}
2016-12-19 14:59:27 +01:00
@Override
2021-02-16 18:43:47 +01:00
public JsonNode createCompleteJsonNode ( ) throws ResourceRegistryException {
2021-02-17 11:29:43 +01:00
return serializeSelfAsJsonNode ( ) ;
2016-12-19 14:59:27 +01:00
}
2017-01-13 12:21:35 +01:00
@Override
2019-11-05 18:36:44 +01:00
protected OVertex reallyCreate ( ) throws FacetAlreadyPresentException , ResourceRegistryException {
2016-12-19 14:59:27 +01:00
return createVertex ( ) ;
}
2017-11-30 18:06:08 +01:00
2016-12-19 14:59:27 +01:00
@Override
2019-11-05 18:36:44 +01:00
protected OVertex reallyUpdate ( ) throws FacetNotFoundException , ResourceRegistryException {
OVertex facet = getElement ( ) ;
2021-02-08 16:26:58 +01:00
facet = ( OVertex ) updateProperties ( oClass , facet , jsonNode , ignoreKeys , ignoreStartWithKeys ) ;
2016-12-19 14:59:27 +01:00
return facet ;
}
2021-03-04 21:57:01 +01:00
@Override
protected Map < UUID , JsonNode > reallyRemoveFromContext ( )
throws ContextException , ResourceRegistryException {
if ( entryPoint ) {
OVertex oVertex = getElement ( ) . getVertices ( ODirection . IN ) . iterator ( ) . next ( ) ;
resourceManagement = new ResourceManagement ( ) ;
resourceManagement . setElement ( oVertex ) ;
}
return super . reallyRemoveFromContext ( ) ;
}
2017-01-13 12:21:35 +01:00
@Override
2021-03-05 11:59:24 +01:00
protected void reallyDelete ( ) throws FacetNotFoundException , ResourceRegistryException {
2021-03-04 21:57:01 +01:00
if ( entryPoint ) {
OVertex oVertex = getElement ( ) . getVertices ( ODirection . IN ) . iterator ( ) . next ( ) ;
resourceManagement = new ResourceManagement ( ) ;
resourceManagement . setElement ( oVertex ) ;
}
2021-03-05 11:59:24 +01:00
affectedInstances . put ( uuid , serializeSelfAsJsonNode ( ) ) ;
getElement ( ) . delete ( ) ;
2016-12-19 14:59:27 +01:00
}
2021-02-22 11:19:11 +01:00
2021-03-02 10:58:23 +01:00
protected void checkResource ( ) throws SchemaViolationException , ResourceRegistryException {
if ( ! entryPoint ) {
return ;
}
if ( operation . isSafe ( ) ) {
/ * You should not be here .
* The sanity check should not be triggered for a safety operation .
* Anyway , using this code as guard .
* /
logger . warn ( " sanityCheck should not be triggered for a safe method (i.e. {}). It is not an error but it slow down the performace. Please contact the developer " , operation . toString ( ) ) ;
return ;
}
if ( operation = = Operation . UPDATE ) {
// an update to the Facet only modify the Facet properties, no need to check the source resource
return ;
}
ODatabaseDocument targetSecurityContextODatabaseDocument = null ;
try {
2021-03-04 21:57:01 +01:00
if ( resourceManagement = = null ) {
OVertex oVertex = getElement ( ) . getVertices ( ODirection . IN ) . iterator ( ) . next ( ) ;
ResourceManagement resourceManagement = new ResourceManagement ( ) ;
resourceManagement . setElement ( oVertex ) ;
}
2021-03-02 10:58:23 +01:00
switch ( operation ) {
case CREATE : case DELETE :
resourceManagement . setWorkingContext ( getWorkingContext ( ) ) ;
resourceManagement . setODatabaseDocument ( oDatabaseDocument ) ;
break ;
case ADD_TO_CONTEXT : case REMOVE_FROM_CONTEXT :
resourceManagement . setWorkingContext ( targetSecurityContext ) ;
targetSecurityContextODatabaseDocument = targetSecurityContext . getDatabaseDocument ( PermissionMode . READER ) ;
resourceManagement . setODatabaseDocument ( targetSecurityContextODatabaseDocument ) ;
break ;
default :
// You should not be here
return ;
}
2021-03-04 11:45:27 +01:00
resourceManagement . setOperation ( operation ) ;
2021-03-02 10:58:23 +01:00
resourceManagement . sanityCheck ( ) ;
2021-03-04 21:57:01 +01:00
} catch ( ResourceRegistryException e ) {
throw e ;
2021-03-02 10:58:23 +01:00
} catch ( Exception e ) {
throw new ResourceRegistryException ( e ) ;
} finally {
if ( targetSecurityContextODatabaseDocument ! = null ) {
targetSecurityContextODatabaseDocument . close ( ) ;
oDatabaseDocument . activateOnCurrentThread ( ) ;
}
}
}
2021-03-02 10:25:58 +01:00
@Override
public void sanityCheck ( ) throws SchemaViolationException , ResourceRegistryException {
super . sanityCheck ( ) ;
2021-03-02 10:58:23 +01:00
checkResource ( ) ;
2021-03-02 10:25:58 +01:00
}
2021-03-02 11:54:59 +01:00
@Override
public String create ( ) throws AlreadyPresentException , ResourceRegistryException {
2021-03-02 16:19:29 +01:00
throw new SchemaViolationException ( " You cannot create a stand alone Facet " ) ;
2021-03-02 11:54:59 +01:00
}
public OVertex internalCreate ( ) throws AlreadyPresentException , ResourceRegistryException {
if ( entryPoint & & operation = = Operation . CREATE ) {
2021-03-02 16:19:29 +01:00
throw new SchemaViolationException ( " You cannot create a stand alone Facet " ) ;
2021-03-02 11:54:59 +01:00
}
return super . internalCreate ( ) ;
}
2016-12-19 14:59:27 +01:00
}