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