Project

General

Profile

Download (16.7 KB) Statistics
| Branch: | Tag: | Revision:
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.FindTaxaAndNamesConfiguratorImpl;
28
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
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.ISimpleTerm;
34
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.common.MarkerType;
36
import eu.etaxonomy.cdm.model.common.TermBase;
37
import eu.etaxonomy.cdm.model.description.FeatureTree;
38
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
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.MessagingUtils;
44
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
47

    
48
/**
49
 * <p>
50
 * PreferencesUtil class.
51
 * </p>
52
 *
53
 * @author p.ciardelli
54
 * @author n.hoffmann
55
 * @created 05.12.2008
56
 * @version 1.0
57
 */
58
public class PreferencesUtil implements IPreferenceKeys {
59

    
60
	/**
61
	 *
62
	 */
63
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
64

    
65
	/**
66
	 * <p>
67
	 * getPreferenceStore
68
	 * </p>
69
	 *
70
	 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
71
	 */
72
	public static IPreferenceStore getPreferenceStore() {
73
		return TaxeditorStorePlugin.getDefault().getPreferenceStore();
74
	}
75

    
76
	/**
77
	 * <p>
78
	 * setPreferredNomenclaturalCode
79
	 * </p>
80
	 *
81
	 * @param preferredCode
82
	 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
83
	 *            object.
84
	 */
85
	public static void setPreferredNomenclaturalCode(
86
			NomenclaturalCode preferredCode) {
87
		getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
88
				getPreferenceKey(preferredCode));
89
	}
90

    
91
	/**
92
	 * <p>
93
	 * getPreferredNomenclaturalCode
94
	 * </p>
95
	 *
96
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
97
	 */
98
	public static NomenclaturalCode getPreferredNomenclaturalCode() {
99

    
100
		for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
101
			String preferredCode = getPreferenceStore().getString(
102
					PREFERRED_NOMENCLATURAL_CODE_KEY);
103
			if (getPreferenceKey(code).equals(preferredCode)) {
104
				return code;
105
			}
106
		}
107
		return null;
108
	}
109

    
110
	/**
111
	 * Get the match strategy for the given class that was stored in preferences
112
	 * or the default strategy if it was not stored in preferences
113
	 *
114
	 * @param clazz
115
	 *            a {@link java.lang.Class} object.
116
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
117
	 */
118
	public static IMatchStrategy getMatchStrategy(Class clazz) {
119
		String className = clazz.getName();
120
		if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
121
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
122

    
123
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
124
				String matchModeName = getPreferenceStore().getString(
125
						getMatchStrategyFieldName(className, fieldName));
126
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
127
				try {
128
					matchStrategy.setMatchMode(fieldName, matchMode);
129
				} catch (MatchException e) {
130
					MessagingUtils.error(PreferencesUtil.class, e);
131
					throw new RuntimeException(e);
132
				}
133
			}
134

    
135
			return matchStrategy;
136
		}
137
		return getDefaultMatchStrategy(clazz);
138
	}
139

    
140
	/**
141
	 * Stores a matchStrategy into the preference store.
142
	 *
143
	 * @param matchStrategy
144
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
145
	 *            object.
146
	 */
147
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
148
		String className = matchStrategy.getMatchClass().getName();
149
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
150

    
151
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
152

    
153
		for (String fieldName : matchFields) {
154
			getPreferenceStore().setValue(
155
					getMatchStrategyFieldName(className, fieldName),
156
					matchStrategy.getMatchMode(fieldName).name());
157
		}
158
	}
159

    
160
	/**
161
	 * Helper method to create the preference property for a match field.
162
	 *
163
	 * @param className
164
	 * @param fieldName
165
	 * @return
166
	 */
167
	private static String getMatchStrategyFieldName(String className,
168
			String fieldName) {
169
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
170
	}
171

    
172
	/**
173
	 * Returns the default match strategy for a given class.
174
	 *
175
	 * @param clazz
176
	 *            a {@link java.lang.Class} object.
177
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
178
	 */
179
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
180
		return DefaultMatchStrategy.NewInstance(clazz);
181
	}
182

    
183
	/**
184
	 * <p>
185
	 * getDateFormatPattern
186
	 * </p>
187
	 *
188
	 * @return a {@link java.lang.String} object.
189
	 */
190
	public static String getDateFormatPattern() {
191
		// TODO make this configurable in properties
192
		String pattern = "Y-M-d H:m";
193
		return pattern;
194
	}
195

    
196
	/**
197
	 * <p>
198
	 * addTermToPreferredTerms
199
	 * </p>
200
	 *
201
	 * @param term
202
	 *            a T object.
203
	 * @param <T>
204
	 *            a T object.
205
	 */
206
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
207

    
208
		// VocabularyEnum vocabulary =
209
		// VocabularyEnum.getVocabularyEnum(term.getClass());
210
		//
211
		// getPreferenceStore().setValue(getPreferenceKey(term),
212
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
213
		//
214
		// firePreferencesChanged(term.getClass());
215
	}
