33323b602c19e8320c2991f2571773adcd3fb1ed
[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.Arrays;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.swt.widgets.Shell;
23
24 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
25 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
26 import eu.etaxonomy.cdm.api.service.ITermService;
27 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
28 import eu.etaxonomy.cdm.api.service.config.impl.TaxonServiceConfiguratorImpl;
29 import eu.etaxonomy.cdm.common.CdmUtils;
30 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
31 import eu.etaxonomy.cdm.model.common.ICdmBase;
32 import eu.etaxonomy.cdm.model.common.IDefinedTerm;
33 import eu.etaxonomy.cdm.model.common.Language;
34 import eu.etaxonomy.cdm.model.common.MarkerType;
35 import eu.etaxonomy.cdm.model.common.TermBase;
36 import eu.etaxonomy.cdm.model.description.FeatureTree;
37 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
38 import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
39 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
40 import eu.etaxonomy.cdm.strategy.match.MatchException;
41 import eu.etaxonomy.cdm.strategy.match.MatchMode;
42 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
43 import eu.etaxonomy.taxeditor.store.CdmStore;
44 import eu.etaxonomy.taxeditor.store.StoreUtil;
45 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
46
47 /**
48 * <p>PreferencesUtil class.</p>
49 *
50 * @author p.ciardelli
51 * @author n.hoffmann
52 * @created 05.12.2008
53 * @version 1.0
54 */
55 public class PreferencesUtil implements IPreferenceKeys{
56
57 /**
58 *
59 */
60 public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
61
62
63
64 /**
65 * <p>getPreferenceStore</p>
66 *
67 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
68 */
69 public static IPreferenceStore getPreferenceStore() {
70 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
71 }
72
73 /**
74 * <p>setPreferredNomenclaturalCode</p>
75 *
76 * @param preferredCode a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
77 */
78 public static void setPreferredNomenclaturalCode(NomenclaturalCode preferredCode) {
79 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY, getPreferenceKey(preferredCode));
80 }
81
82 /**
83 * <p>getPreferredNomenclaturalCode</p>
84 *
85 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
86 */
87 public static NomenclaturalCode getPreferredNomenclaturalCode() {
88
89 for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
90 String preferredCode = getPreferenceStore().getString(PREFERRED_NOMENCLATURAL_CODE_KEY);
91 if (getPreferenceKey(code).equals(preferredCode)) {
92 return code;
93 }
94 }
95 return null;
96 }
97
98 /**
99 * Get the match strategy for the given class that was stored in preferences
100 * or the default strategy if it was not stored in preferences
101 *
102 * @param clazz a {@link java.lang.Class} object.
103 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
104 */
105 public static IMatchStrategy getMatchStrategy(Class clazz){
106 String className = clazz.getName();
107 if(getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)){
108 IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
109
110 for(String fieldName : matchStrategy.getMatchFieldPropertyNames()){
111 String matchModeName = getPreferenceStore().getString(getMatchStrategyFieldName(className, fieldName));
112 MatchMode matchMode = MatchMode.valueOf(matchModeName);
113 try {
114 matchStrategy.setMatchMode(fieldName, matchMode);
115 } catch (MatchException e) {
116 StoreUtil.error(PreferencesUtil.class, e);
117 throw new RuntimeException(e);
118 }
119 }
120
121 return matchStrategy;
122 }
123 return getDefaultMatchStrategy(clazz);
124 }
125
126 /**
127 * Stores a matchStrategy into the preference store.
128 *
129 * @param matchStrategy a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
130 */
131 public static void setMatchStrategy(IMatchStrategy matchStrategy){
132 String className = matchStrategy.getMatchClass().getName();
133 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
134
135 Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
136
137 for(String fieldName : matchFields){
138 getPreferenceStore().setValue(getMatchStrategyFieldName(className, fieldName),
139 matchStrategy.getMatchMode(fieldName).name());
140 }
141 }
142
143 /**
144 * Helper method to create the preference property for a match field.
145 *
146 * @param className
147 * @param fieldName
148 * @return
149 */
150 private static String getMatchStrategyFieldName(String className, String fieldName){
151 return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
152 }
153
154 /**
155 * Returns the default match strategy for a given class.
156 *
157 * @param clazz a {@link java.lang.Class} object.
158 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
159 */
160 public static IMatchStrategy getDefaultMatchStrategy(Class clazz){
161 return DefaultMatchStrategy.NewInstance(clazz);
162 }
163
164
165 /**
166 * <p>getDateFormatPattern</p>
167 *
168 * @return a {@link java.lang.String} object.
169 */
170 public static String getDateFormatPattern(){
171 // TODO make this configurable in properties
172 String pattern = "Y-M-d H:m";
173 return pattern;
174 }
175
176 /**
177 * <p>addTermToPreferredTerms</p>
178 *
179 * @param term a T object.
180 * @param <T> a T object.
181 */
182 public static <T extends TermBase> void addTermToPreferredTerms(T term){
183
184 // VocabularyEnum vocabulary = VocabularyEnum.getVocabularyEnum(term.getClass());
185 //
186 // getPreferenceStore().setValue(getPreferenceKey(term), VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
187 //
188 // firePreferencesChanged(term.getClass());
189 }
190
191 /**
192 * Construct a unique key using the CdmBase object's uuid
193 *
194 * @param cdmBase
195 * @return
196 */
197 private static String getPreferenceKey(ICdmBase cdmBase) {
198 cdmBase = (ICdmBase) HibernateProxyHelper.deproxy(cdmBase);
199
200 String key = cdmBase.getClass().getName()
201 . concat(".")
202 . concat(cdmBase.getUuid().toString());
203 if (key.contains("javassist")) {
204 StoreUtil.info("proxy");
205 }
206 return key;
207 }
208
209 /**
210 * Construct a unique key using the CdmBase object's uuid
211 *
212 * @param cdmBase
213 * @return
214 */
215 private static String getPreferenceKey(IDefinedTerm definedTerm) {
216 definedTerm = (IDefinedTerm) HibernateProxyHelper.deproxy(definedTerm);
217 String key = definedTerm.getClass().getName()
218 . concat(".")
219 . concat(definedTerm.getUuid().toString());
220 if (key.contains("javassist")) {
221 StoreUtil.warn(PreferencesUtil.class, "Trying to persist a preference based on a proxy class.");
222 }
223 return key;
224 }
225
226 /**
227 * Retrieves search preferences from the preference store
228 *
229 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
230 */
231 public static ITaxonServiceConfigurator getSearchConfigurator() {
232 ITaxonServiceConfigurator configurator = initializeSearchConfigurator();
233
234 configurator.setDoTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_TAXA));
235 configurator.setDoSynonyms(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
236 configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_NAMES));
237 configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
238
239 return configurator;
240 }
241
242 /**
243 * create new preferences, setting all search options to true
244 *
245 * @return a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
246 */
247 public static ITaxonServiceConfigurator initializeSearchConfigurator(){
248 ITaxonServiceConfigurator configurator = TaxonServiceConfiguratorImpl.NewInstance();
249
250 configurator.setDoTaxa(true);
251 configurator.setDoSynonyms(true);
252 configurator.setDoNamesWithoutTaxa(true);
253 configurator.setDoTaxaByCommonNames(true);
254
255 configurator.setTaxonPropertyPath(Arrays.asList("$",
256 "titleCache", "name", "name.$", "relationsFromThisTaxon.$"));
257
258 configurator.setSynonymPropertyPath(Arrays.asList("$",
259 "titleCache", "name", "name.$", "synonymRelations.relatedTo.*"));
260
261 // DEFAULT VALUES
262 // match mode is a simple like, actually all other match modes are kind of bogus
263 configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.LIKE);
264 // we set page number and size here as this should always be unlimited
265 configurator.setPageNumber(0);
266 // TODO currently limit results to 10000
267 configurator.setPageSize(10000);
268
269 return configurator;
270 }
271
272 /**
273 * Store search preferences
274 *
275 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
276 */
277 public static void setSearchConfigurator(ITaxonServiceConfigurator configurator){
278 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA, configurator.isDoTaxa());
279 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS, configurator.isDoSynonyms());
280 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES, configurator.isDoNamesWithoutTaxa());
281 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES, configurator.isDoTaxaByCommonNames());
282 }
283
284 /**
285 * <p>firePreferencesChanged</p>
286 *
287 * @param clazz a {@link java.lang.Class} object.
288 */
289 public static void firePreferencesChanged(Class clazz) {
290 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE, null, clazz);
291 }
292
293 /**
294 * Set default values for preferences
295 */
296 public static void setDefaults() {
297 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
298 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS, true);
299 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT, "http://edit.br.fgov.be/edit_wp5/v1/areas.php");
300 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, true);
301 }
302
303 /**
304 * <p>checkNomenclaturalCode</p>
305 */
306 public static void checkNomenclaturalCode() {
307 // First time Editor is opened, no nomenclatural code has been set
308 if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
309
310 StoreUtil.info("No nomencatural code set.");
311
312 Shell shell = StoreUtil.getShell();
313
314 // Query user re: preferred nom. code
315 Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
316 dialog.open();
317
318 // Short message confirming user's choice
319 NomenclaturalCode code = PreferencesUtil.getPreferredNomenclaturalCode();
320 MessageDialog.openInformation(shell, "Nomenclatural code set",
321 "The following has been set as your preferred nomenclatural code:\n\n\t" +
322 NomenclaturalCodeHelper.getDescription(code) + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
323 }
324 }
325
326 /**
327 * <p>getMapServiceAccessPoint</p>
328 *
329 * @return a {@link java.lang.String} object.
330 */
331 public static String getMapServiceAccessPoint() {
332 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
333 }
334
335 /**
336 * <p>shouldConnectAtStartUp</p>
337 *
338 * @return a boolean.
339 */
340 public static boolean shouldConnectAtStartUp() {
341 return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
342 }
343
344 /**
345 * <p>getDefaultFeatureTreeForTextualDescription</p>
346 *
347 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
348 */
349 public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
350 String uuidString = getPreferenceStore().getString(FEATURE_TREE_DEFAULT_TEXT);
351 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(IFeatureTreeService.class).load(UUID.fromString(uuidString));
352 }
353
354 /**
355 * <p>getDefaultFeatureTreeForStructuredDescription</p>
356 *
357 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
358 */
359 public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
360 String uuidString = getPreferenceStore().getString(FEATURE_TREE_DEFAULT_STRUCTURE);
361 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(IFeatureTreeService.class).load(UUID.fromString(uuidString));
362 }
363
364 /**
365 * <p>setSortRanksHierarchichally</p>
366 *
367 * @param selection a boolean.
368 */
369 public static void setSortRanksHierarchichally(boolean selection) {
370 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
371 }
372
373 /**
374 * <p>getSortRanksHierarchichally</p>
375 *
376 * @return a boolean.
377 */
378 public static boolean getSortRanksHierarchichally(){
379 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
380 }
381
382 public static boolean isMultilanguageTextEditingCapability() {
383 return getPreferenceStore().getBoolean(MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
384 }
385
386 public static Language getGlobalLanguage(){
387 String languageUuidString = getPreferenceStore().getString(GLOBAL_LANGUAGE_UUID);
388
389 if(CdmUtils.isEmpty(languageUuidString)){
390 return Language.DEFAULT();
391 }
392
393 UUID languageUuid = UUID.fromString(languageUuidString);
394 return (Language) CdmStore.getService(ITermService.class).load(languageUuid);
395 }
396
397 public static void setGlobalLanguage(Language language){
398 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID, language.getUuid().toString());
399 CdmStore.setDefaultLanguage(language);
400 }
401
402 /**
403 * @return
404 */
405 public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
406 List<MarkerType> markerTypes = CdmStore.getTermManager().getPreferredMarkerTypes();
407
408 Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
409
410 for(MarkerType markerType : markerTypes){
411 String name = getMarkerTypeEditingPreferenceKey(markerType);
412 Boolean value = getPreferenceStore().getBoolean(name);
413
414 result.put(markerType, value);
415 }
416
417 return result;
418 }
419
420 /**
421 * @param markerTypeEditingMap
422 */
423 public static void setEditMarkerTypePreferences(
424 Map<MarkerType, Boolean> markerTypeEditingMap) {
425 for(MarkerType markerType : markerTypeEditingMap.keySet()){
426 String name = getMarkerTypeEditingPreferenceKey(markerType);
427 getPreferenceStore().setValue(name, markerTypeEditingMap.get(markerType));
428 }
429
430 }
431
432 private static String getMarkerTypeEditingPreferenceKey(MarkerType markerType){
433 markerType = (MarkerType) HibernateProxyHelper.deproxy(markerType);
434 return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
435 }
436
437 /**
438 * <p>setEditMarkerTypePreference</p>
439 *
440 * @param input a {@link org.eclipse.ui.IEditorInput} object.
441 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
442 * @param edit a boolean.
443 */
444 public static void setEditMarkerTypePreference(MarkerType markerType, boolean edit) {
445 getPreferenceStore().setValue(getMarkerTypeEditingPreferenceKey(markerType), edit);
446 }
447
448 /**
449 * @return
450 */
451 public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
452 DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator.NewInstance();
453 configurator.setMoveDerivedUnitMediaToGallery(true);
454 configurator.setMoveFieldObjectMediaToGallery(true);
455 return configurator;
456 }
457
458 }