Project

General

Profile

Download (71.3 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.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.IPreferencePredicate;
57
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
58
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
59
import eu.etaxonomy.cdm.model.metadata.TermDisplayEnum;
60
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
61
import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
62
import eu.etaxonomy.cdm.strategy.match.FieldMatcher;
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.internal.TaxeditorStorePlugin;
72
import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
73

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

    
85
	/**
86
	 *
87
	 */
88
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
89

    
90
	public static final String P2_REPOSITORIES_DELIM = ",";
91
	public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
92

    
93
	private final static Logger logger = Logger.getLogger(PreferencesUtil.class);
94

    
95

    
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
	static String prefKey(String name) {
111
	    return name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
112
	}
113

    
114
	public static String prefOverrideKey(String name) {
115
        return name + "_OVERRIDE_";
116
    }
117

    
118
	/**
119
     * <p>
120
     * setStringValue
121
     * </p>
122
     *
123
     *
124
     **/
125
    public static void setStringValue(String name, String value) {
126
        getPreferenceStore().setValue(prefKey(name), value);
127
    }
128

    
129
    /**
130
     * <p>
131
     * setIntValue
132
     * </p>
133
     *
134
     *
135
     **/
136
    public static void setIntValue(String name, int value) {
137
        getPreferenceStore().setValue(prefKey(name), value);
138
    }
139

    
140
    /**
141
     * <p>
142
     * setBooleanValue
143
     * </p>
144
     *
145
     *
146
     **/
147
    public static void setBooleanValue(String name, boolean value) {
148
        getPreferenceStore().setValue(prefKey(name), value);
149
    }
150

    
151
    /**
152
     * <p>
153
     * setBooleanValue
154
     * </p>
155
     *
156
     *
157
     **/
158
    public static void setDoubleValue(String name, double value) {
159
        getPreferenceStore().setValue(prefKey(name), value);
160
    }
161

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

    
173
    /**
174
     * <p>
175
     * setFloatValue
176
     * </p>
177
     *
178
     *
179
     **/
180
    public static void setLongValue(String name, long value) {
181
        getPreferenceStore().setValue(prefKey(name), value);
182
    }
183

    
184
    /**
185
     * <p>
186
     * getStringValue
187
     * </p>
188
     *
189
     *
190
     **/
191
    public static String getStringValue(String name, boolean local) {
192

    
193
        String prefValue = getDBPreferenceValue(name);
194
        if (prefValue == null || local){
195
            String dbSpecific = prefKey(name);
196
            if (getPreferenceStore().contains(dbSpecific)){
197
                prefValue = getPreferenceStore().getString(dbSpecific);
198
            }else{
199
                prefValue = getPreferenceStore().
200
                        getString(name);
201
            }
202
        }
203
        return prefValue;
204

    
205
    }
206

    
207
    public static String getStringValue(String name){
208
        return getStringValue(name, false);
209
    }
210

    
211
    private static String getDBPreferenceValue(String name) {
212
        CdmPreferenceCache cache = CdmPreferenceCache.instance();
213
        if (cache.get(name) != null){
214
            CdmPreference pref = cache.get(name);
215
            if (!pref.isAllowOverride() || !getBooleanValue(prefOverrideKey(name))){
216
                return pref.getValue();
217
            }
218
        }
219
        return null;
220
    }
221

    
222
    /**
223
     * <p>
224
     * setIntValue
225
     * </p>
226
     *
227
     *
228
     **/
229
    public static int getIntValue(String name) {
230
        String prefValue = getDBPreferenceValue(name);
231
        Integer result = null;
232
        try{
233
            result = Integer.parseInt(prefValue);
234
        }catch(NumberFormatException e){
235
            logger.debug("Preference value of " + name + " is not a number");
236
        }
237
        if (result == null){
238
            String dbSpecific = prefKey(name);
239
            if (getPreferenceStore().contains(dbSpecific)){
240
                result = getPreferenceStore().getInt(dbSpecific);
241
            }else{
242
                result =  getPreferenceStore().
243
                        getInt(name);
244
            }
245
        }
246
        return result;
247

    
248

    
249
    }
250

    
251
    /**
252
     * <p>
253
     * setBooleanValue
254
     * </p>
255
     *
256
     *
257
     **/
258
    public static boolean getBooleanValue(String name) {
259
        if (CdmStore.isActive()){
260
            String prefValue = getDBPreferenceValue(name);
261
            if (prefValue == null){
262
                String dbSpecific = prefKey(name);
263
                if (getPreferenceStore().contains(dbSpecific)){
264
                    return getPreferenceStore().getBoolean(dbSpecific);
265
                }else{
266
                    return getPreferenceStore().
267
                            getBoolean(name);
268
                }
269
            }else{
270
                return Boolean.valueOf(prefValue);
271
            }
272

    
273
        }else{
274
            return getPreferenceStore().getBoolean(name);
275
        }
276

    
277
    }
278

    
279
    /**
280
     * <p>
281
     * setBooleanValue
282
     * </p>
283
     *
284
     *
285
     **/
286
    public static double getDoubleValue(String name) {
287
        String prefValue = getDBPreferenceValue(name);
288
        Double result = null;
289
        try{
290
            result = Double.parseDouble(prefValue);
291
        }catch(NumberFormatException e){
292
            logger.debug("Preference value of " + name + " is not a number");
293
        }
294
        if (result == null){
295
            String dbSpecific = prefKey(name);
296
            if (getPreferenceStore().contains(dbSpecific)){
297
                result = getPreferenceStore().getDouble(dbSpecific);
298
            }else{
299
                result =  getPreferenceStore().
300
                        getDouble(name);
301
            }
302
        }
303
        return result;
304

    
305
    }
306

    
307
    /**
308
     * <p>
309
     * getFloatValue
310
     * </p>
311
     *
312
     *
313
     **/
314
    public static float getFloatValue(String name) {
315
        String prefValue = getDBPreferenceValue(name);
316
        Float result = null;
317
        try{
318
            result = Float.parseFloat(prefValue);
319
        }catch(NumberFormatException e){
320
            logger.debug("Preference value of " + name + " is not a number");
321
        }
322
        if (result == null){
323
            String dbSpecific = prefKey(name);
324
            if (getPreferenceStore().contains(dbSpecific)){
325
                result = getPreferenceStore().getFloat(dbSpecific);
326
            }else{
327
                result =  getPreferenceStore().
328
                        getFloat(name);
329
            }
330
        }
331
        return result;
332

    
333
    }
334

    
335
    /**
336
     * <p>
337
     * getLongValue
338
     * </p>
339
     *
340
     *
341
     **/
342
    public static long getLongValue(String name) {
343
        String prefValue = getDBPreferenceValue(name);
344
        Long result = null;
345
        try{
346
            result = Long.parseLong(prefValue);
347
        }catch(NumberFormatException e){
348
            logger.debug("Preference value of " + name + " is not a number");
349
        }
350
        if (result == null){
351
            String dbSpecific = prefKey(name);
352
            if (getPreferenceStore().contains(dbSpecific)){
353
                result = getPreferenceStore().getLong(dbSpecific);
354
            }else{
355
                result =  getPreferenceStore().
356
                        getLong(name);
357
            }
358
        }
359
        return result;
360
    }
361

    
362

    
363
	/**
364
	 * <p>
365
	 * setPreferredNomenclaturalCode
366
	 * </p>
367
	 *
368
	 * @param preferredCode
369
	 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
370
	 *            object.
371
	 */
372
	public static CdmPreference setPreferredNomenclaturalCode(
373
			String preferenceValue, boolean local) {
374
	    if (local){
375
	        setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
376
	                preferenceValue);
377
	    }
378
	    else{
379
    		ICdmRepository controller;
380
    		controller = CdmStore.getCurrentApplicationConfiguration();
381
    		if (controller == null){
382
                return null;
383
            }
384
    		PrefKey key = null;
385
    		try{
386
    		    key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
387
    		}catch (Exception e){
388
    		    System.out.println(e.getStackTrace());
389
    		}
390
    		CdmPreference preference = null;
391

    
392
    		if (preferenceValue == null){
393
    			preference = controller.getPreferenceService().find(key);
394
    			if (preference == null){
395
    				return null;
396
    			} else{
397
    			    setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
398
    				        preference.getValue());
399

    
400
    				return preference;
401
    			}
402
    		} else{
403
    			preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferenceValue);
404
    			controller.getPreferenceService().set(preference);
405

    
406
    		}
