finalizing preferred refactoring
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / VocabularyStore.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.Set;
14 import java.util.SortedSet;
15
16 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
17 import eu.etaxonomy.cdm.api.service.ITermService;
18 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
20 import eu.etaxonomy.cdm.model.common.Language;
21 import eu.etaxonomy.cdm.model.common.OrderedTermBase;
22 import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
23 import eu.etaxonomy.cdm.model.common.TermVocabulary;
24 import eu.etaxonomy.cdm.model.common.VocabularyEnum;
25 import eu.etaxonomy.cdm.model.description.Feature;
26 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
27 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
28 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
29 import eu.etaxonomy.cdm.model.name.Rank;
30 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
31 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
32
33 /**
34 * All vocabularies may be accessed through this store.
35 * Note: This is for internal use. When using vocabularies in the UI, try to use the preferred terms from
36 * PreferenceUtil
37 *
38 * @author n.hoffmann
39 * @created 25.06.2009
40 * @version 1.0
41 */
42 public class VocabularyStore {
43
44 /**
45 * The singleton instance
46 */
47 private static VocabularyStore instance;
48
49 /**
50 * Retrieve the singleton instance
51 *
52 * @return
53 */
54 private static synchronized VocabularyStore getDefault(){
55 if(instance == null){
56 instance = new VocabularyStore();
57 }
58 return instance;
59 }
60
61 /**
62 *
63 * @return
64 */
65 public static SortedSet<SpecimenTypeDesignationStatus> getSpecimenTypeDesignationStatus() {
66 SortedSet specimenTypeDesignationStatus = getDefault().getTermVocabulary(VocabularyEnum.SpecimenTypeDesignationStatus).getTermsOrderedByLabels(getDefaultLanguage());
67 return specimenTypeDesignationStatus;
68 }
69
70 /**
71 *
72 * @return
73 */
74 public static SortedSet<TaxonRelationshipType> getTaxonRelationshipTypes(){
75 SortedSet taxonRelationshipTypes = getDefault().getOrderedTermVocabulary(VocabularyEnum.TaxonRelationshipType).getOrderedTerms();
76 return taxonRelationshipTypes;
77 }
78
79 /**
80 *
81 * @return
82 *
83 * @deprecated handle via preferences
84 */
85 public static SortedSet<TaxonRelationshipType> getConceptRelationshipTypes() {
86 SortedSet<TaxonRelationshipType> conceptRelationshipTypes = getTaxonRelationshipTypes();
87 // remove these two relations as they are considered standard taxon relations
88 conceptRelationshipTypes.remove(TaxonRelationshipType.MISAPPLIED_NAME_FOR());
89 conceptRelationshipTypes.remove(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
90
91 return conceptRelationshipTypes;
92 }
93
94 /**
95 *
96 * @return
97 */
98 public static SortedSet<Language> getLanguages(){
99 SortedSet languages = getDefault().getTermVocabulary(VocabularyEnum.Language).getTermsOrderedByLabels(getDefaultLanguage());
100 return languages;
101 }
102
103 /**
104 *
105 * @return
106 */
107 public static SortedSet<Feature> getFeatures() {
108 SortedSet features = getDefault().getTermVocabulary(VocabularyEnum.Feature).getTermsOrderedByLabels(getDefaultLanguage());
109 return features;
110
111 }
112
113 /**
114 *
115 * @return
116 */
117 public static SortedSet<NameRelationshipType> getNameRelationshipTypes() {
118 SortedSet nameRelationshipTypes = getDefault().getTermVocabulary(VocabularyEnum.NameRelationshipType).getTermsOrderedByLabels(getDefaultLanguage());
119 return nameRelationshipTypes;
120 }
121
122 /**
123 *
124 * @return
125 */
126 public static Set<NomenclaturalStatusType> getNomenclaturalStatusTypes(){
127 // TODO sort types
128 Set nomenclaturalStatusTypes = getDefault().getTermVocabulary(VocabularyEnum.NomenclaturalStatusType).getTermsOrderedByLabels(getDefaultLanguage());
129 return nomenclaturalStatusTypes;
130 }
131
132 /**
133 * Retrieve all ranks from data store.
134 *
135 * @return
136 */
137 public static SortedSet<Rank> getRanks(){
138 SortedSet ranks = getDefault().getOrderedTermVocabulary(VocabularyEnum.Rank).getOrderedTerms();
139 return ranks;
140 }
141
142
143 /**
144 * Retrieve all presence and absence terms from data store.
145 *
146 * @return
147 */
148 public static SortedSet<PresenceAbsenceTermBase<PresenceAbsenceTermBase<?>>> getPresenceAbsenceTerms() {
149 SortedSet presenceAbsenceTerms = getDefault().getTermVocabulary(VocabularyEnum.PresenceTerm).getTermsOrderedByLabels(getDefaultLanguage());
150 presenceAbsenceTerms.addAll(getDefault().getTermVocabulary(VocabularyEnum.AbsenceTerm).getTermsOrderedByLabels(getDefaultLanguage()));
151
152 return presenceAbsenceTerms;
153 }
154
155 /*************** save methods ******************************/
156 /**
157 * Adds a term to a vocabulary
158 *
159 * @param feature
160 */
161 public static void saveTerm(DefinedTermBase term, VocabularyEnum vocabularyType) {
162 TermVocabulary<DefinedTermBase> vocabulary = getDefault().getTermVocabulary(vocabularyType);
163
164 vocabulary.addTerm(term);
165
166 getDefault().getTermService().saveTermVocabulary(vocabulary);
167 }
168
169 /**
170 * Save a vacabulary to data store
171 *
172 * @param feature
173 */
174 public static void updateVocabulary(VocabularyEnum vocabularyType) {
175 TermVocabulary<DefinedTermBase> vocabulary = getDefault().getTermVocabulary(vocabularyType);
176
177 getDefault().getTermService().saveTermVocabulary(vocabulary);
178 }
179
180
181
182 /*************** internal methods **************************/
183
184 private static Language getDefaultLanguage(){
185 return CdmStore.getDefaultLanguage();
186 }
187
188 private OrderedTermVocabulary<? extends OrderedTermBase> getOrderedTermVocabulary(VocabularyEnum vocabularyType){
189 TermVocabulary<DefinedTermBase> vocabulary = getTermVocabulary(vocabularyType);
190 return HibernateProxyHelper.deproxy(vocabulary, OrderedTermVocabulary.class);
191 }
192
193 private TermVocabulary<DefinedTermBase> getTermVocabulary(VocabularyEnum vocabularyType){
194 return getTermService().getVocabulary(vocabularyType);
195 }
196
197 private ITermService getTermService(){
198 return getApplicationController().getTermService();
199 }
200
201 private CdmApplicationController getApplicationController(){
202 return CdmStore.getApplicationController();
203 }
204 }