Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
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.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.common.AnnotationType;
21
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
22
import eu.etaxonomy.cdm.model.common.ExtensionType;
23
import eu.etaxonomy.cdm.model.common.Language;
24
import eu.etaxonomy.cdm.model.common.MarkerType;
25
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
26
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
27
import eu.etaxonomy.cdm.model.common.TermVocabulary;
28
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
29
import eu.etaxonomy.cdm.model.description.AbsenceTerm;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
32
import eu.etaxonomy.cdm.model.description.Modifier;
33
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
34
import eu.etaxonomy.cdm.model.description.PresenceTerm;
35
import eu.etaxonomy.cdm.model.description.Scope;
36
import eu.etaxonomy.cdm.model.description.Stage;
37
import eu.etaxonomy.cdm.model.description.State;
38
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
39
import eu.etaxonomy.cdm.model.location.NamedAreaType;
40
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
41
import eu.etaxonomy.cdm.model.media.RightsTerm;
42
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
43
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
44
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
45
import eu.etaxonomy.cdm.model.name.Rank;
46
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
47
import eu.etaxonomy.cdm.model.occurrence.DeterminationModifier;
48
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
49
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
50
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
51

    
52
/**
53
 * All terms may be accessed through this store.
54
 * Note: This is for internal use. When using vocabularies in the UI, try to use the preferred terms from
55
 * PreferenceUtil
56
 *
57
 * @author n.hoffmann
58
 * @created 25.06.2009
59
 * @version 1.0
60
 */