216

    
217
	/**
218
	 * Construct a unique key using the CdmBase object's uuid
219
	 *
220
	 * @param cdmBase
221
	 * @return
222
	 */
223
	private static String getPreferenceKey(ICdmBase cdmBase) {
224
		cdmBase = (ICdmBase) HibernateProxyHelper.deproxy(cdmBase);
225

    
226
		String key = cdmBase.getClass().getName().concat(".")
227
				.concat(cdmBase.getUuid().toString());
228
		if (key.contains("javassist")) {
229
			MessagingUtils.info("proxy");
230
		}
231
		return key;
232
	}
233

    
234
	/**
235
	 * Construct a unique key using the CdmBase object's uuid
236
	 *
237
	 * @param cdmBase
238
	 * @return
239
	 */
240
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
241
		simpleTerm = (ISimpleTerm) HibernateProxyHelper.deproxy(simpleTerm);
242
		String key = simpleTerm.getClass().getName().concat(".")
243
				.concat(simpleTerm.getUuid().toString());
244
		if (key.contains("javassist")) {
245
			MessagingUtils.warn(PreferencesUtil.class,
246
					"Trying to persist a preference based on a proxy class.");
247
		}
248
		return key;
249
	}
250

    
251

    
252

    
253
	/**
254
	 * Construct a unique key using the CdmBase object's uuid
255
	 *
256
	 * @param cdmBase
257
	 * @return
258
	 */
259
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
260
		definedTerm = (IDefinedTerm) HibernateProxyHelper.deproxy(definedTerm);
261
		String key = definedTerm.getClass().getName().concat(".")
262
				.concat(definedTerm.getUuid().toString());
263
		if (key.contains("javassist")) {
264
			MessagingUtils.warn(PreferencesUtil.class,
265
					"Trying to persist a preference based on a proxy class.");
266
		}
267
		return key;
268
	}
269

    
270
	/**
271
	 * Retrieves search preferences from the preference store
272
	 *
273
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
274
	 */
275
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
276
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
277

    
278
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
279
				TAXON_SERVICE_CONFIGURATOR_TAXA));
280
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
281
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
282
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
283
				TAXON_SERVICE_CONFIGURATOR_NAMES));
284
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
285
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
286

    
287
		return configurator;
288
	}
289

    
290
	/**
291
	 * create new preferences, setting all search options to true
292
	 *
293
	 * @return a
294
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
295
	 *         object.
296
	 */
297
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
298
		IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
299

    
300
		configurator.setDoTaxa(true);
301
		configurator.setDoSynonyms(true);
302
		configurator.setDoNamesWithoutTaxa(true);
303
		configurator.setDoTaxaByCommonNames(true);
304

    
305
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
306
				"name", "name.$", "relationsFromThisTaxon.$"));
307

    
308
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
309
				"name", "name.$", "synonymRelations.relatedTo.*"));
310

    
311
		// DEFAULT VALUES
312
		// match mode is a simple like, actually all other match modes are kind
313
		// of bogus
314
		configurator
315
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
316
		// we set page number and size here as this should always be unlimited
317
		configurator.setPageNumber(0);
318
		// TODO currently limit results to 10000
319
		configurator.setPageSize(10000);
320

    
321
		return configurator;
322
	}
323

    
324
	/**
325
	 * Store search preferences
326
	 *
327
	 * @param configurator
328
	 *            a
329
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
330
	 *            object.
331
	 */
332
	public static void setSearchConfigurator(
333
			IFindTaxaAndNamesConfigurator configurator) {
334
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
335
				configurator.isDoTaxa());
336
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
337
				configurator.isDoSynonyms());
338
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
339
				configurator.isDoNamesWithoutTaxa());
340
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
341
				configurator.isDoTaxaByCommonNames());
342
	}
343

    
344
	/**
345
	 * <p>
346
	 * firePreferencesChanged
347
	 * </p>
348
	 *
349
	 * @param clazz
350
	 *            a {@link java.lang.Class} object.
351
	 */
352
	public static void firePreferencesChanged(Class clazz) {
353
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
354
				null, clazz);
355
	}
356

    
357
	/**
358
	 * Set default values for preferences
359
	 */
360
	public static void setDefaults() {
361
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
362
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
363
				true);
364
		getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
365
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
366
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
367
		getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
368
		getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
369
				"http://www.biodiversitylibrary.org/openurl");
370
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
371
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
372
	}
373

    
374
	/**
375
	 * <p>
376
	 * checkNomenclaturalCode
377
	 * </p>
378
	 */
