Project

General

Profile

Download (50 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.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.net.URI;
17
import java.net.URISyntaxException;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Properties;
24
import java.util.Set;
25
import java.util.StringTokenizer;
26
import java.util.UUID;
27

    
28
import org.apache.commons.lang.StringUtils;
29
import org.apache.log4j.Logger;
30
import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
31
import org.eclipse.jface.preference.IPreferenceStore;
32
import org.eclipse.jface.window.Window;
33
import org.eclipse.swt.widgets.Composite;
34
import org.eclipse.swt.widgets.Control;
35
import org.eclipse.swt.widgets.Shell;
36
import org.eclipse.ui.PlatformUI;
37

    
38
import eu.etaxonomy.cdm.api.application.ICdmRepository;
39
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
40
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
41
import eu.etaxonomy.cdm.api.service.ITermService;
42
import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
43
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
44
import eu.etaxonomy.cdm.common.CdmUtils;
45
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
46
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
47
import eu.etaxonomy.cdm.model.common.ICdmBase;
48
import eu.etaxonomy.cdm.model.common.IDefinedTerm;
49
import eu.etaxonomy.cdm.model.common.ISimpleTerm;
50
import eu.etaxonomy.cdm.model.common.Language;
51
import eu.etaxonomy.cdm.model.common.MarkerType;
52
import eu.etaxonomy.cdm.model.common.TermBase;
53
import eu.etaxonomy.cdm.model.description.FeatureTree;
54
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
55
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
56
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
57
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
58
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
59
import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
60
import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
61
import eu.etaxonomy.cdm.strategy.match.MatchException;
62
import eu.etaxonomy.cdm.strategy.match.MatchMode;
63
import eu.etaxonomy.taxeditor.model.AbstractUtility;
64
import eu.etaxonomy.taxeditor.model.MessagingUtils;
65
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
66
import eu.etaxonomy.taxeditor.store.CdmStore;
67
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
68
import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
69

    
70
/**
71
 * <p>
72
 * PreferencesUtil class.
73
 * </p>
74
 *
75
 * @author p.ciardelli
76
 * @author n.hoffmann
77
 * @created 05.12.2008
78
 * @version 1.0
79
 */
80
public class PreferencesUtil implements IPreferenceKeys {
81

    
82
	/**
83
	 *
84
	 */
85
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
86

    
87
	public static final String P2_REPOSITORIES_DELIM = ",";
88
	public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
89

    
90
	private final static Logger logger = Logger.getLogger(PreferencesUtil.class);
91

    
92
	/**
93
	 * <p>
94
	 * getPreferenceStore
95
	 * </p>
96
	 *
97
	 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
98
	 */
99
	public static IPreferenceStore getPreferenceStore() {
100
		return TaxeditorStorePlugin.getDefault().getPreferenceStore();
101
	}
102

    
103
	/**
104
	 * <p>
105
	 * setPreferredNomenclaturalCode
106
	 * </p>
107
	 *
108
	 * @param preferredCode
109
	 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
110
	 *            object.
111
	 */
112
	public static CdmPreference setPreferredNomenclaturalCode(
113
			NomenclaturalCode preferredCode, boolean local) {
114
	    if (local){
115
	        getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
116
                    getPreferenceKey(preferredCode));
117
	    }
118
	    else{
119
    		ICdmRepository controller;
120
    		controller = CdmStore.getCurrentApplicationConfiguration();
121
    		PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
122
    		CdmPreference preference = null;
123
    		if (controller == null){
124
    			return null;
125
    		}
126
    		if (preferredCode == null){
127
    			preference = controller.getPreferenceService().find(key);
128
    			if (preference == null){
129
    				return null;
130
    			} else{
131
    				int index = StringUtils.lastIndexOf(preference.getValue(), ".");
132
    				UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
133
    				preferredCode = NomenclaturalCode.getByUuid(uuid);
134

    
135
    				getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
136
    	                    getPreferenceKey(preferredCode));
137
    				getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,preference.isAllowOverride());
138
    				return preference;
139
    			}
140
    		} else{
141
    			preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferredCode.getKey());
142
    			controller.getPreferenceService().set(preference);
143

    
144
    		}
145
	    }
146
        return null;
147

    
148

    
149

    
150
	}
