# HG changeset patch # User jurzua # Date 1480339570 -3600 # Node ID 467843399e707de05d9a3d413ba5297c318d7782 # Parent b7a8db041f68feb92e9aee97fca4165fb367b6a0 Fixs for lost relation, when editing clone entities in parallel diff -r b7a8db041f68 -r 467843399e70 src/main/java/org/mpi/openmind/repository/bo/Relation.java --- 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; diff -r b7a8db041f68 -r 467843399e70 src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java --- 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 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 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.