Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / FeatureNodeContainerTree.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.model;
11
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.Comparator;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18
19 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20 import eu.etaxonomy.cdm.model.description.DescriptionBase;
21 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
22 import eu.etaxonomy.cdm.model.description.Feature;
23 import eu.etaxonomy.cdm.model.description.FeatureNode;
24 import eu.etaxonomy.cdm.model.description.FeatureTree;
25 import eu.etaxonomy.cdm.model.description.TaxonDescription;
26
27 /**
28 * @author n.hoffmann
29 * @created Dec 2, 2010
30 * @version 1.0
31 */
32 public class FeatureNodeContainerTree {
33
34 /** Constant <code>comparator</code> */
35 // TODO we will do this with a ViewerSorter
36 public static Comparator<DescriptionElementBase> comparator = new Comparator<DescriptionElementBase>() {
37
38 @Override
39 public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
40 if (o1.equals(o2)){
41 return 0;
42 }
43 int result = DescriptionHelper.getLabel(o1).compareTo(DescriptionHelper.getLabel(o2));
44 if (result == 0){
45 return o1.getUuid().compareTo(o2.getUuid());
46 }
47 return result;
48 }
49 };
50
51 private Set<FeatureNodeContainer> featureNodeContainers = new HashSet<FeatureNodeContainer>();
52
53 private FeatureNodeContainer root;
54 private DescriptionBase description;
55 private FeatureTree featureTree;
56
57 public FeatureNodeContainerTree(DescriptionBase description, FeatureTree featureTree){
58 this.description = description;
59 this.featureTree = featureTree;
60
61 root = new FeatureNodeContainer(this);
62
63 buildTree();
64 }
65
66 /**
67 * Traverses the given {@link FeatureNode} and computes a FeatureNodeContainer branch if the
68 * given {@link TaxonDescription} has elements for the given feature node or any of its children
69 * or null if there are no elements.
70 */
71 public void buildTree(){
72 List<FeatureNode> children = featureTree.getRootChildren();
73
74 // if(description.getElements().isEmpty()){
75 // return;
76 // }
77
78 for(FeatureNode featureNode : children){
79 root.findLeaves(featureNode);
80 }
81 }
82
83 /**
84 * <p>getFeatureNodeContainerForDescriptionElement</p>
85 *
86 * @param descriptionElement a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
87 * @return a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
88 */
89 public FeatureNodeContainer getFeatureNodeContainerForDescriptionElement (DescriptionElementBase descriptionElement) {
90 List<FeatureNodeContainer> leafs = root.getLeafs();
91
92 for(FeatureNodeContainer container : leafs){
93 if(container.getDescriptionElements().contains(descriptionElement)){
94 return container;
95 }
96 }
97 return null;
98 }
99
100 /**
101 * Returns all elements contained in the given {@link DescriptionBase description} that have the given {@link Feature feature}.
102 *
103 * @param description
104 * @param feature
105 * @return a list of description elements with the given feature alphabetically sorted by {@link DescriptionHelper#getLabel(Object)}
106 */
107 public List<DescriptionElementBase> getDescriptionsElementsForFeature(Feature feature) {
108 List<DescriptionElementBase> featureElements = new ArrayList<DescriptionElementBase>();
109
110 Set<DescriptionElementBase> elements = description.getElements();
111
112 if (elements != null) {
113 for (DescriptionElementBase element : elements) {
114 Feature elementFeature = HibernateProxyHelper.deproxy(element.getFeature());
115
116 if (feature.equals(elementFeature)) {
117 featureElements.add(element);
118 }
119 }
120 }
121
122 if(featureElements.size() != 0){
123 Collections.sort(featureElements, comparator);
124 }
125
126 return featureElements;
127 }
128
129
130 public FeatureNodeContainer getFeatureNodeContainer(Feature feature) {
131 List<FeatureNodeContainer> leafs = root.getLeafs();
132
133 for(FeatureNodeContainer container : leafs){
134 if(container.getFeature().equals(feature)){
135 return container;
136 }
137 }
138 return null;
139 }
140
141 public FeatureNodeContainer getFeatureNodeContainer(FeatureNode featureNode){
142 List<FeatureNodeContainer> leafs = root.getLeafs();
143
144 for(FeatureNodeContainer container : leafs){
145 if(container.getFeatureNode().equals(featureNode)){
146 return container;
147 }
148 }
149 return null;
150 }
151
152 public void addContainer(FeatureNodeContainer container){
153 featureNodeContainers.add(container);
154 }
155
156 /**
157 *
158 */
159 public void removeContainer(FeatureNodeContainer container) {
160 featureNodeContainers.remove(container);
161 }
162
163 public FeatureNodeContainer getRoot() {
164 return root;
165 }
166
167 public DescriptionBase getDescription() {
168 return description;
169 }
170
171 public FeatureTree getFeatureTree() {
172 return featureTree;
173 }
174
175 }