379
	public static void checkNomenclaturalCode() {
380
		// First time Editor is opened, no nomenclatural code has been set
381
		
382
		
383
		if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
384
			PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
385
			/*
386

    
387
			StoreUtil.info("No nomencatural code set.");
388

    
389
			Shell shell = StoreUtil.getShell();
390

    
391
		 Query user re: preferred nom. code
392
			Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
393
			dialog.open();
394

    
395
			// Short message confirming user's choice
396
			NomenclaturalCode code = PreferencesUtil
397
					.getPreferredNomenclaturalCode();
398
			MessageDialog
399
					.openInformation(
400
							shell,
401
							"Nomenclatural code set",
402
							"The following has been set as your preferred nomenclatural code:\n\n\t"
403
									+ NomenclaturalCodeHelper
404
											.getDescription(code)
405
									+ "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
406
		}
407
	}
408

    
409
	/**
410
	 * <p>
411
	 * getMapServiceAccessPoint
412
	 * </p>
413
	 *
414
	 * @return a {@link java.lang.String} object.
415
	 */
416
	public static String getMapServiceAccessPoint() {
417
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
418
	}
419

    
420
	/**
421
	 * <p>
422
	 * shouldConnectAtStartUp
423
	 * </p>
424
	 *
425
	 * @return a boolean.
426
	 */
427
	public static boolean shouldConnectAtStartUp() {
428
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
429
		//return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
430
		return false;
431
	}
432

    
433
	/**
434
	 * <p>
435
	 * getDefaultFeatureTreeForTextualDescription
436
	 * </p>
437
	 *
438
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
439
	 */
440
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
441
		String uuidString = getPreferenceStore().getString(
442
				FEATURE_TREE_DEFAULT_TEXT);
443
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
444
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
445
	}
446

    
447
	/**
448
	 * <p>
449
	 * getDefaultFeatureTreeForStructuredDescription
450
	 * </p>
451
	 *
452
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
453
	 */
454
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
455
		String uuidString = getPreferenceStore().getString(
456
				FEATURE_TREE_DEFAULT_STRUCTURE);
457
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
458
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
459
	}
460

    
461
	/**
462
	 * <p>
463
	 * setSortRanksHierarchichally
464
	 * </p>
465
	 *
466
	 * @param selection
467
	 *            a boolean.
468
	 */
469
	public static void setSortRanksHierarchichally(boolean selection) {
470
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
471
	}
472

    
473
	/**
474
	 * <p>
475
	 * getSortRanksHierarchichally
476
	 * </p>
477
	 *
478
	 * @return a boolean.
479
	 */
480
	public static boolean getSortRanksHierarchichally() {
481
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
482
	}
483

    
484
	public static boolean isMultilanguageTextEditingCapability() {
485
		return getPreferenceStore().getBoolean(
486
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
487
	}
488

    
489
	public static Language getGlobalLanguage() {
490
		String languageUuidString = getPreferenceStore().getString(
491
				GLOBAL_LANGUAGE_UUID);
492

    
493
		if (CdmUtils.isBlank(languageUuidString)) {
494
			return Language.getDefaultLanguage();
495
		}
496

    
497
		UUID languageUuid = UUID.fromString(languageUuidString);
498
		return (Language) CdmStore.getService(ITermService.class).load(
499
				languageUuid);
500
	}
501

    
502
	public static void setGlobalLanguage(Language language) {
503
		getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,
504
				language.getUuid().toString());
505
		CdmStore.setDefaultLanguage(language);
506
	}
507

    
508
	/**
509
	 * @return
510
	 */
511
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
512
		List<MarkerType> markerTypes = CdmStore.getTermManager()
513
				.getPreferredTerms(MarkerType.class);
514

    
515
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
516

    
517
		for (MarkerType markerType : markerTypes) {
518
			String name = getMarkerTypeEditingPreferenceKey(markerType);
519
			Boolean value = getPreferenceStore().getBoolean(name);
520

    
521
			result.put(markerType, value);
522
		}
523

    
524
		return result;
525
	}
526

    
527
	/**
528
	 * @param markerTypeEditingMap
529
	 */
530
	public static void setEditMarkerTypePreferences(
531
			Map<MarkerType, Boolean> markerTypeEditingMap) {
532
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
533
			String name = getMarkerTypeEditingPreferenceKey(markerType);
534
			getPreferenceStore().setValue(name,
535
					markerTypeEditingMap.get(markerType));
536
		}
537

    
538
	}
539

    
540
	private static String getMarkerTypeEditingPreferenceKey(
541
			MarkerType markerType) {
542
		markerType = (MarkerType) HibernateProxyHelper.deproxy(markerType);
543
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
544
	}
545

    
546
	/**
547
	 * <p>
548
	 * setEditMarkerTypePreference
549
	 * </p>
550
	 *
551
	 * @param input
552
	 *            a {@link org.eclipse.ui.IEditorInput} object.
553
	 * @param markerType
554
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
555
	 * @param edit
556
	 *            a boolean.
557
	 */
558
	public static void setEditMarkerTypePreference(MarkerType markerType,
559
			boolean edit) {
560
		getPreferenceStore().setValue(
561
				getMarkerTypeEditingPreferenceKey(markerType), edit);
562
	}
563

    
564
	/**
565
	 * @return
566
	 */
567
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
568
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
569
				.NewInstance();
570
		configurator.setMoveDerivedUnitMediaToGallery(true);
571
		configurator.setMoveFieldObjectMediaToGallery(true);
572
		return configurator;
573
	}
574

    
575
}
(13-13/19)