make update title cache writable
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / DistributionTree.java
index fe2d8523ce9bc795e78d8e8d9587a54d15ca4665..220d9e4519c0c394da949c96082b034f1b21e8c9 100644 (file)
@@ -1,3 +1,12 @@
+/**\r
+* Copyright (C) 2007 EDIT\r
+* European Distributed Institute of Taxonomy\r
+* http://www.e-taxonomy.eu\r
+*\r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/\r
+\r
 package eu.etaxonomy.cdm.api.service;\r
 \r
 import java.util.ArrayList;\r
@@ -19,7 +28,7 @@ public class DistributionTree extends Tree<Distribution>{
                NamedArea area = new NamedArea();\r
                Distribution data = Distribution.NewInstance();\r
                data.setArea(area);\r
-               data.addModifyingText("test", Language.ENGLISH());\r
+               data.putModifyingText(Language.ENGLISH(), "test");\r
                TreeNode<Distribution> rootElement = new TreeNode<Distribution>();\r
                List<TreeNode<Distribution>> children = new ArrayList<TreeNode<Distribution>>();\r
                \r
@@ -67,8 +76,35 @@ public class DistributionTree extends Tree<Distribution>{
                return result;\r
        }\r
        \r
+       private List<Distribution> orderDistributionsByLevel(List<Distribution> distList){\r
+               boolean flag = true;\r
+               int length = distList.size()-1;\r
+               Distribution dist;\r
+               List<Distribution> orderedList = new ArrayList<Distribution>(length);\r
+               orderedList.addAll(distList);\r
+               \r
+               for (int i = 0; i < length && flag; i++) {\r
+                       flag = false;\r
+                       for (int j = 0; j < length-1; j++) {\r
+                               String level1 = orderedList.get(j).getArea().getLevel().toString();\r
+                               String level2 = orderedList.get(j+1).getArea().getLevel().toString();\r
+                               //if level from j+1 is greater than level from j\r
+                               if (level2.compareTo(\r
+                                               level1) < 0) {\r
+                                       dist = orderedList.get(j);\r
+                                       orderedList.set(j, orderedList.get(j+1));\r
+                                       orderedList.set(j+1, dist);\r
+                                       flag = true;                                    \r
+                               }\r
+                       }\r
+               }\r
+               return orderedList;\r
+       }\r
+       \r
        public void merge(List<Distribution> distList, Set<NamedAreaLevel> omitLevels){\r
-               for (Distribution distribution : distList) {\r
+               List<Distribution> orderedDistList = orderDistributionsByLevel(distList);\r
+       \r
+               for (Distribution distribution : orderedDistList) {\r
                        List<NamedArea> levelList = \r
                                this.getAreaLevelPathList(distribution.getArea(), omitLevels);\r
                        mergeAux(distribution, distribution.getArea().getLevel(), levelList, this.getRootElement());\r
@@ -98,6 +134,7 @@ public class DistributionTree extends Tree<Distribution>{
                                                  TreeNode<Distribution> root){\r
                TreeNode<Distribution> highestDistNode;\r
                TreeNode<Distribution> child;// the new child to add or the child to follow through the tree\r
+                               \r
                //if the list to merge is empty finish the execution\r
                if (areaHierarchieList.isEmpty()) {\r
                        return;\r
@@ -115,7 +152,7 @@ public class DistributionTree extends Tree<Distribution>{
                        highestDistNode = new TreeNode<Distribution>(data);\r
                }\r
                if(highestDistNode.data.getModifyingText().isEmpty()){\r
-                       highestDistNode.data.addModifyingText("test", Language.ENGLISH());\r
+                       highestDistNode.data.putModifyingText(Language.ENGLISH(), "test");\r
                }\r
 \r
                if (root.getChildren().isEmpty() || !containsChild(root, highestDistNode)) {\r
@@ -135,12 +172,12 @@ public class DistributionTree extends Tree<Distribution>{
        \r
        private List<NamedArea> getAreaLevelPathList(NamedArea area, Set<NamedAreaLevel> omitLevels){\r
                List<NamedArea> result = new ArrayList<NamedArea>();\r
-               if (!omitLevels.contains(area.getLevel())){\r
+               if (omitLevels == null || !omitLevels.contains(area.getLevel())){\r
                        result.add(area);               \r
                }\r
                while (area.getPartOf() != null) {\r
                        area = area.getPartOf();\r
-                       if (!omitLevels.contains(area.getLevel())){\r
+                       if (omitLevels == null || !omitLevels.contains(area.getLevel())){\r
                                result.add(0, area);\r
                        }\r
                }\r