151

    
152
	public static NomenclaturalCode getPreferredNomenclaturalCode(){
153
	    if (getPreferenceStore().getBoolean(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
154
            return getPreferredNomenclaturalCode(true);
155
        } else{
156
            return getPreferredNomenclaturalCode(false);
157
        }
158
	}
159

    
160
	/**
161
	 * <p>
162
	 * getPreferredNomenclaturalCode
163
	 * </p>
164
	 *
165
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
166
	 */
167
	public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
168

    
169
		CdmPreference pref = null;
170
		if (!useLocal){
171
		    pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
172
		}
173

    
174
	    String preferredCode;
175
	    if(pref == null){
176
	    	preferredCode = getPreferenceStore().getString(
177
					PREFERRED_NOMENCLATURAL_CODE_KEY);
178

    
179
	    }else{
180
	    	preferredCode = pref.getValue();
181
	    }
182

    
183
		for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
184
			if (getPreferenceKey(code).equals(preferredCode)) {
185
				return code;
186
			}
187
		}
188
		return null;
189
	}
190

    
191
	public static boolean isShowTaxonAssociations(){
192
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
193
            return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
194
        } else{
195
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
196

    
197
            return Boolean.valueOf(pref.getValue());
198
        }
199
    }
200

    
201
	public static boolean isShowLifeForm(){
202
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
203
            return getPreferenceStore().getBoolean(SHOW_LIFE_FORM);
204
        } else{
205
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
206

    
207
            return Boolean.valueOf(pref.getValue());
208
        }
209
    }
210

    
211
	public static boolean isDeterminationOnlyForFieldUnits(){
212
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
213
            return getPreferenceStore().getBoolean(DETERMINATION_ONLY_FOR_FIELD_UNITS);
214
        } else{
215
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
216

    
217
            return Boolean.valueOf(pref.getValue());
218
        }
219
    }
220

    
221
	public static boolean isCollectingAreaInGeneralSection(){
222
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
223
            return getPreferenceStore().getBoolean(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION);
224
        } else{
225
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
226

    
227
            return Boolean.valueOf(pref.getValue());
228
        }
229
    }
230

    
231
	public static boolean isShowSimpleDetailsView(){
232
        if (getPreferenceStore().getBoolean(OVERRIDE_NAME_DETAILS) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_NAME_DETAILS)) {
233
            return getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION);
234
        } else{
235
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
236

    
237
            return Boolean.valueOf(pref.getValue());
238
        }
239
    }
240

    
241

    
242

    
243
    public static CdmPreference getPreferenceFromDB(PreferencePredicate predicate){
244
        ICdmRepository controller;
245
        CdmPreference pref = null;
246

    
247
        try{
248
            controller = CdmStore.getCurrentApplicationConfiguration();
249
            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), predicate);
250
            pref = controller.getPreferenceService().find(key);
251
        }catch(Exception e){
252
            e.printStackTrace();
253
        }
254

    
255
        return pref;
256

    
257
    }
258

    
259

    
260
	public static String getPreferredDefaultLangugae(){
261
	    String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
262
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
263
	        return preferredLanguage;
264
	    }
265
	    return null;
266
	}
267

    
268
	/**
269
	 * Get the match strategy for the given class that was stored in preferences
270
	 * or the default strategy if it was not stored in preferences
271
	 *
272
	 * @param clazz
273
	 *            a {@link java.lang.Class} object.
274
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
275
	 */
276
	public static IMatchStrategy getMatchStrategy(Class clazz) {
277
		String className = clazz.getName();
278
		if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
279
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
280

    
281
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
282
				String matchModeName = getPreferenceStore().getString(
283
						getMatchStrategyFieldName(className, fieldName));
284
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
285
				try {
286
					matchStrategy.setMatchMode(fieldName, matchMode);
287
				} catch (MatchException e) {
288
					MessagingUtils.error(PreferencesUtil.class, e);
289
					throw new RuntimeException(e);
290
				}
291
			}
292

    
293
			return matchStrategy;
294
		}
295
		return getDefaultMatchStrategy(clazz);
296
	}
297

    
298
	/**
299
	 * Stores a matchStrategy into the preference store.
300
	 *
301
	 * @param matchStrategy
302
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
303
	 *            object.
304
	 */
305
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
306
		String className = matchStrategy.getMatchClass().getName();
307
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
308

    
309
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
310

    
311
		for (String fieldName : matchFields) {
312
			getPreferenceStore().setValue(
313
					getMatchStrategyFieldName(className, fieldName),
314
					matchStrategy.getMatchMode(fieldName).name());
315
		}
316
	}
317

    
318
	/**
319
	 * Helper method to create the preference property for a match field.
320
	 *
321
	 * @param className
322
	 * @param fieldName
323
	 * @return
324
	 */
325
	private static String getMatchStrategyFieldName(String className,
326
			String fieldName) {
327
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
328
	}
329

    
330
	/**
331
	 * Returns the default match strategy for a given class.
332
	 *
333
	 * @param clazz
334
	 *            a {@link java.lang.Class} object.
335
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
336
	 */
337
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
338
		return DefaultMatchStrategy.NewInstance(clazz);
339
	}
