Project

General

Profile

Download (73.5 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.Collections;
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Properties;
25
import java.util.Set;
26
import java.util.StringTokenizer;
27
import java.util.UUID;
28

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

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

    
75
/**
76
 * <p>
77
 * PreferencesUtil class.
78
 * </p>
79
 *
80
 * @author p.ciardelli
81
 * @author n.hoffmann
82
 * @created 05.12.2008
83
 * @version 1.0
84
 */
85
public class PreferencesUtil implements IPreferenceKeys {
86

    
87
	/**
88
	 *
89
	 */
90
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
91

    
92
	public static final String P2_REPOSITORIES_DELIM = ",";
93
	public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
94

    
95
	private final static Logger logger = Logger.getLogger(PreferencesUtil.class);
96

    
97

    
98

    
99
	/**
100
	 * <p>
101
	 * getPreferenceStore
102
	 * </p>
103
	 *
104
	 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
105
	 */
106
	public static IPreferenceStore getPreferenceStore() {
107
		return TaxeditorStorePlugin.getDefault().getPreferenceStore();
108
	}
109

    
110
	private static String prefKey(String name) {
111
	    return name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
112
	}
113

    
114
	/**
115
     * <p>
116
     * setStringValue
117
     * </p>
118
     *
119
     *
120
     **/
121
    public static void setStringValue(String name, String value) {
122
        getPreferenceStore().setValue(prefKey(name), value);
123
    }
124

    
125
    /**
126
     * <p>
127
     * setIntValue
128
     * </p>
129
     *
130
     *
131
     **/
132
    public static void setIntValue(String name, int value) {
133
        getPreferenceStore().setValue(prefKey(name), value);
134
    }
135

    
136
    /**
137
     * <p>
138
     * setBooleanValue
139
     * </p>
140
     *
141
     *
142
     **/
143
    public static void setBooleanValue(String name, boolean value) {
144
        getPreferenceStore().setValue(prefKey(name), value);
145
    }
146

    
147
    /**
148
     * <p>
149
     * setBooleanValue
150
     * </p>
151
     *
152
     *
153
     **/
154
    public static void setDoubleValue(String name, double value) {
155
        getPreferenceStore().setValue(prefKey(name), value);
156
    }
157

    
158
    /**
159
     * <p>
160
     * setFloatValue
161
     * </p>
162
     *
163
     *
164
     **/
165
    public static void setFloatValue(String name, float value) {
166
        getPreferenceStore().setValue(name + ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString(), value);
167
    }
168

    
169
    /**
170
     * <p>
171
     * setFloatValue
172
     * </p>
173
     *
174
     *
175
     **/
176
    public static void setLongValue(String name, long value) {
177
        getPreferenceStore().setValue(prefKey(name), value);
178
    }
179

    
180
    /**
181
     * <p>
182
     * setStringValue
183
     * </p>
184
     *
185
     *
186
     **/
187
    public static String getStringValue(String name) {
188

    
189
        String dbSpecific = prefKey(name);
190
        if (getPreferenceStore().contains(dbSpecific)){
191
            return getPreferenceStore().getString(dbSpecific);
192
        }else{
193
            return getPreferenceStore().
194
                    getString(name);
195
        }
196

    
197
    }
198

    
199
    /**
200
     * <p>
201
     * setIntValue
202
     * </p>
203
     *
204
     *
205
     **/
206
    public static int getIntValue(String name) {
207
        String dbSpecific = prefKey(name);
208
        if (getPreferenceStore().contains(dbSpecific)){
209
            return getPreferenceStore().getInt(dbSpecific);
210
        }else{
211
            return getPreferenceStore().
212
                    getInt(name);
213
        }
214

    
215
    }
216

    
217
    /**
218
     * <p>
219
     * setBooleanValue
220
     * </p>
221
     *
222
     *
223
     **/
224
    public static boolean getBooleanValue(String name) {
225
        if (CdmStore.isActive()){
226
            String dbSpecific = prefKey(name);
227
            if (getPreferenceStore().contains(dbSpecific)){
228
                return getPreferenceStore().getBoolean(dbSpecific);
229
            }else{
230
                return getPreferenceStore().
231
                        getBoolean(name);
232
            }
233

    
234
        }else{
235

    
236
            return getPreferenceStore().getBoolean(name);
237
        }
238

    
239
    }
240

    
241
    /**
242
     * <p>
243
     * setBooleanValue
244
     * </p>
245
     *
246
     *
247
     **/
248
    public static double getDoubleValue(String name) {
249
        String dbSpecific = prefKey(name);
250
        if (getPreferenceStore().contains(dbSpecific)){
251
            return getPreferenceStore().getDouble(dbSpecific);
252
        }else{
253
            return getPreferenceStore().
254
                    getDouble(name);
255
        }
256

    
257
    }
258

    
259
    /**
260
     * <p>
261
     * getFloatValue
262
     * </p>
263
     *
264
     *
265
     **/
266
    public static float getFloatValue(String name) {
267
        String dbSpecific = prefKey(name);
268
        if (getPreferenceStore().contains(dbSpecific)){
269
            return getPreferenceStore().getFloat(dbSpecific);
270
        }else{
271
            return getPreferenceStore().
272
                    getFloat(name);
273
        }
274

    
275
    }
276

    
277
    /**
278
     * <p>
279
     * getLongValue
280
     * </p>
281
     *
282
     *
283
     **/
284
    public static long getLongValue(String name) {
285
        String dbSpecific = prefKey(name);
286
        if (getPreferenceStore().contains(dbSpecific)){
287
            return getPreferenceStore().getLong(dbSpecific);
288
        }else{
289
            return getPreferenceStore().
290
                    getLong(name);
291
        }
292
    }
293

    
294

    
295
	/**
296
	 * <p>
297
	 * setPreferredNomenclaturalCode
298
	 * </p>
299
	 *
300
	 * @param preferredCode
301
	 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
302
	 *            object.
303
	 */
304
	public static CdmPreference setPreferredNomenclaturalCode(
305
			String preferenceKey, boolean local) {
306
	    if (local){
307
	        setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
308
	                preferenceKey);
309
	    }
310
	    else{
311
    		ICdmRepository controller;
312
    		controller = CdmStore.getCurrentApplicationConfiguration();
313
    		if (controller == null){
314
                return null;
315
            }
316
    		PrefKey key = null;
317
    		try{
318
    		    key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
319
    		}catch (Exception e){
320
    		    System.out.println(e.getStackTrace());
321
    		}
322
    		CdmPreference preference = null;
323

    
324
    		if (preferenceKey == null){
325
    			preference = controller.getPreferenceService().find(key);
326
    			if (preference == null){
327
    				return null;
328
    			} else{
329
    			    setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
330
    				        preference.getValue());
331
    			    setBooleanValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,preference.isAllowOverride());
332
    				return preference;
333
    			}
334
    		} else{
335
    			preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferenceKey);
336
    			controller.getPreferenceService().set(preference);
337

    
338
    		}
339
	    }
340
        return null;
341

    
342

    
343

    
344
	}
345

    
346
	public static NomenclaturalCode getPreferredNomenclaturalCode(){
347
	    if (getBooleanValue(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
348
            return getPreferredNomenclaturalCode(true);
349
        } else{
350
            return getPreferredNomenclaturalCode(false);
351
        }
352
	}
353

    
354
	/**
355
	 * <p>
356
	 * getPreferredNomenclaturalCode
357
	 * </p>
358
	 *
359
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
360
	 */
361
	public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
362

    
363
		CdmPreference pref = null;
364
		if (!useLocal){
365
		    pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
366
		}
367

    
368
	    String preferredCode;
369
	    if(pref == null){
370
	    	preferredCode = getStringValue(
371
					PREFERRED_NOMENCLATURAL_CODE_KEY);
372

    
373
	    }else{
374
	    	preferredCode = pref.getValue();
375
	    }
376

    
377
	    return getPreferredNomenclaturalCode(preferredCode);
378

    
379
	}
380

    
381
	/**
382
     * <p>
383
     * getPreferredNomenclaturalCode
384
     * </p>
385
     *
386
     * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
387
     */
