47650d4e1103c934781539764713f91a62437cf3
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / TermManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.store;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.UUID;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.ui.IMemento;
22
23 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 import eu.etaxonomy.cdm.model.common.TermBase;
25 import eu.etaxonomy.cdm.model.common.TermType;
26 import eu.etaxonomy.cdm.model.common.TermVocabulary;
27 import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
28 import eu.etaxonomy.taxeditor.model.TaxonRelationshipTypeInverseContainer;
29
30 /**
31 * @author n.hoffmann
32 * @created Dec 7, 2010
33 * @version 1.0
34 */
35 public class TermManager extends ContextListenerAdapter{
36
37 private static final String TERMS = "terms";
38 private static final String TERM = "term";
39 private static final String HIDE = "hide";
40
41 private static final String CLASS = "class";
42 private static final String UUID_STRING = "uuid";
43
44 private final Map<String, Collection<UUID>> hiddenTermsMap = new HashMap<String, Collection<UUID>>();
45
46 public TermManager(){
47 CdmStore.getContextManager().addContextListener(this);
48 }
49
50 /**
51 *
52 * @param clazz
53 * @return
54 */
55 public List<DefinedTermBase<?>> getPreferredTerms(TermType termType){
56 return getFilteredTerms(TermStore.getTerms(termType, null));
57 }
58
59 /**
60 *
61 * @param clazz
62 * @return
63 */
64 public <T extends DefinedTermBase> List<T> getPreferredTerms(TermVocabulary<T> termVocabulary){
65 return getFilteredTerms(new ArrayList<T>(TermStore.getTerms(termVocabulary, null)));
66 }
67
68 /**
69 *
70 * @param clazz
71 * @return
72 */
73 public <T extends DefinedTermBase> List<T> getPreferredTerms(Class<T> clazz){
74 return getFilteredTerms(TermStore.getTerms(clazz));
75 }
76
77 @Override
78 public void contextStart(IMemento memento, IProgressMonitor monitor) {
79 // read preferred terms from memento into an in memory representation
80 if(memento == null){
81 return;
82 }
83
84 IMemento terms = memento.getChild(TERMS);
85
86 if(terms == null){
87 return;
88 }
89
90 for(IMemento term : terms.getChildren(TERM)){
91 String typeName = term.getString(CLASS);
92 Collection<UUID> hiddenTermUuids = new ArrayList<UUID>();
93 for(IMemento hide : term.getChildren(HIDE)){
94 String uuidString = hide.getString(UUID_STRING);
95 UUID uuid = UUID.fromString(uuidString);
96 hiddenTermUuids.add(uuid);
97 }
98 hiddenTermsMap.put(typeName, hiddenTermUuids);
99 }
100 }
101
102 @Override
103 public void contextStop(IMemento memento, IProgressMonitor monitor) {
104 saveTerms(memento, monitor);
105 }
106
107 @Override
108 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
109 saveTerms(memento, monitor);
110 }
111
112 private void saveTerms(IMemento memento, IProgressMonitor monitor){
113 if(memento == null) {
114 return;
115 }
116
117 IMemento terms = memento.createChild(TERMS);
118 for (String preferredTerm : hiddenTermsMap.keySet()){
119 IMemento term = terms.createChild(TERM);
120 term.putString(CLASS, preferredTerm);
121 for(UUID uuid : hiddenTermsMap.get(preferredTerm)){
122 IMemento hide = term.createChild(HIDE);
123 hide.putString(UUID_STRING, uuid.toString());
124 }
125 }
126 }
127
128 /**
129 * Generic method to get term preferences for a term vocabulary
130 *
131 * @param initialTerms a {@link java.util.List} object.
132 * @return a {@link java.util.List} object.
133 */
134 public <T extends DefinedTermBase> List<T> getFilteredTerms(List<T> initialTerms){
135
136 List<T> filteredTerms = new ArrayList<T>();
137
138 String typeName = getTermClass(initialTerms);
139 Collection<UUID> hiddenTermUuids = hiddenTermsMap.get(typeName);
140
141
142 if(hiddenTermUuids == null){
143 return initialTerms;
144 }
145
146 for (T term : initialTerms){
147 if (term instanceof TaxonRelationshipTypeInverseContainer){
148 if(! hiddenTermUuids.contains(((TaxonRelationshipTypeInverseContainer) term).getType().getUuid())){
149 T type = (T)((TaxonRelationshipTypeInverseContainer) term).getType();
150 filteredTerms.add(type);
151
152 }
153 }
154 if(! hiddenTermUuids.contains(term.getUuid())){
155 filteredTerms.add(term);
156 }
157 }
158
159 return filteredTerms;
160 }
161
162 private <T extends DefinedTermBase> String getTermClass(Collection<T> initialTerms){
163
164 String result = null;
165 if(!initialTerms.isEmpty()){
166
167 //TODO: there should be a more generic solution!!
168 T entity = initialTerms.iterator().next();
169 if (entity instanceof TaxonRelationshipTypeInverseContainer){
170 result = ((TaxonRelationshipTypeInverseContainer) entity).getType().getClass().getName();
171 } else {
172 result = entity.getClass().getName();
173 }
174 return result;
175 }
176 return null;
177 }
178
179 // /**
180 // * <p>getPreferredSpecimenTypeDesignationStatus</p>
181 // *
182 // * @return a {@link java.util.List} object.
183 // */
184 // public List<SpecimenTypeDesignationStatus> getPreferredSpecimenTypeDesignationStatus() { return getFilteredTerms(TermStore.getSpecimenTypeDesignationStatus());}
185 //
186 // /**
187 // * <p>getPreferredNameTypeDesignationStatus</p>
188 // *
189 // * @return a {@link java.util.List} object.
190 // */
191 // public List<NameTypeDesignationStatus> getPreferredNameTypeDesignationStatus() { return getFilteredTerms(TermStore.getNameTypeDesignationStatus()); }
192 //
193 // /**
194 // * <p>getPreferredTaxonRelationshipTypes</p>
195 // *
196 // * @return a {@link java.util.List} object.
197 // */
198 // public List<TaxonRelationshipType> getPreferredTaxonRelationshipTypes() { return getFilteredTerms(TermStore.getTaxonRelationshipTypes());}
199 //
200 // private static List<TaxonRelationshipType> excludeTaxonRelationshipTypes = Arrays.asList(new TaxonRelationshipType[]{
201 // TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
202 // TaxonRelationshipType.MISAPPLIED_NAME_FOR(),
203 // TaxonRelationshipType.ALL_RELATIONSHIPS()
204 // });
205 //
206 // /**
207 // * Please note that the {@link TaxonRelationshipType}s TAXONOMICALLY_INCLUDED_IN, MISAPPLIED_NAME_FOR and ALL_RELATIONSHIPS
208 // * are filtered out as they are most likely not needed. If you do need them please refactor this method to your needs.
209 // *
210 // * @return a list of {@link TaxonRelationshipTypeInverseContainer} elements
211 // */
212 // public List<TaxonRelationshipTypeInverseContainer> getPreferredRelationshipTypesWithInverses(){
213 // List<TaxonRelationshipTypeInverseContainer> relationshipTypeInverseContainers = new ArrayList<TaxonRelationshipTypeInverseContainer>();
214 //
215 // List<TaxonRelationshipType> relationshipTypes = getPreferredTaxonRelationshipTypes();
216 //
217 // relationshipTypes.removeAll(excludeTaxonRelationshipTypes);
218 //
219 // for (TaxonRelationshipType relationshipType : relationshipTypes){
220 // if(!relationshipType.isSymmetric()){
221 // TaxonRelationshipTypeInverseContainer inverseContainer = new TaxonRelationshipTypeInverseContainer(relationshipType, true);
222 // relationshipTypeInverseContainers.add(inverseContainer);
223 // }
224 // TaxonRelationshipTypeInverseContainer container = new TaxonRelationshipTypeInverseContainer(relationshipType, false);
225 // relationshipTypeInverseContainers.add(container);
226 // }
227 //
228 // return relationshipTypeInverseContainers;
229 // }
230
231
232
233
234 //
235 // /**
236 // * <p>getPreferredFeatures</p>
237 // *
238 // * @return a {@link java.util.List} object.
239 // */
240 // public List<Feature> getPreferredFeatures() { return getFilteredTerms(TermStore.getFeatures());}
241 //
242 // /**
243 // * <p>getPreferredRanks</p>
244 // *
245 // * @return a {@link java.util.List} object.
246 // */
247 // public List<Rank> getPreferredRanks() { return getFilteredTerms(TermStore.getRanks());}
248 //
249 // /**
250 // * <p>getPreferredPresenceAbsenceTerms</p>
251 // *
252 // * @return a {@link java.util.List} object.
253 // */
254 // public List<PresenceAbsenceTermBase> getPreferredPresenceAbsenceTerms(){ return getFilteredTerms(TermStore.getPresenceAbsenceTerms());}
255 //
256 // /**
257 // * <p>getPreferredNomenclaturalStatusTypes</p>
258 // *
259 // * @return a {@link java.util.List} object.
260 // */
261 // public List<NomenclaturalStatusType> getPreferredNomenclaturalStatusTypes(){ return getFilteredTerms(TermStore.getNomenclaturalStatusTypes());}
262 //
263 // /**
264 // * <p>getPreferredNameRelationshipTypes</p>
265 // *
266 // * @return a {@link java.util.List} object.
267 // */
268 // public List<NameRelationshipType> getPreferredNameRelationshipTypes(){ return getFilteredTerms(TermStore.getNameRelationshipTypes());}
269 //
270 // /**
271 // * <p>getPreferredLanguages</p>
272 // *
273 // * @return a {@link java.util.List} object.
274 // */
275 // public List<Language> getPreferredLanguages() { return getFilteredTerms(TermStore.getLanguages()); }
276 //
277 // /**
278 // * <p>getPreferredMarkerTypes</p>
279 // *
280 // * @return a {@link java.util.List} object.
281 // */
282 // public List<MarkerType> getPreferredMarkerTypes() { return getFilteredTerms(TermStore.getNonTechnicalMarkerTypes()); }
283 //
284 // /**
285 // * <p>getPreferredExtensionTypes</p>
286 // *
287 // * @return a {@link java.util.List} object.
288 // */
289 // public List<ExtensionType> getPreferredExtensionTypes() { return getFilteredTerms(TermStore.getExtensionTypes()); }
290 //
291 // /**
292 // * <p>getPreferredRightsTypes</p>
293 // *
294 // * @return a {@link java.util.List} object.
295 // */
296 // public List<RightsType> getPreferredRightsTypes() { return getFilteredTerms(TermStore.getRightsTypes());}
297 //
298 // /**
299 // * <p>getPreferredNamedAreaTypes</p>
300 // *
301 // * @return a {@link java.util.List} object.
302 // */
303 // public List<NamedAreaType> getPreferredNamedAreaTypes() { return getFilteredTerms(TermStore.getNamedAreaTypes()); }
304 //
305 // /**
306 // * <p>getPreferredNamedAreaTypes</p>
307 // *
308 // * @return a {@link java.util.List} object.
309 // */
310 // public List<NamedAreaLevel> getPreferredNamedAreaLevels() { return getFilteredTerms(TermStore.getNamedAreaLevels()); }
311 //
312 // /**
313 // * <p>getPreferredAnnotationTypes</p>
314 // *
315 // * @return a {@link java.util.List} object.
316 // */
317 // public List<AnnotationType> getPreferredAnnotationTypes() { return getFilteredTerms(TermStore.getAnnotationTypes()); }
318 //
319 // /**
320 // * <p>getPreferredStages</p>
321 // *
322 // * @return a {@link java.util.List} object.
323 // */
324 // public List<Stage> getPreferredStages() { return getFilteredTerms(TermStore.getStages()); }
325 //
326 // /**
327 // * <p>getPreferredPreservationMethods</p>
328 // *
329 // * @return a {@link java.util.List} object.
330 // */
331 // public List<PreservationMethod> getPreferredPreservationMethods() { return getFilteredTerms(TermStore.getPreservationMethods()); }
332 //
333 // /**
334 // * <p>getPreferredMeasurementUnits</p>
335 // *
336 // * @return a {@link java.util.List} object.
337 // */
338 // public List<MeasurementUnit> getPreferredMeasurementUnits() { return getFilteredTerms(TermStore.getMeasurementUnits()); }
339 //
340 // /**
341 // * <p>getPreferredStates</p>
342 // *
343 // * @return a {@link java.util.List} object.
344 // */
345 // public List<State> getPreferredStates() { return getFilteredTerms(TermStore.getStates()); }
346 //
347 // /**
348 // * <p>getPreferredModifiers</p>
349 // *
350 // * @return a {@link java.util.List} object.
351 // */
352 // public List<Modifier> getPreferredModifiers() { return getFilteredTerms(TermStore.getModifiers()); }
353 //
354 // /**
355 // * <p>getPreferredStatisticalMeasures</p>
356 // *
357 // * @return a {@link java.util.List} object.
358 // */
359 // public List<StatisticalMeasure> getPreferredStatisticalMeasures() { return getFilteredTerms(TermStore.getStatisticalMeasures()); }
360 //
361 // /**
362 // * <p>getPreferredScopes</p>
363 // *
364 // * @return a {@link java.util.List} object.
365 // */
366 // public List<Scope> getPreferredScopes() { return getFilteredTerms(TermStore.getScopes()); }
367 //
368 // /**
369 // * <p>getPreferredDeterminationModifiers</p>
370 // *
371 // * @return a {@link java.util.List} object.
372 // */
373 // public List<DeterminationModifier> getPreferredDeterminationModifiers() { return getFilteredTerms(TermStore.getDeterminationModifiers()); }
374 //
375 /**
376 * Generic method to set term preferences
377 *
378 * @param preferredTerms a {@link java.util.List} object.
379 * @param initialTerms a {@link java.util.List} object.
380 * @param <T> a T object.
381 */
382 public <T extends DefinedTermBase> void setPreferredTerms(Collection<T> preferredTerms, Collection<T> initialTerms){
383 String typeName = getTermClass(initialTerms);
384
385 Collection<UUID> undesiredTermUuids = new ArrayList<UUID>();
386
387 for(TermBase term : initialTerms){
388 if(! preferredTerms.contains(term)){
389 undesiredTermUuids.add(term.getUuid());
390 }
391 }
392
393 hiddenTermsMap.put(typeName, undesiredTermUuids);
394 }
395 }