340

    
341
	/**
342
	 * <p>
343
	 * getDateFormatPattern
344
	 * </p>
345
	 *
346
	 * @return a {@link java.lang.String} object.
347
	 */
348
	public static String getDateFormatPattern() {
349
		// TODO make this configurable in properties
350
		String pattern = "Y-M-d H:m";
351
		return pattern;
352
	}
353

    
354
	/**
355
	 * <p>
356
	 * addTermToPreferredTerms
357
	 * </p>
358
	 *
359
	 * @param term
360
	 *            a T object.
361
	 * @param <T>
362
	 *            a T object.
363
	 */
364
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
365

    
366
		// VocabularyEnum vocabulary =
367
		// VocabularyEnum.getVocabularyEnum(term.getClass());
368
		//
369
		// getPreferenceStore().setValue(getPreferenceKey(term),
370
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
371
		//
372
		// firePreferencesChanged(term.getClass());
373
	}
374

    
375
	/**
376
	 * Construct a unique key using the CdmBase object's uuid
377
	 *
378
	 * @param cdmBase
379
	 * @return
380
	 */
381
	private static String getPreferenceKey(ICdmBase cdmBase) {
382
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
383

    
384
		String key = cdmBase.getClass().getName().concat(".")
385
				.concat(cdmBase.getUuid().toString());
386
		if (key.contains("javassist")) {
387
			MessagingUtils.info("proxy");
388
		}
389
		return key;
390
	}
391

    
392
	/**
393
	 * Construct a unique key using the CdmBase object's uuid
394
	 *
395
	 * @param cdmBase
396
	 * @return
397
	 */
398
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
399
		simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
400
		String key = simpleTerm.getClass().getName().concat(".")
401
				.concat(simpleTerm.getUuid().toString());
402
		if (key.contains("javassist")) {
403
			MessagingUtils.warn(PreferencesUtil.class,
404
					"Trying to persist a preference based on a proxy class.");
405
		}
406
		return key;
407
	}
408

    
409

    
410

    
411
	/**
412
	 * Construct a unique key using the CdmBase object's uuid
413
	 *
414
	 * @param cdmBase
415
	 * @return
416
	 */
417
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
418
		definedTerm = HibernateProxyHelper.deproxy(definedTerm);
419
		String key = definedTerm.getClass().getName().concat(".")
420
				.concat(definedTerm.getUuid().toString());
421
		if (key.contains("javassist")) {
422
			MessagingUtils.warn(PreferencesUtil.class,
423
					"Trying to persist a preference based on a proxy class.");
424
		}
425
		return key;
426
	}
427

    
428
	/**
429
	 * Retrieves search preferences from the preference store
430
	 *
431
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
432
	 */
433
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
434
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
435

    
436
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
437
				TAXON_SERVICE_CONFIGURATOR_TAXA));
438
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
439
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
440
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
441
				TAXON_SERVICE_CONFIGURATOR_NAMES));
442
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
443
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
444
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
445

    
446
		return configurator;
447
	}
448

    
449
	/**
450
	 * create new preferences, setting all search options to true
451
	 *
452
	 * @return a
453
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
454
	 *         object.
455
	 */
456
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
457
		IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
458

    
459
		configurator.setDoTaxa(true);
460
		configurator.setDoSynonyms(true);
461
		configurator.setDoNamesWithoutTaxa(true);
462
		configurator.setDoTaxaByCommonNames(true);
463

    
464
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
465
				"name", "name.$", "relationsFromThisTaxon.$"));
466

    
467
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
468
				"name", "name.$", "synonymRelations.relatedTo.*"));
469

    
470
		// DEFAULT VALUES
471
		// match mode is a simple like, actually all other match modes are kind
472
		// of bogus
473
		configurator
474
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
475
		// we set page number and size here as this should always be unlimited
476
		configurator.setPageNumber(0);
477
		// TODO currently limit results to 10000
478
		configurator.setPageSize(10000);
479

    
480
		return configurator;
481
	}
482

    
483
	/**
484
	 * Store search preferences
485
	 *
486
	 * @param configurator
487
	 *            a
488
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
489
	 *            object.
490
	 */
491
	public static void setSearchConfigurator(
492
			IFindTaxaAndNamesConfigurator configurator) {
493
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
494
				configurator.isDoTaxa());
495
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
496
				configurator.isDoSynonyms());
497
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
498
				configurator.isDoNamesWithoutTaxa());
499
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
500
				configurator.isDoTaxaByCommonNames());
501
	}
502

    
503
	/**
504
	 * <p>
505
	 * firePreferencesChanged
506
	 * </p>
507
	 *
508
	 * @param clazz
509
	 *            a {@link java.lang.Class} object.
510
	 */
511
	public static void firePreferencesChanged(Class clazz) {
512
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
513
				null, clazz);