407
	    }
408
        return null;
409

    
410

    
411

    
412
	}
413

    
414
	/**
415
     * <p>
416
     * setPreferredNomenclaturalCode
417
     * </p>
418
     *
419
     * @param preferredCode
420
     *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
421
     *            object.
422
     */
423
    public static void setPreferredNomenclaturalCode(
424
        CdmPreference preference) {
425

    
426
        ICdmRepository controller;
427
        controller = CdmStore.getCurrentApplicationConfiguration();
428
        if (controller == null){
429
            return;
430
        }
431
        PrefKey key = null;
432
        try{
433
            key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
434
        }catch (Exception e){
435
            System.out.println(e.getStackTrace());
436
        }
437

    
438
        controller.getPreferenceService().set(preference);
439

    
440
    }
441

    
442

    
443
	public static NomenclaturalCode getPreferredNomenclaturalCode(){
444
	    if (getBooleanValue(prefOverrideKey(PreferencePredicate.NomenclaturalCode.getKey()))) {
445
            return getPreferredNomenclaturalCode(true);
446
        } else{
447
            return getPreferredNomenclaturalCode(false);
448
        }
449
	}
450

    
451
	/**
452
	 * <p>
453
	 * getPreferredNomenclaturalCode
454
	 * </p>
455
	 *
456
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
457
	 */
458
	public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
459

    
460
		CdmPreference pref = null;
461
		if (!useLocal){
462
		    pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
463
		}
464

    
465
	    String preferredCode;
466
	    if(pref == null){
467
	    	preferredCode = getStringValue(
468
					PREFERRED_NOMENCLATURAL_CODE_KEY, true);
469

    
470
	    }else{
471
	    	preferredCode = pref.getValue();
472
	    }
473

    
474
	    return getPreferredNomenclaturalCode(preferredCode);
475

    
476
	}
477

    
478
	/**
479
     * <p>
480
     * getPreferredNomenclaturalCode
481
     * </p>
482
     *
483
     * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
484
     */
485
    public static NomenclaturalCode getPreferredNomenclaturalCode(String preferenceKeyNomenclaturalCode) {
486

    
487
        for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
488
            if (getPreferenceKey(code).equals(preferenceKeyNomenclaturalCode)) {
489
                return code;
490
            }
491
        }
492
        return null;
493
    }
494

    
495
	public static boolean isShowTaxonAssociations(){
496
        boolean result = getBooleanValue(EditorPreferencePredicate.ShowTaxonAssociations.getKey());
497
        return result;
498
    }
499

    
500
	public static boolean isShowLifeForm(){
501
	    boolean result =  getBooleanValue(EditorPreferencePredicate.ShowLifeForm.getKey());
502
	    return result;
503
    }
504

    
505
	public static boolean isDeterminationOnlyForFieldUnits(){
506
	    boolean result =  getBooleanValue(EditorPreferencePredicate.DeterminationOnlyForFieldUnits.getKey());
507
        return result;
508
    }
509

    
510
	public static boolean isCollectingAreaInGeneralSection(){
511
	    boolean result =  getBooleanValue(EditorPreferencePredicate.ShowCollectingAreasInGeneralSection.getKey());
512
        return result;
513
	}
514

    
515
	public static CdmPreference getPreferenceFromDB(IPreferencePredicate predicate){
516
        ICdmRepository controller;
517
        CdmPreference pref = null;
518

    
519
        try{
520
            if(CdmStore.isActive()){
521
                controller = CdmStore.getCurrentApplicationConfiguration();
522
                PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), predicate);
523
                pref = controller.getPreferenceService().find(key);
524
            }
525
        }catch(Exception e){
526
            e.printStackTrace();
527
        }
528

    
529
        return pref;
530

    
531
    }
532

    
533
    public static void setPreferenceToDB(CdmPreference preference){
534
        ICdmRepository controller;
535
        try{
536
            if(CdmStore.isActive()){
537
                controller = CdmStore.getCurrentApplicationConfiguration();
538
                controller.getPreferenceService().set(preference);
539
            }
540
        }catch(Exception e){
541
            e.printStackTrace();
542
        }
543

    
544
    }
545

    
546

    
547

    
548
	public static String getPreferredDefaultLangugae(){
549
	    String preferredLanguage = getStringValue(DEFAULT_LANGUAGE_EDITOR);
550
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
551
	        return preferredLanguage;
552
	    }
553
	    return null;
554
	}
555

    
556
	public static boolean isShowMediaPreview(){
557
        boolean isShowMediaPreview = getBooleanValue(SHOW_MEDIA_PREVIEW);
558
        return isShowMediaPreview;
559
    }
560

    
561
	/**
562
	 * Get the match strategy for the given class that was stored in preferences
563
	 * or the default strategy if it was not stored in preferences
564
	 *
565
	 * @param clazz
566
	 *            a {@link java.lang.Class} object.
567
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
568
	 */
569
	public static IMatchStrategy getMatchStrategy(Class<?> clazz) {
570
		String className = clazz.getName();
571
		if (getBooleanValue(MATCH_STRATEGY_PREFIX + className)) {
572
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
573

    
574
			//TODO CacheMatchers (or multiple field matchers in future) are missing here
575
			for (FieldMatcher fieldMatcher : matchStrategy.getMatching().getFieldMatchers(false)) {
576
				String fieldName = fieldMatcher.getPropertyName();
577
				String matchModeName = getStringValue(
578
						getMatchStrategyFieldName(className, fieldName));
579
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
580
				try {
581
					matchStrategy.setMatchMode(fieldName, matchMode);
582
				} catch (MatchException e) {
583
					MessagingUtils.error(PreferencesUtil.class, e);
584
					throw new RuntimeException(e);
585
				}
586
			}
587

    
588
			return matchStrategy;
589
		}
590
		return getDefaultMatchStrategy(clazz);
591
	}
592

    
593
	/**
594
	 * Stores a matchStrategy into the preference store.
595
	 *
596
	 * @param matchStrategy
597
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
598
	 *            object.
599
	 */
600
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
601
		String className = "ANY class"; //FIXME was: matchStrategy.getMatchClass().getName(); seems currently not in use
602
		setBooleanValue(MATCH_STRATEGY_PREFIX + className, true);
603

    
604
		List<FieldMatcher> fieldMatchers = matchStrategy.getMatching().getFieldMatchers(false);
605

    
606
		for (FieldMatcher fieldMatcher : fieldMatchers) {
607
			String fieldName = fieldMatcher.getPropertyName();
608
			setStringValue(
609
					getMatchStrategyFieldName(className, fieldName),
610
					fieldMatcher.getMatchMode().name());
611
		}
612
	}
