fixes #1343
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiontree / DescriptiveInformationProvider.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.descriptiontree;
5
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
14 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
15 import eu.etaxonomy.cdm.model.description.Feature;
16 import eu.etaxonomy.cdm.model.description.TaxonDescription;
17
18 /**
19 * @author p.ciardelli
20 *
21 */
22 public class DescriptiveInformationProvider implements
23 IDescriptiveInformationProvider {
24
25 /* (non-Javadoc)
26 * @see eu.etaxonomy.taxeditor.editor.descriptionnew.IDescriptiveInformationProvider#getDescriptionElements(eu.etaxonomy.cdm.model.description.TaxonDescription, eu.etaxonomy.cdm.model.description.Feature)
27 */
28 public List<DescriptionElementBase> getDescriptionElements(
29 TaxonDescription description, Feature feature) {
30 List<DescriptionElementBase> featureElements = new ArrayList<DescriptionElementBase>();
31
32 Set<DescriptionElementBase> elements = description.getElements();
33 if (elements != null) {
34 for (DescriptionElementBase element : elements) {
35 if (element.getFeature().equals(feature)) {
36 featureElements.add(element);
37 }
38 }
39 }
40 return featureElements;
41 }
42
43 /**
44 * TaxonDescriptionFeature objects are cached so that they pass description tree viewer's equality test.
45 */
46 private Map<TaxonDescription, Map<Feature, TaxonDescriptionFeature>> descriptionFeatureCache = new HashMap<TaxonDescription, Map<Feature, TaxonDescriptionFeature>>();
47
48 /* (non-Javadoc)
49 * @see eu.etaxonomy.taxeditor.editor.descriptionnew.IDescriptiveInformationProvider#getDescriptionFeatures(eu.etaxonomy.cdm.model.description.TaxonDescription)
50 */
51 public List<TaxonDescriptionFeature> getDescriptionFeatures(TaxonDescription description) {
52
53 Map<Feature, TaxonDescriptionFeature> featureCache = getFeatureCache(description);
54
55 Set<Feature> featuresTemp = new HashSet<Feature>();
56
57 Set<DescriptionElementBase> elements = description.getElements();
58 if (elements != null) {
59 for (DescriptionElementBase element : elements) {
60 Feature feature = HibernateProxyHelper.deproxy(element.getFeature(), Feature.class);
61 featuresTemp.add(feature);
62
63 // Is there already an entry in map for this Feature?
64 if (!(featureCache.containsKey(feature))) {
65 featureCache.put(feature, new TaxonDescriptionFeature(description, feature));
66 }
67 }
68 }
69
70 // Check whether any features need to be removed, i.e. their elements were deleted
71 Set<Feature> keys = featureCache.keySet();
72 HashSet<Feature> removeFeatures = new HashSet<Feature>();
73 for (Feature feature : keys) {
74 if (!featuresTemp.contains(feature)) {
75 removeFeatures.add(feature);
76 }
77 }
78 for (Feature feature : removeFeatures) {
79 featureCache.remove(feature);
80 }
81
82 return new ArrayList<TaxonDescriptionFeature>(featureCache.values());
83 }
84
85 private Map<Feature, TaxonDescriptionFeature> getFeatureCache(TaxonDescription description) {
86 Map<Feature, TaxonDescriptionFeature> featureCache = descriptionFeatureCache.get(description);
87 if (featureCache == null) {
88 featureCache = new HashMap<Feature, TaxonDescriptionFeature>();
89 descriptionFeatureCache.put(description, featureCache);
90 }
91 return featureCache;
92 }
93
94 /* (non-Javadoc)
95 * @see eu.etaxonomy.taxeditor.editor.descriptiontree.IDescriptiveInformationProvider#getDescriptionFeature(eu.etaxonomy.cdm.model.description.DescriptionElementBase)
96 */
97 public TaxonDescriptionFeature getDescriptionFeature(DescriptionElementBase element) {
98
99 TaxonDescription description = (TaxonDescription) element.getInDescription();
100 Feature feature = HibernateProxyHelper.deproxy(element.getFeature(), Feature.class);
101
102 Map<Feature, TaxonDescriptionFeature> featureCache = getFeatureCache(description);
103 TaxonDescriptionFeature tdf = featureCache.get(feature);
104 if (tdf == null) {
105 tdf = new TaxonDescriptionFeature(description, feature);
106 featureCache.put(feature, tdf);
107 }
108 return tdf;
109 }
110 }