had to rename the packages to make them compliant with buckminster
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / TermStore.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.Collections;
15 import java.util.Comparator;
16 import java.util.List;
17
18 import eu.etaxonomy.cdm.api.service.ITermService;
19 import eu.etaxonomy.cdm.api.service.IVocabularyService;
20 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
21 import eu.etaxonomy.cdm.model.common.AnnotationType;
22 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
23 import eu.etaxonomy.cdm.model.common.ExtensionType;
24 import eu.etaxonomy.cdm.model.common.Language;
25 import eu.etaxonomy.cdm.model.common.MarkerType;
26 import eu.etaxonomy.cdm.model.common.OrderedTermBase;
27 import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
28 import eu.etaxonomy.cdm.model.common.TermVocabulary;
29 import eu.etaxonomy.cdm.model.common.VocabularyEnum;
30 import eu.etaxonomy.cdm.model.description.AbsenceTerm;
31 import eu.etaxonomy.cdm.model.description.Feature;
32 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
33 import eu.etaxonomy.cdm.model.description.Modifier;
34 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
35 import eu.etaxonomy.cdm.model.description.PresenceTerm;
36 import eu.etaxonomy.cdm.model.description.Scope;
37 import eu.etaxonomy.cdm.model.description.Stage;
38 import eu.etaxonomy.cdm.model.description.State;
39 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
40 import eu.etaxonomy.cdm.model.location.NamedAreaType;
41 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
42 import eu.etaxonomy.cdm.model.media.RightsTerm;
43 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
44 import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
45 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
46 import eu.etaxonomy.cdm.model.name.Rank;
47 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
48 import eu.etaxonomy.cdm.model.occurrence.DeterminationModifier;
49 import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
50 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
51 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
52
53 /**
54 * All terms may be accessed through this store.
55 * Note: This is for internal use. When using vocabularies in the UI, try to use the preferred terms from
56 * PreferenceUtil
57 *
58 * @author n.hoffmann
59 * @created 25.06.2009
60 * @version 1.0
61 */
62 public class TermStore {
63
64 /**
65 * <p>getSpecimenTypeDesignationStatus</p>
66 *
67 * @return a {@link java.util.List} object.
68 */
69 public static List<SpecimenTypeDesignationStatus> getSpecimenTypeDesignationStatus() {
70 return getTerms(SpecimenTypeDesignationStatus.class);
71 }
72
73
74 /**
75 * <p>getNameTypeDesignationStatus</p>
76 *
77 * @return a {@link java.util.List} object.
78 */
79 public static List<NameTypeDesignationStatus> getNameTypeDesignationStatus() {
80 return getTerms(NameTypeDesignationStatus.class);
81 }
82
83 /**
84 * <p>getNamedAreaTypes</p>
85 *
86 * @return a {@link java.util.List} object.
87 */
88 public static List<NamedAreaType> getNamedAreaTypes(){
89 return getTerms(NamedAreaType.class);
90 }
91
92 /**
93 * <p>getAnnotationTypes</p>
94 *
95 * @return a {@link java.util.List} object.
96 */
97 public static List<AnnotationType> getAnnotationTypes(){
98 return getTerms(AnnotationType.class);
99 }
100
101 /**
102 * <p>getTaxonRelationshipTypes</p>
103 *
104 * @return a {@link java.util.List} object.
105 */
106 public static List<TaxonRelationshipType> getTaxonRelationshipTypes(){
107 return getTerms(TaxonRelationshipType.class);
108 }
109
110
111 /**
112 * <p>getLanguages</p>
113 *
114 * @return a {@link java.util.List} object.
115 */
116 public static List<Language> getLanguages(){
117 return getTerms(Language.class);
118 }
119
120 /**
121 * <p>getFeatures</p>
122 *
123 * @return a {@link java.util.List} object.
124 */
125 public static List<Feature> getFeatures() {
126 List<Feature> features = getTerms(Feature.class);
127 features.remove(Feature.IMAGE());
128 return features;
129
130 }
131
132 /**
133 * <p>getNameRelationshipTypes</p>
134 *
135 * @return a {@link java.util.List} object.
136 */
137 public static List<NameRelationshipType> getNameRelationshipTypes() {
138 return getTerms(NameRelationshipType.class);
139 }
140
141 /**
142 * <p>getNomenclaturalStatusTypes</p>
143 *
144 * @return a {@link java.util.List} object.
145 */
146 public static List<NomenclaturalStatusType> getNomenclaturalStatusTypes(){
147 return getTerms(NomenclaturalStatusType.class);
148 }
149
150
151 /**
152 * <p>getNonTechnicalMarkerTypes</p>
153 *
154 * @return a {@link java.util.List} object.
155 */
156 public static List<MarkerType> getNonTechnicalMarkerTypes() {
157 List<MarkerType> nonTechnicalMarkerTypes = new ArrayList<MarkerType>();
158 List<MarkerType> markerTypes = getTerms(MarkerType.class);
159
160 for (Object type : markerTypes) {
161 if (((MarkerType) type).isTechnical() == false) {
162 nonTechnicalMarkerTypes.add((MarkerType) type);
163 }
164 }
165
166 return nonTechnicalMarkerTypes;
167 }
168
169
170 /**
171 * <p>getExtensionTypes</p>
172 *
173 * @return a {@link java.util.List} object.
174 */
175 public static List<ExtensionType> getExtensionTypes() {
176 return getTerms(ExtensionType.class);
177 }
178
179 /**
180 * <p>getRightsTypes</p>
181 *
182 * @return a {@link java.util.List} object.
183 */
184 public static List<RightsTerm> getRightsTypes() {
185 return getTerms(RightsTerm.class);
186 }
187
188
189 /**
190 * Retrieve all ranks from data store.
191 *
192 * @return a {@link java.util.List} object.
193 */
194 public static List<Rank> getRanks(){
195 if(PreferencesUtil.getSortRanksHierarchichally()){
196 return getTerms(Rank.class, new Comparator<Rank>(){
197
198 @Override
199 public int compare(Rank o1, Rank o2) {
200 return o1.compareTo(o2);
201 }
202
203 });
204 }else{
205 return getTerms(Rank.class);
206 }
207 }
208
209
210 /**
211 * Retrieve all presence and absence terms from data store.
212 *
213 * @return a {@link java.util.List} object.
214 */
215 public static List<PresenceAbsenceTermBase> getPresenceAbsenceTerms() {
216 List presenceAbsenceTerms = getTerms(PresenceTerm.class);
217 presenceAbsenceTerms.addAll(getTerms(AbsenceTerm.class));
218
219 return presenceAbsenceTerms;
220 }
221
222 /**
223 * Retrieve all stages from data store.
224 *
225 * @return a {@link java.util.List} object.
226 */
227 public static List<Stage> getStages(){
228 return getTerms(Stage.class);
229 }
230
231 /**
232 * Retrieve all states from data store.
233 *
234 * @return a {@link java.util.List} object.
235 */
236 public static List<State> getStates() {
237 return getTerms(State.class);
238 }
239
240 /**
241 * Retrieve all preservation methods from data store.
242 *
243 * @return a {@link java.util.List} object.
244 */
245 public static List<PreservationMethod> getPreservationMethods(){
246 return getTerms(PreservationMethod.class);
247 }
248
249
250
251 /**
252 * <p>getMeasurementUnits</p>
253 *
254 * @return a {@link java.util.List} object.
255 */
256 public static List<MeasurementUnit> getMeasurementUnits() {
257 return getTerms(MeasurementUnit.class);
258 }
259
260
261 /**
262 * <p>getModifiers</p>
263 *
264 * @return a {@link java.util.List} object.
265 */
266 public static List<Modifier> getModifiers() {
267 return getTerms(Modifier.class);
268 }
269
270
271 /**
272 * <p>getStatisticalMeasures</p>
273 *
274 * @return a {@link java.util.List} object.
275 */
276 public static List<StatisticalMeasure> getStatisticalMeasures() {
277 return getTerms(StatisticalMeasure.class);
278 }
279
280 /**
281 * <p>getScopes</p>
282 *
283 * @return a {@link java.util.List} object.
284 */
285 public static List<Scope> getScopes() {
286 return getTerms(Scope.class);
287 }
288
289 /**
290 * <p>getDeterminationModifiers</p>
291 *
292 * @return a {@link java.util.List} object.
293 */
294 public static List<DeterminationModifier> getDeterminationModifiers() {
295 return getTerms(DeterminationModifier.class);
296 }
297
298 /**
299 * <p>getReferenceSystems</p>
300 *
301 * @return a {@link java.util.List} object.
302 */
303 public static List<ReferenceSystem> getReferenceSystems() {
304 return getTerms(ReferenceSystem.class);
305 }
306
307 /**
308 ************* save methods *****************************
309 *
310 * @param term a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
311 */
312 public static void saveTerm(DefinedTermBase term){
313 getTermService().saveOrUpdate(term);
314 }
315
316 /**
317 * <p>delete</p>
318 *
319 * @param selected a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
320 */
321 public static void delete(DefinedTermBase selected) {
322 getTermService().delete(selected);
323 }
324
325 /**
326 * Save a vacabulary to data store
327 *
328 * @param term a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
329 */
330 public static void updateVocabulary(DefinedTermBase term) {
331 getTermService().saveOrUpdate(term);
332 }
333
334
335
336 /*************** internal methods **************************/
337
338 private static Language getDefaultLanguage(){
339 return CdmStore.getDefaultLanguage();
340 }
341
342 private static OrderedTermVocabulary<? extends OrderedTermBase> getOrderedTermVocabulary(VocabularyEnum vocabularyType){
343 TermVocabulary<DefinedTermBase> vocabulary = getTermVocabulary(vocabularyType);
344 return HibernateProxyHelper.deproxy(vocabulary, OrderedTermVocabulary.class);
345 }
346
347 /**
348 * <p>getTermVocabulary</p>
349 *
350 * @param vocabularyType a {@link eu.etaxonomy.cdm.model.common.VocabularyEnum} object.
351 * @return a {@link eu.etaxonomy.cdm.model.common.TermVocabulary} object.
352 */
353 public static TermVocabulary<DefinedTermBase> getTermVocabulary(VocabularyEnum vocabularyType){
354
355 return CdmStore.getService(IVocabularyService.class).getVocabulary(vocabularyType);
356 }
357
358 private static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz){
359 Comparator<TERM> comparator = new Comparator<TERM>() {
360 @Override
361 public int compare(TERM o1, TERM o2) {
362 String label1 = o1.getLabel(getDefaultLanguage()) != null ? o1.getLabel(getDefaultLanguage()) : o1.getTitleCache();
363 String label2 = o2.getLabel(getDefaultLanguage()) != null ? o2.getLabel(getDefaultLanguage()) : o2.getTitleCache();
364 return label1.compareTo(label2);
365 }
366 };
367
368 return getTerms(clazz, comparator);
369 }
370
371
372 private static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz, Comparator<TERM> comparator){
373
374
375 List<TERM> terms = new ArrayList<TERM>();
376 if(getTermService() != null){
377 terms = getTermService().listByTermClass(clazz, null, null, null, null);
378
379 Collections.sort(terms, comparator);
380 }
381
382 return terms;
383
384 }
385
386 private static ITermService getTermService(){
387 return CdmStore.getService(ITermService.class);
388 }
389 }