388
    public static NomenclaturalCode getPreferredNomenclaturalCode(String preferenceKeyNomenclaturalCode) {
389

    
390
        for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
391
            if (getPreferenceKey(code).equals(preferenceKeyNomenclaturalCode)) {
392
                return code;
393
            }
394
        }
395
        return null;
396
    }
397

    
398
	public static boolean isShowTaxonAssociations(){
399
        if (getBooleanValue(SHOW_TAXON_ASSOCIATIONS_OVERRIDE) && getBooleanValue(SHOW_TAXON_ASSOCIATIONS_ALLOW_OVERRIDE)) {
400
            return getBooleanValue(SHOW_TAXON_ASSOCIATIONS);
401
        } else{
402
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
403

    
404
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
405
        }
406
    }
407

    
408
	public static boolean isShowLifeForm(){
409
        if (getBooleanValue(SHOW_LIFE_FORM_OVERRIDE) && getBooleanValue(SHOW_LIFE_FORM_ALLOW_OVERRIDE)) {
410
            return getBooleanValue(SHOW_LIFE_FORM);
411
        } else{
412
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
413

    
414
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
415
        }
416
    }
417

    
418
	public static boolean isDeterminationOnlyForFieldUnits(){
419
        if (getBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS_OVERRIDE) && getBooleanValue(DETERMINATIONS_ONLY_FOR_FIELDUNITS_ALLOW_OVERRIDE)) {
420
            return getBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS);
421
        } else{
422
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
423

    
424
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
425
        }
426
    }
427

    
428
	public static boolean isCollectingAreaInGeneralSection(){
429
        if (getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION_OVERRIDE) && getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION_ALLOW_OVERRIDE)) {
430
            return getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION);
431
        } else{
432
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
433

    
434
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
435
        }
436
    }
437

    
438
	public static boolean isShowSimpleDetailsView(){
439
        if (getBooleanValue(OVERRIDE_NAME_DETAILS) && getBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS)) {
440
            return getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION);
441
        } else{
442
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
443

    
444
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
445
        }
446
    }
447

    
448

    
449

    
450
    public static CdmPreference getPreferenceFromDB(PreferencePredicate predicate){
451
        ICdmRepository controller;
452
        CdmPreference pref = null;
453

    
454
        try{
455
            if(CdmStore.isActive()){
456
                controller = CdmStore.getCurrentApplicationConfiguration();
457
                PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), predicate);
458
                pref = controller.getPreferenceService().find(key);
459
            }
460
        }catch(Exception e){
461
            e.printStackTrace();
462
        }
463

    
464
        return pref;
465

    
466
    }
467

    
468
    public static void setPreferenceToDB(CdmPreference preference){
469
        ICdmRepository controller;
470
        try{
471
            if(CdmStore.isActive()){
472
                controller = CdmStore.getCurrentApplicationConfiguration();
473
                controller.getPreferenceService().set(preference);
474
            }
475
        }catch(Exception e){
476
            e.printStackTrace();
477
        }
478

    
479
    }
480

    
481

    
482

    
483
	public static String getPreferredDefaultLangugae(){
484
	    String preferredLanguage = getStringValue(DEFAULT_LANGUAGE_EDITOR);
485
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
486
	        return preferredLanguage;
487
	    }
488
	    return null;
489
	}
490

    
491
	public static boolean isShowMediaPreview(){
492
        boolean isShowMediaPreview = getBooleanValue(SHOW_MEDIA_PREVIEW);
493
        return isShowMediaPreview;
494
    }
495

    
496
	/**
497
	 * Get the match strategy for the given class that was stored in preferences
498
	 * or the default strategy if it was not stored in preferences
499
	 *
500
	 * @param clazz
501
	 *            a {@link java.lang.Class} object.
502
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
503
	 */
504
	public static IMatchStrategy getMatchStrategy(Class clazz) {
505
		String className = clazz.getName();
506
		if (getBooleanValue(MATCH_STRATEGY_PREFIX + className)) {
507
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
508

    
509
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
510
				String matchModeName = getStringValue(
511
						getMatchStrategyFieldName(className, fieldName));
512
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
513
				try {
514
					matchStrategy.setMatchMode(fieldName, matchMode);
515
				} catch (MatchException e) {
516
					MessagingUtils.error(PreferencesUtil.class, e);
517
					throw new RuntimeException(e);
518
				}
519
			}
520

    
521
			return matchStrategy;
522
		}
523
		return getDefaultMatchStrategy(clazz);
524
	}
525

    
526
	/**
527
	 * Stores a matchStrategy into the preference store.
528
	 *
529
	 * @param matchStrategy
530
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
531
	 *            object.
532
	 */
533
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
534
		String className = matchStrategy.getMatchClass().getName();
535
		setBooleanValue(MATCH_STRATEGY_PREFIX + className, true);
536

    
537
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
538

    
539
		for (String fieldName : matchFields) {
540
			setStringValue(
541
					getMatchStrategyFieldName(className, fieldName),
542
					matchStrategy.getMatchMode(fieldName).name());
543
		}
544
	}
545

    
546
	/**
547
	 * Helper method to create the preference property for a match field.
548
	 *
549
	 * @param className
550
	 * @param fieldName
551
	 * @return
552
	 */
553
	private static String getMatchStrategyFieldName(String className,
554
			String fieldName) {
555
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
556
	}
557

    
558
	/**
559
	 * Returns the default match strategy for a given class.
560
	 *
561
	 * @param clazz
562
	 *            a {@link java.lang.Class} object.
563
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
564
	 */
565
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
566
		return DefaultMatchStrategy.NewInstance(clazz);
567
	}
568

    
569
	/**
570
	 * <p>
571
	 * getDateFormatPattern
572
	 * </p>
573
	 *
574
	 * @return a {@link java.lang.String} object.
575
	 */
576
	public static String getDateFormatPattern() {
577
		// TODO make this configurable in properties
578
		String pattern = "Y-M-d H:m";
579
		return pattern;
580
	}
581

    
582
	/**
583
	 * <p>
584
	 * addTermToPreferredTerms
585
	 * </p>
586
	 *
587
	 * @param term
588
	 *            a T object.
589
	 * @param <T>
590
	 *            a T object.
591
	 */
592
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
593

    
594
		// VocabularyEnum vocabulary =
595
		// VocabularyEnum.getVocabularyEnum(term.getClass());
596
		//
597
		// getPreferenceStore().setValue(getPreferenceKey(term),
598
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
599
		//
600
		// firePreferencesChanged(term.getClass());
601
	}
602

    
603
	/**
604
	 * Construct a unique key using the CdmBase object's uuid
605
	 *
606
	 * @param cdmBase
607
	 * @return
608
	 */
609
	private static String getPreferenceKey(ICdmBase cdmBase) {
610
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
611

    
612
		String key = cdmBase.getClass().getName().concat(".")
613
				.concat(cdmBase.getUuid().toString());
614
		if (key.contains("javassist")) {
615
			MessagingUtils.info("proxy");
616
		}
617
		return key;
618
	}
619

    
620
	/**
621
	 * Construct a unique key using the CdmBase object's uuid
622
	 *
623
	 * @param cdmBase
624
	 * @return
625
	 */
626
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
627
		simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
628
		String key = simpleTerm.getClass().getName().concat(".")
629
				.concat(simpleTerm.getUuid().toString());
630
		if (key.contains("javassist")) {
631
			MessagingUtils.warn(PreferencesUtil.class,
632
					"Trying to persist a preference based on a proxy class.");
633
		}
634
		return key;
635
	}
636

    
637

    
638

    
639
	/**
640
	 * Construct a unique key using the CdmBase object's uuid
641
	 *
642
	 * @param cdmBase
643
	 * @return
644
	 */
645
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
646
		definedTerm = HibernateProxyHelper.deproxy(definedTerm);
647
		String key = definedTerm.getClass().getName().concat(".")
648
				.concat(definedTerm.getUuid().toString());
649
		if (key.contains("javassist")) {
650
			MessagingUtils.warn(PreferencesUtil.class,
651
					"Trying to persist a preference based on a proxy class.");
652
		}
653
		return key;
