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