613

    
614
	/**
615
	 * Helper method to create the preference property for a match field.
616
	 *
617
	 * @param className
618
	 * @param fieldName
619
	 * @return
620
	 */
621
	private static String getMatchStrategyFieldName(String className,
622
			String fieldName) {
623
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
624
	}
625

    
626
	/**
627
	 * Returns the default match strategy for a given class.
628
	 *
629
	 * @param clazz
630
	 *            a {@link java.lang.Class} object.
631
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
632
	 */
633
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
634
		return DefaultMatchStrategy.NewInstance(clazz);
635
	}
636

    
637
	/**
638
	 * <p>
639
	 * getDateFormatPattern
640
	 * </p>
641
	 *
642
	 * @return a {@link java.lang.String} object.
643
	 */
644
	public static String getDateFormatPattern() {
645
		// TODO make this configurable in properties
646
		String pattern = "Y-M-d H:m";
647
		return pattern;
648
	}
649

    
650
	/**
651
	 * <p>
652
	 * addTermToPreferredTerms
653
	 * </p>
654
	 *
655
	 * @param term
656
	 *            a T object.
657
	 * @param <T>
658
	 *            a T object.
659
	 */
660
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
661

    
662
		// VocabularyEnum vocabulary =
663
		// VocabularyEnum.getVocabularyEnum(term.getClass());
664
		//
665
		// getPreferenceStore().setValue(getPreferenceKey(term),
666
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
667
		//
668
		// firePreferencesChanged(term.getClass());
669
	}
670

    
671
	/**
672
	 * Construct a unique key using the CdmBase object's uuid
673
	 *
674
	 * @param cdmBase
675
	 * @return
676
	 */
677
	private static String getPreferenceKey(ICdmBase cdmBase) {
678
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
679

    
680
		String key = cdmBase.getClass().getName().concat(".")
681
				.concat(cdmBase.getUuid().toString());
682
		if (key.contains("javassist")) {
683
			MessagingUtils.info("proxy");
684
		}
685
		return key;
686
	}
687

    
688
	/**
689
	 * Construct a unique key using the CdmBase object's uuid
690
	 *
691
	 * @param cdmBase
692
	 * @return
693
	 */
694
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
695
		simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
696
		String key = simpleTerm.getClass().getName().concat(".")
697
				.concat(simpleTerm.getUuid().toString());
698
		if (key.contains("javassist")) {
699
			MessagingUtils.warn(PreferencesUtil.class,
700
					"Trying to persist a preference based on a proxy class.");
701
		}
702
		return key;
703
	}
704

    
705

    
706

    
707
	/**
708
	 * Construct a unique key using the CdmBase object's uuid
709
	 *
710
	 * @param cdmBase
711
	 * @return
712
	 */
713
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
714
		definedTerm = HibernateProxyHelper.deproxy(definedTerm);
715
		String key = definedTerm.getClass().getName().concat(".")
716
				.concat(definedTerm.getUuid().toString());
717
		if (key.contains("javassist")) {
718
			MessagingUtils.warn(PreferencesUtil.class,
719
					"Trying to persist a preference based on a proxy class.");
720
		}
721
		return key;
722
	}
723

    
724
	/**
725
	 * Retrieves search preferences from the preference store
726
	 *
727
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
728
	 */
729
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
730
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
731

    
732
		configurator.setDoTaxa(getBooleanValue(
733
				TAXON_SERVICE_CONFIGURATOR_TAXA));
734
		configurator.setDoSynonyms(getBooleanValue(
735
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
736
		configurator.setDoNamesWithoutTaxa(getBooleanValue(
737
				TAXON_SERVICE_CONFIGURATOR_NAMES));
738
		configurator.setDoTaxaByCommonNames(getBooleanValue(
739
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
740
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getStringValue(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
741

    
742
		return configurator;
743
	}
744

    
745
	/**
746
	 * create new preferences, setting all search options to true
747
	 *
748
	 * @return a
749
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
750
	 *         object.
751
	 */
752
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
753
		IFindTaxaAndNamesConfigurator configurator = FindTaxaAndNamesConfiguratorImpl.NewInstance();
754

    
755
		configurator.setDoTaxa(true);
756
		configurator.setDoSynonyms(true);
757
		configurator.setDoNamesWithoutTaxa(true);
758
		configurator.setDoTaxaByCommonNames(true);
759

    
760
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
761
				"name", "name.$", "relationsFromThisTaxon.$"));
762

    
763
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
764
				"name", "name.$", "synonyms.relatedTo.*"));
765

    
766
		// DEFAULT VALUES
767
		// match mode is a simple like, actually all other match modes are kind
768
		// of bogus
769
		configurator
770
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
771
		// we set page number and size here as this should always be unlimited
772
		configurator.setPageNumber(0);
773
		// TODO currently limit results to 10000
774
		configurator.setPageSize(10000);
775
		setSearchConfigurator(configurator) ;
776
		return configurator;
777
	}
778

    
779
	/**
780
	 * Store search preferences
781
	 *
782
	 * @param configurator
783
	 *            a
784
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
785
	 *            object.
786
	 */
787
	public static void setSearchConfigurator(
788
			IFindTaxaAndNamesConfigurator configurator) {
789
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
790
				configurator.isDoTaxa());
791
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
792
				configurator.isDoSynonyms());
793
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
794
				configurator.isDoNamesWithoutTaxa());
795
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
796
				configurator.isDoTaxaByCommonNames());
797
	}
798

    
799
	/**
800
	 * <p>
801
	 * firePreferencesChanged
802
	 * </p>
803
	 *
804
	 * @param clazz
805
	 *            a {@link java.lang.Class} object.
806
	 */
807
	public static void firePreferencesChanged(Class clazz) {
808
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
809
				null, clazz);
810
	}
811

    
812
	public static String createPreferenceString(String property){
813
	   return prefKey(property);
814

    
815
	}
816
	public static String createOverridePreferenceString(String property){
817
	       return prefOverrideKey(property);
818

    
819
	    }
820

    
821
	/**
822
	 * Set default values for preferences
823
	 */
824
	public static void setDefaults() {
825
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
826
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
827
				true);
828
		getPreferenceStore().setDefault(createPreferenceString(EDIT_MAP_SERVICE_ACCES_POINT),
829
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
830
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
831
		getPreferenceStore().setDefault(createPreferenceString(SHOULD_CONNECT_AT_STARTUP), false);
832
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_ACCESS_POINT),
833
				"http://www.biodiversitylibrary.org/openurl");
834
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_IMAGE_MAX_WIDTH), "1000");
835
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_IMAGE_MAX_HEIGHT), "1000");
836
		//Distribution Editor:
837
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.DistributionEditorActivated.getKey()), Boolean.valueOf(EditorPreferencePredicate.DistributionEditorActivated.getDefaultValue().toString()));
838
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getKey()), EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getDefaultValue().toString());
839
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.DisplayOfStatus.getKey()), EditorPreferencePredicate.DisplayOfStatus.getDefaultValue().toString());
840

    
841

    
842
		//Name Details
843
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS), true);
844
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE), true);
845
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP), true);
846
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_HYBRID), true);
847
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_LSID), true);
848
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP), true);
849
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NAMECACHE), true);
850
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE), true);
851
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE), true);
852
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS), true);
853
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE), true);
854
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_RANK), true);
855
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION), true);
856
		//Navigator preferences
857
		getPreferenceStore().setDefault(createPreferenceString(SORT_NODES), NavigatorOrderEnum.RankAndNameOrder.getKey());
858

    
859
		getPreferenceStore().setDefault(createPreferenceString(SORT_TAXA_BY_RANK_AND_NAME), true);