514
	}
515

    
516
	/**
517
	 * Set default values for preferences
518
	 */
519
	public static void setDefaults() {
520
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
521
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
522
				true);
523
		getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
524
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
525
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
526
		getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
527
		getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
528
				"http://www.biodiversitylibrary.org/openurl");
529
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
530
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
531
		getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, false);
532
		getPreferenceStore().setDefault(CHECKLIST_ID_IN_VOCABULARY, true);
533
		getPreferenceStore().setDefault(CHECKLIST_SYMBOL, false);
534
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, true);
535
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE, true);
536
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, true);
537
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_HYBRID, true);
538
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_LSID, true);
539
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, true);
540
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAMECACHE, true);
541
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, true);
542
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, true);
543
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, true);
544
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, true);
545
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_RANK, true);
546
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, true);
547
		getPreferenceStore().setDefault(SORT_NODES_NATURALLY, false);
548
		getPreferenceStore().setDefault(SORT_NODES_ALPHABETICALLY, false);
549

    
550
	}
551

    
552
	/**
553
	 * <p>
554
	 * checkNomenclaturalCode
555
	 * </p>
556
	 */
557
	public static void checkNomenclaturalCode() {
558
		// First time Editor is opened, no nomenclatural code has been set
559

    
560

    
561
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
562
			PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP, true);
563
		}
564

    
565

    
566

    
567
	}
568
	public static void setNomenclaturalCodePreferences(){
569
	    ICdmRepository controller;
570
        controller = CdmStore.getCurrentApplicationConfiguration();
571
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
572
        CdmPreference preference = null;
573
        if (controller == null){
574
            return ;
575
        }
576
        preference = controller.getPreferenceService().find(key);
577
        if (preference == null){
578
            return;
579
        }
580
        getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
581

    
582
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
583
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
584
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
585

    
586
        getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
587
                getPreferenceKey(preferredCode));
588

    
589
        if (!preference.isAllowOverride()){
590
            getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
591
                    getPreferenceKey(preferredCode));
592
        }
593
	}
594

    
595
	public static void checkDefaultLanguage(){
596
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
597
	       Shell shell = AbstractUtility.getShell();
598
	       int open = new DefaultLanguageDialog(shell).open();
599
	       if(open == Window.OK){
600
	           PlatformUI.getWorkbench().restart();
601
	       }
602
	    }else{
603
	        //TODO:In case of a reinstall, the config.ini will be overwritten
604
	        //     here you create config.ini with the stored key from preferences
605
	    }
606
	}
607

    
608
	/**
609
	 * <p>
610
	 * getMapServiceAccessPoint
611
	 * </p>
612
	 *
613
	 * @return a {@link java.lang.String} object.
614
	 */
615
	public static String getMapServiceAccessPoint() {
616
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
617
	}
618

    
619
	/**
620
	 * <p>
621
	 * shouldConnectAtStartUp
622
	 * </p>
623
	 *
624
	 * @return a boolean.
625
	 */
626
	public static boolean shouldConnectAtStartUp() {
627
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
628
		//return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
629
		return false;
630
	}
631

    
632
	/**
633
	 * <p>
634
	 * getDefaultFeatureTreeForTextualDescription
635
	 * </p>
636
	 *
637
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
638
	 */
639
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
640
		String uuidString = getPreferenceStore().getString(
641
				FEATURE_TREE_DEFAULT_TEXT);
642
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
643
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
644
	}
645

    
646
	/**
647
	 * <p>
648
	 * getDefaultFeatureTreeForStructuredDescription
649
	 * </p>
650
	 *
651
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
652
	 */
653
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
654
		String uuidString = getPreferenceStore().getString(
655
				FEATURE_TREE_DEFAULT_STRUCTURE);
656
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
657
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
658
	}
659

    
660
	/**
661
	 * <p>
662
	 * setSortRanksHierarchichally
663
	 * </p>
664
	 *
665
	 * @param selection
666
	 *            a boolean.
667
	 */
668
	public static void setSortRanksHierarchichally(boolean selection) {
669
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
670
	}
671

    
672
	/**
673
	 * <p>
674
	 * getSortRanksHierarchichally
675
	 * </p>
676
	 *
677
	 * @return a boolean.
678
	 */
679
	public static boolean getSortRanksHierarchichally() {
680
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
681
	}