654
	}
655

    
656
	/**
657
	 * Retrieves search preferences from the preference store
658
	 *
659
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
660
	 */
661
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
662
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
663

    
664
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
665
				TAXON_SERVICE_CONFIGURATOR_TAXA));
666
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
667
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
668
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
669
				TAXON_SERVICE_CONFIGURATOR_NAMES));
670
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
671
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
672
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getStringValue(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
673

    
674
		return configurator;
675
	}
676

    
677
	/**
678
	 * create new preferences, setting all search options to true
679
	 *
680
	 * @return a
681
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
682
	 *         object.
683
	 */
684
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
685
		IFindTaxaAndNamesConfigurator configurator = FindTaxaAndNamesConfiguratorImpl.NewInstance();
686

    
687
		configurator.setDoTaxa(true);
688
		configurator.setDoSynonyms(true);
689
		configurator.setDoNamesWithoutTaxa(true);
690
		configurator.setDoTaxaByCommonNames(true);
691

    
692
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
693
				"name", "name.$", "relationsFromThisTaxon.$"));
694

    
695
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
696
				"name", "name.$", "synonyms.relatedTo.*"));
697

    
698
		// DEFAULT VALUES
699
		// match mode is a simple like, actually all other match modes are kind
700
		// of bogus
701
		configurator
702
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
703
		// we set page number and size here as this should always be unlimited
704
		configurator.setPageNumber(0);
705
		// TODO currently limit results to 10000
706
		configurator.setPageSize(10000);
707

    
708
		return configurator;
709
	}
710

    
711
	/**
712
	 * Store search preferences
713
	 *
714
	 * @param configurator
715
	 *            a
716
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
717
	 *            object.
718
	 */
719
	public static void setSearchConfigurator(
720
			IFindTaxaAndNamesConfigurator configurator) {
721
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
722
				configurator.isDoTaxa());
723
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
724
				configurator.isDoSynonyms());
725
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
726
				configurator.isDoNamesWithoutTaxa());
727
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
728
				configurator.isDoTaxaByCommonNames());
729
	}
730

    
731
	/**
732
	 * <p>
733
	 * firePreferencesChanged
734
	 * </p>
735
	 *
736
	 * @param clazz
737
	 *            a {@link java.lang.Class} object.
738
	 */
739
	public static void firePreferencesChanged(Class clazz) {
740
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
741
				null, clazz);
742
	}
743

    
744
	public static String createPreferenceString(String property){
745
	   return prefKey(property);
746

    
747
	}
748

    
749
	/**
750
	 * Set default values for preferences
751
	 */
752
	public static void setDefaults() {
753
		getPreferenceStore().setDefault(createPreferenceString(TAXON_SERVICE_CONFIGURATOR_TAXA), true);
754
		getPreferenceStore().setDefault(createPreferenceString(TAXON_SERVICE_CONFIGURATOR_SYNONYMS),
755
				true);
756
		getPreferenceStore().setDefault(createPreferenceString(EDIT_MAP_SERVICE_ACCES_POINT),
757
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
758
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
759
		getPreferenceStore().setDefault(createPreferenceString(SHOULD_CONNECT_AT_STARTUP), false);
760
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_ACCESS_POINT),
761
				"http://www.biodiversitylibrary.org/openurl");
762
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_IMAGE_MAX_WIDTH), "1000");
763
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_IMAGE_MAX_HEIGHT), "1000");
764
		//Distribution Editor:
765
		getPreferenceStore().setDefault(createPreferenceString(DISTRIBUTION_AREA_PREFRENCES_ACTIVE), true);
766
		getPreferenceStore().setDefault(createPreferenceString(CHECKLIST_AREA_DISPLAY), CHECKLIST_AREA_DISPLAY_TITLE);
767
		getPreferenceStore().setDefault(createPreferenceString(CHECKLIST_SYMBOL), false);
768

    
769

    
770
		//Name Details
771
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS), true);
772
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE), true);
773
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP), true);
774
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_HYBRID), true);
775
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_LSID), true);
776
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP), true);
777
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NAMECACHE), true);
778
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE), true);
779
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE), true);
780
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS), true);
781
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE), true);
782
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_RANK), true);
783
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION), true);
784
		//Navigator preferences
785
		getPreferenceStore().setDefault(createPreferenceString(SORT_NODES_NATURALLY), false);
786
		getPreferenceStore().setDefault(createPreferenceString(SORT_NODES_ALPHABETICALLY), false);
787
		getPreferenceStore().setDefault(createPreferenceString(SORT_TAXA_BY_RANK_AND_NAME), true);
788
		//override db preferences
789
		getPreferenceStore().setDefault(createPreferenceString(ABCD_IMPORT_OVERRIDE), false);
790
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SPECIMEN_OVERRIDE), false);
791
		getPreferenceStore().setDefault(createPreferenceString(OVERRIDE_NAME_DETAILS), false);
792
		getPreferenceStore().setDefault(createPreferenceString(DISTRIBUTION_AREA_PREFRENCES_ACTIVE_OVERRIDE), false);
793

    
794
		getPreferenceStore().setDefault(createPreferenceString(FILTER_COMMON_NAME_REFERENCES), false);
795
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SPECIMEN), true);
796
		getPreferenceStore().setDefault(createPreferenceString(SHOW_TAXONNODE_WIZARD), true);
797
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_IN_SOURCE), true);
798
		getPreferenceStore().setDefault(createPreferenceString(SHOW_ID_IN_SOURCE), true);
799
		getPreferenceStore().setDefault(createPreferenceString(DISABLE_MULTI_CLASSIFICATION), false);
800
		getPreferenceStore().setDefault(createPreferenceString(DISABLE_SEC), false);
801
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SEC_DETAILS), true);
802
	}
803

    
804
	/**
805
	 * <p>
806
	 * checkNomenclaturalCode
807
	 * </p>
808
	 */
809
	public static void checkNomenclaturalCode() {
810
		// First time Editor is opened, no nomenclatural code has been set
811
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
812
			PreferencesUtil.setPreferredNomenclaturalCode(getPreferenceKey(NomenclaturalCode.ICNAFP), true);
813
		}
814

    
815

    
816

    
817
	}
818
	public static void setNomenclaturalCodePreferences(){
819
	    ICdmRepository controller;
820
        controller = CdmStore.getCurrentApplicationConfiguration();
821
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
822
        CdmPreference preference = null;
823
        if (controller == null){
824
            return ;
825
        }
826
        preference = controller.getPreferenceService().find(key);
827
        if (preference == null){
828
            return;
829
        }
830
        setBooleanValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
831

    
832
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
833
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
834
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
835

    
836
        setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
837
                getPreferenceKey(preferredCode));
838

    
839
	}
840

    
841
	public static void checkDefaultLanguage(){
842
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
843
	       Shell shell = AbstractUtility.getShell();
844
	       int open = new DefaultLanguageDialog(shell).open();
845
	       if(open == Window.OK){
846
	           PlatformUI.getWorkbench().restart();
847
	       }
848
	    }else{
849
	        //TODO:In case of a reinstall, the config.ini will be overwritten
850
	        //     here you create config.ini with the stored key from preferences
851
	    }
852
	}
853

    
854
	/**
855
	 * <p>
856
	 * getMapServiceAccessPoint
857
	 * </p>
858
	 *
859
	 * @return a {@link java.lang.String} object.
860
	 */
861
	public static String getMapServiceAccessPoint() {
862
		return getStringValue(EDIT_MAP_SERVICE_ACCES_POINT);
863
	}
864

    
865
	/**
866
	 * <p>
867
	 * shouldConnectAtStartUp
868
	 * </p>
869
	 *
870
	 * @return a boolean.
871
	 */
872
	public static boolean shouldConnectAtStartUp() {
873
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
874
		//return getBooleanValue(SHOULD_CONNECT_AT_STARTUP);
875
		return false;
876
	}
877

    
878
	/**
879
	 * <p>
880
	 * getDefaultFeatureTreeForTextualDescription
881
	 * </p>
882
	 *
883
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
884
	 */
885
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
886
		String uuidString = getStringValue(
887
				FEATURE_TREE_DEFAULT_TEXT);