860
		//override db preferences
861
		getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(EditorPreferencePredicate.AbcdImportConfig.getKey())), false);
862
		getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(EditorPreferencePredicate.ShowSpecimen.getKey())), true);
863
		getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(EditorPreferencePredicate.NameDetailsView.getKey())), false);
864
		getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(EditorPreferencePredicate.DistributionEditorActivated.getKey())), false);
865

    
866
		getPreferenceStore().setDefault(createPreferenceString(FILTER_COMMON_NAME_REFERENCES), false);
867
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.ShowTaxonNodeWizard.getKey()), Boolean.valueOf(EditorPreferencePredicate.ShowTaxonNodeWizard.getDefaultValue().toString()));
868
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.ShowNamespaceInSource.getKey()), Boolean.valueOf(EditorPreferencePredicate.ShowNamespaceInSource.getDefaultValue().toString()));
869
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.ShowIdInSource.getKey()), Boolean.valueOf(EditorPreferencePredicate.ShowIdInSource.getDefaultValue().toString()));
870
		getPreferenceStore().setDefault(createPreferenceString(EditorPreferencePredicate.DisableMultiClassification.getKey()), Boolean.valueOf(EditorPreferencePredicate.DisableMultiClassification.getDefaultValue().toString()));
871
		getPreferenceStore().setDefault(createPreferenceString(DISABLE_SEC), false);
872
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SEC_DETAILS), true);
873
	}
874

    
875
	/**
876
	 * <p>
877
	 * checkNomenclaturalCode
878
	 * </p>
879
	 */
880
	public static void checkNomenclaturalCode() {
881
		// First time Editor is opened, no nomenclatural code has been set
882
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
883
			PreferencesUtil.setPreferredNomenclaturalCode(getPreferenceKey(NomenclaturalCode.ICNAFP), true);
884
		}
885

    
886

    
887

    
888
	}
889
	public static void setNomenclaturalCodePreferences(){
890
	    ICdmRepository controller;
891
        controller = CdmStore.getCurrentApplicationConfiguration();
892
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
893
        CdmPreference preference = null;
894
        if (controller == null){
895
            return ;
896
        }
897
        preference = controller.getPreferenceService().find(key);
898
        if (preference == null){
899
            return;
900
        }
901
//        setBooleanValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
902

    
903
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
904
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
905
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
906

    
907
        setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
908
                getPreferenceKey(preferredCode));
909

    
910
	}
911

    
912
	public static void checkDefaultLanguage(){
913
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
914
	       Shell shell = AbstractUtility.getShell();
915
	       int open = new DefaultLanguageDialog(shell).open();
916
	       if(open == Window.OK){
917
	           PlatformUI.getWorkbench().restart();
918
	       }
919
	    }else{
920
	        //TODO:In case of a reinstall, the config.ini will be overwritten
921
	        //     here you create config.ini with the stored key from preferences
922
	    }
923
	}
924

    
925
	/**
926
	 * <p>
927
	 * getMapServiceAccessPoint
928
	 * </p>
929
	 *
930
	 * @return a {@link java.lang.String} object.
931
	 */
932
	public static String getMapServiceAccessPoint() {
933
		return getStringValue(EDIT_MAP_SERVICE_ACCES_POINT);
934
	}
935

    
936
	/**
937
	 * <p>
938
	 * shouldConnectAtStartUp
939
	 * </p>
940
	 *
941
	 * @return a boolean.
942
	 */
943
	public static boolean shouldConnectAtStartUp() {
944
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
945
		//return getBooleanValue(SHOULD_CONNECT_AT_STARTUP);
946
		return false;
947
	}
948

    
949
	/**
950
	 * <p>
951
	 * getDefaultFeatureTreeForTextualDescription
952
	 * </p>
953
	 *
954
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
955
	 */
956
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
957
		String uuidString = getStringValue(
958
				FEATURE_TREE_DEFAULT_TEXT);
959
		if (StringUtils.isBlank(uuidString)) {
960
            return null;
961
        }
962
		FeatureTree tree = CdmStore.getService(
963
	                IFeatureTreeService.class).load(UUID.fromString(uuidString));
964
		if (tree.getId() == 0) {
965
            return null;
966
        }
967
		return tree;
968
	}
969

    
970
	/**
971
	 * <p>
972
	 * getDefaultFeatureTreeForStructuredDescription
973
	 * </p>
974
	 *
975
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
976
	 */
977
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
978
		String uuidString = getStringValue(
979
				FEATURE_TREE_DEFAULT_STRUCTURE);
980
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
981
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
982
	}
983

    
984
	/**
985
	 * <p>
986
	 * setSortRanksHierarchichally
987
	 * </p>
988
	 *
989
	 * @param selection
990
	 *            a boolean.
991
	 */
992
	public static void setSortRanksHierarchichally(boolean selection) {
993
		setBooleanValue(SORT_RANKS_HIERARCHICHALLY, selection);
994
	}
995

    
996
	/**
997
	 * <p>
998
	 * getSortRanksHierarchichally
999
	 * </p>
1000
	 *
1001
	 * @return a boolean.
1002
	 */
1003
	public static boolean getSortRanksHierarchichally() {
1004
		return getBooleanValue(SORT_RANKS_HIERARCHICHALLY);
1005
	}
1006

    
1007
	public static boolean isMultilanguageTextEditingCapability() {
1008
		return getBooleanValue(
1009
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
1010
	}
1011

    
1012
	public static Language getGlobalLanguage() {
1013

    
1014

    
1015
		String languageUuidString = getStringValue(
1016
				GLOBAL_LANGUAGE_UUID);
1017

    
1018
		if(!CdmStore.isActive()) {
1019
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
1020
            return null;
1021
        }
1022

    
1023
		if (CdmUtils.isBlank(languageUuidString)) {
1024
			return Language.getDefaultLanguage();
1025
		}
1026

    
1027
		UUID languageUuid = UUID.fromString(languageUuidString);
1028
		return (Language) CdmStore.getService(ITermService.class).load(
1029
				languageUuid);
1030
	}
1031

    
1032
	public static void setGlobalLanguage(Language language) {
1033
	    if(language != null) {
1034
	        setStringValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
1035
	        CdmStore.setDefaultLanguage(language);
1036
	    }
1037

    
1038
	}
1039

    
1040
	/**
1041
	 * @return
1042
	 */
1043
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
1044
		List<MarkerType> markerTypes = CdmStore.getTermManager()
1045
				.getPreferredTerms(MarkerType.class);
1046

    
1047
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
1048

    
1049
		for (MarkerType markerType : markerTypes) {
1050
			String name = getMarkerTypeEditingPreferenceKey(markerType);
1051
			Boolean value = getBooleanValue(name);
1052

    
1053
			result.put(markerType, value);
1054
		}
1055

    
1056
		return result;
1057
	}
1058

    
1059
	/**
1060
	 * @param markerTypeEditingMap
1061
	 */
1062
	public static void setEditMarkerTypePreferences(
1063
			Map<MarkerType, Boolean> markerTypeEditingMap) {
1064
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
1065
			String name = getMarkerTypeEditingPreferenceKey(markerType);
1066
			setBooleanValue(name,
1067
					markerTypeEditingMap.get(markerType));
1068
		}
1069

    
1070
	}
1071

    
1072
	private static String getMarkerTypeEditingPreferenceKey(
1073
			MarkerType markerType) {
1074
		markerType = HibernateProxyHelper.deproxy(markerType);
1075
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
1076
	}
