comparison src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java @ 33:e52f593f9e0d

new transaction logger "openmind.transactionlog" logging entity save actions and their data. more comments in code.
author casties
date Fri, 26 Aug 2016 11:42:41 +0200
parents 8ea716da439f
children 5737ab564b94
comparison
equal deleted inserted replaced
32:9c54842f5e86 33:e52f593f9e0d
23 * 23 *
24 * @author jurzua 24 * @author jurzua
25 */ 25 */
26 public abstract class AbstractPersistenceService { 26 public abstract class AbstractPersistenceService {
27 27
28 private ConfigurationService configurationService; 28 private ConfigurationService configurationService;
29 29
30 private OwnValueGenerator ownValueGenerator; 30 private OwnValueGenerator ownValueGenerator;
31 31
32 private final static String NODE_SEQUENCE = "nodeSequence"; 32 private final static String NODE_SEQUENCE = "nodeSequence";
33 33
34 private static Logger logger = Logger 34 private static Logger logger = Logger.getLogger(AbstractPersistenceService.class);
35 .getLogger(AbstractPersistenceService.class); 35
36 36 public static final String TRANSACTION_LOGGER = "openmind.transactionlog";
37 private boolean importModus = false; 37
38 private static Logger txLog = Logger.getLogger(TRANSACTION_LOGGER);
39
40 private boolean importMode = false;
38 41
39 /* 42 /*
40 * static { logger.setLevel(Level.DEBUG); PatternLayout layout = new 43 * static { logger.setLevel(Level.DEBUG); PatternLayout layout = new
41 * PatternLayout( "%d{ABSOLUTE} %5p %c{1}:%L - %m%n"); Appender stdout = new 44 * PatternLayout( "%d{ABSOLUTE} %5p %c{1}:%L - %m%n"); Appender stdout = new
42 * ConsoleAppender(layout, "System.out"); logger.addAppender(stdout); } 45 * ConsoleAppender(layout, "System.out"); logger.addAppender(stdout); }
560 .println("[PersistenceService.saveEntity] found more than one current entities!"); 563 .println("[PersistenceService.saveEntity] found more than one current entities!");
561 } 564 }
562 565
563 Entity previousEntity = previousEntityList.get(0); 566 Entity previousEntity = previousEntityList.get(0);
564 logger.info("Saving previous entity: " + previousEntity); 567 logger.info("Saving previous entity: " + previousEntity);
565 this.savePreviousEntity(session, previousEntity); 568 this.persistEntityAsPrevious(session, previousEntity);
566 } 569 }
567 } 570 }
568 session.getTransaction().commit(); 571 session.getTransaction().commit();
569 } 572 }
570 573
571 /* 574
572 * public void removeEntity(Long entId, String type) { 575 /**
573 * logger.info("Deleting entity [ID=" + entId + ", type=" + type + 576 * Save the entity to the database.
574 * "]. But keeping history in DB."); Session session = 577 *
575 * HibernateUtil.getSessionFactory().getCurrentSession(); 578 * Creates a new version of the entity. Runs in a transaction.
576 * session.getTransaction().begin(); 579 *
577 * 580 * @param entity
578 * if (entId != null) { List<Entity> previousEntityList = 581 * @return
579 * this.getEntities(session, entId, Node.SYS_STATUS_CURRENT_VERSION, type, 582 */
580 * null); if (previousEntityList.size() > 0) { if (previousEntityList.size() 583 public void saveEntity(Entity entity) throws Exception {
581 * > 1) { System.err .println(
582 * "[PersistenceService.saveEntity] found more than one current entities!");
583 * }
584 *
585 * Entity previousEntity = previousEntityList.get(0);
586 * logger.info("Saving previous entity: " + previousEntity);
587 * this.savePreviousEntity(session, previousEntity); } } }
588 */
589
590 public void savePreviousEntity(Entity entity) throws Exception {
591 584
592 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 585 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
593 session.getTransaction().begin(); 586 session.getTransaction().begin();
594 587
595 if (entity.getId() != null) { 588 saveEntity0(session, entity);
596 List<Entity> previousEntityList = this.getEntities(session, 589
597 entity.getId(), Node.SYS_STATUS_CURRENT_VERSION,
598 entity.getType(), null, false);
599 if (previousEntityList.size() > 0) {
600 if (previousEntityList.size() > 1) {
601 logger.error("[PersistenceService.saveEntity] found more than one current entities!");
602 }
603 Entity previousEntity = previousEntityList.get(0);
604 logger.debug("Saving previous entity: " + previousEntity);
605 this.savePreviousEntity(session, previousEntity);
606 }
607 }
608 this.saveCurrentEntity(session, entity, null);
609 session.getTransaction().commit(); 590 session.getTransaction().commit();
610 } 591
611 592 }
612 /** 593
613 * 594 /**
595 * Save the entity to the database using the given session.
596 *
597 * Creates a new version and sets the existing entity to PREVIOUS_VERSION.
598 *
599 * @param session
614 * @param entity 600 * @param entity
615 * @return 601 * @throws Exception
616 */ 602 */
617 public void saveEntity(Entity entity) throws Exception {
618
619 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
620 session.getTransaction().begin();
621
622 saveEntity0(session, entity);
623
624 session.getTransaction().commit();
625
626 }
627
628 private void saveEntity0(Session session, Entity entity) throws Exception { 603 private void saveEntity0(Session session, Entity entity) throws Exception {
629 if (entity.getId() != null) { 604 if (entity.getId() != null) {
630 List<Entity> previousEntityList = this.getEntities(session, entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, entity.getType(), null, false); 605 List<Entity> previousEntityList = this.getEntities(session, entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, entity.getType(), null, false);
631 if (previousEntityList.size() > 0) { 606 if (previousEntityList.size() > 0) {
632 if (previousEntityList.size() > 1) { 607 if (previousEntityList.size() > 1) {
633 logger.error("[PersistenceService.saveEntity] found more than one current entities!"); 608 logger.error("[PersistenceService.saveEntity] found more than one current entities!");
634 } 609 }
635 610
636 Entity previousEntity = previousEntityList.get(0); 611 Entity previousEntity = previousEntityList.get(0);
637 logger.info("Saving previous entity: " + previousEntity); 612 logger.info("Saving previous entity: " + previousEntity);
638 this.savePreviousEntity(session, previousEntity); 613 this.persistEntityAsPrevious(session, previousEntity);
639 } 614 }
640 } 615 }
641 this.saveCurrentEntity(session, entity, null); 616 this.saveCurrentEntity(session, entity, null);
642 } 617 }
643 618
619 /**
620 * Save the entity as (new) current version using the given session.
621 *
622 * Sets system_status to CURRENT_VERSION and updates row_id, version, modification_time,
623 * etc.
624 *
625 * @param session
626 * @param entity
627 * @param idSequence
628 * @throws Exception
629 */
644 private void saveCurrentEntity(Session session, Entity entity, 630 private void saveCurrentEntity(Session session, Entity entity,
645 Sequence idSequence) throws Exception { 631 Sequence idSequence) throws Exception {
646 Long time = System.currentTimeMillis(); 632 Long time = System.currentTimeMillis();
647 entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); 633 entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
648 entity.resetRowId(); 634 entity.resetRowId();
652 entity.setType(entity.getType()); 638 entity.setType(entity.getType());
653 entity.setUser(entity.getUser()); 639 entity.setUser(entity.getUser());
654 entity.autoNormalize(); 640 entity.autoNormalize();
655 // generating of id, connecting rels, atts and views to the entity 641 // generating of id, connecting rels, atts and views to the entity
656 this.prepareEntity(session, entity, idSequence); 642 this.prepareEntity(session, entity, idSequence);
657 this.saveEntity(session, entity); 643 this.persistEntity(session, entity);
658 } 644 }
659 645
660 /** 646 /**
661 * Here the modification time will be propagated from the entity to its 647 * Update the entity to prepare it for persisting.
662 * relations an attributes. 648 *
649 * If the ID is null new IDs will be assigned for the entity and its
650 * attributes and relations.
651 * The modification time is propagated from the entity to its
652 * attributes and relations.
653 * Own-values are normalized.
663 * 654 *
664 * @param session 655 * @param session
665 * @param entity 656 * @param entity
666 * @return 657 * @return
667 */ 658 */
770 } 761 }
771 return entity; 762 return entity;
772 } 763 }
773 764
774 /** 765 /**
775 * Saves an entity setting its system state as previous_version 766 * Set an entities' system state to PREVIOUS_VERSION and persist it.
767 *
768 * Also persists the entities' attributes and relations.
776 * 769 *
777 * @param session 770 * @param session
778 * @param entity 771 * @param entity
779 */ 772 */
780 private void savePreviousEntity(Session session, Entity entity) { 773 private void persistEntityAsPrevious(Session session, Entity entity) {
781 entity.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); 774 entity.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION);
775 txLog.debug("* START save previous entity...");
776 txLog.info("save previous entity: "+entity.toEncString());
782 session.save(entity); 777 session.save(entity);
783 for (Attribute attribute : entity.getAttributes()) { 778 for (Attribute attribute : entity.getAttributes()) {
784 logger.debug(" saving prev entity attribute: "+attribute); 779 txLog.info("save previous entity attribute: "+attribute.toEncString());
785 session.save(attribute); 780 session.save(attribute);
786 } 781 }
787 for (Relation rel : entity.getSourceRelations()) { 782 for (Relation rel : entity.getSourceRelations()) {
783 txLog.info("save previous source relation: "+rel.toEncString());
788 session.save(rel); 784 session.save(rel);
789 for (Attribute att : rel.getAttributes()) { 785 for (Attribute att : rel.getAttributes()) {
790 logger.debug(" saving prev (source)relation attribute: "+rel); 786 txLog.info("save previous source relation attribute: "+att.toEncString());
791 session.save(att); 787 session.save(att);
792 } 788 }
793 } 789 }
794 for (Relation rel : entity.getTargetRelations()) { 790 for (Relation rel : entity.getTargetRelations()) {
791 txLog.info("save previous target relation: "+rel.toEncString());
795 session.save(rel); 792 session.save(rel);
796 for (Attribute att : rel.getAttributes()) { 793 for (Attribute att : rel.getAttributes()) {
797 logger.debug(" saving prev (target)relation attribute: "+rel); 794 txLog.info("save previous target relation attribute: "+att.toEncString());
798 session.save(att); 795 session.save(att);
799 } 796 }
800 } 797 }
801 for (View view : entity.getViews()) { 798 for (View view : entity.getViews()) {
802 session.save(view); 799 session.save(view);
803 } 800 }
804 } 801 txLog.debug("* End ...save previous entity");
805 802 }
806 private void saveEntity(Session session, Entity entity) throws Exception { 803
807 logger.info("Saving current entity: " + entity); 804 /**
805 * Persist a (current) entity and its attributes and relations.
806 *
807 * @param session
808 * @param entity
809 * @throws Exception
810 */
811 private void persistEntity(Session session, Entity entity) throws Exception {
812 txLog.info("* START save entity...");
813 txLog.info("save entity: "+entity.toEncString());
808 session.save(entity); 814 session.save(entity);
809 for (Attribute attribute : entity.getAttributes()) { 815 for (Attribute attribute : entity.getAttributes()) {
810 logger.debug(" saving entity attribute: "+attribute); 816 txLog.info("save entity attribute: "+attribute.toEncString());
811 session.save(attribute); 817 session.save(attribute);
812 } 818 }
813 for (Relation rel : entity.getSourceRelations()) { 819 for (Relation rel : entity.getSourceRelations()) {
814 rel.setSource(entity); 820 rel.setSource(entity);
815 if (rel.getSourceId() == null || rel.getTargetId() == null) { 821 if (rel.getSourceId() == null || rel.getTargetId() == null) {
816 throw new IllegalStateException( 822 throw new IllegalStateException(
817 "Either the sourceId or the targetId was not initialized to the source relation: " 823 "Either the sourceId or the targetId was not initialized to the source relation: "
818 + rel.getOwnValue()); 824 + rel.getOwnValue());
819 } 825 }
826 txLog.info("save source relation: "+rel.toEncString());
820 session.save(rel); 827 session.save(rel);
821 for (Attribute att : rel.getAttributes()) { 828 for (Attribute att : rel.getAttributes()) {
822 logger.debug(" saving (source)relation attribute: "+att); 829 txLog.info("save source relation attribute: "+att.toEncString());
823 session.save(att); 830 session.save(att);
824 } 831 }
825 } 832 }
826 for (Relation rel : entity.getTargetRelations()) { 833 for (Relation rel : entity.getTargetRelations()) {
827 rel.setTarget(entity); 834 rel.setTarget(entity);
828 if (rel.getSourceId() == null || rel.getTargetId() == null) { 835 if (rel.getSourceId() == null || rel.getTargetId() == null) {
829 throw new IllegalStateException( 836 throw new IllegalStateException(
830 "Either the sourceId or the targetId was not initialized to the target relation: " 837 "Either the sourceId or the targetId was not initialized to the target relation: "
831 + rel.getOwnValue()); 838 + rel.getOwnValue());
832 } 839 }
840 txLog.info("save target relation: "+rel.toEncString());
833 session.save(rel); 841 session.save(rel);
834 for (Attribute att : rel.getAttributes()) { 842 for (Attribute att : rel.getAttributes()) {
835 logger.debug(" saving (target)relation attribute: "+att); 843 txLog.info("save target relation attribute: "+att.toEncString());
836 session.save(att); 844 session.save(att);
837 } 845 }
838 } 846 }
839 847
840 // Call of ownValue Generator. 848 // Call of ownValue Generator.
841 // TODO: the method should be used always? what would happen if the 849 // TODO: the method should be used always? what would happen if the
842 // ownValue returns null? 850 // ownValue returns null?
843 if (!isImportModus()) { 851 if (!isImportMode()) {
844 String ownValue = this.getOwnValueGenerator().generateOwnValue( 852 String ownValue = this.getOwnValueGenerator().generateOwnValue(entity, session);
845 entity, session);
846 if (StringUtils.isNotEmpty(ownValue)) { 853 if (StringUtils.isNotEmpty(ownValue)) {
847 entity.setOwnValue(ownValue); 854 entity.setOwnValue(ownValue);
848 entity.autoNormalize(); 855 entity.autoNormalize();
849 session.save(entity); 856 session.save(entity);
850 } 857 }
851 } 858 }
859 txLog.debug("* ...END save entity");
852 } 860 }
853 861
854 public OwnValueGenerator getOwnValueGenerator() { 862 public OwnValueGenerator getOwnValueGenerator() {
855 return ownValueGenerator; 863 return ownValueGenerator;
856 } 864 }
859 this.ownValueGenerator = ownValueGenerator; 867 this.ownValueGenerator = ownValueGenerator;
860 } 868 }
861 869
862 /** 870 /**
863 * <p> 871 * <p>
864 * Returns entities with their corresponded contain. 872 * Returns entities with their corresponding contents.
865 * </p> 873 * </p>
866 * 874 *
867 * @param session 875 * @param session
868 * @param id 876 * @param id
869 * @param systemStatus 877 * @param systemStatus
925 return entities; 933 return entities;
926 } 934 }
927 935
928 /** 936 /**
929 * <p> 937 * <p>
930 * Returns entities with their corresponded contain. 938 * Returns entities with their corresponding content.
931 * </p> 939 * </p>
932 * 940 *
933 * @param id 941 * @param id
934 * @param systemStatus 942 * @param systemStatus
935 * @param type 943 * @param type
1053 entity.setLightweight(false); 1061 entity.setLightweight(false);
1054 entity.setAttributes(new ArrayList<Attribute>()); 1062 entity.setAttributes(new ArrayList<Attribute>());
1055 entity.setSourceRelations(new ArrayList<Relation>()); 1063 entity.setSourceRelations(new ArrayList<Relation>());
1056 entity.setTargetRelations(new ArrayList<Relation>()); 1064 entity.setTargetRelations(new ArrayList<Relation>());
1057 1065
1058 // FIXME: getNodes finds all attributes if id=null! 1066 /*
1067 * Danger: getNodes finds all attributes in the db and tries to attach them to
1068 * this entity if id=null!
1069 */
1059 if (entity.getId() == null) { 1070 if (entity.getId() == null) {
1060 logger.error("Entity with id=null! Abort loading attributes."); 1071 logger.error("Entity with id=null! Abort loading attributes.");
1061 return entity; 1072 return entity;
1062 } 1073 }
1063 1074
1078 if (node instanceof Attribute) { 1089 if (node instanceof Attribute) {
1079 entity.addAttribute((Attribute) node); 1090 entity.addAttribute((Attribute) node);
1080 } else if (node instanceof Relation) { 1091 } else if (node instanceof Relation) {
1081 Relation rel = (Relation) node; 1092 Relation rel = (Relation) node;
1082 // new attr for relations 1093 // new attr for relations
1083 // FIXME: getNodes finds all attributes if id=null! 1094 /*
1095 * Danger: getNodes finds all attributes in the db and tries to attach them to
1096 * this relation if id=null!
1097 */
1084 if (rel.getId() == null) { 1098 if (rel.getId() == null) {
1085 logger.error("Relation with id=null! Abort loading attributes."); 1099 logger.error("Relation with id=null! Abort loading attributes.");
1086 continue; 1100 continue;
1087 } 1101 }
1088 List<Node> attrs = this.getNodes(session, null, 1102 List<Node> attrs = this.getNodes(session, null,
1099 entity.getSourceRelations().add(rel); 1113 entity.getSourceRelations().add(rel);
1100 } 1114 }
1101 } else if (node instanceof View) { 1115 } else if (node instanceof View) {
1102 entity.getViews().add((View) node); 1116 entity.getViews().add((View) node);
1103 } else { 1117 } else {
1104 throw new IllegalArgumentException("Invalid node found: " 1118 throw new IllegalArgumentException("Invalid node found: " + node);
1105 + node);
1106 } 1119 }
1107 } 1120 }
1108 1121
1109 // getting TargetRelations 1122 // getting TargetRelations
1110 List<Node> tarRels = null; 1123 List<Node> tarRels = null;
1121 1134
1122 for (Node node : tarRels) { 1135 for (Node node : tarRels) {
1123 if (node instanceof Relation) { 1136 if (node instanceof Relation) {
1124 Relation rel = (Relation) node; 1137 Relation rel = (Relation) node;
1125 // new attr for relations 1138 // new attr for relations
1126 // FIXME: getNodes finds all attributes if id=null! 1139 /*
1140 * Danger: getNodes finds all attributes in the db and tries to attach them to
1141 * this relation if id=null!
1142 */
1127 if (rel.getId() == null) { 1143 if (rel.getId() == null) {
1128 logger.error("Relation with id=null! Abort loading attributes."); 1144 logger.error("Relation with id=null! Abort loading attributes.");
1129 continue; 1145 continue;
1130 } 1146 }
1131 List<Node> attrs = this.getNodes(session, null, 1147 List<Node> attrs = this.getNodes(session, null,
1431 public void setConfigurationService( 1447 public void setConfigurationService(
1432 ConfigurationService configurationService) { 1448 ConfigurationService configurationService) {
1433 this.configurationService = configurationService; 1449 this.configurationService = configurationService;
1434 } 1450 }
1435 1451
1436 public boolean isImportModus() { 1452 public boolean isImportMode() {
1437 return importModus; 1453 return importMode;
1438 } 1454 }
1439 1455
1440 public void setImportModus(boolean importModus) { 1456 public void setImportModus(boolean importModus) {
1441 this.importModus = importModus; 1457 this.importMode = importModus;
1442 } 1458 }
1443 1459
1444 // ################################################################# 1460 // #################################################################
1445 // ################################################################# 1461 // #################################################################
1446 // ################################################################# 1462 // #################################################################