resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/instances/type/relations/RelationTypeDefinitionManag...

135 lines
5.7 KiB
Java

package org.gcube.informationsystem.resourceregistry.instances.type.relations;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfNotFoundException;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.relations.BaseRelationManagement;
import org.gcube.informationsystem.resourceregistry.instances.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.instances.context.entities.ContextManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.entities.EntityTypeDefinitionManagement;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.reference.entities.EntityTypeDefinition;
import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
public class RelationTypeDefinitionManagement<R extends RelationTypeDefinition<SETD, TETD, S, T>,
SEM extends EntityTypeDefinitionManagement<SETD>, TEM extends EntityTypeDefinitionManagement<TETD>,
SETD extends EntityTypeDefinition<S>, TETD extends EntityTypeDefinition<T>,
S extends BaseEntity, T extends BaseEntity>
extends BaseRelationManagement<R, SEM, TEM, SETD, TETD> {
@SuppressWarnings("unchecked")
public RelationTypeDefinitionManagement() {
super(AccessType.RELATION_TYPE_DEFINITION, (Class<SETD>) EntityTypeDefinition.class, (Class<TETD>) EntityTypeDefinition.class);
}
public RelationTypeDefinitionManagement(OrientGraph orientGraph) throws ResourceRegistryException {
this();
this.orientGraph = orientGraph;
getWorkingContext();
}
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
this.workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
}
return workingContext;
}
@Override
protected IsParentOfNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
return new IsParentOfNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected IsParentOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new IsParentOfAlreadyPresentException(message);
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(false, true);
}
public JsonNode serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JsonNode relation = serializeSelfOnly();
try {
Vertex source = element.getVertex(Direction.OUT);
ContextManagement sourceContextManagement = new ContextManagement(orientGraph);
sourceContextManagement.setElement(source);
if(includeSource) {
((ObjectNode)relation).replace(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly());
}
Vertex target = element.getVertex(Direction.IN);
ContextManagement targetContextManagement = new ContextManagement(orientGraph);
targetContextManagement.setElement(target);
if(includeTarget) {
((ObjectNode)relation).replace(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly());
}
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw e;
} catch(Exception e) {
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw new ResourceRegistryException(e);
}
return relation;
}
@Override
protected SEM newSourceEntityManagement() throws ResourceRegistryException {
@SuppressWarnings("unchecked")
SEM sem = (SEM) new EntityTypeDefinitionManagement<SETD>(orientGraph);
return sem;
}
@Override
protected TEM newTargetEntityManagement() throws ResourceRegistryException {
@SuppressWarnings("unchecked")
TEM tem = (TEM) new EntityTypeDefinitionManagement<TETD>(orientGraph);
return tem;
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
throw new UnsupportedOperationException();
}
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
throw new UnsupportedOperationException();
}
}