resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java

108 lines
5.0 KiB
Java
Raw Normal View History

2019-11-06 12:14:27 +01:00
package org.gcube.informationsystem.resourceregistry.instances.model.relations;
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.base.reference.entities.EntityElement;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Resource;
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.DeleteConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
2023-05-11 18:35:56 +02:00
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
import org.gcube.informationsystem.types.reference.entities.EntityType;
2021-02-22 16:36:19 +01:00
import org.gcube.informationsystem.types.reference.entities.ResourceType;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-02-22 16:36:19 +01:00
public class IsRelatedToManagement extends RelationManagement<ResourceManagement, ResourceType> {
public static final PropagationConstraint DEFAULT_IS_RELATED_TO_PC;
static {
DEFAULT_IS_RELATED_TO_PC = new PropagationConstraintImpl();
DEFAULT_IS_RELATED_TO_PC.setDeleteConstraint(DeleteConstraint.keep);
DEFAULT_IS_RELATED_TO_PC.setRemoveConstraint(RemoveConstraint.keep);
DEFAULT_IS_RELATED_TO_PC.setAddConstraint(AddConstraint.unpropagate);
}
public IsRelatedToManagement() {
super(AccessType.IS_RELATED_TO, Resource.class, DEFAULT_IS_RELATED_TO_PC);
}
@Override
2021-03-05 10:46:59 +01:00
protected IsRelatedToNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new IsRelatedToNotFoundException(e.getMessage(), e.getCause());
}
@Override
2021-03-05 10:46:59 +01:00
public IsRelatedToAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
String message) {
return new IsRelatedToAvailableInAnotherContextException(message);
}
@Override
2021-03-05 10:46:59 +01:00
protected IsRelatedToAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new IsRelatedToAlreadyPresentException(message);
}
@Override
protected ResourceManagement newTargetEntityManagement() throws ResourceRegistryException {
2021-02-10 15:45:48 +01:00
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setWorkingContext(getWorkingContext());
2023-05-11 18:35:56 +02:00
resourceManagement.setDatabase(database);
2021-02-10 15:45:48 +01:00
return resourceManagement;
}
2021-02-05 17:50:16 +01:00
@Override
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
JsonNode relation = serializeSelfAsJsonNode();
try {
if(includeSource) {
EntityElementManagement<? extends EntityElement, ? extends EntityType> sourceEntityManagement = getSourceEntityManagement();
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfAsJsonNode());
}
if(includeTarget) {
EntityElementManagement<? extends EntityElement, ? extends EntityType> targetEntityManagement = getTargetEntityManagement();
/*
* This function differ from the super implementation in RelationElementManagement from the following line of code
* An IsRelatedTo must not include the whole target Resource (with all the facets) instead only the Resource entity.
*
* ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJsonNode());
*/
((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeSelfAsJsonNode());
}
} catch(ResourceRegistryException e) {
2023-05-11 18:35:56 +02:00
logger.error("Unable to correctly serialize {}. {}", element, DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw e;
} catch(Exception e) {
2023-05-11 18:35:56 +02:00
logger.error("Unable to correctly serialize {}. {}", element, DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw new ResourceRegistryException(e);
}
return relation;
}
2021-02-05 17:50:16 +01:00
@Override
2021-02-19 19:32:23 +01:00
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
super.sanityCheck();
2021-02-05 17:50:16 +01:00
}
}