#5679 catching and ignoring LIEs during the attempt to clean up NULL items in sets
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / taxon / TaxonNode.java
index 27deb069e133d28d82e8726f4b9f7b386d5a9e4e..f7209eaa82bdec224b6faeda6462c0d74ff3ae1a 100755 (executable)
@@ -33,6 +33,7 @@ import javax.xml.bind.annotation.XmlSchemaType;
 import javax.xml.bind.annotation.XmlType;
 
 import org.apache.log4j.Logger;
+import org.hibernate.LazyInitializationException;
 import org.hibernate.annotations.Cascade;
 import org.hibernate.annotations.CascadeType;
 import org.hibernate.annotations.Index;
@@ -193,7 +194,11 @@ public class TaxonNode extends AnnotatableEntity implements ITaxonTreeNode, ITre
 
 // ************************* GETTER / SETTER *******************************/
 
+    @Transient
     public Integer getSortIndex() {
+        TaxonNode parent = HibernateProxyHelper.deproxy(this.parent, TaxonNode.class);
+        parent.removeNullValueFromChildren();
+
                return sortIndex;
        }
     /**
@@ -224,6 +229,7 @@ public class TaxonNode extends AnnotatableEntity implements ITaxonTreeNode, ITre
 
     @Override
     public List<TaxonNode> getChildNodes() {
+
         return childNodes;
     }
        protected void setChildNodes(List<TaxonNode> childNodes) {
@@ -360,6 +366,8 @@ public class TaxonNode extends AnnotatableEntity implements ITaxonTreeNode, ITre
 
     @Override
     public TaxonNode addChildTaxon(Taxon taxon, int index, Reference citation, String microCitation) {
+        Classification classification = HibernateProxyHelper.deproxy(this.getClassification(), Classification.class);
+        taxon = HibernateProxyHelper.deproxy(taxon, Taxon.class);
         if (this.getClassification().isTaxonInTree(taxon)){
             throw new IllegalArgumentException(String.format("Taxon may not be in a classification twice: %s", taxon.getTitleCache()));
        }
@@ -484,9 +492,7 @@ public class TaxonNode extends AnnotatableEntity implements ITaxonTreeNode, ITre
      */
     protected boolean removeChildNode(TaxonNode childNode){
         boolean result = true;
-        while (childNodes.contains(null)){
-            childNodes.remove(null);
-        }
+        removeNullValueFromChildren();
         if(childNode == null){
             throw new IllegalArgumentException("TaxonNode may not be null");
         }
@@ -619,10 +625,14 @@ public class TaxonNode extends AnnotatableEntity implements ITaxonTreeNode, ITre
         parent = HibernateProxyHelper.deproxy(parent, TaxonNode.class);
         List<TaxonNode> parentChildren = parent.getChildNodes();
        //TODO: Only as a workaround. We have to find out why merge creates null entries.
+
         while (parentChildren.contains(null)){
             parentChildren.remove(null);
         }
         parent.updateSortIndex(0);
+        if (index > parent.getChildNodes().size()){
+            index = parent.getChildNodes().size();
+        }
         if (parentChildren.contains(this)){
             //avoid duplicates
             if (parentChildren.indexOf(this) < index){
@@ -849,6 +859,19 @@ public class TaxonNode extends AnnotatableEntity implements ITaxonTreeNode, ITre
         return hasTaxon() ? getTaxon().getNullSafeRank() : null;
     }
 
+    private void removeNullValueFromChildren(){
+        try {
+            if (childNodes.contains(null)){
+                while(childNodes.contains(null)){
+                    childNodes.remove(null);
+                }
+            }
+            this.updateSortIndex(0);
+        } catch (LazyInitializationException e) {
+            logger.info("Cannot clean up uninitialized children without a session, skipping.");
+        }
+    }
+
 
 
 //*********************** CLONE ********************************************************/