1077

    
1078
	/**
1079
	 * <p>
1080
	 * setEditMarkerTypePreference
1081
	 * </p>
1082
	 *
1083
	 * @param input
1084
	 *            a {@link org.eclipse.ui.IEditorInput} object.
1085
	 * @param markerType
1086
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
1087
	 * @param edit
1088
	 *            a boolean.
1089
	 */
1090
	public static void setEditMarkerTypePreference(MarkerType markerType,
1091
			boolean edit) {
1092
		setBooleanValue(
1093
				getMarkerTypeEditingPreferenceKey(markerType), edit);
1094
	}
1095

    
1096
	/**
1097
	 * @return
1098
	 */
1099
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
1100
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
1101
				.NewInstance();
1102
		configurator.setMoveDerivedUnitMediaToGallery(true);
1103
		configurator.setMoveFieldObjectMediaToGallery(true);
1104
		return configurator;
1105
	}
1106

    
1107
	/**
1108
	 * This method will write language properties to the config.ini located in the configuration folder
1109
	 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
1110
	 *
1111
	 * @param setLanguage 0 is for german and 1 for english.
1112
	 * @throws IOException
1113
	 */
1114
    public void writePropertyToConfigFile(int setLanguage) throws IOException {
1115
        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
1116
        //give warning to user if the directory has no write access
1117
        if(file == null){
1118
            throw new IOException();
1119
        }
1120
        Properties properties = load(file.getAbsolutePath()+"/config.ini");
1121
        switch(setLanguage){
1122
        case 0:
1123
            properties.setProperty("osgi.nl", "de");
1124
            setStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
1125
            break;
1126
        case 1:
1127
            properties.setProperty("osgi.nl", "en");
1128
            setStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
1129
            break;
1130
        default:
1131
            break;
1132
        }
1133
        save(file+"/config.ini", properties);
1134
    }
1135

    
1136
    /**
1137
     * This method loads a property from a given file and returns it.
1138
     *
1139
     * @param filename
1140
     * @return
1141
     * @throws IOException
1142
     */
1143
    private Properties load(String filename) throws IOException {
1144
        FileInputStream in = new FileInputStream(filename);
1145
        Properties prop = new Properties();
1146
        prop.load(in);
1147
        in.close();
1148
        return prop;
1149
    }
1150

    
1151
    /**
1152
     * This method saves a property to the specified file.
1153
     *
1154
     * @param filename
1155
     * @param properties
1156
     * @throws IOException
1157
     */
1158
    private void save(String filename, Properties properties) throws IOException{
1159
        FileOutputStream fos =  new FileOutputStream(filename);
1160
        properties.store(fos, "");
1161
        fos.close();
1162
    }
1163

    
1164
    /**
1165
     * Saves a list of P2 Metadata Repositories as string with specified delimiters
1166
     *
1167
     * @param p2Repos
1168
     */
1169
    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
1170
        StringBuilder sb = new StringBuilder();
1171
        for(MetadataRepositoryElement p2Repo : p2Repos) {
1172
            sb.append(P2_REPOSITORIES_DELIM);
1173
            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
1174
                sb.append("-");
1175
            } else {
1176
                sb.append(p2Repo.getName());
1177
            }
1178
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
1179
            sb.append(p2Repo.getLocation().toString());
1180
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
1181
            sb.append(String.valueOf(p2Repo.isEnabled()));
1182
        }
1183
        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
1184
    }
1185

    
1186

    
1187
    /**
1188
     * Retrieves a list of previously saved P2 repositories
1189
     *
1190
     * @return
1191
     */
1192
    public static List<MetadataRepositoryElement> getP2Repositories() {
1193
        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
1194
        String p2ReposPref =  getStringValue(P2_REPOSITORY_LIST);
1195
        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
1196
            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
1197

    
1198
            while(p2ReposPrefST.hasMoreTokens()) {
1199
                String p2RepoStr = p2ReposPrefST.nextToken();
1200
                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
1201
                if(p2ReposStrST.countTokens()==3) {
1202
                    String nickname = p2ReposStrST.nextToken();
1203
                    URI uri = null;
1204
                    try {
1205
                        uri = new URI(p2ReposStrST.nextToken());
1206
                    } catch (URISyntaxException e) {
1207
                        continue;
1208
                    }
1209
                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
1210
                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
1211
                    mre.setNickname(nickname);
1212
                    mre.setEnabled(enabled);
1213
                    p2Repos.add(mre);
1214
                }
1215
            }
1216
        }
1217

    
1218
        return p2Repos;
1219
    }
1220

    
1221
    /**
1222
     * enables/disables nested composite. <br>
1223
     *
1224
     * @param ctrl - Composite to be en-/disabeld
1225
     * @param enabled - boolean
1226
     */
1227
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
1228
        if (ctrl instanceof Composite) {
1229
            Composite comp = (Composite) ctrl;
1230
            for (Control c : comp.getChildren()) {
1231
                recursiveSetEnabled(c, enabled);
1232
            }
1233
        } else {
1234
            ctrl.setEnabled(enabled);
1235
        }
1236
    }
1237

    
1238

    
1239

    
1240

    
1241
    /**
1242
     * @param orderActivated
1243
     */
1244
    public static void setSortNodes(NavigatorOrderEnum nodesOrder) {
1245
        setStringValue(SORT_NODES, nodesOrder.key);
1246

    
1247
    }
1248

    
1249
    /**
1250
     * @param orderActivated
1251
     */
1252
    public static NavigatorOrderEnum getSortNodes() {
1253
        return NavigatorOrderEnum.valueOf(getStringValue(SORT_NODES));
1254

    
1255
    }
1256

    
1257
    /**
1258
     * @param orderActivated
1259
     */
1260
    public static boolean isNodesSortedNaturally() {
1261
        NavigatorOrderEnum value = NavigatorOrderEnum.valueOf(getStringValue(SORT_NODES));
1262
        return value.equals(NavigatorOrderEnum.NaturalOrder);
1263

    
1264
    }
1265

    
1266
    /**
1267
     * @param orderActivated
1268
     */
1269
    public static boolean isNodesSortedByName() {
1270
        NavigatorOrderEnum value = NavigatorOrderEnum.valueOf(getStringValue(SORT_NODES));
1271
        return value.equals(NavigatorOrderEnum.AlphabeticalOrder);
1272

    
1273
    }
1274

    
1275
    /**
1276
     * @param orderActivated
1277
     */
1278
    public static boolean isNodesSortedByNameAndRank() {
1279
        NavigatorOrderEnum value = NavigatorOrderEnum.valueOf(getStringValue(SORT_NODES));
1280
        return value.equals(NavigatorOrderEnum.RankAndNameOrder);
1281

    
1282
    }
1283
	/**
1284
	 * <p>
1285
	 * setStoreNavigatorState
1286
	 * </p>
1287
	 *
1288
	 * @param selection
1289
	 *            a boolean.
1290
	 */
1291
	public static boolean isStoreNavigatorState() {
1292
		return getBooleanValue(RESTORE_NAVIGATOR_STATE);
1293

    
1294
	}
1295

    
1296
	/**
1297
	 * <p>
1298
	 * setStoreNavigatorState
1299
	 * </p>
1300
	 *
1301
	 * @param selection
1302
	 *            a boolean.
1303
	 */
1304
	public static void setStoreNavigatorState(boolean selection) {
1305
		setBooleanValue(RESTORE_NAVIGATOR_STATE, selection);
1306

    
1307
	}
1308

    
1309
    /**
1310
     * @return
1311
     */
1312
    public static boolean isShowUpWidgetIsDisposedMessages() {
1313
       return getBooleanValue(IS_SHOW_UP_WIDGET_IS_DISPOSED);
1314
    }
