resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java

130 lines
5.0 KiB
Java
Raw Normal View History

2020-01-27 17:07:37 +01:00
package org.gcube.informationsystem.resourceregistry.contexts.relations;
import java.util.Map;
import java.util.UUID;
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.context.reference.entities.Context;
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.relation.isparentof.IsParentOfAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfNotFoundException;
2021-02-18 18:22:39 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.relations.RelationElementManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
2021-02-22 16:36:19 +01:00
import org.gcube.informationsystem.types.reference.entities.EntityType;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.ODirection;
import com.orientechnologies.orient.core.record.OVertex;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-02-22 16:36:19 +01:00
public class IsParentOfManagement extends RelationElementManagement<ContextManagement, ContextManagement, EntityType, EntityType> {
2021-02-19 19:32:23 +01:00
public IsParentOfManagement() {
super(AccessType.IS_PARENT_OF, Context.class, Context.class);
}
2021-02-19 19:32:23 +01:00
public IsParentOfManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
this();
this.oDatabaseDocument = oDatabaseDocument;
getWorkingContext();
includeSource = false;
includeTarget = true;
}
@Override
public Map<UUID,JsonNode> getAffectedInstances() {
throw new UnsupportedOperationException();
}
2021-02-19 19:32:23 +01:00
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
2021-02-19 19:32:23 +01:00
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.CONTEXT_SECURITY_CONTEXT_UUID);
}
return workingContext;
}
2021-02-19 19:32:23 +01:00
@Override
2021-03-05 10:46:59 +01:00
protected IsParentOfNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new IsParentOfNotFoundException(e.getMessage(), e.getCause());
}
2021-02-19 19:32:23 +01:00
@Override
2021-03-05 10:46:59 +01:00
protected IsParentOfAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new IsParentOfAlreadyPresentException(message);
}
2021-02-19 19:32:23 +01:00
@Override
public JsonNode createCompleteJsonNode()
2021-02-19 19:32:23 +01:00
throws ResourceRegistryException {
2021-02-17 11:29:43 +01:00
JsonNode relation = serializeSelfAsJsonNode();
2021-02-19 19:32:23 +01:00
try {
OVertex source = element.getVertex(ODirection.OUT);
ContextManagement sourceContextManagement = new ContextManagement(oDatabaseDocument);
sourceContextManagement.setElement(source);
2021-02-19 19:32:23 +01:00
if (includeSource) {
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY,
sourceContextManagement.serializeSelfAsJsonNode());
}
2021-02-19 19:32:23 +01:00
OVertex target = element.getVertex(ODirection.IN);
ContextManagement targetContextManagement = new ContextManagement(oDatabaseDocument);
targetContextManagement.setElement(target);
2021-02-19 19:32:23 +01:00
if (includeTarget) {
((ObjectNode) relation).replace(Relation.TARGET_PROPERTY,
targetContextManagement.serializeSelfAsJsonNode());
}
2021-02-19 19:32:23 +01:00
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw e;
2021-02-19 19:32:23 +01:00
} catch (Exception e) {
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw new ResourceRegistryException(e);
}
2021-02-19 19:32:23 +01:00
return relation;
}
2021-02-19 19:32:23 +01:00
@Override
protected ContextManagement newSourceEntityManagement() throws ResourceRegistryException {
return new ContextManagement(oDatabaseDocument);
}
2021-02-19 19:32:23 +01:00
@Override
protected ContextManagement newTargetEntityManagement() throws ResourceRegistryException {
return new ContextManagement(oDatabaseDocument);
}
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
throw new UnsupportedOperationException();
}
2021-02-18 18:22:39 +01:00
@Override
2021-02-19 19:32:23 +01:00
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
2021-02-18 18:22:39 +01:00
// Nothing to do
}
2021-02-19 19:32:23 +01:00
@Override
protected void checksourceAndTargetEntityCompliancy() throws NotFoundException, AvailableInAnotherContextException,
SchemaViolationException, ResourceRegistryException {
// The compliancy is ensured by the code
}
}