diff src/main/java/org/mpi/openmind/cache/WrapperService.java @ 66:3e4b05a6cb47

new EditIntent for saveEntity().
author casties
date Mon, 30 Jan 2017 20:32:26 +0100
parents 74cd973f6ece
children ae732119447f
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/cache/WrapperService.java	Fri Jan 27 19:55:33 2017 +0100
+++ b/src/main/java/org/mpi/openmind/cache/WrapperService.java	Mon Jan 30 20:32:26 2017 +0100
@@ -24,6 +24,7 @@
 import org.mpi.openmind.repository.bo.utils.RelationSortByTargetOW;
 import org.mpi.openmind.repository.services.PersistenceService;
 import org.mpi.openmind.repository.services.utils.AttributeFilter;
+import org.mpi.openmind.repository.services.utils.EditIntent;
 import org.mpi.openmind.repository.utils.ImportOM3Util;
 import org.mpi.openmind.repository.utils.NormalizerUtils;
 import org.mpi.openmind.repository.utils.RomanizationLoC;
@@ -650,10 +651,11 @@
      * 
 	 * @param entity
 	 * @param user
+	 * @param intent TODO
 	 * @return
 	 * @throws Exception
 	 */
-	public Entity saveEntity(Entity entity, String user) throws Exception {
+	public Entity saveEntity(Entity entity, String user, EditIntent intent) throws Exception {
 		long start = System.currentTimeMillis();
 		if (StringUtils.isEmpty(entity.getType())) {
 			entity.setType(Node.TYPE_ABOX);
@@ -666,9 +668,71 @@
 		txLog.debug("** START save entity: user="+user+" entity="+entity.toSmallString());
 		// check if entity is up to date with cache
 		if (!cache.isEntityCurrent(entity)) {
-			logger.error("Entity to save not up to date with cache!");
+			logger.error("Possible collision: Entity to save not current!");
+			// create differences from cache
 			EntityDiff diff = cache.diffEntityCache(entity, true);
-			logger.debug(diff);
+			if (diff != null) {
+				if (intent != null) {
+					/*
+					 *  check if diff was intended
+					 */
+					if (!intent.isEntModificationIntended(entity.getObjectClass())) {
+						logger.error("unintended modification of entity: "+entity);
+					}
+					for (Attribute a : diff.addedAttributes) {
+						if (!intent.isAttModificationIntended(a.getName())) {
+							logger.error("unintended addition: "+a);
+						}
+					}
+					for (Attribute a : diff.modifiedAttributes) {
+						if (!intent.isAttModificationIntended(a.getName())) {
+							logger.error("unintended modification: "+a);
+						}
+					}
+					for (Attribute a : diff.removedAttributes) {
+						if (!intent.isAttModificationIntended(a.getName())) {
+							logger.error("unintended removal prevented: "+a);
+							entity.addAttribute(a);
+						}
+					}
+					for (Relation r : diff.addedSrcRels) {
+						if (!intent.isSrcRelModificationIntended(r.getObjectClass())) {
+							logger.error("unintended addition: "+r);
+						}
+					}
+					for (Relation r : diff.modifiedSrcRels) {
+						if (!intent.isSrcRelModificationIntended(r.getObjectClass())) {
+							logger.error("unintended modification: "+r);
+						}
+					}
+					for (Relation r : diff.removedSrcRels) {
+						if (!intent.isSrcRelModificationIntended(r.getObjectClass())) {
+							logger.error("unintended removal prevented: "+r);
+							// re-add
+							entity.addSourceRelation(r);
+						}
+					}
+					for (Relation r : diff.addedTarRels) {
+						if (!intent.isTarRelModificationIntended(r.getObjectClass())) {
+							logger.error("unintended addition: "+r);
+						}
+					}
+					for (Relation r : diff.modifiedTarRels) {
+						if (!intent.isTarRelModificationIntended(r.getObjectClass())) {
+							logger.error("unintended modification: "+r);
+						}
+					}
+					for (Relation r : diff.removedTarRels) {
+						if (!intent.isTarRelModificationIntended(r.getObjectClass())) {
+							logger.error("unintended removal prevented: "+r);
+							// re-add
+							entity.addTargetRelation(r);
+						}
+					}
+				} else {
+					logger.warn("save without EditIntent!");
+				}
+			}
 		}
 		
 		// save in database
@@ -1184,14 +1248,14 @@
 	}
 
 	/**
-	 * Save the entity. Calls {@link #saveEntity(String, String)}.
+	 * Save the entity. Calls {@link #saveEntity(String, String, EditIntent)}.
 	 * 
 	 * @param entity
 	 * @param user
 	 * @throws Exception
 	 */
 	public void saveAssertion(Entity entity, String user) throws Exception {
-		this.saveEntity(entity, user);
+		this.saveEntity(entity, user, null);
 	}
 
 	/**