888
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
889
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
890
	}
891

    
892
	/**
893
	 * <p>
894
	 * getDefaultFeatureTreeForStructuredDescription
895
	 * </p>
896
	 *
897
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
898
	 */
899
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
900
		String uuidString = getStringValue(
901
				FEATURE_TREE_DEFAULT_STRUCTURE);
902
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
903
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
904
	}
905

    
906
	/**
907
	 * <p>
908
	 * setSortRanksHierarchichally
909
	 * </p>
910
	 *
911
	 * @param selection
912
	 *            a boolean.
913
	 */
914
	public static void setSortRanksHierarchichally(boolean selection) {
915
		setBooleanValue(SORT_RANKS_HIERARCHICHALLY, selection);
916
	}
917

    
918
	/**
919
	 * <p>
920
	 * getSortRanksHierarchichally
921
	 * </p>
922
	 *
923
	 * @return a boolean.
924
	 */
925
	public static boolean getSortRanksHierarchichally() {
926
		return getBooleanValue(SORT_RANKS_HIERARCHICHALLY);
927
	}
928

    
929
	public static boolean isMultilanguageTextEditingCapability() {
930
		return getBooleanValue(
931
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
932
	}
933

    
934
	public static Language getGlobalLanguage() {
935

    
936

    
937
		String languageUuidString = getStringValue(
938
				GLOBAL_LANGUAGE_UUID);
939

    
940
		if(!CdmStore.isActive()) {
941
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
942
            return null;
943
        }
944

    
945
		if (CdmUtils.isBlank(languageUuidString)) {
946
			return Language.getDefaultLanguage();
947
		}
948

    
949
		UUID languageUuid = UUID.fromString(languageUuidString);
950
		return (Language) CdmStore.getService(ITermService.class).load(
951
				languageUuid);
952
	}
953

    
954
	public static void setGlobalLanguage(Language language) {
955
	    if(language != null) {
956
	        setStringValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
957
	        CdmStore.setDefaultLanguage(language);
958
	    }
959

    
960
	}
961

    
962
	/**
963
	 * @return
964
	 */
965
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
966
		List<MarkerType> markerTypes = CdmStore.getTermManager()
967
				.getPreferredTerms(MarkerType.class);
968

    
969
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
970

    
971
		for (MarkerType markerType : markerTypes) {
972
			String name = getMarkerTypeEditingPreferenceKey(markerType);
973
			Boolean value = getBooleanValue(name);
974

    
975
			result.put(markerType, value);
976
		}
977

    
978
		return result;
979
	}
980

    
981
	/**
982
	 * @param markerTypeEditingMap
983
	 */
984
	public static void setEditMarkerTypePreferences(
985
			Map<MarkerType, Boolean> markerTypeEditingMap) {
986
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
987
			String name = getMarkerTypeEditingPreferenceKey(markerType);
988
			setBooleanValue(name,
989
					markerTypeEditingMap.get(markerType));
990
		}
991

    
992
	}
993

    
994
	private static String getMarkerTypeEditingPreferenceKey(
995
			MarkerType markerType) {
996
		markerType = HibernateProxyHelper.deproxy(markerType);
997
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
998
	}
999

    
1000
	/**
1001
	 * <p>
1002
	 * setEditMarkerTypePreference
1003
	 * </p>
1004
	 *
1005
	 * @param input
1006
	 *            a {@link org.eclipse.ui.IEditorInput} object.
1007
	 * @param markerType
1008
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
1009
	 * @param edit
1010
	 *            a boolean.
1011
	 */
1012
	public static void setEditMarkerTypePreference(MarkerType markerType,
1013
			boolean edit) {
1014
		setBooleanValue(
1015
				getMarkerTypeEditingPreferenceKey(markerType), edit);
1016
	}
1017

    
1018
	/**
1019
	 * @return
1020
	 */
1021
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
1022
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
1023
				.NewInstance();
1024
		configurator.setMoveDerivedUnitMediaToGallery(true);
1025
		configurator.setMoveFieldObjectMediaToGallery(true);
1026
		return configurator;
1027
	}
1028

    
1029
	/**
1030
	 * This method will write language properties to the config.ini located in the configuration folder
1031
	 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
1032
	 *
1033
	 * @param setLanguage 0 is for german and 1 for english.
1034
	 * @throws IOException
1035
	 */
1036
    public void writePropertyToConfigFile(int setLanguage) throws IOException {
1037
        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
1038
        //give warning to user if the directory has no write access
1039
        if(file == null){
1040
            throw new IOException();
1041
        }
1042
        Properties properties = load(file.getAbsolutePath()+"/config.ini");
1043
        switch(setLanguage){
1044
        case 0:
1045
            properties.setProperty("osgi.nl", "de");
1046
            setStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
1047
            break;
1048
        case 1:
1049
            properties.setProperty("osgi.nl", "en");
1050
            setStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
1051
            break;
1052
        default:
1053
            break;
1054
        }
1055
        save(file+"/config.ini", properties);
1056
    }
1057

    
1058
    /**
1059
     * This method loads a property from a given file and returns it.
1060
     *
1061
     * @param filename
1062
     * @return
1063
     * @throws IOException
1064
     */
1065
    private Properties load(String filename) throws IOException {
1066
        FileInputStream in = new FileInputStream(filename);
1067
        Properties prop = new Properties();
1068
        prop.load(in);
1069
        in.close();
1070
        return prop;
1071
    }
1072

    
1073
    /**
1074
     * This method saves a property to the specified file.
1075
     *
1076
     * @param filename
1077
     * @param properties
1078
     * @throws IOException
1079
     */
1080
    private void save(String filename, Properties properties) throws IOException{
1081
        FileOutputStream fos =  new FileOutputStream(filename);
1082
        properties.store(fos, "");
1083
        fos.close();
1084
    }
1085

    
1086
    /**
1087
     * Saves a list of P2 Metadata Repositories as string with specified delimiters
1088
     *
1089
     * @param p2Repos
1090
     */
1091
    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
1092
        StringBuilder sb = new StringBuilder();
1093
        for(MetadataRepositoryElement p2Repo : p2Repos) {
1094
            sb.append(P2_REPOSITORIES_DELIM);
1095
            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
1096
                sb.append("-");
1097
            } else {
1098
                sb.append(p2Repo.getName());
1099
            }
1100
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
1101
            sb.append(p2Repo.getLocation().toString());
1102
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
1103
            sb.append(String.valueOf(p2Repo.isEnabled()));
1104
        }
1105
        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
1106
    }
1107

    
1108

    
1109
    /**
1110
     * Retrieves a list of previously saved P2 repositories
1111
     *
1112
     * @return
1113
     */
1114
    public static List<MetadataRepositoryElement> getP2Repositories() {
1115
        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
1116
        String p2ReposPref =  getStringValue(P2_REPOSITORY_LIST);
1117
        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
1118
            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
1119

    
1120
            while(p2ReposPrefST.hasMoreTokens()) {
1121
                String p2RepoStr = p2ReposPrefST.nextToken();
1122
                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
1123
                if(p2ReposStrST.countTokens()==3) {
1124
                    String nickname = p2ReposStrST.nextToken();
1125
                    URI uri = null;
1126
                    try {
1127
                        uri = new URI(p2ReposStrST.nextToken());
1128
                    } catch (URISyntaxException e) {
1129
                        continue;
1130
                    }
1131
                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
1132
                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
1133
                    mre.setNickname(nickname);
1134
                    mre.setEnabled(enabled);
1135
                    p2Repos.add(mre);
1136
                }
1137
            }
1138
        }
1139

    
1140
        return p2Repos;
1141
    }
1142

    
1143
    /**
1144
     * enables/disables nested composite. <br>
1145
     *
1146
     * @param ctrl - Composite to be en-/disabeld
1147
     * @param enabled - boolean
1148
     */
1149
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
1150
        if (ctrl instanceof Composite) {
1151
            Composite comp = (Composite) ctrl;
1152
            for (Control c : comp.getChildren()) {
1153
                recursiveSetEnabled(c, enabled);
1154
            }
1155
        } else {
1156
            ctrl.setEnabled(enabled);
1157
        }
