1 package eu
.etaxonomy
.cdm
.api
.service
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collections
;
5 import java
.util
.Iterator
;
9 import eu
.etaxonomy
.cdm
.common
.Tree
;
10 import eu
.etaxonomy
.cdm
.common
.TreeNode
;
11 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
12 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
13 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
14 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
16 public class DistributionTree
extends Tree
<Distribution
>{
18 public DistributionTree(){
19 NamedArea area
= new NamedArea();
20 Distribution data
= Distribution
.NewInstance();
22 data
.putModifyingText(Language
.ENGLISH(), "test");
23 TreeNode
<Distribution
> rootElement
= new TreeNode
<Distribution
>();
24 List
<TreeNode
<Distribution
>> children
= new ArrayList
<TreeNode
<Distribution
>>();
26 rootElement
.setData(data
);
27 rootElement
.setChildren(children
);
28 setRootElement(rootElement
);
31 public boolean containsChild(TreeNode
<Distribution
> root
, TreeNode
<Distribution
> treeNode
){
32 boolean result
= false;
33 Iterator
<TreeNode
<Distribution
>> it
= root
.getChildren().iterator();
34 while (it
.hasNext() && !result
) {
35 TreeNode
<Distribution
> node
= (TreeNode
<Distribution
>) it
.next();
36 if (node
.getData().equalsForTree(treeNode
.getData())) {
41 while (!result && it.hasNext()) {
42 if (it.next().data.equalsForTree(treeNode.data)){
50 public TreeNode
<Distribution
> getChild(TreeNode
<Distribution
> root
, TreeNode
<Distribution
> TreeNode
) {
51 boolean found
= false;
52 TreeNode
<Distribution
> result
= null;
53 Iterator
<TreeNode
<Distribution
>> it
= root
.children
.iterator();
54 while (!found
&& it
.hasNext()) {
55 result
= (TreeNode
<Distribution
>) it
.next();
56 if (result
.data
.equalsForTree(TreeNode
.data
)){
62 throw new Exception("The node was not found in among children and that is a precondition of getChild(node) method");
63 } catch (Exception e
) {
70 private List
<Distribution
> orderDistributionsByLevel(List
<Distribution
> distList
){
72 int length
= distList
.size()-1;
74 List
<Distribution
> orderedList
= new ArrayList
<Distribution
>(length
);
75 orderedList
.addAll(distList
);
77 for (int i
= 0; i
< length
&& flag
; i
++) {
79 for (int j
= 0; j
< length
-1; j
++) {
80 String level1
= orderedList
.get(j
).getArea().getLevel().toString();
81 String level2
= orderedList
.get(j
+1).getArea().getLevel().toString();
82 //if level from j+1 is greater than level from j
85 dist
= orderedList
.get(j
);
86 orderedList
.set(j
, orderedList
.get(j
+1));
87 orderedList
.set(j
+1, dist
);
95 public void merge(List
<Distribution
> distList
, Set
<NamedAreaLevel
> omitLevels
){
96 List
<Distribution
> orderedDistList
= orderDistributionsByLevel(distList
);
98 for (Distribution distribution
: orderedDistList
) {
99 List
<NamedArea
> levelList
=
100 this.getAreaLevelPathList(distribution
.getArea(), omitLevels
);
101 mergeAux(distribution
, distribution
.getArea().getLevel(), levelList
, this.getRootElement());
105 public void sortChildren(){
106 sortChildrenAux(this.getRootElement());
109 private void sortChildrenAux(TreeNode
<Distribution
> treeNode
){
110 DistributionNodeComparator comp
= new DistributionNodeComparator();
111 if (treeNode
.children
== null) {
112 //nothing => stop condition
115 Collections
.sort(treeNode
.children
, comp
);
116 for (TreeNode
<Distribution
> child
: treeNode
.children
) {
117 sortChildrenAux(child
);
122 private void mergeAux(Distribution distribution
,
123 NamedAreaLevel level
,
124 List
<NamedArea
> areaHierarchieList
,
125 TreeNode
<Distribution
> root
){
126 TreeNode
<Distribution
> highestDistNode
;
127 TreeNode
<Distribution
> child
;// the new child to add or the child to follow through the tree
129 //if the list to merge is empty finish the execution
130 if (areaHierarchieList
.isEmpty()) {
133 //getting the highest area and inserting it into the tree
134 NamedArea highestArea
= areaHierarchieList
.get(0);
135 //NamedAreaLevel highestAreaLevel = (NamedAreaLevel) HibernateProxyHelper.deproxy(highestArea.getLevel());
136 //NamedAreaLevel currentLevel = (NamedAreaLevel) HibernateProxyHelper.deproxy(level);
137 //if (highestAreaLevel.compareTo(currentLevel) == 0){//if distribution.status is relevant
139 if (highestArea
.getLevel().getLabel().compareTo(level
.getLabel()) == 0){
140 highestDistNode
= new TreeNode
<Distribution
>(distribution
);//distribution.area comes from proxy!!!!
141 }else{ //if distribution.status is not relevant
142 Distribution data
= Distribution
.NewInstance(highestArea
, null);
143 highestDistNode
= new TreeNode
<Distribution
>(data
);
145 if(highestDistNode
.data
.getModifyingText().isEmpty()){
146 highestDistNode
.data
.putModifyingText(Language
.ENGLISH(), "test");
149 if (root
.getChildren().isEmpty() || !containsChild(root
, highestDistNode
)) {
150 //if the highest level is not on the depth-1 of the tree we add it.
151 //child = highestDistNode;
152 child
= new TreeNode
<Distribution
>(highestDistNode
.data
);
153 root
.addChild(child
);//child.getData().getArea().getUuid().toString().equals("8cfc1722-e1e8-49d3-95a7-9879de6de490");
155 //if the deepth-1 of the tree contains the highest area level
156 //get the subtree or create it in order to continuing merging
157 child
= getChild(root
,highestDistNode
);
159 //continue merging with the next highest area of the list.
160 List
<NamedArea
> newList
= areaHierarchieList
.subList(1, areaHierarchieList
.size());
161 mergeAux(distribution
, level
, newList
, child
);
164 private List
<NamedArea
> getAreaLevelPathList(NamedArea area
, Set
<NamedAreaLevel
> omitLevels
){
165 List
<NamedArea
> result
= new ArrayList
<NamedArea
>();
166 if (omitLevels
== null || !omitLevels
.contains(area
.getLevel())){
169 while (area
.getPartOf() != null) {
170 area
= area
.getPartOf();
171 if (omitLevels
== null || !omitLevels
.contains(area
.getLevel())){