diff --git a/src/main/java/org/gcube/informationsystem/orientdb/hooks/ConsistsOfHook.java b/src/main/java/org/gcube/informationsystem/orientdb/hooks/ConsistsOfHook.java index 43f6b49..ecf0474 100644 --- a/src/main/java/org/gcube/informationsystem/orientdb/hooks/ConsistsOfHook.java +++ b/src/main/java/org/gcube/informationsystem/orientdb/hooks/ConsistsOfHook.java @@ -3,8 +3,11 @@ */ package org.gcube.informationsystem.orientdb.hooks; -import org.gcube.informationsystem.model.embedded.RelationProperty.ReferentialIntegrity; -import org.gcube.informationsystem.model.relation.ConsistsOf; +import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl; +import org.gcube.informationsystem.model.embedded.PropagationConstraint; +import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint; +import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint; +import org.gcube.informationsystem.model.relation.IsRelatedTo; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; @@ -13,12 +16,20 @@ import com.orientechnologies.orient.core.db.document.ODatabaseDocument; */ public class ConsistsOfHook extends RelationHook { + public static PropagationConstraint propagationConstraint; + + static { + propagationConstraint = new PropagationConstraintImpl(); + propagationConstraint.setRemoveConstraint(RemoveConstraint.cascadeWhenOrphan); + propagationConstraint.setAddConstraint(AddConstraint.propagate); + } + public ConsistsOfHook() { - super(ConsistsOf.NAME, ReferentialIntegrity.onDeleteCascadeWhenOrphan); + super(IsRelatedTo.NAME, propagationConstraint); } public ConsistsOfHook(ODatabaseDocument database) { - super(database, ConsistsOf.NAME, ReferentialIntegrity.onDeleteCascadeWhenOrphan); + super(database, IsRelatedTo.NAME, propagationConstraint); } } \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/orientdb/hooks/IsRelatedToHook.java b/src/main/java/org/gcube/informationsystem/orientdb/hooks/IsRelatedToHook.java index 79f3588..c322f7c 100644 --- a/src/main/java/org/gcube/informationsystem/orientdb/hooks/IsRelatedToHook.java +++ b/src/main/java/org/gcube/informationsystem/orientdb/hooks/IsRelatedToHook.java @@ -1,25 +1,32 @@ -/** - * - */ package org.gcube.informationsystem.orientdb.hooks; -import org.gcube.informationsystem.model.embedded.RelationProperty.ReferentialIntegrity; +import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl; +import org.gcube.informationsystem.model.embedded.PropagationConstraint; +import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint; +import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint; import org.gcube.informationsystem.model.relation.IsRelatedTo; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; /** * @author Luca Frosini (ISTI - CNR) - * @param */ public class IsRelatedToHook extends RelationHook { + public static PropagationConstraint propagationConstraint; + + static { + propagationConstraint = new PropagationConstraintImpl(); + propagationConstraint.setRemoveConstraint(RemoveConstraint.keep); + propagationConstraint.setAddConstraint(AddConstraint.unpropagate); + } + public IsRelatedToHook() { - super(IsRelatedTo.NAME, ReferentialIntegrity.onDeleteKeep); + super(IsRelatedTo.NAME, propagationConstraint); } public IsRelatedToHook(ODatabaseDocument database) { - super(database, IsRelatedTo.NAME, ReferentialIntegrity.onDeleteKeep); + super(database, IsRelatedTo.NAME, propagationConstraint); } } \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/orientdb/hooks/RelationHook.java b/src/main/java/org/gcube/informationsystem/orientdb/hooks/RelationHook.java index 2b9fdf6..fe82fd2 100644 --- a/src/main/java/org/gcube/informationsystem/orientdb/hooks/RelationHook.java +++ b/src/main/java/org/gcube/informationsystem/orientdb/hooks/RelationHook.java @@ -3,8 +3,9 @@ */ package org.gcube.informationsystem.orientdb.hooks; -import org.gcube.informationsystem.model.embedded.RelationProperty; -import org.gcube.informationsystem.model.embedded.RelationProperty.ReferentialIntegrity; +import org.gcube.informationsystem.model.embedded.PropagationConstraint; +import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint; +import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint; import org.gcube.informationsystem.model.relation.Relation; import com.orientechnologies.common.log.OLogManager; @@ -22,24 +23,26 @@ public abstract class RelationHook extends ODocumentHookAbstract implements ODatabaseLifecycleListener { protected final String relationType; - protected final RelationProperty.ReferentialIntegrity referentiaIntegrity; + protected final PropagationConstraint propagationConstraint; protected void init(String relationType) { setIncludeClasses(relationType); } @SuppressWarnings("deprecation") - public RelationHook(String relationType, ReferentialIntegrity referentiaIntegrity) { + public RelationHook(String relationType, + PropagationConstraint propagationConstraint) { super(); this.relationType = relationType; - this.referentiaIntegrity = referentiaIntegrity; + this.propagationConstraint = propagationConstraint; init(relationType); } - public RelationHook(ODatabaseDocument database, String relationType, ReferentialIntegrity referentiaIntegrity) { + public RelationHook(ODatabaseDocument database, String relationType, + PropagationConstraint propagationConstraint) { super(database); this.relationType = relationType; - this.referentiaIntegrity = referentiaIntegrity; + this.propagationConstraint = propagationConstraint; init(relationType); } @@ -47,82 +50,92 @@ public abstract class RelationHook extends ODocumentHookAbstract implements public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() { return DISTRIBUTED_EXECUTION_MODE.BOTH; } - - protected RESULT checkRelationProperty(final ODocument iDocument) { - OLogManager.instance().debug(this, "Checking %s on %s", - RelationProperty.NAME, iDocument.toJSON()); - RESULT result = RESULT.RECORD_NOT_CHANGED; + @SuppressWarnings({ "rawtypes", "unchecked" }) + protected RESULT fixConstraint(RESULT result, + ODocument oDocument, String property, Class clz, T enumValue) { - ODocument oDocument = iDocument.field(Relation.RELATION_PROPERTY); - if (oDocument == null) { + Object propertyObject = oDocument.field(property); + + if (propertyObject == null) { OLogManager.instance().debug(this, - "%s not present. Going to create it on %s", - RelationProperty.NAME, iDocument.toJSON()); - - oDocument = new ODocument(RelationProperty.NAME); - - oDocument.field(RelationProperty.REFERENTIAL_INTEGRITY, - referentiaIntegrity.toString()); - - iDocument.field(Relation.RELATION_PROPERTY, oDocument); - - OLogManager.instance().debug(this, "%s has now an %s", - iDocument.toJSON(), RelationProperty.NAME); + "%s not present. Going to create it on %s", property, + oDocument.toJSON()); + oDocument.field(property, enumValue.toString()); result = RESULT.RECORD_CHANGED; - } else { - OLogManager.instance().debug(this, - "%s already present on %s. Going to validate it.", - Relation.RELATION_PROPERTY, iDocument.toJSON()); - - Object referentialIntegrityObject = oDocument - .field(RelationProperty.REFERENTIAL_INTEGRITY); - - if (referentialIntegrityObject == null) { - OLogManager.instance().debug(this, - "%s not present. Going to create it on %s", - RelationProperty.REFERENTIAL_INTEGRITY, oDocument.toJSON()); - oDocument.field(RelationProperty.REFERENTIAL_INTEGRITY, - referentiaIntegrity.toString()); + try { + Enum.valueOf(clz, propertyObject.toString()); + } catch (Exception e) { + OLogManager + .instance() + .warn(this, + "%s is not a valid value for % in %. Going to set default value %s.", + propertyObject.toString(), property, + relationType, enumValue.toString()); + oDocument.field(property, enumValue.toString()); result = RESULT.RECORD_CHANGED; - } else { - try { - /* Checking provided ReferentiaIntegrity value */ - Enum.valueOf(ReferentialIntegrity.class, - referentialIntegrityObject.toString()); - } catch (Exception e) { - OLogManager - .instance() - .warn(this, - "%s is not a valid value for % in %. Going to set default value %s.", - referentialIntegrityObject.toString(), - RelationProperty.NAME, relationType, - referentiaIntegrity.toString()); - oDocument.field(RelationProperty.REFERENTIAL_INTEGRITY, - referentiaIntegrity.toString()); - result = RESULT.RECORD_CHANGED; - } - } } - + + return result; + } + + protected RESULT checkPropagationConstraint(final ODocument iDocument) { + OLogManager.instance().debug(this, "Checking %s on %s", + PropagationConstraint.NAME, iDocument.toJSON()); + + RESULT result = RESULT.RECORD_NOT_CHANGED; + + ODocument oDocument = iDocument.field(Relation.PROPAGATION_CONSTRAINT); + if (oDocument == null) { + OLogManager.instance().debug(this, + "%s not present. Going to create it on %s", + PropagationConstraint.NAME, iDocument.toJSON()); + + oDocument = new ODocument(PropagationConstraint.NAME); + + oDocument.field(PropagationConstraint.REMOVE_PROPERTY, + propagationConstraint.getRemoveConstraint().toString()); + oDocument.field(PropagationConstraint.ADD_PROPERTY, + propagationConstraint.getAddConstraint().toString()); + + iDocument.field(Relation.PROPAGATION_CONSTRAINT, oDocument); + + OLogManager.instance().debug(this, "%s has now a %s", + iDocument.toJSON(), PropagationConstraint.NAME); + result = RESULT.RECORD_CHANGED; + + } else { + OLogManager.instance().debug(this, + "%s already present on %s. Going to validate it.", + Relation.PROPAGATION_CONSTRAINT, iDocument.toJSON()); + + result = fixConstraint(result, oDocument, + PropagationConstraint.REMOVE_PROPERTY, + RemoveConstraint.class, + propagationConstraint.getRemoveConstraint()); + + result = fixConstraint(result, oDocument, + PropagationConstraint.ADD_PROPERTY, AddConstraint.class, + propagationConstraint.getAddConstraint()); + + } + return result; } - @Override public RESULT onRecordBeforeCreate(final ODocument iDocument) { - return checkRelationProperty(iDocument); + return checkPropagationConstraint(iDocument); } - @Override public RESULT onRecordBeforeUpdate(final ODocument iDocument) { - return checkRelationProperty(iDocument); + return checkPropagationConstraint(iDocument); } - + @Override public PRIORITY getPriority() { return PRIORITY.REGULAR;