changeset 66:3e4b05a6cb47

new EditIntent for saveEntity().
author casties
date Mon, 30 Jan 2017 20:32:26 +0100
parents 74cd973f6ece
children ae732119447f
files src/main/java/org/mpi/openmind/cache/WrapperService.java src/main/java/org/mpi/openmind/repository/bo/Node.java src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java src/main/java/org/mpi/openmind/repository/services/utils/EditIntent.java
diffstat 4 files changed, 172 insertions(+), 8 deletions(-) [+]
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);
 	}
 
 	/**
--- a/src/main/java/org/mpi/openmind/repository/bo/Node.java	Fri Jan 27 19:55:33 2017 +0100
+++ b/src/main/java/org/mpi/openmind/repository/bo/Node.java	Mon Jan 30 20:32:26 2017 +0100
@@ -53,7 +53,7 @@
     private Long rowId;
 
     
-    /** modification time for frontend access */
+    /** modification time as DateTime for frontend access */
     public String getTimeStamp(){
     	return DateFormat.getDateTimeInstance(
                 DateFormat.MEDIUM, DateFormat.SHORT).format(new Date(this.modificationTime));
--- a/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Fri Jan 27 19:55:33 2017 +0100
+++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Mon Jan 30 20:32:26 2017 +0100
@@ -928,8 +928,6 @@
 		}
 
 		// Call of ownValue Generator.
-		// TODO: the method should be used always? what would happen if the
-		// ownValue returns null?
 		if (!isImportMode()) {
 			String ownValue = this.getOwnValueGenerator().generateOwnValue(entity, session);
 			if (StringUtils.isNotEmpty(ownValue)) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/mpi/openmind/repository/services/utils/EditIntent.java	Mon Jan 30 20:32:26 2017 +0100
@@ -0,0 +1,102 @@
+package org.mpi.openmind.repository.services.utils;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Class documenting the parts (attributes, relations) if an Entity that may be
+ * edited.
+ * 
+ * @author casties
+ *
+ */
+public class EditIntent {
+	String modifiedEntity;
+	Set<String> modifiedAttributes;
+	Set<String> modifiedSourceRelations;
+	Set<String> modifiedTargetRelations;
+
+	/**
+	 * Creates an EditIntent with the given attribute and relation names.
+	 * 
+	 * @param modifiedEntity
+	 * @param modifiedAttributes
+	 * @param modifiedSourceRelations
+	 * @param modifiedTargetRelations
+	 */
+	public EditIntent(String modifiedEntity, Set<String> modifiedAttributes, Set<String> modifiedSourceRelations,
+			Set<String> modifiedTargetRelations) {
+		super();
+		this.modifiedEntity = modifiedEntity;
+		this.modifiedAttributes = modifiedAttributes;
+		this.modifiedSourceRelations = modifiedSourceRelations;
+		this.modifiedTargetRelations = modifiedTargetRelations;
+	}
+
+	/**
+	 * Creates an EditIntent with the given attribute and relation names.
+	 * 
+	 * @param modifiedEntity
+	 * @param modifiedAttributes
+	 * @param modifiedSourceRelations
+	 * @param modifiedTargetRelations
+	 */
+	public EditIntent(String modifiedEntity, String[] modifiedAttributes, String[] modifiedSourceRelations, String[] modifiedTargetRelations) {
+		super();
+		this.modifiedEntity = modifiedEntity;
+		this.modifiedAttributes = new HashSet<String>();
+		for (String s : modifiedAttributes) {
+			this.modifiedAttributes.add(s);
+		}
+		
+		this.modifiedSourceRelations = new HashSet<String>();
+		for (String s : modifiedSourceRelations) {
+			this.modifiedSourceRelations.add(s);
+		}
+
+		this.modifiedTargetRelations = new HashSet<String>();
+		for (String s : modifiedTargetRelations) {
+			this.modifiedTargetRelations.add(s);
+		}
+	}
+	
+	/**
+	 * Returns if the modification of the Entity with the given name is intended.
+	 * 
+	 * @param name
+	 * @return
+	 */
+	public boolean isEntModificationIntended(String name) {
+		return modifiedEntity.equals(name);
+	}
+	
+	/**
+	 * Returns if the modification of the Attribute with the given name is intended.
+	 * 
+	 * @param name
+	 * @return
+	 */
+	public boolean isAttModificationIntended(String name) {
+		return modifiedAttributes.contains(name);
+	}
+
+	/**
+	 * Returns if the modification of the Relation with the given name is intended.
+	 * 
+	 * @param name
+	 * @return
+	 */
+	public boolean isSrcRelModificationIntended(String name) {
+		return modifiedSourceRelations.contains(name);
+	}
+
+	/**
+	 * Returns if the modification of the Relation with the given name is intended.
+	 * 
+	 * @param name
+	 * @return
+	 */
+	public boolean isTarRelModificationIntended(String name) {
+		return modifiedTargetRelations.contains(name);
+	}
+}