Mercurial > hg > openmind
comparison src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java @ 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 | d2833ab25c54 |
children | e08f3dd127a5 |
comparison
equal
deleted
inserted
replaced
55:b7a8db041f68 | 56:467843399e70 |
---|---|
639 * @param idSequence | 639 * @param idSequence |
640 * @throws Exception | 640 * @throws Exception |
641 */ | 641 */ |
642 private void saveCurrentEntity(Session session, Entity entity, Sequence idSequence) throws Exception { | 642 private void saveCurrentEntity(Session session, Entity entity, Sequence idSequence) throws Exception { |
643 Long time = System.currentTimeMillis(); | 643 Long time = System.currentTimeMillis(); |
644 refreshEntity(session, entity); | |
644 entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | 645 entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); |
645 entity.resetRowId(); | 646 entity.resetRowId(); |
646 entity.increaseVersion(); | 647 entity.increaseVersion(); |
647 entity.setObjectClass(entity.getObjectClass()); | 648 entity.setObjectClass(entity.getObjectClass()); |
648 entity.setModificationTime(time); | 649 entity.setModificationTime(time); |
650 entity.setUser(entity.getUser()); | 651 entity.setUser(entity.getUser()); |
651 entity.autoNormalize(); | 652 entity.autoNormalize(); |
652 // generating of id, connecting rels, atts and views to the entity | 653 // generating of id, connecting rels, atts and views to the entity |
653 this.prepareEntity(session, entity, idSequence); | 654 this.prepareEntity(session, entity, idSequence); |
654 this.persistEntity(session, entity); | 655 this.persistEntity(session, entity); |
656 } | |
657 | |
658 /** | |
659 * This class must be called, | |
660 * because sometimes the current entity is related to other entities that | |
661 * have been changed in parallel to the edition of this entity. | |
662 * For this reason, some relations of the current entity can have old information of the related entities (like the modification time). | |
663 * This method updates the relations of the current entities getting the actual information of the related entities directly from the DB | |
664 * @param session | |
665 * @param entity | |
666 * @throws Exception | |
667 */ | |
668 private void refreshEntity(Session session, Entity entity) throws Exception{ | |
669 for(Relation rel : entity.getSourceRelations()){ | |
670 refreshTarget(session, rel); | |
671 } | |
672 | |
673 for(Relation rel : entity.getTargetRelations()){ | |
674 refreshSource(session, rel); | |
675 } | |
676 } | |
677 | |
678 private void refreshTarget(Session session, Relation rel) throws Exception{ | |
679 long targetModifOld = rel.getTargetModif(); | |
680 StringBuilder sb = new StringBuilder("refreshTarget\n" + rel.toString() + "\n"); | |
681 sb.append(rel.printTarget() + "\n"); | |
682 List<Entity> entityList = this.getEntities(session, rel.getTargetId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false); | |
683 if(entityList.isEmpty()){ | |
684 throw new Exception("Saving entity " + rel.getSourceId() + ", the system found a relation without target. " + rel.toString()); | |
685 } | |
686 Entity target = entityList.get(0); | |
687 rel.setTarget(target); | |
688 sb.append(rel.printTarget() + "\n"); | |
689 // the refresh is printed in the standard output only if we detect a difference between the relation and the target entity. | |
690 if(targetModifOld != rel.getTargetModif()){ | |
691 logger.info(sb.toString()); | |
692 } | |
693 } | |
694 | |
695 private void refreshSource(Session session, Relation rel) throws Exception{ | |
696 long sourceModifOld = rel.getSourceModif(); | |
697 StringBuilder sb = new StringBuilder("refreshSource\n" + rel.toString() + "\n"); | |
698 sb.append(rel.printSource() + "\n"); | |
699 List<Entity> entityList = this.getEntities(session, rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false); | |
700 if(entityList.isEmpty()){ | |
701 throw new Exception("Saving entity " + rel.getTargetId() + ", the system found a relation without source. " + rel.toString()); | |
702 } | |
703 Entity source = entityList.get(0); | |
704 rel.setSource(source); | |
705 sb.append(rel.printSource() + "\n"); | |
706 // the refresh is printed in the standard output only if we detect a difference between the relation and the source entity. | |
707 if(sourceModifOld != rel.getSourceModif()){ | |
708 logger.info(sb.toString()); | |
709 } | |
655 } | 710 } |
656 | 711 |
657 /** | 712 /** |
658 * Update the entity to prepare it for persistence using the session. | 713 * Update the entity to prepare it for persistence using the session. |
659 * | 714 * |