4 package eu
.etaxonomy
.taxeditor
.editor
.descriptiontree
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
8 import java
.util
.HashSet
;
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
;
22 public class DescriptiveInformationProvider
implements
23 IDescriptiveInformationProvider
{
26 * @see eu.etaxonomy.taxeditor.editor.descriptionnew.IDescriptiveInformationProvider#getDescriptionElements(eu.etaxonomy.cdm.model.description.TaxonDescription, eu.etaxonomy.cdm.model.description.Feature)
28 public List
<DescriptionElementBase
> getDescriptionElements(
29 TaxonDescription description
, Feature feature
) {
30 List
<DescriptionElementBase
> featureElements
= new ArrayList
<DescriptionElementBase
>();
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
);
40 return featureElements
;
44 * TaxonDescriptionFeature objects are cached so that they pass description tree viewer's equality test.
46 private Map
<TaxonDescription
, Map
<Feature
, TaxonDescriptionFeature
>> descriptionFeatureCache
= new HashMap
<TaxonDescription
, Map
<Feature
, TaxonDescriptionFeature
>>();
49 * @see eu.etaxonomy.taxeditor.editor.descriptionnew.IDescriptiveInformationProvider#getDescriptionFeatures(eu.etaxonomy.cdm.model.description.TaxonDescription)
51 public List
<TaxonDescriptionFeature
> getDescriptionFeatures(TaxonDescription description
) {
53 Map
<Feature
, TaxonDescriptionFeature
> featureCache
= getFeatureCache(description
);
55 Set
<Feature
> featuresTemp
= new HashSet
<Feature
>();
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
);
63 // Is there already an entry in map for this Feature?
64 if (!(featureCache
.containsKey(feature
))) {
65 featureCache
.put(feature
, new TaxonDescriptionFeature(description
, feature
));
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
);
78 for (Feature feature
: removeFeatures
) {
79 featureCache
.remove(feature
);
82 return new ArrayList
<TaxonDescriptionFeature
>(featureCache
.values());
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
);
95 * @see eu.etaxonomy.taxeditor.editor.descriptiontree.IDescriptiveInformationProvider#getDescriptionFeature(eu.etaxonomy.cdm.model.description.DescriptionElementBase)
97 public TaxonDescriptionFeature
getDescriptionFeature(DescriptionElementBase element
) {
99 TaxonDescription description
= (TaxonDescription
) element
.getInDescription();
100 Feature feature
= HibernateProxyHelper
.deproxy(element
.getFeature(), Feature
.class);
102 Map
<Feature
, TaxonDescriptionFeature
> featureCache
= getFeatureCache(description
);
103 TaxonDescriptionFeature tdf
= featureCache
.get(feature
);
105 tdf
= new TaxonDescriptionFeature(description
, feature
);
106 featureCache
.put(feature
, tdf
);