1158
    }
1159

    
1160
    /**
1161
	 * <p>
1162
	 * getSortRanksNaturally
1163
	 * </p>
1164
    	 *
1165
    	 * @return a boolean.
1166
	 */
1167
	public static boolean getSortNodesNaturally() {
1168
		return getBooleanValue(SORT_NODES_NATURALLY);
1169
	}
1170

    
1171
	/**
1172
	 * <p>
1173
	 * setSortRanksNaturally
1174
	 * </p>
1175
	 *
1176
	 * @param selection
1177
	 *            a boolean.
1178
	 */
1179
	public static void setSortNodesNaturally(boolean selection) {
1180
		setBooleanValue(SORT_NODES_NATURALLY, selection);
1181
	}
1182

    
1183

    
1184
	/**
1185
	 * <p>
1186
	 * getSortRanksNaturally
1187
	 * </p>
1188
	 *
1189
	 * @return a boolean.
1190
	 */
1191
	public static boolean getSortNodesStrictlyAlphabetically() {
1192
		return getBooleanValue(SORT_NODES_ALPHABETICALLY);
1193
	}
1194

    
1195
	/**
1196
	 * <p>
1197
	 * setSortRanksNaturally
1198
	 * </p>
1199
	 *
1200
	 * @param selection
1201
	 *            a boolean.
1202
	 */
1203
	public static void setSortNodesStrictlyAlphabetically(boolean selection) {
1204
		setBooleanValue(SORT_NODES_ALPHABETICALLY, selection);
1205
	}
1206

    
1207
	/**
1208
	 * <p>
1209
	 * setStoreNavigatorState
1210
	 * </p>
1211
	 *
1212
	 * @param selection
1213
	 *            a boolean.
1214
	 */
1215
	public static boolean isStoreNavigatorState() {
1216
		return getBooleanValue(RESTORE_NAVIGATOR_STATE);
1217

    
1218
	}
1219

    
1220
	/**
1221
	 * <p>
1222
	 * setStoreNavigatorState
1223
	 * </p>
1224
	 *
1225
	 * @param selection
1226
	 *            a boolean.
1227
	 */
1228
	public static void setStoreNavigatorState(boolean selection) {
1229
		setBooleanValue(RESTORE_NAVIGATOR_STATE, selection);
1230

    
1231
	}
1232

    
1233
    /**
1234
     * @return
1235
     */
1236
    public static boolean isShowUpWidgetIsDisposedMessages() {
1237
       return getBooleanValue(IS_SHOW_UP_WIDGET_IS_DISPOSED);
1238
    }
1239
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
1240
        setBooleanValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
1241
    }
1242

    
1243
    /**
1244
     * @return
1245
     */
1246
    public static boolean isShowIdInVocabularyInChecklistEditor() {
1247
        String area_display = getStringValue(IPreferenceKeys.CHECKLIST_AREA_DISPLAY);
1248
        if (area_display.equals(CHECKLIST_AREA_DISPLAY_ID_IN_VOCABULARY)) {
1249
            return true;
1250
        }else{
1251
            return false;
1252
        }
1253
    }
1254
    public static boolean isShowSymbol1InChecklistEditor() {
1255
        String area_display = getStringValue(IPreferenceKeys.CHECKLIST_AREA_DISPLAY);
1256
        if (area_display.equals(CHECKLIST_AREA_DISPLAY_SYMBOL1)) {
1257
            return true;
1258
        }else{
1259
            return false;
1260
        }
1261
     }
1262
    public static boolean isShowSymbol2InChecklistEditor() {
1263
        String area_display = getStringValue(IPreferenceKeys.CHECKLIST_AREA_DISPLAY);
1264
        if (area_display.equals(CHECKLIST_AREA_DISPLAY_SYMBOL2)) {
1265
            return true;
1266
        }else{
1267
            return false;
1268
        }
1269
     }
1270
    public static void setShowIdInVocabularyInChecklistEditor(String selection) {
1271
        setStringValue(CHECKLIST_AREA_DISPLAY, selection);
1272
    }
1273

    
1274
    /**
1275
     * @return
1276
     */
1277
    public static boolean isShowSymbolInChecklistEditor() {
1278
       return getBooleanValue(IPreferenceKeys.CHECKLIST_SYMBOL);
1279
    }
1280
    public static void setShowSymbolInChecklistEditor(boolean selection) {
1281
        setBooleanValue(CHECKLIST_SYMBOL, selection);
1282
    }
1283

    
1284
    /**
1285
     * @return
1286
     */
1287
    public static boolean isShowRankInChecklistEditor() {
1288
        return getBooleanValue(IPreferenceKeys.CHECKLIST_SHOW_RANK);
1289
    }
1290
    public static void setShowRankInChecklistEditor(boolean selection) {
1291
       setBooleanValue(CHECKLIST_SHOW_RANK, selection);
1292
    }
1293

    
1294
    /**
1295
     * @param object
1296
     * @param b
1297
     * @return
1298
     */
1299
    public static NameDetailsConfigurator getPreferredNameDetailsConfiguration( boolean local) {
1300
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1301

    
1302
        CdmPreference preference = null;
1303

    
1304
        if (!local) {
1305
            preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1306
            if (preference == null){
1307
                return null;
1308
            }
1309

    
1310
            setBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1311

    
1312
            //the preference value is build like this:
1313
            //<section1>:true;<section2>:false....
1314

    
1315
            String value = preference.getValue();
1316
            String [] sections = value.split(";");
1317
            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1318
            String[] sectionValues;
1319
            for (String sectionValue: sections){
1320
                sectionValues = sectionValue.split(":");
1321
                sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1322
            }
1323
            config.setAllowOverride(preference.isAllowOverride());
1324
            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1325

    
1326
    //        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1327
    //                (getValue(sectionMap, "taxon")));
1328
            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1329

    
1330
            config.setSecDetailsActivated(getValue(sectionMap, "taxon.SecEnabled"));
1331
            config.setSecEnabled(getValue(sectionMap, "taxon.SecDetails"));
1332

    
1333
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1334
    //                (getValue(sectionMap, "lsid")));
1335
            config.setLSIDActivated(getValue(sectionMap, "lsid"));
1336

    
1337
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1338
    //                (getValue(sectionMap, "nc")));
1339
            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1340

    
1341
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1342
    //                (getValue(sectionMap, "ap")));
1343
            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1344

    
1345
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1346
    //                (getValue(sectionMap, "rank")));
1347
            config.setRankActivated(getValue(sectionMap, "rank"));
1348

    
1349

    
1350
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1351
    //                (getValue(sectionMap, "atomisedEpithets")));
1352
            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1353

    
1354
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1355
    //                (getValue(sectionMap,"author")));
1356
            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1357

    
1358
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1359
    //                (getValue(sectionMap, "nomRef")));
1360
            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1361

    
1362
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1363
    //                (getValue(sectionMap, "nomStat")));
1364
            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1365

    
1366

    
1367
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1368
    //                (getValue(sectionMap,"protologue")));
1369
            config.setProtologueActivated(getValue(sectionMap,"protologue"));
1370

    
1371
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1372
    //                (getValue(sectionMap,"typeDes")));
1373
            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1374

    
1375
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1376
    //                (getValue(sectionMap,"nameRelation")));
1377
            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1378

    
1379
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1380
    //                (getValue(sectionMap, "hybrid")));
1381
            config.setHybridActivated(getValue(sectionMap,"hybrid"));
1382
        }else{
1383
            config.setSimpleDetailsViewActivated(getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION));
1384
            config.setTaxonSectionActivated(getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_TAXON));
1385
            config.setSecDetailsActivated(getBooleanValue(SHOW_SEC_DETAILS));
1386
            config.setSecEnabled(getBooleanValue(DISABLE_SEC));
1387
            config.setLSIDActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_LSID));
1388
            config.setNomenclaturalCodeActived(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1389
            config.setAppendedPhraseActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1390
            config.setRankActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_RANK));