682

    
683
	public static boolean isMultilanguageTextEditingCapability() {
684
		return getPreferenceStore().getBoolean(
685
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
686
	}
687

    
688
	public static Language getGlobalLanguage() {
689

    
690

    
691
		String languageUuidString = getPreferenceStore().getString(
692
				GLOBAL_LANGUAGE_UUID);
693

    
694
		if(!CdmStore.isActive()) {
695
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
696
            return null;
697
        }
698

    
699
		if (CdmUtils.isBlank(languageUuidString)) {
700
			return Language.getDefaultLanguage();
701
		}
702

    
703
		UUID languageUuid = UUID.fromString(languageUuidString);
704
		return (Language) CdmStore.getService(ITermService.class).load(
705
				languageUuid);
706
	}
707

    
708
	public static void setGlobalLanguage(Language language) {
709
	    if(language != null) {
710
	        getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
711
	        CdmStore.setDefaultLanguage(language);
712
	    }
713

    
714
	}
715

    
716
	/**
717
	 * @return
718
	 */
719
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
720
		List<MarkerType> markerTypes = CdmStore.getTermManager()
721
				.getPreferredTerms(MarkerType.class);
722

    
723
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
724

    
725
		for (MarkerType markerType : markerTypes) {
726
			String name = getMarkerTypeEditingPreferenceKey(markerType);
727
			Boolean value = getPreferenceStore().getBoolean(name);
728

    
729
			result.put(markerType, value);
730
		}
731

    
732
		return result;
733
	}
734

    
735
	/**
736
	 * @param markerTypeEditingMap
737
	 */
738
	public static void setEditMarkerTypePreferences(
739
			Map<MarkerType, Boolean> markerTypeEditingMap) {
740
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
741
			String name = getMarkerTypeEditingPreferenceKey(markerType);
742
			getPreferenceStore().setValue(name,
743
					markerTypeEditingMap.get(markerType));
744
		}
745

    
746
	}
747

    
748
	private static String getMarkerTypeEditingPreferenceKey(
749
			MarkerType markerType) {
750
		markerType = HibernateProxyHelper.deproxy(markerType);
751
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
752
	}
753

    
754
	/**
755
	 * <p>
756
	 * setEditMarkerTypePreference
757
	 * </p>
758
	 *
759
	 * @param input
760
	 *            a {@link org.eclipse.ui.IEditorInput} object.
761
	 * @param markerType
762
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
763
	 * @param edit
764
	 *            a boolean.
765
	 */
766
	public static void setEditMarkerTypePreference(MarkerType markerType,
767
			boolean edit) {
768
		getPreferenceStore().setValue(
769
				getMarkerTypeEditingPreferenceKey(markerType), edit);
770
	}
771

    
772
	/**
773
	 * @return
774
	 */
775
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
776
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
777
				.NewInstance();
778
		configurator.setMoveDerivedUnitMediaToGallery(true);
779
		configurator.setMoveFieldObjectMediaToGallery(true);
780
		return configurator;
781
	}
782

    
783
	/**
784
	 * This method will write language properties to the config.ini located in the configuration folder
785
	 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
786
	 *
787
	 * @param setLanguage 0 is for german and 1 for english.
788
	 * @throws IOException
789
	 */
790
    public void writePropertyToConfigFile(int setLanguage) throws IOException {
791
        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
792
        //give warning to user if the directory has no write access
793
        if(file == null){
794
            throw new IOException();
795
        }
796
        Properties properties = load(file.getAbsolutePath()+"/config.ini");
797
        switch(setLanguage){
798
        case 0:
799
            properties.setProperty("osgi.nl", "de");
800
            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
801
            break;
802
        case 1:
803
            properties.setProperty("osgi.nl", "en");
804
            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
805
            break;
806
        default:
807
            break;
808
        }
809
        save(file+"/config.ini", properties);
810
    }
811

    
812
    /**
813
     * This method loads a property from a given file and returns it.
814
     *
815
     * @param filename
816
     * @return
817
     * @throws IOException
818
     */
819
    private Properties load(String filename) throws IOException {
820
        FileInputStream in = new FileInputStream(filename);
821
        Properties prop = new Properties();
822
        prop.load(in);
823
        in.close();
824
        return prop;
825
    }
826

    
827
    /**
828
     * This method saves a property to the specified file.
829
     *
830
     * @param filename
831
     * @param properties
832
     * @throws IOException
833
     */
834
    private void save(String filename, Properties properties) throws IOException{
835
        FileOutputStream fos =  new FileOutputStream(filename);
836
        properties.store(fos, "");
837
        fos.close();
838
    }
839

    
840
    /**
841
     * Saves a list of P2 Metadata Repositories as string with specified delimiters
842
     *
843
     * @param p2Repos
844
     */
845
    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
846
        StringBuilder sb = new StringBuilder();
847
        for(MetadataRepositoryElement p2Repo : p2Repos) {
848
            sb.append(P2_REPOSITORIES_DELIM);
849
            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
850
                sb.append("-");
851
            } else {
852
                sb.append(p2Repo.getName());
853
            }
854
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
855
            sb.append(p2Repo.getLocation().toString());
