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-09-14 15:58:00 +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;
|
2021-09-14 15:58:00 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.entities.EntityElement;
|
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.Resource;
|
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;
|
2023-02-02 21:18:28 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.DeleteConstraint;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
|
2021-09-14 15:58:00 +02:00
|
|
|
import org.gcube.informationsystem.model.reference.relations.Relation;
|
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;
|
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;
|
2021-09-14 15:58:00 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
|
2019-11-06 12:13:19 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
|
2023-05-11 18:35:56 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
|
2021-09-14 15:58:00 +02:00
|
|
|
import org.gcube.informationsystem.types.reference.entities.EntityType;
|
2021-02-22 16:36:19 +01:00
|
|
|
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2021-02-22 16:36:19 +01:00
|
|
|
public class IsRelatedToManagement extends RelationManagement<ResourceManagement, ResourceType> {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2018-06-11 11:01:10 +02:00
|
|
|
public static final PropagationConstraint DEFAULT_IS_RELATED_TO_PC;
|
|
|
|
|
|
|
|
static {
|
|
|
|
DEFAULT_IS_RELATED_TO_PC = new PropagationConstraintImpl();
|
2023-02-02 21:18:28 +01:00
|
|
|
DEFAULT_IS_RELATED_TO_PC.setDeleteConstraint(DeleteConstraint.keep);
|
2018-06-11 11:01:10 +02:00
|
|
|
DEFAULT_IS_RELATED_TO_PC.setRemoveConstraint(RemoveConstraint.keep);
|
|
|
|
DEFAULT_IS_RELATED_TO_PC.setAddConstraint(AddConstraint.unpropagate);
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public IsRelatedToManagement() {
|
2019-11-04 18:01:20 +01:00
|
|
|
super(AccessType.IS_RELATED_TO, Resource.class, DEFAULT_IS_RELATED_TO_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
|
2021-03-05 10:46:59 +01:00
|
|
|
protected IsRelatedToNotFoundException getSpecificNotFoundException(NotFoundException e) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new IsRelatedToNotFoundException(e.getMessage(), e.getCause());
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
public IsRelatedToAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
|
2017-11-17 11:42:37 +01:00
|
|
|
String message) {
|
|
|
|
return new IsRelatedToAvailableInAnotherContextException(message);
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
protected IsRelatedToAlreadyPresentException getSpecificAlreadyPresentException(String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new IsRelatedToAlreadyPresentException(message);
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-17 12:31:22 +01:00
|
|
|
@Override
|
2017-11-17 15:59:25 +01:00
|
|
|
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;
|
2017-11-17 12:31:22 +01:00
|
|
|
}
|
2021-02-05 17:50:16 +01:00
|
|
|
|
2021-09-14 15:58:00 +02: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);
|
2021-09-14 15:58:00 +02:00
|
|
|
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);
|
2021-09-14 15:58:00 +02:00
|
|
|
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 {
|
2021-09-16 15:23:04 +02:00
|
|
|
super.sanityCheck();
|
2021-02-05 17:50:16 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|