2019-11-06 12:14:27 +01:00
package org.gcube.informationsystem.resourceregistry.instances.model.relations ;
2016-12-19 14:59:27 +01:00
2021-03-03 17:18:36 +01:00
import java.util.Iterator ;
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.impl.properties.PropagationConstraintImpl ;
2019-11-04 18:01:20 +01:00
import org.gcube.informationsystem.model.reference.entities.Facet ;
2019-02-13 12:33:29 +01:00
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint ;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint ;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint ;
2021-03-03 17:18:36 +01:00
import org.gcube.informationsystem.model.reference.relations.ConsistsOf ;
2018-06-04 15:31:13 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException ;
2017-11-17 12:31:22 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException ;
2017-11-17 11:42:37 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAlreadyPresentException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAvailableInAnotherContextException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfNotFoundException ;
2021-02-16 17:17:40 +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 ;
2019-11-06 12:13:19 +01:00
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement ;
2021-03-02 10:58:23 +01:00
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement ;
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 ;
2021-03-03 17:18:36 +01:00
import com.orientechnologies.orient.core.record.OEdge ;
2021-03-02 10:58:23 +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 ConsistsOfManagement extends RelationManagement < FacetManagement , FacetType > {
2017-11-30 18:06:08 +01:00
2018-06-11 11:01:10 +02:00
public static final PropagationConstraint DEFAULT_CONSISTS_OF_PC ;
static {
DEFAULT_CONSISTS_OF_PC = new PropagationConstraintImpl ( ) ;
DEFAULT_CONSISTS_OF_PC . setRemoveConstraint ( RemoveConstraint . cascadeWhenOrphan ) ;
DEFAULT_CONSISTS_OF_PC . setAddConstraint ( AddConstraint . propagate ) ;
}
2016-12-19 14:59:27 +01:00
public ConsistsOfManagement ( ) {
2019-11-04 18:01:20 +01:00
super ( AccessType . CONSISTS_OF , Facet . class , DEFAULT_CONSISTS_OF_PC ) ;
2016-12-19 14:59:27 +01:00
}
2017-11-30 18:06:08 +01:00
2017-11-17 11:42:37 +01:00
@Override
2018-06-04 15:31:13 +02:00
protected ConsistsOfNotFoundException getSpecificElementNotFoundException ( NotFoundException e ) {
2017-11-17 11:42:37 +01:00
return new ConsistsOfNotFoundException ( e . getMessage ( ) , e . getCause ( ) ) ;
}
2017-11-30 18:06:08 +01:00
2017-11-17 11:42:37 +01:00
@Override
2021-02-05 17:50:16 +01:00
public ConsistsOfAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException (
2017-11-17 11:42:37 +01:00
String message ) {
return new ConsistsOfAvailableInAnotherContextException ( message ) ;
}
2016-12-19 14:59:27 +01:00
2017-11-17 11:42:37 +01:00
@Override
protected ConsistsOfAlreadyPresentException getSpecificERAlreadyPresentException ( String message ) {
return new ConsistsOfAlreadyPresentException ( message ) ;
}
2017-11-17 12:31:22 +01:00
@Override
2017-11-17 15:59:25 +01:00
protected FacetManagement newTargetEntityManagement ( ) throws ResourceRegistryException {
2021-02-10 15:45:48 +01:00
FacetManagement facetManagement = new FacetManagement ( ) ;
2021-02-23 14:44:37 +01:00
facetManagement . setODatabaseDocument ( oDatabaseDocument ) ;
2021-02-10 15:45:48 +01:00
facetManagement . setWorkingContext ( getWorkingContext ( ) ) ;
return facetManagement ;
2017-11-17 12:31:22 +01:00
}
2021-02-05 17:50:16 +01:00
2021-03-03 17:18:36 +01:00
@Override
protected OEdge reallyCreate ( ) throws ResourceRegistryException {
OEdge thisOEdge = super . reallyCreate ( ) ;
OVertex target = ( OVertex ) getTargetEntityManagement ( ) . getElement ( ) ;
int count = 0 ;
Iterable < OEdge > iterable = target . getEdges ( ODirection . IN ) ;
Iterator < OEdge > iterator = iterable . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
iterator . next ( ) ;
+ + count ;
}
if ( count > 1 ) {
throw new SchemaViolationException ( " It is not possible to create multiple " + ConsistsOf . NAME + " between the same " + Facet . NAME ) ;
}
return thisOEdge ;
}
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 ) {
2021-03-03 17:18:36 +01:00
// an update to the Facet only modify the ConsistsOf properties, no need to check the source resource
2021-03-02 10:58:23 +01:00
return ;
}
ODatabaseDocument targetSecurityContextODatabaseDocument = null ;
try {
2021-03-04 21:57:01 +01:00
ResourceManagement resourceManagement = getSourceEntityManagement ( ) ;
2021-03-02 10:58:23 +01:00
switch ( operation ) {
case CREATE : case DELETE :
break ;
2021-03-04 21:57:01 +01:00
2021-03-02 10:58:23 +01:00
case ADD_TO_CONTEXT : case REMOVE_FROM_CONTEXT :
2021-03-04 21:57:01 +01:00
resourceManagement = new ResourceManagement ( ) ;
resourceManagement . setElementType ( sourceEntityManagement . getTypeName ( ) ) ;
resourceManagement . setUUID ( sourceEntityManagement . getUUID ( ) ) ;
2021-03-02 10:58:23 +01:00
resourceManagement . setWorkingContext ( targetSecurityContext ) ;
targetSecurityContextODatabaseDocument = targetSecurityContext . getDatabaseDocument ( PermissionMode . READER ) ;
resourceManagement . setODatabaseDocument ( targetSecurityContextODatabaseDocument ) ;
break ;
default :
// You should not be here
return ;
}
2021-03-04 21:57:01 +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-02-05 17:50:16 +01:00
@Override
2021-02-19 19:32:23 +01:00
public void sanityCheck ( ) throws SchemaViolationException , ResourceRegistryException {
2021-03-02 10:58:23 +01:00
super . sanityCheck ( ) ;
checkResource ( ) ;
2021-02-05 17:50:16 +01:00
}
2017-11-30 18:06:08 +01:00
2016-12-19 14:59:27 +01:00
}