856
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
857
            sb.append(String.valueOf(p2Repo.isEnabled()));
858
        }
859
        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
860
    }
861

    
862

    
863
    /**
864
     * Retrieves a list of previously saved P2 repositories
865
     *
866
     * @return
867
     */
868
    public static List<MetadataRepositoryElement> getP2Repositories() {
869
        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
870
        String p2ReposPref =  getPreferenceStore().getString(P2_REPOSITORY_LIST);
871
        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
872
            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
873

    
874
            while(p2ReposPrefST.hasMoreTokens()) {
875
                String p2RepoStr = p2ReposPrefST.nextToken();
876
                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
877
                if(p2ReposStrST.countTokens()==3) {
878
                    String nickname = p2ReposStrST.nextToken();
879
                    URI uri = null;
880
                    try {
881
                        uri = new URI(p2ReposStrST.nextToken());
882
                    } catch (URISyntaxException e) {
883
                        continue;
884
                    }
885
                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
886
                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
887
                    mre.setNickname(nickname);
888
                    mre.setEnabled(enabled);
889
                    p2Repos.add(mre);
890
                }
891
            }
892
        }
893

    
894
        return p2Repos;
895
    }
896

    
897
    /**
898
     * enables/disables nested composite. <br>
899
     *
900
     * @param ctrl - Composite to be en-/disabeld
901
     * @param enabled - boolean
902
     */
903
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
904
        if (ctrl instanceof Composite) {
905
            Composite comp = (Composite) ctrl;
906
            for (Control c : comp.getChildren()) {
907
                recursiveSetEnabled(c, enabled);
908
            }
909
        } else {
910
            ctrl.setEnabled(enabled);
911
        }
912
    }
913

    
914
    /**
915
	 * <p>
916
	 * getSortRanksNaturally
917
	 * </p>
918
    	 *
919
    	 * @return a boolean.
920
	 */
921
	public static boolean getSortNodesNaturally() {
922
		return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
923
	}
924

    
925
	/**
926
	 * <p>
927
	 * setSortRanksNaturally
928
	 * </p>
929
	 *
930
	 * @param selection
931
	 *            a boolean.
932
	 */
933
	public static void setSortNodesNaturally(boolean selection) {
934
		getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
935
	}
936

    
937

    
938
	/**
939
	 * <p>
940
	 * getSortRanksNaturally
941
	 * </p>
942
	 *
943
	 * @return a boolean.
944
	 */
945
	public static boolean getSortNodesStrictlyAlphabetically() {
946
		return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
947
	}
948

    
949
	/**
950
	 * <p>
951
	 * setSortRanksNaturally
952
	 * </p>
953
	 *
954
	 * @param selection
955
	 *            a boolean.
956
	 */
957
	public static void setSortNodesStrictlyAlphabetically(boolean selection) {
958
		getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
959
	}
960

    
961
	/**
962
	 * <p>
963
	 * setStoreNavigatorState
964
	 * </p>
965
	 *
966
	 * @param selection
967
	 *            a boolean.
968
	 */
969
	public static boolean isStoreNavigatorState() {
970
		return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
971

    
972
	}
973

    
974
	/**
975
	 * <p>
976
	 * setStoreNavigatorState
977
	 * </p>
978
	 *
979
	 * @param selection
980
	 *            a boolean.
981
	 */
982
	public static void setStoreNavigatorState(boolean selection) {
983
		getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
984

    
985
	}
986

    
987
    /**
988
     * @return
989
     */
990
    public static boolean isShowUpWidgetIsDisposedMessages() {
991
       return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
992
    }
993
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
994
        getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
995
    }
996

    
997
    /**
998
     * @return
999
     */
1000
    public static boolean isShowIdInVocabularyInChecklistEditor() {
1001
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
1002
    }
1003
    public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
1004
        getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
1005
    }
1006

    
1007
    /**
1008
     * @return
1009
     */
1010
    public static boolean isShowSymbolInChecklistEditor() {
1011
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SYMBOL);
1012
    }
1013
    public static void setShowSymbolInChecklistEditor(boolean selection) {
1014
        getPreferenceStore().setValue(CHECKLIST_SYMBOL, selection);
1015
    }
1016

    
1017
    /**
1018
     * @return
1019
     */
1020
    public static boolean isShowRankInChecklistEditor() {
1021
        return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
1022
    }
1023
    public static void setShowRankInChecklistEditor(boolean selection) {
1024
        getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
1025
    }
1026

    
1027
    /**
1028
     * @param object
1029
     * @param b
1030
     * @return
1031
     */