61
public class TermStore {
62
	
63
	/**
64
	 * <p>getSpecimenTypeDesignationStatus</p>
65
	 *
66
	 * @return a {@link java.util.List} object.
67
	 */
68
	public static List<SpecimenTypeDesignationStatus> getSpecimenTypeDesignationStatus() {
69
		return getTerms(SpecimenTypeDesignationStatus.class);
70
	}
71
	
72

    
73
	/**
74
	 * <p>getNameTypeDesignationStatus</p>
75
	 *
76
	 * @return a {@link java.util.List} object.
77
	 */
78
	public static List<NameTypeDesignationStatus> getNameTypeDesignationStatus() {
79
		return getTerms(NameTypeDesignationStatus.class);
80
	}
81
	
82
	/**
83
	 * <p>getNamedAreaTypes</p>
84
	 *
85
	 * @return a {@link java.util.List} object.
86
	 */
87
	public static List<NamedAreaType> getNamedAreaTypes(){
88
		return getTerms(NamedAreaType.class);
89
	}
90
	
91
	/**
92
	 * <p>getAnnotationTypes</p>
93
	 *
94
	 * @return a {@link java.util.List} object.
95
	 */
96
	public static List<AnnotationType> getAnnotationTypes(){
97
		return getTerms(AnnotationType.class);
98
	}
99
	
100
	/**
101
	 * <p>getTaxonRelationshipTypes</p>
102
	 *
103
	 * @return a {@link java.util.List} object.
104
	 */
105
	public static List<TaxonRelationshipType> getTaxonRelationshipTypes(){
106
		return getTerms(TaxonRelationshipType.class);
107
	}
108
	
109
	
110
	/**
111
	 * <p>getLanguages</p>
112
	 *
113
	 * @return a {@link java.util.List} object.
114
	 */
115
	public static List<Language> getLanguages(){
116
		return getTerms(Language.class);
117
	}
118
	
119
	/**
120
	 * <p>getFeatures</p>
121
	 *
122
	 * @return a {@link java.util.List} object.
123
	 */
124
	public static List<Feature> getFeatures() {
125
		List<Feature> features = getTerms(Feature.class);
126
		features.remove(Feature.IMAGE());
127
		return features;
128

    
129
	}
130
	
131
	/**
132
	 * <p>getNameRelationshipTypes</p>
133
	 *
134
	 * @return a {@link java.util.List} object.
135
	 */
136
	public static List<NameRelationshipType> getNameRelationshipTypes() {
137
		return getTerms(NameRelationshipType.class);
138
	}
139
	
140
	/**
141
	 * <p>getNomenclaturalStatusTypes</p>
142
	 *
143
	 * @return a {@link java.util.List} object.
144
	 */
145
	public static List<NomenclaturalStatusType> getNomenclaturalStatusTypes(){
146
		return getTerms(NomenclaturalStatusType.class);
147
	}
148
	
149

    
150
	/**
151
	 * <p>getNonTechnicalMarkerTypes</p>
152
	 *
153
	 * @return a {@link java.util.List} object.
154
	 */
155
	public static List<MarkerType> getNonTechnicalMarkerTypes() {
156
		List<MarkerType> nonTechnicalMarkerTypes = new ArrayList<MarkerType>();
157
		List<MarkerType> markerTypes = getTerms(MarkerType.class);
158
		
159
		for (Object type : markerTypes) {
160
			if (((MarkerType) type).isTechnical() == false) {
161
				nonTechnicalMarkerTypes.add((MarkerType) type);
162
			}
163
		}
164
		
165
		return nonTechnicalMarkerTypes;
166
	}
167
	
168

    
169
	/**
170
	 * <p>getExtensionTypes</p>
171
	 *
172
	 * @return a {@link java.util.List} object.
173
	 */
174
	public static List<ExtensionType> getExtensionTypes() {
175
		return getTerms(ExtensionType.class);
176
	}
177
	
178
	/**
179
	 * <p>getRightsTypes</p>
180
	 *
181
	 * @return a {@link java.util.List} object.
182
	 */
183
	public static List<RightsTerm> getRightsTypes() {
184
		return getTerms(RightsTerm.class);
185
	}
186
	
187
	
188
	/**
189
	 * Retrieve all ranks from data store.
190
	 *
191
	 * @return a {@link java.util.List} object.
192
	 */
193
	public static List<Rank> getRanks(){
194
		if(PreferencesUtil.getSortRanksHierarchichally()){
195
			return getTerms(Rank.class, new Comparator<Rank>(){
196
	
197
				@Override
198
				public int compare(Rank o1, Rank o2) {
199
					return o1.compareTo(o2);
200
				}
201
				
202
			});
203
		}else{
204
			return getTerms(Rank.class);
205
		}
206
	}
207
	
208
	
209
	/**
210
	 * Retrieve all presence and absence terms from data store.
211
	 *
212
	 * @return a {@link java.util.List} object.
213
	 */
214
	public static List<PresenceAbsenceTermBase> getPresenceAbsenceTerms() {
215
		List presenceAbsenceTerms = getTerms(PresenceTerm.class);
216
		presenceAbsenceTerms.addAll(getTerms(AbsenceTerm.class));
217

    
218
		return presenceAbsenceTerms;
219
	}
220

    
221
	/**
222
	 * Retrieve all stages from data store.
223
	 *
224
	 * @return a {@link java.util.List} object.
225
	 */
226
	public static List<Stage> getStages(){
227
		return getTerms(Stage.class);
228
	}
229
	
230
	/**
231
	 * Retrieve all states from data store.
232
	 *
233
	 * @return a {@link java.util.List} object.
234
	 */
235
	public static List<State> getStates() {
236
		return getTerms(State.class);
237
	}
238
	
239
	/**
240
	 * Retrieve all preservation methods from data store.
241
	 *
242
	 * @return a {@link java.util.List} object.
243
	 */
244
	public static List<PreservationMethod> getPreservationMethods(){
245
		return getTerms(PreservationMethod.class);
246
	}
247
	
248
	
249

    
250
	/**
251
	 * <p>getMeasurementUnits</p>
252
	 *
253
	 * @return a {@link java.util.List} object.
254
	 */
255
	public static List<MeasurementUnit> getMeasurementUnits() {
256
		return getTerms(MeasurementUnit.class);
257
	}
258
	
259

    
260
	/**
261
	 * <p>getModifiers</p>
262
	 *
263
	 * @return a {@link java.util.List} object.
264
	 */
265
	public static List<Modifier> getModifiers() {
266
		return getTerms(Modifier.class);
267
	}
268

    
269

    
270
	/**
271
	 * <p>getStatisticalMeasures</p>
272
	 *
273
	 * @return a {@link java.util.List} object.
274
	 */
275
	public static List<StatisticalMeasure> getStatisticalMeasures() {
276
		return getTerms(StatisticalMeasure.class);
277
	}
278
	
279
	/**
280
	 * <p>getScopes</p>
281
	 *
282
	 * @return a {@link java.util.List} object.
283
	 */
284
	public static List<Scope> getScopes() {
285
		return getTerms(Scope.class);
286
	}
287
	
288
	/**
289
	 * <p>getDeterminationModifiers</p>
290
	 *
291
	 * @return a {@link java.util.List} object.
292
	 */
293
	public static List<DeterminationModifier> getDeterminationModifiers() {
294
		return getTerms(DeterminationModifier.class);
295
	}
296
	
297
	/**
298
	 * <p>getReferenceSystems</p>
299
	 *
300
	 * @return a {@link java.util.List} object.
301
	 */
302
	public static List<ReferenceSystem> getReferenceSystems() {
303
		return getTerms(ReferenceSystem.class);
304
	}
305
	
306
	/**
307
	 ************* save methods *****************************
308
	 *
309
	 * @param term a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
310
	 */
311
	public static void saveTerm(DefinedTermBase term){
312
		getTermService().saveOrUpdate(term);
313
	}
314
	
315
	/**
316
	 * <p>delete</p>
317
	 *
318
	 * @param selected a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
319
	 */
320
	public static void delete(DefinedTermBase selected) {
321
		getTermService().delete(selected);
322
	}
323
	
324
	/**
325
	 * Save a vacabulary to data store
326
	 *
327
	 * @param term a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
328
	 */
329
	public static void updateVocabulary(DefinedTermBase term) {
330
		getTermService().saveOrUpdate(term);
331
	}
332
	
333
	
334
	
335
	/*************** internal methods **************************/
336
	
337
	private static Language getDefaultLanguage(){
338
		return CdmStore.getDefaultLanguage();
339
	}
340
	
341
	private static OrderedTermVocabulary<? extends OrderedTermBase> getOrderedTermVocabulary(VocabularyEnum vocabularyType){
342
 		TermVocabulary<DefinedTermBase> vocabulary = getTermVocabulary(vocabularyType);		
343
 		return HibernateProxyHelper.deproxy(vocabulary, OrderedTermVocabulary.class);
344
	}
345
	
346
	/**
347
	 * <p>getTermVocabulary</p>
348
	 *
349
	 * @param vocabularyType a {@link eu.etaxonomy.cdm.model.common.VocabularyEnum} object.
350
	 * @return a {@link eu.etaxonomy.cdm.model.common.TermVocabulary} object.
351
	 */
352
	public static TermVocabulary<DefinedTermBase> getTermVocabulary(VocabularyEnum vocabularyType){
353
		
354
		return CdmStore.getVocabularyService().getVocabulary(vocabularyType);
355
	}
356
	
357
	private static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz){
358
		Comparator<TERM> comparator = new Comparator<TERM>() {
359
			@Override
360
			public int compare(TERM o1, TERM o2) {
361
				String label1 = o1.getLabel(getDefaultLanguage()) != null ? o1.getLabel(getDefaultLanguage()) : o1.getTitleCache();
362
				String label2 = o2.getLabel(getDefaultLanguage()) != null ? o2.getLabel(getDefaultLanguage()) : o2.getTitleCache();
363
				return label1.compareTo(label2);
364
			}
365
		};
366
		
367
		return getTerms(clazz, comparator);
368
	}
369
	
370
	
371
	private static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz, Comparator<TERM> comparator){
372

    
373
		
374
		List<TERM> terms = new ArrayList<TERM>();
375
		if(getTermService() != null){
376
			terms = getTermService().listByTermClass(clazz, null, null, null, null);
377
		
378
			Collections.sort(terms, comparator); 
379
		}
380
		
381
		return terms;
382
		
383
	}
384
	
385
	private static ITermService getTermService(){
386
		return CdmStore.getTermService();
387
	}
388
}
(6-6/6)