1315
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
1316
        setBooleanValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
1317
    }
1318

    
1319
    /**
1320
     * @return
1321
     */
1322
    public static boolean isShowIdInVocabularyInChecklistEditor() {
1323
        String area_display = getStringValue(EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getKey());
1324
        if (area_display.equals(TermDisplayEnum.IdInVocabulary.getKey())) {
1325
            return true;
1326
        }else{
1327
            return false;
1328
        }
1329
    }
1330
    public static boolean isShowSymbol1InChecklistEditor() {
1331
        String area_display = getStringValue(EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getKey());
1332
        if (area_display.equals(TermDisplayEnum.Symbol1.getKey())) {
1333
            return true;
1334
        }else{
1335
            return false;
1336
        }
1337
     }
1338
    public static boolean isShowSymbol2InChecklistEditor() {
1339
        String area_display = getStringValue(EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getKey());
1340
        if (area_display.equals(TermDisplayEnum.Symbol2.getKey())) {
1341
            return true;
1342
        }else{
1343
            return false;
1344
        }
1345
     }
1346
    public static void setAreaDisplayInChecklistEditor(String selection) {
1347
        setStringValue(EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getKey(), selection);
1348
    }
1349

    
1350
    public static void setOwnDescriptionForChecklistEditor(boolean selection) {
1351
        setBooleanValue(EditorPreferencePredicate.OwnDescriptionForDistributionEditor.getKey(), selection);
1352
    }
1353

    
1354
    public static boolean isOwnDescriptionForChecklistEditor() {
1355
        return getBooleanValue(EditorPreferencePredicate.OwnDescriptionForDistributionEditor.getKey());
1356
    }
1357

    
1358
    /**
1359
     * @return
1360
     */
1361
    public static String displayAreaInChecklistEditor() {
1362
        String result = getStringValue(EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getKey());
1363
        if (result == null){
1364
            return (String) EditorPreferencePredicate.DisplayOfAreasInDistributionEditor.getDefaultValue();
1365
        }
1366
       return result;
1367
    }
1368

    
1369
    /**
1370
     * @return
1371
     */
1372
    public static String displayStatusInChecklistEditor() {
1373
        String result = getStringValue(EditorPreferencePredicate.DisplayOfStatus.getKey());
1374
        if (result == null){
1375
            return (String) EditorPreferencePredicate.DisplayOfStatus.getDefaultValue();
1376
        }
1377
       return result;
1378
    }
1379
    public static void setDisplayStatusInChecklistEditor(String selection) {
1380
        setStringValue(EditorPreferencePredicate.DisplayOfStatus.getKey(), selection);
1381
    }
1382

    
1383
    /**
1384
     * @return
1385
     */
1386
    public static boolean isShowRankInChecklistEditor() {
1387
        return getBooleanValue(EditorPreferencePredicate.ShowRankInDistributionEditor.getKey());
1388
    }
1389
    public static void setShowRankInChecklistEditor(boolean selection) {
1390
       setBooleanValue(EditorPreferencePredicate.ShowRankInDistributionEditor.getKey(), selection);
1391
    }
1392

    
1393
    /**
1394
     * @param object
1395
     * @param b
1396
     * @return
1397
     */
1398
    public static NameDetailsConfigurator getPreferredNameDetailsConfiguration( boolean local) {
1399
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1400
        CdmPreferenceCache cache = CdmPreferenceCache.instance();
1401
        CdmPreference preference = null;
1402

    
1403
        if (!local) {
1404
            preference = cache.get(EditorPreferencePredicate.NameDetailsView.getKey());
1405
            if (preference == null){
1406
                return null;
1407
            }
1408

    
1409
            setBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1410

    
1411
            //the preference value is build like this:
1412
            //<section1>:true;<section2>:false....
1413

    
1414
            String value = preference.getValue();
1415
            String [] sections = value.split(";");
1416
            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1417
            String[] sectionValues;
1418
            for (String sectionValue: sections){
1419
                sectionValues = sectionValue.split(":");
1420
                sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1421
            }
1422
            config.setAllowOverride(preference.isAllowOverride());
1423
            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1424

    
1425
    //        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1426
    //                (getValue(sectionMap, "taxon")));
1427
            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1428

    
1429
            config.setSecDetailsActivated(getValue(sectionMap, "taxon.SecEnabled"));
1430
            config.setSecEnabled(getValue(sectionMap, "taxon.SecDetails"));
1431

    
1432
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1433
    //                (getValue(sectionMap, "lsid")));
1434
            config.setLSIDActivated(getValue(sectionMap, "lsid"));
1435

    
1436
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1437
    //                (getValue(sectionMap, "nc")));
1438
            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1439

    
1440
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1441
    //                (getValue(sectionMap, "ap")));
1442
            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1443

    
1444
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1445
    //                (getValue(sectionMap, "rank")));
1446
            config.setRankActivated(getValue(sectionMap, "rank"));
1447

    
1448

    
1449
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1450
    //                (getValue(sectionMap, "atomisedEpithets")));
1451
            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1452

    
1453
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1454
    //                (getValue(sectionMap,"author")));
1455
            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1456

    
1457
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1458
    //                (getValue(sectionMap, "nomRef")));
1459
            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1460

    
1461
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1462
    //                (getValue(sectionMap, "nomStat")));
1463
            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1464

    
1465

    
1466
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1467
    //                (getValue(sectionMap,"protologue")));
1468
            config.setProtologueActivated(getValue(sectionMap,"protologue"));
1469

    
1470
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1471
    //                (getValue(sectionMap,"typeDes")));
1472
            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1473

    
1474
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1475
    //                (getValue(sectionMap,"nameRelation")));
1476
            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1477

    
1478
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1479
    //                (getValue(sectionMap, "hybrid")));
1480
            config.setHybridActivated(getValue(sectionMap,"hybrid"));
1481
        }else{
1482
            config.setSimpleDetailsViewActivated(getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION));
1483
            config.setTaxonSectionActivated(getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_TAXON));
1484
            config.setSecDetailsActivated(getBooleanValue(SHOW_SEC_DETAILS));
1485
            config.setSecEnabled(getBooleanValue(DISABLE_SEC));
1486
            config.setLSIDActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_LSID));
1487
            config.setNomenclaturalCodeActived(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1488
            config.setAppendedPhraseActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1489
            config.setRankActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_RANK));
1490
            config.setAtomisedEpithetsActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1491
            config.setAuthorshipSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1492
            config.setNomenclaturalReferenceSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1493
            config.setNomenclaturalStatusSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1494
            config.setProtologueActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1495
            config.setTypeDesignationSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1496
            config.setNameRelationsSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1497
            config.setHybridActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_HYBRID));
1498
        }
1499
        return config;
1500
    }
1501

    
1502
    /**
1503
     * @param object
1504
     * @param b
1505
     * @return
1506
     */
1507
    public static void setPreferredNameDetailsConfiguration(NameDetailsConfigurator config, boolean local) {
1508
        CdmPreference preference = null;
1509

    
1510
        if (!local) {
1511
            preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), EditorPreferencePredicate.NameDetailsView, config.toString());
1512

    
1513
            setPreferenceToDB(preference);
1514
        }
1515
        //also add to local preferences
1516
        setBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION, config.isSimpleDetailsViewActivated());