1032
    public static NameDetailsConfigurator setPreferredNameDetailsConfiguration( boolean local) {
1033
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1034

    
1035
        CdmPreference preference = null;
1036

    
1037
        if (!local) {
1038
            preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1039
        }
1040
        if (preference == null){
1041
            return null;
1042
        }
1043

    
1044
        getPreferenceStore().setValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1045

    
1046
        //the preference value is build like this:
1047
        //<section1>:true;<section2>:false....
1048

    
1049
        String value = preference.getValue();
1050
        String [] sections = value.split(";");
1051
        Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1052
        String[] sectionValues;
1053
        for (String sectionValue: sections){
1054
            sectionValues = sectionValue.split(":");
1055
            sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1056
        }
1057

    
1058

    
1059

    
1060
//        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1061
//                (getValue(sectionMap, "taxon")));
1062
        config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1063

    
1064
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1065
//                (getValue(sectionMap, "lsid")));
1066
        config.setLSIDActivated(getValue(sectionMap, "lsid"));
1067

    
1068
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1069
//                (getValue(sectionMap, "nc")));
1070
        config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1071

    
1072
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1073
//                (getValue(sectionMap, "ap")));
1074
        config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1075

    
1076
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1077
//                (getValue(sectionMap, "rank")));
1078
        config.setRankActivated(getValue(sectionMap, "rank"));
1079

    
1080

    
1081
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1082
//                (getValue(sectionMap, "atomisedEpithets")));
1083
        config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1084

    
1085
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1086
//                (getValue(sectionMap,"author")));
1087
        config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1088

    
1089
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1090
//                (getValue(sectionMap, "nomRef")));
1091
        config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1092

    
1093
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1094
//                (getValue(sectionMap, "nomStat")));
1095
        config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1096

    
1097

    
1098
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1099
//                (getValue(sectionMap,"protologue")));
1100
        config.setProtologueActivated(getValue(sectionMap,"protologue"));
1101

    
1102
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1103
//                (getValue(sectionMap,"typeDes")));
1104
        config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1105

    
1106
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1107
//                (getValue(sectionMap,"nameRelation")));
1108
        config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1109

    
1110
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1111
//                (getValue(sectionMap, "hybrid")));
1112
        config.setHybridActivated(getValue(sectionMap,"hybrid"));
1113

    
1114
        return config;
1115
    }
1116

    
1117
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1118
		if (sectionMap.containsKey(string)){
1119
			return sectionMap.get(string);
1120
		}else{
1121
			return true;
1122
		}
1123

    
1124
	}
1125

    
1126
//	public static boolean isDeterminiationOnlyForFieldUnit(boolean local){
1127
//        CdmPreference preference = null;
1128
//
1129
//        if (!local) {
1130
//            preference = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
1131
//        }
1132
//        if (preference == null){
1133
//            return getPreferenceStore().getBoolean(DETERMINATION_ONLY_FOR_FIELD_UNITS);
1134
//        }
1135
//        return Boolean.valueOf(preference.getValue());
1136
//    }
1137
//
1138
//    public static boolean isShowCollectingAreasInGeneralSection(boolean local){
1139
//        CdmPreference preference = null;
1140
//
1141
//        if (!local) {
1142
//            preference = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
1143
//        }
1144
//        if (preference == null){
1145
//            return getPreferenceStore().getBoolean(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION);
1146
//        }
1147
//        return Boolean.valueOf(preference.getValue());
1148
//    }
1149
//
1150
//    public static boolean isShowTaxonAssociations(boolean local){
1151
//        CdmPreference preference = null;
1152
//
1153
//        if (!local) {
1154
//            preference = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
1155
//        }
1156
//        if (preference == null){
1157
//            return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
1158
//        }
1159
//        return Boolean.valueOf(preference.getValue());
1160
//    }
1161

    
1162
//    public static boolean getBioCaseProvider(boolean local){
1163
//        CdmPreference preference = null;
1164
//
1165
//        if (!local) {
1166
//            preference = getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
1167
//        }
1168
//        if (preference == null){
1169
//            return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
1170
//        }
1171
//        return Boolean.valueOf(preference.getValue());
1172
//    }
1173

    
1174

    
1175
    /**
1176
     * <p>
1177
     * setAbcdConfigurator
1178
     * </p>
1179
     *
1180
     * @param preferredConfiguration
1181
     *            a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1182
     *            object.
1183
     */
