(no commit message)
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.preference;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.jface.preference.IPreferenceStore;
18
19 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
20 import eu.etaxonomy.cdm.api.service.config.impl.TaxonServiceConfiguratorImpl;
21 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
22 import eu.etaxonomy.cdm.model.common.TermBase;
23 import eu.etaxonomy.cdm.model.description.Feature;
24 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
25 import eu.etaxonomy.cdm.model.name.BotanicalName;
26 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
27 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
28 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
29 import eu.etaxonomy.cdm.model.name.NonViralName;
30 import eu.etaxonomy.cdm.model.name.Rank;
31 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
32 import eu.etaxonomy.cdm.model.name.ZoologicalName;
33 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
34 import eu.etaxonomy.cdm.persistence.query.MatchMode;
35 import eu.etaxonomy.taxeditor.model.Resources;
36 import eu.etaxonomy.taxeditor.store.VocabularyStore;
37 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
38
39 /**
40 * @author p.ciardelli
41 * @created 05.12.2008
42 * @version 1.0
43 * @author n.hoffmann
44 */
45 public class PreferencesUtil {
46 private static final Logger logger = Logger
47 .getLogger(PreferencesUtil.class);
48
49 public static final String TAXON_SERVICE_CONFIGURATOR_TAXA = "TAXON_SERVICE_CONFIGURATOR_TAXA";
50 public static final String TAXON_SERVICE_CONFIGURATOR_SYNONYMS = "TAXON_SERVICE_CONFIGURATOR_SYNONYMS";
51 public static final String TAXON_SERVICE_CONFIGURATOR_NAMES = "TAXON_SERVICE_CONFIGURATOR_NAMES";
52 public static final String TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES = "TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES";
53
54 /**
55 *
56 * @return
57 */
58 public static IPreferenceStore getPreferenceStore() {
59 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
60 }
61
62 /**
63 *
64 * @return
65 */
66 private static String getNameCodePreference() {
67 try{
68 return getPreferenceStore().getString(Resources.CODE_PREFERENCE);
69 }catch(NullPointerException e){
70 logger.warn("PreferenceStore was not available. That is OK if you are running a unit test. Setting ICBN as default code.");
71 return Resources.CODE_PREFERENCE_ICBN;
72 }
73 }
74
75 /**
76 *
77 * @param preferredCode
78 */
79 public static void setNomenclaturalCode(String preferredCode) {
80 getPreferenceStore().setValue(Resources.CODE_PREFERENCE, preferredCode);
81 }
82
83 /**
84 *
85 */
86 public static void setDefaultNomenclaturalCode() {
87 setNomenclaturalCode(Resources.DEFAULT_CODE_PREFERENCE);
88 }
89
90 /**
91 *
92 * @return
93 */
94 public static String getPreferredNomenclaturalCodeAsString() {
95 String nameCodePreference = getNameCodePreference();
96
97 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
98 return "International Code of Botanical Nomenclature (ICBN)";
99 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
100 return "International Code of Zoological Nomenclature (ICZN)";
101 }
102 return null;
103 }
104
105 /**
106 * @return
107 */
108 public static NomenclaturalCode getPreferredNomenclaturalCode() {
109
110 String nameCodePreference = getNameCodePreference();
111
112 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
113 return NomenclaturalCode.ICBN;
114 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
115 return NomenclaturalCode.ICZN;
116 }
117 return null;
118 }
119
120
121 /**
122 * Returns a new name object that conforms to the nomenclatural
123 * code specified in user preferences.
124 *
125 * @return
126 */
127 public static NonViralName<?> getInstanceOfPreferredNameClass() {
128 NomenclaturalCode code = getPreferredNomenclaturalCode();
129
130 // Check whether name code preference needs to be initialized
131 if (code == null) {
132 setDefaultNomenclaturalCode();
133 }
134
135 if (code.equals(NomenclaturalCode.ICBN)) {
136 return BotanicalName.NewInstance(null);
137 } else if (code.equals(NomenclaturalCode.ICZN)) {
138 return ZoologicalName.NewInstance(null);
139 }
140 return NonViralName.NewInstance(null);
141 }
142
143 /**
144 * Generic method to get term preferences for a term vocabulary
145 *
146 * @param <T>
147 * @param initialTerms
148 * @return
149 */
150 public static <T extends DefinedTermBase> List<T> getPreferredTerms(Set<T> initialTerms){
151
152 // set default for non existent preferences
153 defaultTerms(initialTerms);
154
155 List<T> preferredTerms = new ArrayList<T>();
156
157 for (T term : initialTerms){
158 if(getPreferenceStore().getBoolean(getPreferenceKey(term))){
159 preferredTerms.add(term);
160 }
161 }
162
163 return preferredTerms;
164 }
165
166 private static <T extends DefinedTermBase> boolean defaultTerms(Set<T> initialTerms){
167 // set default for non existen preferences
168 for(T term : initialTerms){
169 if(getPreferenceStore().getBoolean(term.getClass().getName())){
170 break;
171 }else{
172 getPreferenceStore().setValue(term.getClass().getName(), true);
173 setPreferredTerms(new ArrayList(initialTerms), initialTerms);
174 break;
175 }
176 }
177 return true;
178 }
179
180
181 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
182 public static List<SpecimenTypeDesignationStatus> getPreferredSpecimenTypeDesignationStatus() { return getPreferredTerms(VocabularyStore.getSpecimenTypeDesignationStatus());}
183
184 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
185 public static List<TaxonRelationshipType> getPreferredTaxonRelationshipTypes() { return getPreferredTerms(VocabularyStore.getTaxonRelationshipTypes());}
186
187 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
188 public static List<Feature> getPreferredFeatures() { return getPreferredTerms(VocabularyStore.getFeatures());}
189
190 /** @return a <code>List</code> containing user preferred <code>Rank</code> terms */
191 public static List<Rank> getPreferredRanks() { return getPreferredTerms(VocabularyStore.getRanks());}
192
193 /** @return a <code>List</code> containing user preferred <code>PresenceAbsenceTermBase</code> terms */
194 public static List<PresenceAbsenceTermBase<PresenceAbsenceTermBase<?>>> getPreferredPresenceAbsenceTerms(){ return getPreferredTerms(VocabularyStore.getPresenceAbsenceTerms());}
195
196 /** @return a <code>List</code> containing user preferred <code>NomenclaturalStatusType</code> terms */
197 public static List<NomenclaturalStatusType> getPreferredNomenclaturalStatusTypes(){ return getPreferredTerms(VocabularyStore.getNomenclaturalStatusTypes());}
198
199 /** @return a <code>List</code> containing user preferred <code>NameRelationshipType</code> terms */
200 public static List<NameRelationshipType> getPreferredNameRelationshipTypes(){ return getPreferredTerms(VocabularyStore.getNameRelationshipTypes());}
201
202
203 /**
204 * Generic method to set term preferences
205 *
206 * @param preferredTerms
207 * @param initalTerms
208 */
209 public static <T extends DefinedTermBase> void setPreferredTerms(List<T> preferredTerms, Set<T> initalTerms){
210 for(TermBase term : initalTerms){
211 getPreferenceStore().setValue(getPreferenceKey(term), preferredTerms.contains(term));
212 }
213 }
214
215 /**
216 * Construct a unique key using the terms uuid
217 *
218 * @param term
219 * @return
220 */
221 private static String getPreferenceKey(TermBase term) {
222 return term.getClass().getName()
223 . concat(".")
224 . concat(term.getUuid().toString());
225 }
226
227 /**
228 * Retrieves search preferences from the preference store
229 *
230 * @return an <code>ITaxonServiceConfigurator</code> to pass to search methods
231 */
232 public static ITaxonServiceConfigurator getSearchConfigurator() {
233 ITaxonServiceConfigurator configurator = TaxonServiceConfiguratorImpl.NewInstance();
234 // check for existence of at least one preference variable
235 if(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_TAXA)){
236 // preferences exist
237 configurator.setDoTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_TAXA));
238 configurator.setDoSynonyms(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
239 configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_NAMES));
240 configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
241 }else{
242 // create new preferences setting everything to true
243 configurator.setDoTaxa(true);
244 configurator.setDoSynonyms(true);
245 configurator.setDoNamesWithoutTaxa(true);
246 configurator.setDoTaxaByCommonNames(true);
247 setSearchConfigurator(configurator);
248 }
249
250 // DEFAULT VALUES
251 // match mode default only
252 configurator.setMatchMode(MatchMode.BEGINNING);
253 // i don't know what happens to sec at the moment
254 configurator.setSec(null);
255 // we set page number and size here as this should always be unlimited
256 configurator.setPageNumber(0);
257 // TODO currently limit results to 1000
258 configurator.setPageSize(1000);
259
260 return configurator;
261 }
262
263 /**
264 * Store search preferences
265 *
266 * @param configurator
267 */
268 public static void setSearchConfigurator(ITaxonServiceConfigurator configurator){
269 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA, configurator.isDoTaxa());
270 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS, configurator.isDoSynonyms());
271 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES, configurator.isDoNamesWithoutTaxa());
272 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES, configurator.isDoTaxaByCommonNames());
273 }
274 }