1517
        setBooleanValue(SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1518
        setBooleanValue(SHOW_SEC_DETAILS, config.isSecDetailsActivated());
1519
        setBooleanValue(DISABLE_SEC, config.isSecEnabled());
1520
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1521
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, config.isNomenclaturalCodeActived());
1522
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE, config.isAppendedPhraseActivated());
1523
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1524
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, config.isAtomisedEpithetsActivated());
1525
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, config.isAuthorshipSectionActivated());
1526
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, config.isNomenclaturalReferenceSectionActivated());
1527
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, config.isNomenclaturalStatusSectionActivated());
1528
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, config.isProtologueActivated());
1529
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, config.isTypeDesignationSectionActivated());
1530
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, config.isNameRelationsSectionActivated());
1531
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_HYBRID, config.isHybridActivated());
1532

    
1533
    }
1534

    
1535
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1536
		if (sectionMap.containsKey(string)){
1537
			return sectionMap.get(string);
1538
		}else{
1539
			return true;
1540
		}
1541

    
1542
	}
1543

    
1544

    
1545

    
1546
    /**
1547
     * <p>
1548
     * setAbcdConfigurator
1549
     * </p>
1550
     *
1551
     * @param preferredConfiguration
1552
     *            a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1553
     *            object.
1554
     */
1555
    public static Abcd206ImportConfigurator getDBAbcdImportConfigurationPreference() {
1556

    
1557
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1558
        ICdmRepository controller;
1559
        controller = CdmStore.getCurrentApplicationConfiguration();
1560
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.AbcdImportConfig);
1561
        CdmPreference preference = null;
1562
        if (controller == null){
1563
            return null;
1564
        }
1565
        preference = controller.getPreferenceService().find(key);
1566
        if (preference == null){
1567
            return config;
1568
         } else{
1569
             String configString = preference.getValue();
1570
             String[] configArray = configString.split(";");
1571

    
1572
             for (String configItem: configArray){
1573
                 String[] keyValue = configItem.split(":");
1574
                 String keyString = keyValue[0];
1575
                 String valueString = null;
1576
                 if (keyValue.length>1){
1577
                      valueString = keyValue[1];
1578
                 }
1579
                 if (keyString.equals("ignoreImportOfExistingSpecimen")){
1580
                     config.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
1581
                 }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){
1582
                     config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
1583
                 }else if (keyString.equals("reuseExistingTaxaWhenPossible")){
1584
                     config.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
1585
                 }else if (keyString.equals("ignoreAuthorship")){
1586
                     config.setIgnoreAuthorship(Boolean.valueOf(valueString));
1587
                 }else if (keyString.equals("addMediaAsMediaSpecimen")){
1588
                     config.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
1589
                 }else if (keyString.equals("reuseExistingMetaData")){
1590
                     config.setReuseExistingMetaData(Boolean.valueOf(valueString));
1591
                 }else if (keyString.equals("reuseExistingDescriptiveGroups")){
1592
                     config.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
1593
                 }else if (keyString.equals("allowReuseOtherClassifications")){
1594
                     config.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
1595
                 }else if (keyString.equals("deduplicateReferences")){
1596
                     config.setDeduplicateReferences(Boolean.valueOf(valueString));
1597
                 }else if (keyString.equals("deduplicateClassifications")){
1598
                     config.setDeduplicateClassifications(Boolean.valueOf(valueString));
1599
                 }else if (keyString.equals("moveNewTaxaToDefaultClassification")){
1600
                     config.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
1601
                 }else if (keyString.equals("mapUnitIdToCatalogNumber")){
1602
                     config.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
1603
                 }else if (keyString.equals("mapUnitIdToAccessionNumber")){
1604
                     config.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
1605
                 }else if (keyString.equals("mapUnitIdToBarcode")){
1606
                     config.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
1607
                 }else if (keyString.equals("overwriteExistingSpecimens")){
1608
                     config.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
1609
                 }else if (keyString.equals("nomenclaturalCode")){
1610
                         config.setNomenclaturalCode(NomenclaturalCode.fromString(valueString));
1611
                 }else{
1612
                     logger.debug("This key of the abcd configurator needs to be added to the transformer: " + keyString);
1613
                 }
1614

    
1615
             }
1616
        }
1617
        return config;
1618
    }
1619

    
1620
    public static Abcd206ImportConfigurator getLocalAbcdImportConfigurator(){
1621
       Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1622

    
1623
        config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1624

    
1625
        config.setAddMediaAsMediaSpecimen(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1626

    
1627
        config.setAllowReuseOtherClassifications(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1628
        config.setDeduplicateClassifications(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1629
        config.setDeduplicateReferences(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1630
        config.setRemoveCountryFromLocalityText(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REMOVE_COUNTRY_FROM_LOCALITY_TEXT));
1631
        config.setGetSiblings(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1632
        config.setIgnoreAuthorship(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1633
        config.setIgnoreImportOfExistingSpecimen(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1634
        config.setMapUnitIdToAccessionNumber(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1635
        config.setMapUnitIdToBarcode(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1636
        config.setMapUnitIdToCatalogNumber(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1637
        config.setMoveNewTaxaToDefaultClassification(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1638
        config.setOverwriteExistingSpecimens(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1639
        config.setReuseExistingDescriptiveGroups(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1640
        config.setReuseExistingMetaData(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1641
        config.setReuseExistingTaxaWhenPossible(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1642
        config.setNomenclaturalCode(NomenclaturalCode.getByKey(getStringValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE)));
1643

    
1644
        return config;
1645

    
1646
    }
1647

    
1648

    
1649
    public static void updateAbcdImportConfigurationPreference() {
1650
        CdmPreferenceCache cache = CdmPreferenceCache.instance();
1651
        CdmPreference pref = cache.get(EditorPreferencePredicate.AbcdImportConfig.getKey());
1652

    
1653
        if (!getBooleanValue(prefOverrideKey(EditorPreferencePredicate.AbcdImportConfig.getKey())) || !pref.isAllowOverride()){
1654
            resetToDBPreferenceAbcdCOnfigurator();
1655

    
1656
        }
1657
    }
1658

    
1659
    public static void resetToDBPreferenceAbcdCOnfigurator(){
1660
        Abcd206ImportConfigurator config = getDBAbcdImportConfigurationPreference();
1661
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS, config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
1662
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN, config.isAddMediaAsMediaSpecimen());
1663

    
1664
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS, config.isAllowReuseOtherClassifications());
1665
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS, config.isDeduplicateClassifications());
1666
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES, config.isDeduplicateReferences());
1667

    
1668
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS, config.isGetSiblings());
1669
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP, config.isIgnoreAuthorship());
1670
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN, config.isIgnoreImportOfExistingSpecimen());
1671
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER, config.isMapUnitIdToAccessionNumber());
1672
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE, config.isMapUnitIdToBarcode());
1673
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER, config.isMapUnitIdToCatalogNumber());
1674
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION, config.isMoveNewTaxaToDefaultClassification());
1675
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN, config.isOverwriteExistingSpecimens());
1676
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS, config.isReuseExistingDescriptiveGroups());
1677
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA, config.isReuseExistingMetaData());
1678
        setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE, config.isReuseExistingTaxaWhenPossible());
1679
        if (config.getNomenclaturalCode() != null){
1680
            setStringValue(ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE, config.getNomenclaturalCode().getKey());
1681
        }
1682
    }
1683

    
1684

    
1685
    /**
1686
    *
1687
    */
