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