1391
            config.setAtomisedEpithetsActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1392
            config.setAuthorshipSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1393
            config.setNomenclaturalReferenceSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1394
            config.setNomenclaturalStatusSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1395
            config.setProtologueActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1396
            config.setTypeDesignationSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1397
            config.setNameRelationsSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1398
            config.setHybridActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_HYBRID));
1399
        }
1400
        return config;
1401
    }
1402

    
1403
    /**
1404
     * @param object
1405
     * @param b
1406
     * @return
1407
     */
1408
    public static void setPreferredNameDetailsConfiguration(NameDetailsConfigurator config, boolean local) {
1409
        CdmPreference preference = null;
1410

    
1411
        if (!local) {
1412
            preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NameDetailsView, config.toString());
1413

    
1414
            setPreferenceToDB(preference);
1415
        }
1416
        //also add to local preferences
1417
        setBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION, config.isSimpleDetailsViewActivated());
1418
        setBooleanValue(SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1419
        setBooleanValue(SHOW_SEC_DETAILS, config.isSecDetailsActivated());
1420
        setBooleanValue(DISABLE_SEC, config.isSecEnabled());
1421
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1422
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, config.isNomenclaturalCodeActived());
1423
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE, config.isAppendedPhraseActivated());
1424
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1425
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, config.isAtomisedEpithetsActivated());
1426
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, config.isAuthorshipSectionActivated());
1427
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, config.isNomenclaturalReferenceSectionActivated());
1428
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, config.isNomenclaturalStatusSectionActivated());
1429
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, config.isProtologueActivated());
1430
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, config.isTypeDesignationSectionActivated());
1431
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, config.isNameRelationsSectionActivated());
1432
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_HYBRID, config.isHybridActivated());
1433

    
1434
    }
1435

    
1436
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1437
		if (sectionMap.containsKey(string)){
1438
			return sectionMap.get(string);
1439
		}else{
1440
			return true;
1441
		}
1442

    
1443
	}
1444

    
1445

    
1446

    
1447
    /**
1448
     * <p>
1449
     * setAbcdConfigurator
1450
     * </p>
1451
     *
1452
     * @param preferredConfiguration
1453
     *            a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1454
     *            object.
1455
     */
1456
    public static Abcd206ImportConfigurator getAbcdImportConfigurationPreference() {
1457

    
1458
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1459
        ICdmRepository controller;
1460
        controller = CdmStore.getCurrentApplicationConfiguration();
1461
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
1462
        CdmPreference preference = null;
1463
        if (controller == null){
1464
            return null;
1465
        }
1466
        preference = controller.getPreferenceService().find(key);
1467
        boolean allowOverride = true;
1468
        if (preference != null && !preference.isAllowOverride()){
1469
            allowOverride = false;
1470
        }
1471
        if (getBooleanValue(IPreferenceKeys.ABCD_IMPORT_OVERRIDE) && allowOverride){
1472

    
1473
            config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1474

    
1475
            config.setAddMediaAsMediaSpecimen(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1476

    
1477
            config.setAllowReuseOtherClassifications(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1478
            config.setDeduplicateClassifications(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1479
            config.setDeduplicateReferences(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1480

    
1481
            config.setGetSiblings(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1482
            config.setIgnoreAuthorship(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1483
            config.setIgnoreImportOfExistingSpecimen(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1484
            config.setMapUnitIdToAccessionNumber(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1485
            config.setMapUnitIdToBarcode(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1486
            config.setMapUnitIdToCatalogNumber(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1487
            config.setMoveNewTaxaToDefaultClassification(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1488
            config.setOverwriteExistingSpecimens(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1489
            config.setReuseExistingDescriptiveGroups(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1490
            config.setReuseExistingMetaData(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1491
            config.setReuseExistingTaxaWhenPossible(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1492
            config.setNomenclaturalCode(NomenclaturalCode.getByKey(getStringValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE)));
1493
            return config;
1494

    
1495
        }
1496

    
1497
            if (preference == null){
1498
                return config;
1499
             } else{
1500
             String configString = preference.getValue();
1501
             String[] configArray = configString.split(";");
1502

    
1503
             for (String configItem: configArray){
1504
                 String[] keyValue = configItem.split(":");
1505
                 String keyString = keyValue[0];
1506
                 String valueString = keyValue[1];
1507
                 if (keyString.equals("ignoreImportOfExistingSpecimen")){
1508
                     config.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
1509
                 }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){
1510
                     config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
1511
                 }else if (keyString.equals("reuseExistingTaxaWhenPossible")){
1512
                     config.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
1513
                 }else if (keyString.equals("ignoreAuthorship")){
1514
                     config.setIgnoreAuthorship(Boolean.valueOf(valueString));
1515
                 }else if (keyString.equals("addMediaAsMediaSpecimen")){
1516
                     config.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
1517
                 }else if (keyString.equals("reuseExistingMetaData")){
1518
                     config.setReuseExistingMetaData(Boolean.valueOf(valueString));
1519
                 }else if (keyString.equals("reuseExistingDescriptiveGroups")){
1520
                     config.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
1521
                 }else if (keyString.equals("allowReuseOtherClassifications")){
1522
                     config.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
1523
                 }else if (keyString.equals("deduplicateReferences")){
1524
                     config.setDeduplicateReferences(Boolean.valueOf(valueString));
1525
                 }else if (keyString.equals("deduplicateClassifications")){
1526
                     config.setDeduplicateClassifications(Boolean.valueOf(valueString));
1527
                 }else if (keyString.equals("moveNewTaxaToDefaultClassification")){
1528
                     config.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
1529
                 }else if (keyString.equals("mapUnitIdToCatalogNumber")){
1530
                     config.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
1531
                 }else if (keyString.equals("mapUnitIdToAccessionNumber")){
1532
                     config.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
1533
                 }else if (keyString.equals("mapUnitIdToBarcode")){
1534
                     config.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
1535
                 }else if (keyString.equals("overwriteExistingSpecimens")){
1536
                     config.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
1537
                 }else if (keyString.equals("nomenclaturalCode")){
1538
                         config.setNomenclaturalCode(NomenclaturalCode.fromString(valueString));
1539
                 }else{
1540
                     logger.debug("This key of the abcd configurator needs to be added to the transformer: " + keyString);
1541
                 }
1542

    
1543
            }
1544
        }
1545
        return config;
1546
    }
1547

    
1548

    
1549
    public static void updateAbcdImportConfigurationPreference() {
1550

    
1551
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1552
        ICdmRepository controller;
1553
        controller = CdmStore.getCurrentApplicationConfiguration();
1554
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
1555
        CdmPreference preference = null;
1556
        if (controller == null){
1557
            return ;
1558
        }
1559
        preference = controller.getPreferenceService().find(key);
1560
        boolean allowOverride = true;
1561
        if (preference != null && !preference.isAllowOverride()){
1562
            allowOverride = false;
1563
        }
1564
        if (!getBooleanValue(IPreferenceKeys.ABCD_IMPORT_OVERRIDE) || !allowOverride){
1565

    
1566
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS, config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
1567
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN, config.isAddMediaAsMediaSpecimen());
1568

    
1569
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS, config.isAllowReuseOtherClassifications());
1570
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS, config.isDeduplicateClassifications());
1571
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES, config.isDeduplicateReferences());
1572

    
1573
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS, config.isGetSiblings());
1574
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP, config.isIgnoreAuthorship());
1575
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN, config.isIgnoreImportOfExistingSpecimen());
1576
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER, config.isMapUnitIdToAccessionNumber());
1577
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE, config.isMapUnitIdToBarcode());
1578
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER, config.isMapUnitIdToCatalogNumber());
1579
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION, config.isMoveNewTaxaToDefaultClassification());
1580
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN, config.isOverwriteExistingSpecimens());
1581
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS, config.isReuseExistingDescriptiveGroups());
1582
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA, config.isReuseExistingMetaData());
1583
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE, config.isReuseExistingTaxaWhenPossible());
1584
            if (config.getNomenclaturalCode() != null){
1585
                setStringValue(ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE, config.getNomenclaturalCode().getKey());
1586
            }
1587
        }
1588
    }
1589

    
1590

    
1591
    /**
1592
    *
1593
    */
