2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.store
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
20 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
22 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
23 import eu
.etaxonomy
.taxeditor
.model
.TaxonRelationshipTypeInverseContainer
;
24 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
28 * @created Dec 7, 2010
31 public class TermManager
{
33 private static final String TERMS
= "terms";
34 private static final String TERM
= "term";
35 private static final String HIDE
= "hide";
37 private static final String CLASS
= "class";
38 private static final String UUID_STRING
= "uuid";
41 private Map
<Object
, List
> cachedTermMap
= new HashMap
<>();
51 public <T
extends DefinedTermBase
> List
<T
> getPreferredTerms(TermType termType
){
52 List terms
= cachedTermMap
.get(termType
);
54 terms
= getFilteredTerms(TermStore
.<T
>getTerms(termType
, null));
55 cachedTermMap
.put(termType
, terms
);
65 public <T
extends DefinedTermBase
> List
<T
> getPreferredTerms(TermVocabulary
<T
> termVocabulary
){
66 List terms
= cachedTermMap
.get(termVocabulary
);
68 terms
= getFilteredTerms(new ArrayList
<T
>(TermStore
.getTerms(termVocabulary
, null)));
69 cachedTermMap
.put(termVocabulary
, terms
);
79 public <T
extends DefinedTermBase
> List
<T
> getPreferredTerms(Class
<T
> clazz
){
80 List terms
= cachedTermMap
.get(clazz
);
82 terms
= getFilteredTerms(TermStore
.getTerms(clazz
));
83 cachedTermMap
.put(clazz
, terms
);
90 * Generic method to get term preferences for a term vocabulary
92 * @param initialTerms a {@link java.util.List} object.
93 * @return a {@link java.util.List} object.
95 public <T
extends DefinedTermBase
> List
<T
> getFilteredTerms(List
<T
> initialTerms
){
97 List
<T
> filteredTerms
= new ArrayList
<T
>();
99 for (T term
: initialTerms
){
101 if (term
instanceof TaxonRelationshipTypeInverseContainer
){
102 if(!PreferencesUtil
.getBooleanValue(getPrefName(term
))){
103 T type
= (T
)((TaxonRelationshipTypeInverseContainer
) term
).getType();
104 filteredTerms
.add(type
);
109 if(!PreferencesUtil
.getBooleanValue(getPrefName(term
))){
110 filteredTerms
.add(term
);
114 return filteredTerms
;
117 private <T
extends TermBase
> String
getPrefName(T term
) {
118 return term
.getTermType()!=null?term
.getTermType().toString()+term
.getUuid().toString():""+term
.getUuid().toString();
122 private <T
extends DefinedTermBase
> String
getTermClass(Collection
<T
> initialTerms
){
124 String result
= null;
125 if(!initialTerms
.isEmpty()){
127 //TODO: there should be a more generic solution!!
128 T entity
= initialTerms
.iterator().next();
129 if (entity
instanceof TaxonRelationshipTypeInverseContainer
){
130 result
= ((TaxonRelationshipTypeInverseContainer
) entity
).getType().getClass().getName();
132 result
= entity
.getClass().getName();
140 * Generic method to set term preferences
142 * @param preferredTerms a {@link java.util.List} object.
143 * @param initialTerms a {@link java.util.List} object.
144 * @param <T> a T object.
146 public <T
extends DefinedTermBase
> void setPreferredTerms(Collection
<T
> preferredTerms
, Collection
<T
> initialTerms
){
147 String typeName
= getTermClass(initialTerms
);
149 Collection
<UUID
> undesiredTermUuids
= new ArrayList
<UUID
>();
151 for(TermBase term
: initialTerms
){
152 if(! preferredTerms
.contains(term
)){
153 undesiredTermUuids
.add(term
.getUuid());
154 PreferencesUtil
.setBooleanValue(getPrefName(term
), true);
156 PreferencesUtil
.setBooleanValue(getPrefName(term
), false);
166 public void clearTermMapForTermType(TermType termType
){
167 cachedTermMap
.remove(termType
);
171 * Generic method to get term preferences for a term vocabulary
173 * @param initialTerms a {@link java.util.List} object.
174 * @return a {@link java.util.List} object.
176 public <T
extends DefinedTermBase
> List
<T
> getTerms(List
<UUID
> uuidList
, Class clazz
){
178 List
<T
> filteredTerms
= new ArrayList
<T
>();
179 List
<T
> terms
= TermStore
.getTerms(clazz
);
180 for (T term
: terms
){
181 if(uuidList
.contains(term
.getUuid())){
182 filteredTerms
.add(term
);
186 return filteredTerms
;