1688
   public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1689
       NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1690
       if (local){
1691
          config.setSimpleDetailsViewActivated(getBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1692
          config.setAppendedPhraseActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1693
          config.setAtomisedEpithetsActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1694
          config.setAuthorshipSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1695
          config.setLSIDActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1696
          config.setNameApprobiationActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1697
          config.setNameCacheActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1698
          config.setNameRelationsSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1699
          config.setNomenclaturalCodeActived(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1700
          config.setNomenclaturalStatusSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1701
          config.setNomenclaturalReferenceSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1702
          config.setProtologueActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1703
          config.setRankActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1704
          config.setTaxonSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1705
          config.setTypeDesignationSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1706
          config.setHybridActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1707
       }else{
1708

    
1709
       }
1710

    
1711
      return config;
1712
   }
1713

    
1714

    
1715
   public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1716
       setBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1717
               config.isSimpleDetailsViewActivated());
1718
       setBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1719
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1720
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1721
               config.isNomenclaturalCodeActived());
1722
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1723
               config.isNameCacheActivated());
1724
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1725
               config.isAppendedPhraseActivated());
1726
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1727
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1728
               config.isAtomisedEpithetsActivated());
1729
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1730
               config.isAuthorCacheActivated());
1731
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1732
               config.isAuthorshipSectionActivated());
1733
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1734
               config.isNomenclaturalReferenceSectionActivated());
1735
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1736
               config.isNomenclaturalStatusSectionActivated());
1737
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1738
               config.isProtologueActivated());
1739
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1740
               config.isTypeDesignationSectionActivated());
1741
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1742
               config.isNameRelationsSectionActivated());
1743
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1744
               config.isHybridActivated());
1745

    
1746
   }
1747

    
1748
/**
1749
 * @return
1750
 */
1751
public static boolean isSortTaxaByRankAndName() {
1752

    
1753
    return getBooleanValue(IPreferenceKeys.SORT_TAXA_BY_RANK_AND_NAME);
1754
}
1755

    
1756
/**
1757
 * @return
1758
 */
1759
public static boolean isSortNamedAreaByOrderInVocabulary() {
1760

    
1761
    return getBooleanValue(EditorPreferencePredicate.AreasSortedByIdInVocabulary.getKey());
1762
}
1763

    
1764
public static void setSortNamedAreasByOrderInVocabulary(boolean isSortByVocabularyOrder) {
1765
    setBooleanValue(EditorPreferencePredicate.AreasSortedByIdInVocabulary.getKey(), isSortByVocabularyOrder);
1766

    
1767
}
1768

    
1769
/**
1770
 * <p>
1771
 * setPreferredNamedAreasForDistributionEditor
1772
 * </p>
1773
 *
1774
 * @param saveCheckedElements
1775
 * @param saveGrayedElements
1776
 */
1777
public static void setLastSelectedReference(
1778
        List<String> lastSelectedReferences) {
1779

    
1780
        setStringValue(PreferencesUtil.LAST_SELECTED_REFERENCES, lastSelectedReferences.toString());
1781
    }
1782

    
1783
/**
1784
 * <p>
1785
 * setPreferredNamedAreasForDistributionEditor
1786
 * </p>
1787
 *
1788
 * @param saveCheckedElements
1789
 * @param saveGrayedElements
1790
 */
1791
public static List<String> getLastSelectedReferences() {
1792

    
1793
        //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1794
        String lastSelected = getStringValue(PreferencesUtil.LAST_SELECTED_REFERENCES);
1795
        List<String> result = new ArrayList<>();
1796
        if (!StringUtils.isBlank(lastSelected)){
1797
            Collections.addAll(result, lastSelected.substring(1,lastSelected.length()-1).split(", "));
1798
        }
1799
        return result;
1800
    }
1801

    
1802

    
1803
/**
1804
 * <p>
1805
 * setPreferredNamedAreasForDistributionEditor
1806
 * </p>
1807
 *
1808
 * @param saveCheckedElements
1809
 * @param saveGrayedElements
1810
 */
1811
public static void setPreferredNamedAreasForDistributionEditor(
1812
        String saveCheckedElements, String saveGrayedElements, boolean local) {
1813
    if (local){
1814
        setStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey(), saveCheckedElements);
1815

    
1816
    }
1817
    else{
1818
        CdmPreference preference = null;
1819

    
1820
        if (saveCheckedElements == null){
1821
            preference = getPreferenceFromDB(EditorPreferencePredicate.AvailableDistributionAreaTerms);
1822

    
1823
            if (preference == null){
1824
                return ;
1825
            } else{
1826
                setStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey(),
1827
                        saveCheckedElements);
1828
                preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1829
                setPreferenceToDB(preference);
1830

    
1831
            }
1832
        } else{
1833
           preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1834
           setPreferenceToDB(preference);
1835
           setStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey(),
1836
                    saveCheckedElements);
1837

    
1838
        }
1839
    }
1840

    
1841
}
1842

    
1843
/**
1844
 * @param saveCheckedElements
1845
 * @param saveCheckedElements2
1846
 * @param b
1847
 */
1848
public static void setPreferredVocabulariesForDistributionEditor(String saveCheckedElements,
1849
        boolean local, boolean isOverride) {
1850
    if (local){
1851
      setStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey(), saveCheckedElements);
1852
      setBooleanValue(prefOverrideKey(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey()), isOverride);
1853
    }
1854
    else{
1855
        ICdmRepository controller;
1856
        CdmPreference preference = null;
1857

    
1858
        if (saveCheckedElements == null){
1859
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1860

    
1861
            if (preference == null){
1862
                return ;
1863
            } else{
1864
                setStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey(),
1865
                        saveCheckedElements);
1866
                preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1867
                preference.setAllowOverride(isOverride);
1868
                setPreferenceToDB(preference);
1869
            }
1870
        } else{
1871
            preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1872
            preference.setAllowOverride(isOverride);
1873
            setPreferenceToDB(preference);
1874
            setStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey(),
1875
                    saveCheckedElements);
1876

    
1877
        }
1878
    }
1879
}
1880

    
1881

    
1882

    
1883

    
1884
/**
1885
 * @param saveCheckedElements
1886
 * @param saveCheckedElements2
1887
 * @param b
1888
 */
1889
public static String getPreferredVocabulariesForDistributionEditor(boolean local) {
1890
    if (local){
1891

    
1892
        String pref = getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey(), local);
1893
        return pref;
1894
    }
1895
    else{
1896
        CdmPreference preference = null;
1897
        preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1898
        if (preference == null){
1899
            return null;
1900
        } else{
1901
            return preference.getValue();
1902
        }
1903

    
1904
    }
1905

    
1906

    
1907

    
1908
}
1909

    
1910
public static boolean getFilterCommonNameReferences(){
1911
    return getBooleanValue(PreferencesUtil.FILTER_COMMON_NAME_REFERENCES);
1912
}
1913

    
1914
/**
1915
 *
1916
 */
1917
public static void updateDBPreferences() {
1918
    CdmPreference preference = null;
1919
    CdmPreferenceCache cache = CdmPreferenceCache.instance();
1920
    cache.getAllTaxEditorDBPreferences();
1921

    
1922
    //ABCD Configurator
1923

    
1924
    updateAbcdImportConfigurationPreference();
1925

    
1926
    //Name Details
1927
    NameDetailsConfigurator config = getPreferredNameDetailsConfiguration(false);
1928
    if (config != null ){
1929
        if (!getBooleanValue(OVERRIDE_NAME_DETAILS) ||  !getBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS)){
1930
            setPreferredNameDetailsConfiguration(config, false);
1931
        }
1932
    }
1933

    
1934

    
1935

    
1936

    
1937

    
1938

    
1939

    
1940

    
1941
}
1942

    
1943

    
1944

    
1945

    
1946

    
1947

    
1948

    
1949

    
1950
}
(27-27/40)