1594
   public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1595
       NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1596
       if (local){
1597
          config.setSimpleDetailsViewActivated(getBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1598
          config.setAppendedPhraseActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1599
          config.setAtomisedEpithetsActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1600
          config.setAuthorshipSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1601
          config.setLSIDActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1602
          config.setNameApprobiationActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1603
          config.setNameCacheActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1604
          config.setNameRelationsSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1605
          config.setNomenclaturalCodeActived(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1606
          config.setNomenclaturalStatusSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1607
          config.setNomenclaturalReferenceSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1608
          config.setProtologueActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1609
          config.setRankActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1610
          config.setTaxonSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1611
          config.setTypeDesignationSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1612
          config.setHybridActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1613
       }else{
1614

    
1615
       }
1616

    
1617
      return config;
1618
   }
1619

    
1620

    
1621
   public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1622
       setBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1623
               config.isSimpleDetailsViewActivated());
1624
       setBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1625
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1626
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1627
               config.isNomenclaturalCodeActived());
1628
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1629
               config.isNameCacheActivated());
1630
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1631
               config.isAppendedPhraseActivated());
1632
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1633
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1634
               config.isAtomisedEpithetsActivated());
1635
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1636
               config.isAuthorCacheActivated());
1637
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1638
               config.isAuthorshipSectionActivated());
1639
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1640
               config.isNomenclaturalReferenceSectionActivated());
1641
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1642
               config.isNomenclaturalStatusSectionActivated());
1643
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1644
               config.isProtologueActivated());
1645
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1646
               config.isTypeDesignationSectionActivated());
1647
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1648
               config.isNameRelationsSectionActivated());
1649
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1650
               config.isHybridActivated());
1651

    
1652
   }
1653

    
1654
/**
1655
 * @return
1656
 */
1657
public static boolean isSortTaxaByRankAndName() {
1658

    
1659
    return getBooleanValue(IPreferenceKeys.SORT_TAXA_BY_RANK_AND_NAME);
1660
}
1661

    
1662
/**
1663
 * @return
1664
 */
1665
public static boolean isSortNamedAreaByOrderInVocabulary() {
1666

    
1667
    return getBooleanValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER);
1668
}
1669

    
1670
public static void setSortNamedAreasByOrderInVocabulary(boolean isSortByVocabularyOrder) {
1671
    setBooleanValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER, isSortByVocabularyOrder);
1672

    
1673
}
1674

    
1675
/**
1676
 * <p>
1677
 * setPreferredNamedAreasForDistributionEditor
1678
 * </p>
1679
 *
1680
 * @param saveCheckedElements
1681
 * @param saveGrayedElements
1682
 */
1683
public static void setLastSelectedReference(
1684
        List<String> lastSelectedReferences) {
1685

    
1686
        setStringValue(PreferencesUtil.LAST_SELECTED_REFERENCES, lastSelectedReferences.toString());
1687
    }
1688

    
1689
/**
1690
 * <p>
1691
 * setPreferredNamedAreasForDistributionEditor
1692
 * </p>
1693
 *
1694
 * @param saveCheckedElements
1695
 * @param saveGrayedElements
1696
 */
1697
public static List<String> getLastSelectedReferences() {
1698

    
1699
        //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1700
        String lastSelected = getStringValue(PreferencesUtil.LAST_SELECTED_REFERENCES);
1701
        List<String> result = new ArrayList<>();
1702
        if (!StringUtils.isBlank(lastSelected)){
1703
            Collections.addAll(result, lastSelected.substring(1,lastSelected.length()-1).split(", "));
1704
        }
1705
        return result;
1706
    }
1707

    
1708

    
1709
/**
1710
 * <p>
1711
 * setPreferredNamedAreasForDistributionEditor
1712
 * </p>
1713
 *
1714
 * @param saveCheckedElements
1715
 * @param saveGrayedElements
1716
 */
1717
public static void setPreferredNamedAreasForDistributionEditor(
1718
        String saveCheckedElements, String saveGrayedElements, boolean local) {
1719
    if (local){
1720
        setStringValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS, saveCheckedElements);
1721
        setStringValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS_GRAYED, saveGrayedElements);
1722

    
1723
    }
1724
    else{
1725
        CdmPreference preference = null;
1726

    
1727
        if (saveCheckedElements == null){
1728
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaTerms);
1729

    
1730
            if (preference == null){
1731
                return ;
1732
            } else{
1733
                setStringValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1734
                        saveCheckedElements);
1735
                preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1736
                setPreferenceToDB(preference);
1737

    
1738
            }
1739
        } else{
1740
           preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1741
           setPreferenceToDB(preference);
1742
           setStringValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1743
                    saveCheckedElements);
1744

    
1745
        }
1746
    }
1747

    
1748
}
1749

    
1750
/**
1751
 * @param saveCheckedElements
1752
 * @param saveCheckedElements2
1753
 * @param b
1754
 */
1755
public static void setPreferredVocabulariesForDistributionEditor(String saveCheckedElements,
1756
        boolean local) {
1757
    if (local){
1758
      setStringValue(PreferencesUtil.DISTRIBUTION_VOCABULARIES, saveCheckedElements);
1759
    }
1760
    else{
1761
        ICdmRepository controller;
1762
        CdmPreference preference = null;
1763

    
1764
        if (saveCheckedElements == null){
1765
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1766

    
1767
            if (preference == null){
1768
                return ;
1769
            } else{
1770
                setStringValue(DISTRIBUTION_VOCABULARIES,
1771
                        saveCheckedElements);
1772
                preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1773
                setPreferenceToDB(preference);
1774
            }
1775
        } else{
1776
            preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1777
            setPreferenceToDB(preference);
1778
            setStringValue(DISTRIBUTION_VOCABULARIES,
1779
                    saveCheckedElements);
1780

    
1781
        }
1782
    }
1783
}
1784

    
1785

    
1786

    
1787

    
1788
/**
1789
 * @param saveCheckedElements
1790
 * @param saveCheckedElements2
1791
 * @param b
1792
 */
1793
public static String getPreferredVocabulariesForDistributionEditor(boolean local) {
1794
    if (local){
1795

    
1796
        String pref = getStringValue(DISTRIBUTION_VOCABULARIES);
1797
        return pref;
1798
    }
1799
    else{
1800
        CdmPreference preference = null;
1801
        preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1802
        if (preference == null){
1803
            return null;
1804
        } else{
1805
            return preference.getValue();
1806
        }
1807

    
1808
    }
1809

    
1810

    
1811

    
1812
}
1813

    
1814
public static boolean getFilterCommonNameReferences(){
1815
    return getBooleanValue(PreferencesUtil.FILTER_COMMON_NAME_REFERENCES);
1816
}
1817

    
1818
/**
1819
 *
1820
 */
