2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.preference
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
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
;
45 public class PreferencesUtil
{
46 private static final Logger logger
= Logger
47 .getLogger(PreferencesUtil
.class);
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";
58 public static IPreferenceStore
getPreferenceStore() {
59 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
66 private static String
getNameCodePreference() {
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
;
77 * @param preferredCode
79 public static void setNomenclaturalCode(String preferredCode
) {
80 getPreferenceStore().setValue(Resources
.CODE_PREFERENCE
, preferredCode
);
86 public static void setDefaultNomenclaturalCode() {
87 setNomenclaturalCode(Resources
.DEFAULT_CODE_PREFERENCE
);
94 public static String
getPreferredNomenclaturalCodeAsString() {
95 String nameCodePreference
= getNameCodePreference();
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)";
108 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
110 String nameCodePreference
= getNameCodePreference();
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
;
122 * Returns a new name object that conforms to the nomenclatural
123 * code specified in user preferences.
127 public static NonViralName
<?
> getInstanceOfPreferredNameClass() {
128 NomenclaturalCode code
= getPreferredNomenclaturalCode();
130 // Check whether name code preference needs to be initialized
132 setDefaultNomenclaturalCode();
135 if (code
.equals(NomenclaturalCode
.ICBN
)) {
136 return BotanicalName
.NewInstance(null);
137 } else if (code
.equals(NomenclaturalCode
.ICZN
)) {
138 return ZoologicalName
.NewInstance(null);
140 return NonViralName
.NewInstance(null);
144 * Generic method to get term preferences for a term vocabulary
147 * @param initialTerms
150 public static <T
extends DefinedTermBase
> List
<T
> getPreferredTerms(Set
<T
> initialTerms
){
152 // set default for non existent preferences
153 defaultTerms(initialTerms
);
155 List
<T
> preferredTerms
= new ArrayList
<T
>();
157 for (T term
: initialTerms
){
158 if(getPreferenceStore().getBoolean(getPreferenceKey(term
))){
159 preferredTerms
.add(term
);
163 return preferredTerms
;
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())){
172 getPreferenceStore().setValue(term
.getClass().getName(), true);
173 setPreferredTerms(new ArrayList(initialTerms
), initialTerms
);
181 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
182 public static List
<SpecimenTypeDesignationStatus
> getPreferredSpecimenTypeDesignationStatus() { return getPreferredTerms(VocabularyStore
.getSpecimenTypeDesignationStatus());}
184 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
185 public static List
<TaxonRelationshipType
> getPreferredTaxonRelationshipTypes() { return getPreferredTerms(VocabularyStore
.getTaxonRelationshipTypes());}
187 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
188 public static List
<Feature
> getPreferredFeatures() { return getPreferredTerms(VocabularyStore
.getFeatures());}
190 /** @return a <code>List</code> containing user preferred <code>Rank</code> terms */
191 public static List
<Rank
> getPreferredRanks() { return getPreferredTerms(VocabularyStore
.getRanks());}
193 /** @return a <code>List</code> containing user preferred <code>PresenceAbsenceTermBase</code> terms */
194 public static List
<PresenceAbsenceTermBase
<PresenceAbsenceTermBase
<?
>>> getPreferredPresenceAbsenceTerms(){ return getPreferredTerms(VocabularyStore
.getPresenceAbsenceTerms());}
196 /** @return a <code>List</code> containing user preferred <code>NomenclaturalStatusType</code> terms */
197 public static List
<NomenclaturalStatusType
> getPreferredNomenclaturalStatusTypes(){ return getPreferredTerms(VocabularyStore
.getNomenclaturalStatusTypes());}
199 /** @return a <code>List</code> containing user preferred <code>NameRelationshipType</code> terms */
200 public static List
<NameRelationshipType
> getPreferredNameRelationshipTypes(){ return getPreferredTerms(VocabularyStore
.getNameRelationshipTypes());}
204 * Generic method to set term preferences
206 * @param preferredTerms
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
));
216 * Construct a unique key using the terms uuid
221 private static String
getPreferenceKey(TermBase term
) {
222 return term
.getClass().getName()
224 . concat(term
.getUuid().toString());
228 * Retrieves search preferences from the preference store
230 * @return an <code>ITaxonServiceConfigurator</code> to pass to search methods
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
)){
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
));
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
);
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);
264 * Store search preferences
266 * @param configurator
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());