changeset 56:467843399e70

Fixs for lost relation, when editing clone entities in parallel
author jurzua <jjjurzua@hotmail.com>
date Mon, 28 Nov 2016 14:26:10 +0100
parents b7a8db041f68
children e08f3dd127a5
files src/main/java/org/mpi/openmind/repository/bo/Relation.java src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java
diffstat 2 files changed, 63 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/repository/bo/Relation.java	Mon Nov 07 20:01:51 2016 +0100
+++ b/src/main/java/org/mpi/openmind/repository/bo/Relation.java	Mon Nov 28 14:26:10 2016 +0100
@@ -247,6 +247,14 @@
         return false;
     }    
     
+    public String printSource(){
+    	return "Source [sourceObjectClass=" + sourceObjectClass + ", sourceId=" + sourceId + ", sourceModif=" + sourceModif + "]";
+    }
+    
+    public String printTarget(){
+    	return "Target [targetObjectClass=" + targetObjectClass + ", targetId=" + targetId + ", targetModif=" + targetModif + "]";
+    }
+    
     @Override
     public String toString() {
     	String source = ", Src[id=" + sourceId;
--- a/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Mon Nov 07 20:01:51 2016 +0100
+++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Mon Nov 28 14:26:10 2016 +0100
@@ -641,6 +641,7 @@
 	 */
 	private void saveCurrentEntity(Session session, Entity entity, Sequence idSequence) throws Exception {
 		Long time = System.currentTimeMillis();
+		refreshEntity(session, entity);
 		entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
 		entity.resetRowId();
 		entity.increaseVersion();
@@ -653,6 +654,60 @@
 		this.prepareEntity(session, entity, idSequence);
 		this.persistEntity(session, entity);
 	}
+	
+	/**
+	 * This class must be called, 
+	 * because sometimes the current entity is related to other entities that 
+	 * have been changed in parallel to the edition of this entity.
+	 * For this reason, some relations of the current entity can have old information of the related entities (like the modification time).
+	 * This method updates the relations of the current entities getting the actual information of the related entities directly from the DB
+	 * @param session
+	 * @param entity
+	 * @throws Exception
+	 */
+	private void refreshEntity(Session session, Entity entity) throws Exception{
+		for(Relation rel : entity.getSourceRelations()){
+			refreshTarget(session, rel);
+		}
+		
+		for(Relation rel : entity.getTargetRelations()){
+			refreshSource(session, rel);
+		}
+	}
+	
+	private void refreshTarget(Session session, Relation rel) throws Exception{
+		long targetModifOld = rel.getTargetModif();
+		StringBuilder sb = new StringBuilder("refreshTarget\n" + rel.toString() + "\n");
+		sb.append(rel.printTarget() + "\n");
+		List<Entity> entityList = this.getEntities(session, rel.getTargetId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false);
+		if(entityList.isEmpty()){
+			throw new Exception("Saving entity " + rel.getSourceId() + ", the system found a relation without target. " + rel.toString());
+		}
+		Entity target = entityList.get(0);
+		rel.setTarget(target);
+		sb.append(rel.printTarget() + "\n");
+		// the refresh is printed in the standard output only if we detect a difference between the relation and the target entity.
+		if(targetModifOld != rel.getTargetModif()){
+			logger.info(sb.toString());
+		}
+	}
+	
+	private void refreshSource(Session session, Relation rel) throws Exception{
+		long sourceModifOld = rel.getSourceModif();
+		StringBuilder sb = new StringBuilder("refreshSource\n" + rel.toString() + "\n");
+		sb.append(rel.printSource() + "\n");
+		List<Entity> entityList = this.getEntities(session, rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false);
+		if(entityList.isEmpty()){
+			throw new Exception("Saving entity " + rel.getTargetId() + ", the system found a relation without source. " + rel.toString());
+		}
+		Entity source = entityList.get(0);
+		rel.setSource(source);
+		sb.append(rel.printSource() + "\n");
+		// the refresh is printed in the standard output only if we detect a difference between the relation and the source entity.
+		if(sourceModifOld != rel.getSourceModif()){
+			logger.info(sb.toString());
+		}
+	}
 
 	/**
 	 * Update the entity to prepare it for persistence using the session.