cleanup
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / hibernate / PostMergeEntityListener.java
index 82c47b0269fbe3d9e62de393bd090da9143e3dc3..34ad7519edf9d30fd9eda34fc3964eebc6150189 100644 (file)
@@ -32,8 +32,7 @@ import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
 public class PostMergeEntityListener implements MergeEventListener {
 
     private static final long serialVersionUID = 1565797119368313987L;
-    @SuppressWarnings("unused")
-    private static final Logger logger = LogManager.getLogger(PostMergeEntityListener.class);
+    private static final Logger logger = LogManager.getLogger();
 
     private static Map<Session, Set<CdmBase>> newEntitiesMap = new ConcurrentHashMap<>();
 
@@ -51,7 +50,7 @@ public class PostMergeEntityListener implements MergeEventListener {
 
     @Override
     public void onMerge(MergeEvent event) throws HibernateException {
-        //Note AM: TODO is there a reason wyh we neglect onMerge in this case?
+        //Note AM: TODO is there a reason why we neglect onMerge in this case?
         //         Shouldn't we do something like "onMerge(event, new HashMap<>());"
 //        Object entity = event.getEntity();
     }
@@ -81,14 +80,11 @@ public class PostMergeEntityListener implements MergeEventListener {
         if (original != null){
             Class<?> entityClazz = original.getClass();
 
-            if (ITreeNode.class.isAssignableFrom(entityClazz)){  //TaxonNode or TermNode
-                //TODO #10101
-            } else if (PolytomousKeyNode.class.isAssignableFrom(entityClazz)){
-
+            if (PolytomousKeyNode.class.isAssignableFrom(entityClazz)){
                 //For some reason the children list needs to be read once
                 //to guarantee that the sortindex starts with zero
-                PolytomousKeyNode resultPkn = (PolytomousKeyNode)result;
-                resultPkn.getChildren().size();
+                PolytomousKeyNode resultNode = (PolytomousKeyNode)result;
+                resultNode.getChildren().size();
 
                 // #10101 the following code tried to handle orphanRemoval for key nodes that were
                 // really removed from the graph. Generally the removal worked but it was not possible at this
@@ -103,20 +99,43 @@ public class PostMergeEntityListener implements MergeEventListener {
 
 //                EventSource session = event.getSession();
 //                PersistenceContext pc = session.getPersistenceContext();
-//                CollectionEntry childrenEntry = pc.getCollectionEntry((PersistentCollection)resultPkn.getChildren());
+//                CollectionEntry childrenEntry = pc.getCollectionEntry((PersistentCollection)resultNode.getChildren());
 //                List<PolytomousKeyNode> childrenEntrySnapshot = (List<PolytomousKeyNode>)childrenEntry.getSnapshot();
 //                if (childrenEntrySnapshot != null) {
 //                    for (PolytomousKeyNode snapshotChild: childrenEntrySnapshot){
-//                        if (!resultPkn.getChildren().contains(snapshotChild)) {
+//                        if (!resultNode.getChildren().contains(snapshotChild)) {
 //                            EntityEntry currentChild = pc.getEntry(snapshotChild);
 //                            Object parent = currentChild == null ? null :
 //                                currentChild.getLoadedValue("parent");
-//                            if (parent == null || parent == resultPkn) {
+//                            if (parent == null || parent == resultNode) {
 //                                session.delete(snapshotChild);
 //                            }
 //                        }
 //                   }
 //                }
+            } else if (ITreeNode.class.isAssignableFrom(entityClazz)){ //TaxonNode or TermNode
+                //See PolytomousKeyNode above
+                //Not yet tested if necessary here, too.
+
+                try {
+                    ITreeNode<?> resultNode = (ITreeNode<?>)result;
+                    resultNode.getChildNodes().size();
+                } catch (Exception e) {
+                    //#10101
+                    //preliminary catched and logged as it seems to be the cause
+                    //for failing TaxEditor tests in TaxonNameEditorTest
+                    //methods
+                    //    * addDeleteAddHomotypicSynonym,
+                    //    * addDeleteAddHomotypicSynonymWithAnnotations
+                    //    * addHeterotypicSynonym
+                    //    * testAddHomotypicSynonym
+                    //All due to failed to lazily initialize a collection of role: eu.etaxonomy.cdm.model.taxon.TaxonNode.childNodes, could not initialize proxy - no Session
+                    //We need to check if this is an issue in the test behavior or in the solution itself.
+                    //We could also try to atleast add a check if the children list is attached to a session
+                    //before initializing it.
+                    //
+                    logger.warn("Error in PostMergeEntityListener during handleTreeNodes: " + e.getMessage());
+                }
             }
         }
     }