1821
public static void updateDBPreferences() {
1822
    CdmPreference preference = null;
1823
    IPreferenceStore prefStore = getPreferenceStore();
1824

    
1825
    //ABCD Configurator
1826

    
1827
    updateAbcdImportConfigurationPreference();
1828

    
1829
    preference = getPreferenceFromDB(PreferencePredicate.ShowImportExportMenu);
1830
    if (preference != null){
1831
        if(!getBooleanValue(SHOW_IO_MENU) ||  !preference.isAllowOverride()){
1832
           setBooleanValue(SHOW_IO_MENU, Boolean.valueOf(preference.getValue()));
1833
        }
1834
    }else{
1835
        setBooleanValue(SHOW_IO_MENU, true);
1836
    }
1837

    
1838
    preference = getPreferenceFromDB(PreferencePredicate.ShowMediaView);
1839
    if (preference != null){
1840
        if(!getBooleanValue(SHOW_MEDIA) ||  !preference.isAllowOverride()){
1841
            setBooleanValue(SHOW_MEDIA, Boolean.valueOf(preference.getValue()));
1842
        }
1843
    }else{
1844
        setBooleanValue(SHOW_MEDIA, true);
1845
    }
1846

    
1847
    preference = getPreferenceFromDB(PreferencePredicate.ShowChecklistPerspective);
1848
    if (preference != null){
1849
        if(!getBooleanValue(SHOW_CHECKLIST_PERSPECTIVE) ||  !preference.isAllowOverride()){
1850
            setBooleanValue(SHOW_CHECKLIST_PERSPECTIVE, Boolean.valueOf(preference.getValue()));
1851
        }
1852
    }else{
1853
        setBooleanValue(SHOW_CHECKLIST_PERSPECTIVE, false);
1854
    }
1855

    
1856
    //Specimen Details
1857
    preference = getPreferenceFromDB(PreferencePredicate.ShowSpecimen);
1858
    if (preference != null){
1859
        if(!getBooleanValue(SHOW_SPECIMEN) ||  !preference.isAllowOverride()){
1860
            setBooleanValue(SHOW_SPECIMEN, Boolean.valueOf(preference.getValue()));
1861
        }
1862
    }else{
1863
        setBooleanValue(SHOW_SPECIMEN, true);
1864
    }
1865

    
1866
    preference = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
1867
    if (preference != null){
1868
        if(!getBooleanValue(SHOW_TAXON_ASSOCIATIONS_OVERRIDE) ||  !preference.isAllowOverride()){
1869
            setBooleanValue(SHOW_TAXON_ASSOCIATIONS, Boolean.valueOf(preference.getValue()));
1870
        }
1871
    }
1872
    preference = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
1873
    if (preference != null){
1874
        if(!getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION_OVERRIDE) ||  !preference.isAllowOverride()){
1875
            setBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION, Boolean.valueOf(preference.getValue()));
1876
        }
1877
    }
1878
    preference = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
1879
    if (preference != null){
1880
        if(!getBooleanValue(SHOW_LIFE_FORM_OVERRIDE) ||  !preference.isAllowOverride()){
1881
            setBooleanValue(SHOW_LIFE_FORM, Boolean.valueOf(preference.getValue()));
1882
        }
1883
    }
1884
    preference = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
1885
    if (preference != null){
1886
        if(!getBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS_OVERRIDE) ||  !preference.isAllowOverride()){
1887
            setBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS, Boolean.valueOf(preference.getValue()));
1888
        }
1889
    }
1890

    
1891

    
1892
    //Name Details
1893
    NameDetailsConfigurator config = getPreferredNameDetailsConfiguration(false);
1894
    if (config != null ){
1895
        if (!getBooleanValue(OVERRIDE_NAME_DETAILS) ||  !getBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS)){
1896
            setPreferredNameDetailsConfiguration(config, false);
1897
        }
1898
    }
1899

    
1900
    //Distribution Editor
1901
    preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1902
    if (preference != null){
1903
        if (!getBooleanValue(DISTRIBUTION_VOCABULARIES_OVERRIDE) ||  !preference.isAllowOverride()){
1904
            setStringValue(DISTRIBUTION_VOCABULARIES, preference.getValue());
1905
            setBooleanValue(DISTRIBUTION_VOCABULARIES_ALLOW_OVERRIDE, preference.isAllowOverride());
1906
        }
1907
    }else{
1908
        setBooleanValue(DISTRIBUTION_VOCABULARIES_ALLOW_OVERRIDE,true);
1909
        setStringValue(DISTRIBUTION_VOCABULARIES, "");
1910
    }
1911

    
1912
    preference = getPreferenceFromDB(PreferencePredicate.DistributionEditorActivated);
1913
    if (preference != null){
1914
        if (!getBooleanValue(DISTRIBUTION_AREA_PREFRENCES_ACTIVE_OVERRIDE) || !preference.isAllowOverride()){
1915
            setBooleanValue(DISTRIBUTION_AREA_PREFRENCES_ACTIVE, Boolean.valueOf(preference.getValue()));
1916
        }
1917
    }
1918

    
1919
    preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionStatus);
1920
    if (preference != null){
1921
        if (!getBooleanValue(DISTRIBUTION_STATUS_OVERRIDE) || !preference.isAllowOverride()){
1922
            //get terms for the uuids... and add them to the termManager as preferred terms
1923
            ITermService termService = CdmStore.getService(ITermService.class);
1924
            String[] uuidArray = preference.getValue().split(";");
1925
            List<UUID> uuidList = new ArrayList();
1926
            for (String uuidString:uuidArray){
1927
                uuidList.add(UUID.fromString(uuidString));
1928
            }
1929

    
1930
            List<DefinedTermBase> definedTermBases = termService.load(uuidList, null);
1931
            CdmStore.getTermManager().setPreferredTerms(definedTermBases, TermStore.getTerms(TermType.PresenceAbsenceTerm, null));
1932
        }
1933
    }
1934

    
1935
    preference = getPreferenceFromDB(PreferencePredicate.CommonNameAreaVocabularies);
1936
    if (preference != null){
1937
        if (!getBooleanValue(COMMON_NAME_AREA_VOCABULARIES) ||  !preference.isAllowOverride()){
1938
            setStringValue(COMMON_NAME_AREA_VOCABULARIES, preference.getValue());
1939
            setBooleanValue(COMMON_NAME_AREA_VOCABULARIES_ALLOW_OVERRIDE, preference.isAllowOverride());
1940
        }
1941
    }else{
1942
        setStringValue(COMMON_NAME_AREA_VOCABULARIES, "");
1943
        setBooleanValue(COMMON_NAME_AREA_VOCABULARIES_ALLOW_OVERRIDE, true);
1944
    }
1945

    
1946
    preference = getPreferenceFromDB(PreferencePredicate.CommonNameReferencesWithMarker);
1947
    if (preference != null){
1948
        if (!getBooleanValue(FILTER_COMMON_NAME_REFERENCES) ||  !preference.isAllowOverride()){
1949
            setBooleanValue(FILTER_COMMON_NAME_REFERENCES, Boolean.valueOf(preference.getValue()));
1950
        }
1951
    }else{
1952
        setBooleanValue(FILTER_COMMON_NAME_REFERENCES, false);
1953
    }
1954

    
1955

    
1956
    preference = getPreferenceFromDB(PreferencePredicate.ShowTaxonNodeWizard);
1957
    if (preference != null){
1958
        if (!getBooleanValue(SHOW_TAXONNODE_WIZARD) ||  !preference.isAllowOverride()){
1959
            setBooleanValue(SHOW_TAXONNODE_WIZARD, Boolean.valueOf(preference.getValue()));
1960
        }
1961
    }else{
1962
        setBooleanValue(SHOW_TAXONNODE_WIZARD, true);
1963
    }
1964

    
1965
    preference = getPreferenceFromDB(PreferencePredicate.ShowIdInSource);
1966
    if (preference != null){
1967
        if (!getBooleanValue(SHOW_ID_IN_SOURCE) ||  !preference.isAllowOverride()){
1968
            setBooleanValue(SHOW_ID_IN_SOURCE, Boolean.valueOf(preference.getValue()));
1969
        }
1970
    }else{
1971
        setBooleanValue(SHOW_ID_IN_SOURCE, true);
1972
    }
1973

    
1974
    preference = getPreferenceFromDB(PreferencePredicate.ShowNameInSource);
1975
    if (preference != null){
1976
        if (!getBooleanValue(SHOW_NAME_IN_SOURCE) ||  !preference.isAllowOverride()){
1977
            setBooleanValue(SHOW_NAME_IN_SOURCE, Boolean.valueOf(preference.getValue()));
1978
        }
1979
    }else{
1980
        setBooleanValue(SHOW_NAME_IN_SOURCE, true);
1981
    }
1982

    
1983
    preference = getPreferenceFromDB(PreferencePredicate.DisableMultiClassification);
1984
    if (preference != null){
1985
        if (!getBooleanValue(DISABLE_MULTI_CLASSIFICATION) ||  !preference.isAllowOverride()){
1986
            setBooleanValue(DISABLE_MULTI_CLASSIFICATION, Boolean.valueOf(preference.getValue()));
1987
        }
1988
    }else{
1989
        setBooleanValue(DISABLE_MULTI_CLASSIFICATION, false);
1990
    }
1991

    
1992
}
1993

    
1994

    
1995

    
1996

    
1997

    
1998

    
1999

    
2000
}
(19-19/25)