refs #6086: Resoure Registry MUST automatically add to Relation default Referential Integrity directive when the client does not specify anything

https://support.d4science.org/issues/6086

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-orientdb-hooks@135114 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-30 13:52:44 +00:00
parent 376840a3d8
commit 05a4df55ff
3 changed files with 186 additions and 2 deletions

View File

@ -20,9 +20,8 @@ import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Luca Frosini (ISTI - CNR)
* @param <OrientGraphNoTx>
*/
public class HeaderHook<OrientGraphNoTx> extends ODocumentHookAbstract
public class HeaderHook extends ODocumentHookAbstract
implements ODatabaseLifecycleListener {
protected void init() {

View File

@ -0,0 +1,25 @@
/**
*
*/
package org.gcube.informationsystem.orientdb.hooks;
import org.gcube.informationsystem.model.embedded.RelationProperty;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
/**
* @author Luca Frosini (ISTI - CNR)
* @param <OrientGraphNoTx>
*/
public class IsRelatedToHook extends RelationHook {
public IsRelatedToHook() {
super(IsRelatedTo.NAME, RelationProperty.ReferentiaIntegrity.onDeleteKeep);
}
public IsRelatedToHook(ODatabaseDocument database) {
super(database, IsRelatedTo.NAME, RelationProperty.ReferentiaIntegrity.onDeleteKeep);
}
}

View File

@ -0,0 +1,160 @@
/**
*
*/
package org.gcube.informationsystem.orientdb.hooks;
import org.gcube.informationsystem.model.embedded.RelationProperty;
import org.gcube.informationsystem.model.relation.Relation;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.hook.ODocumentHookAbstract;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class RelationHook extends ODocumentHookAbstract implements
ODatabaseLifecycleListener {
protected final String relationType;
protected final RelationProperty.ReferentiaIntegrity referentiaIntegrity;
protected void init(String relationType) {
setIncludeClasses(relationType);
}
@SuppressWarnings("deprecation")
public RelationHook(String relationType,
RelationProperty.ReferentiaIntegrity referentiaIntegrity) {
super();
this.relationType = relationType;
this.referentiaIntegrity = referentiaIntegrity;
init(relationType);
}
public RelationHook(ODatabaseDocument database, String relationType,
RelationProperty.ReferentiaIntegrity referentiaIntegrity) {
super(database);
this.relationType = relationType;
this.referentiaIntegrity = referentiaIntegrity;
init(relationType);
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return DISTRIBUTED_EXECUTION_MODE.BOTH;
}
@Override
public RESULT onRecordBeforeCreate(final ODocument iDocument) {
OLogManager.instance().info(this, "Checking %s on %s",
RelationProperty.NAME, iDocument.toJSON());
RESULT result = RESULT.RECORD_NOT_CHANGED;
ODocument oDocument = iDocument.field(Relation.RELATION_PROPERTY);
if (oDocument == 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());
OLogManager.instance().debug(this, "%s has now an %s",
iDocument.toJSON(), RelationProperty.NAME);
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());
result = RESULT.RECORD_CHANGED;
} else {
try {
/* Checking provided ReferentiaIntegrity value */
Enum.valueOf(RelationProperty.ReferentiaIntegrity.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;
}
@Override
public PRIORITY getPriority() {
return PRIORITY.REGULAR;
}
@Override
public void onCreate(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
// REGISTER THE HOOK
iDatabase.registerHook(this);
}
@Override
public void onOpen(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
// REGISTER THE HOOK
iDatabase.registerHook(this);
}
@Override
public void onClose(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
// REGISTER THE HOOK
iDatabase.unregisterHook(this);
}
@Override
public void onDrop(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
iDatabase.unregisterHook(this);
}
@Override
public void onCreateClass(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase,
OClass iClass) {
}
@Override
public void onDropClass(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase,
OClass iClass) {
}
@Override
public void onLocalNodeConfigurationRequest(ODocument iConfiguration) {
}
}