1184
    public static Abcd206ImportConfigurator getAbcdImportConfigurationPreference(boolean useLocal) {
1185

    
1186
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1187

    
1188
        if (useLocal){
1189
            config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1190

    
1191
            config.setAddMediaAsMediaSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1192

    
1193
            config.setAllowReuseOtherClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1194
            config.setDeduplicateClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1195
            config.setDeduplicateReferences(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1196

    
1197
            config.setGetSiblings(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1198
            config.setIgnoreAuthorship(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1199
            config.setIgnoreImportOfExistingSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1200
            config.setMapUnitIdToAccessionNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1201
            config.setMapUnitIdToBarcode(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1202
            config.setMapUnitIdToCatalogNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1203
            config.setMoveNewTaxaToDefaultClassification(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1204
            config.setOverwriteExistingSpecimens(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1205
            config.setReuseExistingDescriptiveGroups(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1206
            config.setReuseExistingMetaData(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1207
            config.setReuseExistingTaxaWhenPossible(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1208
            return config;
1209

    
1210
        }
1211
            ICdmRepository controller;
1212
            controller = CdmStore.getCurrentApplicationConfiguration();
1213
            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AbcdImportConfig);
1214
            CdmPreference preference = null;
1215
            if (controller == null){
1216
                return null;
1217
            }
1218
            preference = controller.getPreferenceService().find(key);
1219
            if (preference == null){
1220
                return config;
1221
             } else{
1222
             String configString = preference.getValue();
1223
             String[] configArray = configString.split(";");
1224

    
1225
             for (String configItem: configArray){
1226
                 String[] keyValue = configItem.split(":");
1227
                 String keyString = keyValue[0];
1228
                 String valueString = keyValue[1];
1229
                 if (keyString.equals("ignoreImportOfExistingSpecimen")){
1230
                     config.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
1231
                 }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){
1232
                     config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
1233
                 }else if (keyString.equals("reuseExistingTaxaWhenPossible")){
1234
                     config.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
1235
                 }else if (keyString.equals("ignoreAuthorship")){
1236
                     config.setIgnoreAuthorship(Boolean.valueOf(valueString));
1237
                 }else if (keyString.equals("addMediaAsMediaSpecimen")){
1238
                     config.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
1239
                 }else if (keyString.equals("reuseExistingMetaData")){
1240
                     config.setReuseExistingMetaData(Boolean.valueOf(valueString));
1241
                 }else if (keyString.equals("reuseExistingDescriptiveGroups")){
1242
                     config.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
1243
                 }else if (keyString.equals("allowReuseOtherClassifications")){
1244
                     config.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
1245
                 }else if (keyString.equals("deduplicateReferences")){
1246
                     config.setDeduplicateReferences(Boolean.valueOf(valueString));
1247
                 }else if (keyString.equals("deduplicateClassifications")){
1248
                     config.setDeduplicateClassifications(Boolean.valueOf(valueString));
1249
                 }else if (keyString.equals("moveNewTaxaToDefaultClassification")){
1250
                     config.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
1251
                 }else if (keyString.equals("mapUnitIdToCatalogNumber")){
1252
                     config.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
1253
                 }else if (keyString.equals("mapUnitIdToAccessionNumber")){
1254
                     config.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
1255
                 }else if (keyString.equals("mapUnitIdToBarcode")){
1256
                     config.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
1257
                 }else if (keyString.equals("overwriteExistingSpecimens")){
1258
                     config.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
1259
                 }else{
1260
                     logger.debug("This key of the abcd configurator needs to be added to the transformer: " + keyString);
1261
                 }
1262

    
1263
            }
1264
        }
1265
        return config;
1266

    
1267

    
1268

    
1269
    }
1270

    
1271
    /**
1272
    *
1273
    */
1274
   public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1275
       NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1276
       if (local){
1277
          config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1278
          config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1279
          config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1280
          config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1281
          config.setLSIDActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1282
          config.setNameApprobiationActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1283
          config.setNameCacheActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1284
          config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1285
          config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1286
          config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1287
          config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1288
          config.setProtologueActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1289
          config.setRankActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1290
          config.setTaxonSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1291
          config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1292
          config.setHybridActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1293
       }else{
1294

    
1295
       }
1296

    
1297
      return config;
1298
   }
1299

    
1300

    
1301
   public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1302
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1303
               config.isSimpleDetailsViewActivated());
1304
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1305
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1306
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1307
               config.isNomenclaturalCodeActived());
1308
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1309
               config.isNameCacheActivated());
1310
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1311
               config.isAppendedPhraseActivated());
1312
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1313
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1314
               config.isAtomisedEpithetsActivated());
1315
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1316
               config.isAuthorCacheActivated());
1317
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1318
               config.isAuthorshipSectionActivated());
1319
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1320
               config.isNomenclaturalReferenceSectionActivated());
1321
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1322
               config.isNomenclaturalStatusSectionActivated());
1323
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1324
               config.isProtologueActivated());
1325
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1326
               config.isTypeDesignationSectionActivated());
1327
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1328
               config.isNameRelationsSectionActivated());
1329
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1330
               config.isHybridActivated());
1331

    
1332
   }
1333

    
1334

    
1335

    
1336
}
(19-19/25)