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
;
18 import org
.eclipse
.ui
.IEditorInput
;
20 import eu
.etaxonomy
.cdm
.api
.service
.config
.ITaxonServiceConfigurator
;
21 import eu
.etaxonomy
.cdm
.api
.service
.config
.impl
.TaxonServiceConfiguratorImpl
;
22 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
23 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
25 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.IDefinedTerm
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
29 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
31 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
32 import eu
.etaxonomy
.cdm
.model
.media
.RightsTerm
;
33 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
36 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
37 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
39 import eu
.etaxonomy
.cdm
.strategy
.match
.DefaultMatchStrategy
;
40 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
41 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
42 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchMode
;
43 import eu
.etaxonomy
.taxeditor
.model
.NomenclaturalCodeHelper
;
44 import eu
.etaxonomy
.taxeditor
.store
.VocabularyStore
;
45 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
53 public class PreferencesUtil
{
54 private static final Logger logger
= Logger
55 .getLogger(PreferencesUtil
.class);
57 public static final String PREFERRED_NOMENCLATURAL_CODE_KEY
= "code";
59 public static final String TAXON_SERVICE_CONFIGURATOR_TAXA
= "taxonServiceConfigurator.doTaxa";
60 public static final String TAXON_SERVICE_CONFIGURATOR_SYNONYMS
= "taxonServiceConfigurator.doSynonyms";
61 public static final String TAXON_SERVICE_CONFIGURATOR_NAMES
= "taxonServiceConfigurator.doNamesWithoutTaxa";
62 public static final String TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
= "taxonServiceConfigurator.doTaxaByCommonNames";
63 public static final String TAXON_SERVICE_CONFIGURATOR_ORPHANED_TAXA
= "taxonServiceConfigurator.doTaxaByOrphanedTaxa";
65 public static final String SHOW_DATASOURCE_CHOOSER
= "dataSourceChooser.show";
67 public static final String PREFERRED_TERMS_CHANGE
= "preferred_terms";
69 public static final String HIDE_BULKEDITOR_INFO
= "bulkeditorInfo.hide";
71 public static final String EDIT_MARKER_TYPE_PREFIX
= "editMarkerType";
73 public static final String MATCH_STRATEGY_PREFIX
= "matchStrategy.";
79 public static IPreferenceStore
getPreferenceStore() {
80 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
85 * @param preferredCode
87 public static void setPreferredNomenclaturalCode(NomenclaturalCode preferredCode
) {
88 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY
, getPreferenceKey(preferredCode
));
94 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
96 for (NomenclaturalCode code
: NomenclaturalCodeHelper
.getAllCodes()) {
97 String preferredCode
= getPreferenceStore().getString(PREFERRED_NOMENCLATURAL_CODE_KEY
);
98 if (getPreferenceKey(code
).equals(preferredCode
)) {
106 * The ability to edit marker types in the BulkEditor is associated with an input type, e.g. references.
112 public static boolean getEditMarkerTypePreference(IEditorInput input
, MarkerType markerType
) {
113 return getPreferenceStore().getBoolean(getEditMarkerTypePrefKey(input
, markerType
));
116 public static void setEditMarkerTypePreference(IEditorInput input
, MarkerType markerType
, boolean edit
) {
117 getPreferenceStore().setValue(getEditMarkerTypePrefKey(input
, markerType
), edit
);
120 private static String
getEditMarkerTypePrefKey(IEditorInput input
, MarkerType markerType
) {
121 markerType
= (MarkerType
) HibernateProxyHelper
.deproxy(markerType
);
122 return EDIT_MARKER_TYPE_PREFIX
+ "." + input
.getClass().getName() + "." + markerType
.getUuid().toString();
126 * Get the match strategy for the given class that was stored in preferences
127 * or the default strategy if it was not stored in preferences
132 public static IMatchStrategy
getMatchStrategy(Class clazz
){
133 String className
= clazz
.getName();
134 if(getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX
+ className
)){
135 IMatchStrategy matchStrategy
= getDefaultMatchStrategy(clazz
);
137 for(String fieldName
: matchStrategy
.getMatchFieldPropertyNames()){
138 String matchModeName
= getPreferenceStore().getString(getMatchStrategyFieldName(className
, fieldName
));
139 MatchMode matchMode
= MatchMode
.valueOf(matchModeName
);
141 matchStrategy
.setMatchMode(fieldName
, matchMode
);
142 } catch (MatchException e
) {
144 throw new RuntimeException(e
);
148 return matchStrategy
;
150 return getDefaultMatchStrategy(clazz
);
154 * Stores a matchStrategy into the preference store.
158 public static void setMatchStrategy(IMatchStrategy matchStrategy
){
159 String className
= matchStrategy
.getMatchClass().getName();
160 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX
+ className
, true);
162 Set
<String
> matchFields
= matchStrategy
.getMatchFieldPropertyNames();
164 for(String fieldName
: matchFields
){
165 getPreferenceStore().setValue(getMatchStrategyFieldName(className
, fieldName
),
166 matchStrategy
.getMatchMode(fieldName
).name());
171 * Helper method to create the preference property for a match field.
177 private static String
getMatchStrategyFieldName(String className
, String fieldName
){
178 return MATCH_STRATEGY_PREFIX
+ className
+ "." + fieldName
;
182 * Returns the default match strategy for a given class.
187 public static IMatchStrategy
getDefaultMatchStrategy(Class clazz
){
188 return DefaultMatchStrategy
.NewInstance(clazz
);
192 * Generic method to get term preferences for a term vocabulary
195 * @param initialTerms
198 public static <T
extends DefinedTermBase
> List
<T
> getPreferredTerms(Set
<T
> initialTerms
){
200 // set default for non existent preferences
201 defaultTerms(initialTerms
);
203 List
<T
> preferredTerms
= new ArrayList
<T
>();
205 for (T term
: initialTerms
){
206 if(getPreferenceStore().getBoolean(getPreferenceKey((ICdmBase
) term
))){
207 preferredTerms
.add(term
);
211 return preferredTerms
;
214 private static <T
extends DefinedTermBase
> boolean defaultTerms(Set
<T
> initialTerms
){
215 // set default for non existen preferences
216 for(T term
: initialTerms
){
217 if(getPreferenceStore().getBoolean(term
.getClass().getName())){
220 getPreferenceStore().setValue(term
.getClass().getName(), true);
221 setPreferredTerms(new ArrayList(initialTerms
), initialTerms
);
229 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
230 public static List
<SpecimenTypeDesignationStatus
> getPreferredSpecimenTypeDesignationStatus() { return getPreferredTerms(VocabularyStore
.getSpecimenTypeDesignationStatus());}
232 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
233 public static List
<SpecimenTypeDesignationStatus
> getPreferredNameTypeDesignationStatus() { return getPreferredTerms(VocabularyStore
.getNameTypeDesignationStatus());}
235 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
236 public static List
<TaxonRelationshipType
> getPreferredTaxonRelationshipTypes() { return getPreferredTerms(VocabularyStore
.getTaxonRelationshipTypes());}
238 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
239 public static List
<Feature
> getPreferredFeatures() { return getPreferredTerms(VocabularyStore
.getFeatures());}
241 /** @return a <code>List</code> containing user preferred <code>Rank</code> terms */
242 public static List
<Rank
> getPreferredRanks() { return getPreferredTerms(VocabularyStore
.getRanks());}
244 /** @return a <code>List</code> containing user preferred <code>PresenceAbsenceTermBase</code> terms */
245 public static List
<PresenceAbsenceTermBase
<PresenceAbsenceTermBase
<?
>>> getPreferredPresenceAbsenceTerms(){ return getPreferredTerms(VocabularyStore
.getPresenceAbsenceTerms());}
247 /** @return a <code>List</code> containing user preferred <code>NomenclaturalStatusType</code> terms */
248 public static List
<NomenclaturalStatusType
> getPreferredNomenclaturalStatusTypes(){ return getPreferredTerms(VocabularyStore
.getNomenclaturalStatusTypes());}
250 /** @return a <code>List</code> containing user preferred <code>NameRelationshipType</code> terms */
251 public static List
<NameRelationshipType
> getPreferredNameRelationshipTypes(){ return getPreferredTerms(VocabularyStore
.getNameRelationshipTypes());}
253 /** @return a <code>List</code> containing user preferred <code>Language</code> terms */
254 public static List
<Language
> getPreferredLanguages() { return getPreferredTerms(VocabularyStore
.getLanguages()); }
256 /** @return a <code>List</code> containing user preferred <code>MarkerType</code> terms */
257 public static List
<MarkerType
> getPreferredMarkerTypes() { return getPreferredTerms(VocabularyStore
.getNonTechnicalMarkerTypes()); }
259 /** @return a <code>List</code> containing user preferred <code>ExtensionType</code> terms */
260 public static List
<ExtensionType
> getPreferredExtensionTypes() { return getPreferredTerms(VocabularyStore
.getExtensionTypes()); }
262 /** @return a <code>List</code> containing user preferred <code>RightsType</code> terms */
263 public static List
<RightsTerm
> getPreferredRightsTypes() { return getPreferredTerms(VocabularyStore
.getRightsTypes());}
267 * Generic method to set term preferences
269 * @param preferredTerms
270 * @param initialTerms
272 public static <T
extends DefinedTermBase
> void setPreferredTerms(List
<T
> preferredTerms
, Set
<T
> initialTerms
){
273 for(TermBase term
: initialTerms
){
274 getPreferenceStore().setValue(getPreferenceKey(term
), preferredTerms
.contains(term
));
279 * Construct a unique key using the CdmBase object's uuid
284 private static String
getPreferenceKey(ICdmBase cdmBase
) {
285 cdmBase
= (ICdmBase
) HibernateProxyHelper
.deproxy(cdmBase
);
287 String key
= cdmBase
.getClass().getName()
289 . concat(cdmBase
.getUuid().toString());
290 if (key
.contains("javassist")) {
291 logger
.info("proxy");
297 * Construct a unique key using the CdmBase object's uuid
302 private static String
getPreferenceKey(IDefinedTerm definedTerm
) {
303 definedTerm
= (IDefinedTerm
) HibernateProxyHelper
.deproxy(definedTerm
);
304 String key
= definedTerm
.getClass().getName()
306 . concat(definedTerm
.getUuid().toString());
307 if (key
.contains("javassist")) {
308 logger
.info("proxy");
314 * Retrieves search preferences from the preference store
316 * @return an <code>ITaxonServiceConfigurator</code> to pass to search methods
318 public static ITaxonServiceConfigurator
getSearchConfigurator() {
319 ITaxonServiceConfigurator configurator
= initializeSearchConfigurator();
321 configurator
.setDoTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_TAXA
));
322 configurator
.setDoSynonyms(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
));
323 configurator
.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_NAMES
));
324 configurator
.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
));
325 configurator
.setDoTaxaByOrphanedTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_ORPHANED_TAXA
));
331 * create new preferences, setting all search options to true
334 public static ITaxonServiceConfigurator
initializeSearchConfigurator(){
335 ITaxonServiceConfigurator configurator
= TaxonServiceConfiguratorImpl
.NewInstance();
337 configurator
.setDoTaxa(true);
338 configurator
.setDoSynonyms(true);
339 configurator
.setDoNamesWithoutTaxa(true);
340 configurator
.setDoTaxaByCommonNames(true);
341 configurator
.setDoTaxaByOrphanedTaxa(true);
344 // match mode default only
345 configurator
.setMatchMode(eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
.BEGINNING
);
346 // i don't know what happens to sec at the moment
347 configurator
.setSec(null);
348 // we set page number and size here as this should always be unlimited
349 configurator
.setPageNumber(0);
350 // TODO currently limit results to 1000
351 configurator
.setPageSize(1000);
357 * Store search preferences
359 * @param configurator
361 public static void setSearchConfigurator(ITaxonServiceConfigurator configurator
){
362 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA
, configurator
.isDoTaxa());
363 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
, configurator
.isDoSynonyms());
364 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES
, configurator
.isDoNamesWithoutTaxa());
365 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
, configurator
.isDoTaxaByCommonNames());
366 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_ORPHANED_TAXA
, configurator
.isDoTaxaByOrphanedTaxa());
372 public static void firePreferencesChanged(Class clazz
) {
373 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE
, null, clazz
);