Project

General

Profile

Download (72.9 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
	/**
111
     * <p>
112
     * setStringValue
113
     * </p>
114
     *
115
     *
116
     **/
117
    public static void setStringValue(String name, String value) {
118
        getPreferenceStore().setValue(name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString(), value);
119
    }
120

    
121
    /**
122
     * <p>
123
     * setIntValue
124
     * </p>
125
     *
126
     *
127
     **/
128
    public static void setIntValue(String name, int value) {
129
        getPreferenceStore().setValue(name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString(), value);
130
    }
131

    
132
    /**
133
     * <p>
134
     * setBooleanValue
135
     * </p>
136
     *
137
     *
138
     **/
139
    public static void setBooleanValue(String name, boolean value) {
140
        getPreferenceStore().setValue(name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString(), value);
141
    }
142

    
143
    /**
144
     * <p>
145
     * setBooleanValue
146
     * </p>
147
     *
148
     *
149
     **/
150
    public static void setDoubleValue(String name, double value) {
151
        getPreferenceStore().setValue(name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString(), value);
152
    }
153

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

    
165
    /**
166
     * <p>
167
     * setFloatValue
168
     * </p>
169
     *
170
     *
171
     **/
172
    public static void setLongValue(String name, long value) {
173
        getPreferenceStore().setValue(name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString(), value);
174
    }
175

    
176
    /**
177
     * <p>
178
     * setStringValue
179
     * </p>
180
     *
181
     *
182
     **/
183
    public static String getStringValue(String name) {
184
        return getPreferenceStore().getString(name + "_"+ ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString());
185
    }
186

    
187
    /**
188
     * <p>
189
     * setIntValue
190
     * </p>
191
     *
192
     *
193
     **/
194
    public static int getIntValue(String name) {
195
        return getPreferenceStore().getInt(name + "_"+ ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString());
196
    }
197

    
198
    /**
199
     * <p>
200
     * setBooleanValue
201
     * </p>
202
     *
203
     *
204
     **/
205
    public static boolean getBooleanValue(String name) {
206
        if (CdmStore.isActive()){
207
            String test = name + "_"+((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
208
            return getPreferenceStore().
209
                    getBoolean(name + "_"+((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString());
210
        }else{
211
            return getPreferenceStore().getBoolean(name);
212
        }
213

    
214
    }
215

    
216
    /**
217
     * <p>
218
     * setBooleanValue
219
     * </p>
220
     *
221
     *
222
     **/
223
    public static double getDoubleValue(String name) {
224
        return getPreferenceStore().getDouble(name + "_"+  ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString());
225
    }
226

    
227
    /**
228
     * <p>
229
     * setFloatValue
230
     * </p>
231
     *
232
     *
233
     **/
234
    public static float getFloatValue(String name) {
235
        return getPreferenceStore().getFloat(name + "_"+ ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString());
236
    }
237

    
238
    /**
239
     * <p>
240
     * setFloatValue
241
     * </p>
242
     *
243
     *
244
     **/
245
    public static long getLongValue(String name) {
246
        return getPreferenceStore().getLong(name + "_"+ ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString());
247
    }
248

    
249

    
250
	/**
251
	 * <p>
252
	 * setPreferredNomenclaturalCode
253
	 * </p>
254
	 *
255
	 * @param preferredCode
256
	 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
257
	 *            object.
258
	 */
259
	public static CdmPreference setPreferredNomenclaturalCode(
260
			String preferenceKey, boolean local) {
261
	    if (local){
262
	        setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
263
	                preferenceKey);
264
	    }
265
	    else{
266
    		ICdmRepository controller;
267
    		controller = CdmStore.getCurrentApplicationConfiguration();
268
    		if (controller == null){
269
                return null;
270
            }
271
    		PrefKey key = null;
272
    		try{
273
    		    key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
274
    		}catch (Exception e){
275
    		    System.out.println(e.getStackTrace());
276
    		}
277
    		CdmPreference preference = null;
278

    
279
    		if (preferenceKey == null){
280
    			preference = controller.getPreferenceService().find(key);
281
    			if (preference == null){
282
    				return null;
283
    			} else{
284
    			    setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
285
    				        preference.getValue());
286
    			    setBooleanValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,preference.isAllowOverride());
287
    				return preference;
288
    			}
289
    		} else{
290
    			preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferenceKey);
291
    			controller.getPreferenceService().set(preference);
292

    
293
    		}
294
	    }
295
        return null;
296

    
297

    
298

    
299
	}
300

    
301
	public static NomenclaturalCode getPreferredNomenclaturalCode(){
302
	    if (getBooleanValue(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
303
            return getPreferredNomenclaturalCode(true);
304
        } else{
305
            return getPreferredNomenclaturalCode(false);
306
        }
307
	}
308

    
309
	/**
310
	 * <p>
311
	 * getPreferredNomenclaturalCode
312
	 * </p>
313
	 *
314
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
315
	 */
316
	public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
317

    
318
		CdmPreference pref = null;
319
		if (!useLocal){
320
		    pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
321
		}
322

    
323
	    String preferredCode;
324
	    if(pref == null){
325
	    	preferredCode = getStringValue(
326
					PREFERRED_NOMENCLATURAL_CODE_KEY);
327

    
328
	    }else{
329
	    	preferredCode = pref.getValue();
330
	    }
331

    
332
	    return getPreferredNomenclaturalCode(preferredCode);
333

    
334
	}
335

    
336
	/**
337
     * <p>
338
     * getPreferredNomenclaturalCode
339
     * </p>
340
     *
341
     * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
342
     */
343
    public static NomenclaturalCode getPreferredNomenclaturalCode(String preferenceKeyNomenclaturalCode) {
344

    
345
        for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
346
            if (getPreferenceKey(code).equals(preferenceKeyNomenclaturalCode)) {
347
                return code;
348
            }
349
        }
350
        return null;
351
    }
352

    
353
	public static boolean isShowTaxonAssociations(){
354
        if (getBooleanValue(SHOW_TAXON_ASSOCIATIONS_OVERRIDE) && getBooleanValue(SHOW_TAXON_ASSOCIATIONS_ALLOW_OVERRIDE)) {
355
            return getBooleanValue(SHOW_TAXON_ASSOCIATIONS);
356
        } else{
357
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
358

    
359
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
360
        }
361
    }
362

    
363
	public static boolean isShowLifeForm(){
364
        if (getBooleanValue(SHOW_LIFE_FORM_OVERRIDE) && getBooleanValue(SHOW_LIFE_FORM_ALLOW_OVERRIDE)) {
365
            return getBooleanValue(SHOW_LIFE_FORM);
366
        } else{
367
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
368

    
369
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
370
        }
371
    }
372

    
373
	public static boolean isDeterminationOnlyForFieldUnits(){
374
        if (getBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS_OVERRIDE) && getBooleanValue(DETERMINATIONS_ONLY_FOR_FIELDUNITS_ALLOW_OVERRIDE)) {
375
            return getBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS);
376
        } else{
377
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
378

    
379
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
380
        }
381
    }
382

    
383
	public static boolean isCollectingAreaInGeneralSection(){
384
        if (getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION_OVERRIDE) && getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION_ALLOW_OVERRIDE)) {
385
            return getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION);
386
        } else{
387
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
388

    
389
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
390
        }
391
    }
392

    
393
	public static boolean isShowSimpleDetailsView(){
394
        if (getBooleanValue(OVERRIDE_NAME_DETAILS) && getBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS)) {
395
            return getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION);
396
        } else{
397
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
398

    
399
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
400
        }
401
    }
402

    
403

    
404

    
405
    public static CdmPreference getPreferenceFromDB(PreferencePredicate predicate){
406
        ICdmRepository controller;
407
        CdmPreference pref = null;
408

    
409
        try{
410
            if(CdmStore.isActive()){
411
                controller = CdmStore.getCurrentApplicationConfiguration();
412
                PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), predicate);
413
                pref = controller.getPreferenceService().find(key);
414
            }
415
        }catch(Exception e){
416
            e.printStackTrace();
417
        }
418

    
419
        return pref;
420

    
421
    }
422

    
423
    public static void setPreferenceToDB(CdmPreference preference){
424
        ICdmRepository controller;
425
        try{
426
            if(CdmStore.isActive()){
427
                controller = CdmStore.getCurrentApplicationConfiguration();
428
                controller.getPreferenceService().set(preference);
429
            }
430
        }catch(Exception e){
431
            e.printStackTrace();
432
        }
433

    
434
    }
435

    
436

    
437

    
438
	public static String getPreferredDefaultLangugae(){
439
	    String preferredLanguage = getStringValue(DEFAULT_LANGUAGE_EDITOR);
440
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
441
	        return preferredLanguage;
442
	    }
443
	    return null;
444
	}
445

    
446
	public static boolean isShowMediaPreview(){
447
        boolean isShowMediaPreview = getBooleanValue(SHOW_MEDIA_PREVIEW);
448
        return isShowMediaPreview;
449
    }
450

    
451
	/**
452
	 * Get the match strategy for the given class that was stored in preferences
453
	 * or the default strategy if it was not stored in preferences
454
	 *
455
	 * @param clazz
456
	 *            a {@link java.lang.Class} object.
457
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
458
	 */
459
	public static IMatchStrategy getMatchStrategy(Class clazz) {
460
		String className = clazz.getName();
461
		if (getBooleanValue(MATCH_STRATEGY_PREFIX + className)) {
462
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
463

    
464
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
465
				String matchModeName = getStringValue(
466
						getMatchStrategyFieldName(className, fieldName));
467
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
468
				try {
469
					matchStrategy.setMatchMode(fieldName, matchMode);
470
				} catch (MatchException e) {
471
					MessagingUtils.error(PreferencesUtil.class, e);
472
					throw new RuntimeException(e);
473
				}
474
			}
475

    
476
			return matchStrategy;
477
		}
478
		return getDefaultMatchStrategy(clazz);
479
	}
480

    
481
	/**
482
	 * Stores a matchStrategy into the preference store.
483
	 *
484
	 * @param matchStrategy
485
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
486
	 *            object.
487
	 */
488
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
489
		String className = matchStrategy.getMatchClass().getName();
490
		setBooleanValue(MATCH_STRATEGY_PREFIX + className, true);
491

    
492
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
493

    
494
		for (String fieldName : matchFields) {
495
			setStringValue(
496
					getMatchStrategyFieldName(className, fieldName),
497
					matchStrategy.getMatchMode(fieldName).name());
498
		}
499
	}
500

    
501
	/**
502
	 * Helper method to create the preference property for a match field.
503
	 *
504
	 * @param className
505
	 * @param fieldName
506
	 * @return
507
	 */
508
	private static String getMatchStrategyFieldName(String className,
509
			String fieldName) {
510
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
511
	}
512

    
513
	/**
514
	 * Returns the default match strategy for a given class.
515
	 *
516
	 * @param clazz
517
	 *            a {@link java.lang.Class} object.
518
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
519
	 */
520
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
521
		return DefaultMatchStrategy.NewInstance(clazz);
522
	}
523

    
524
	/**
525
	 * <p>
526
	 * getDateFormatPattern
527
	 * </p>
528
	 *
529
	 * @return a {@link java.lang.String} object.
530
	 */
531
	public static String getDateFormatPattern() {
532
		// TODO make this configurable in properties
533
		String pattern = "Y-M-d H:m";
534
		return pattern;
535
	}
536

    
537
	/**
538
	 * <p>
539
	 * addTermToPreferredTerms
540
	 * </p>
541
	 *
542
	 * @param term
543
	 *            a T object.
544
	 * @param <T>
545
	 *            a T object.
546
	 */
547
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
548

    
549
		// VocabularyEnum vocabulary =
550
		// VocabularyEnum.getVocabularyEnum(term.getClass());
551
		//
552
		// getPreferenceStore().setValue(getPreferenceKey(term),
553
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
554
		//
555
		// firePreferencesChanged(term.getClass());
556
	}
557

    
558
	/**
559
	 * Construct a unique key using the CdmBase object's uuid
560
	 *
561
	 * @param cdmBase
562
	 * @return
563
	 */
564
	private static String getPreferenceKey(ICdmBase cdmBase) {
565
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
566

    
567
		String key = cdmBase.getClass().getName().concat(".")
568
				.concat(cdmBase.getUuid().toString());
569
		if (key.contains("javassist")) {
570
			MessagingUtils.info("proxy");
571
		}
572
		return key;
573
	}
574

    
575
	/**
576
	 * Construct a unique key using the CdmBase object's uuid
577
	 *
578
	 * @param cdmBase
579
	 * @return
580
	 */
581
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
582
		simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
583
		String key = simpleTerm.getClass().getName().concat(".")
584
				.concat(simpleTerm.getUuid().toString());
585
		if (key.contains("javassist")) {
586
			MessagingUtils.warn(PreferencesUtil.class,
587
					"Trying to persist a preference based on a proxy class.");
588
		}
589
		return key;
590
	}
591

    
592

    
593

    
594
	/**
595
	 * Construct a unique key using the CdmBase object's uuid
596
	 *
597
	 * @param cdmBase
598
	 * @return
599
	 */
600
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
601
		definedTerm = HibernateProxyHelper.deproxy(definedTerm);
602
		String key = definedTerm.getClass().getName().concat(".")
603
				.concat(definedTerm.getUuid().toString());
604
		if (key.contains("javassist")) {
605
			MessagingUtils.warn(PreferencesUtil.class,
606
					"Trying to persist a preference based on a proxy class.");
607
		}
608
		return key;
609
	}
610

    
611
	/**
612
	 * Retrieves search preferences from the preference store
613
	 *
614
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
615
	 */
616
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
617
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
618

    
619
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
620
				TAXON_SERVICE_CONFIGURATOR_TAXA));
621
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
622
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
623
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
624
				TAXON_SERVICE_CONFIGURATOR_NAMES));
625
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
626
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
627
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getStringValue(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
628

    
629
		return configurator;
630
	}
631

    
632
	/**
633
	 * create new preferences, setting all search options to true
634
	 *
635
	 * @return a
636
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
637
	 *         object.
638
	 */
639
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
640
		IFindTaxaAndNamesConfigurator configurator = FindTaxaAndNamesConfiguratorImpl.NewInstance();
641

    
642
		configurator.setDoTaxa(true);
643
		configurator.setDoSynonyms(true);
644
		configurator.setDoNamesWithoutTaxa(true);
645
		configurator.setDoTaxaByCommonNames(true);
646

    
647
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
648
				"name", "name.$", "relationsFromThisTaxon.$"));
649

    
650
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
651
				"name", "name.$", "synonyms.relatedTo.*"));
652

    
653
		// DEFAULT VALUES
654
		// match mode is a simple like, actually all other match modes are kind
655
		// of bogus
656
		configurator
657
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
658
		// we set page number and size here as this should always be unlimited
659
		configurator.setPageNumber(0);
660
		// TODO currently limit results to 10000
661
		configurator.setPageSize(10000);
662

    
663
		return configurator;
664
	}
665

    
666
	/**
667
	 * Store search preferences
668
	 *
669
	 * @param configurator
670
	 *            a
671
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
672
	 *            object.
673
	 */
674
	public static void setSearchConfigurator(
675
			IFindTaxaAndNamesConfigurator configurator) {
676
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
677
				configurator.isDoTaxa());
678
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
679
				configurator.isDoSynonyms());
680
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
681
				configurator.isDoNamesWithoutTaxa());
682
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
683
				configurator.isDoTaxaByCommonNames());
684
	}
685

    
686
	/**
687
	 * <p>
688
	 * firePreferencesChanged
689
	 * </p>
690
	 *
691
	 * @param clazz
692
	 *            a {@link java.lang.Class} object.
693
	 */
694
	public static void firePreferencesChanged(Class clazz) {
695
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
696
				null, clazz);
697
	}
698

    
699
	public static String createPreferenceString(String property){
700
	   return property +"_"+((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
701

    
702
	}
703

    
704
	/**
705
	 * Set default values for preferences
706
	 */
707
	public static void setDefaults() {
708
		getPreferenceStore().setDefault(createPreferenceString(TAXON_SERVICE_CONFIGURATOR_TAXA), true);
709
		getPreferenceStore().setDefault(createPreferenceString(TAXON_SERVICE_CONFIGURATOR_SYNONYMS),
710
				true);
711
		getPreferenceStore().setDefault(createPreferenceString(EDIT_MAP_SERVICE_ACCES_POINT),
712
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
713
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
714
		getPreferenceStore().setDefault(createPreferenceString(SHOULD_CONNECT_AT_STARTUP), false);
715
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_ACCESS_POINT),
716
				"http://www.biodiversitylibrary.org/openurl");
717
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_IMAGE_MAX_WIDTH), "1000");
718
		getPreferenceStore().setDefault(createPreferenceString(OPENURL_IMAGE_MAX_HEIGHT), "1000");
719
		//Distribution Editor:
720
		getPreferenceStore().setDefault(createPreferenceString(DISTRIBUTION_AREA_PREFRENCES_ACTIVE), true);
721
		getPreferenceStore().setDefault(createPreferenceString(CHECKLIST_AREA_DISPLAY), CHECKLIST_AREA_DISPLAY_TITLE);
722
		getPreferenceStore().setDefault(createPreferenceString(CHECKLIST_SYMBOL), false);
723

    
724

    
725
		//Name Details
726
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS), true);
727
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE), true);
728
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP), true);
729
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_HYBRID), true);
730
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_LSID), true);
731
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP), true);
732
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NAMECACHE), true);
733
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE), true);
734
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE), true);
735
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS), true);
736
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE), true);
737
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_RANK), true);
738
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION), true);
739
		//Navigator preferences
740
		getPreferenceStore().setDefault(createPreferenceString(SORT_NODES_NATURALLY), false);
741
		getPreferenceStore().setDefault(createPreferenceString(SORT_NODES_ALPHABETICALLY), false);
742
		getPreferenceStore().setDefault(createPreferenceString(SORT_TAXA_BY_RANK_AND_NAME), true);
743
		//override db preferences
744
		getPreferenceStore().setDefault(createPreferenceString(ABCD_IMPORT_OVERRIDE), false);
745
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SPECIMEN_OVERRIDE), false);
746
		getPreferenceStore().setDefault(createPreferenceString(OVERRIDE_NAME_DETAILS), false);
747
		getPreferenceStore().setDefault(createPreferenceString(DISTRIBUTION_AREA_PREFRENCES_ACTIVE_OVERRIDE), false);
748

    
749
		getPreferenceStore().setDefault(createPreferenceString(FILTER_COMMON_NAME_REFERENCES), false);
750
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SPECIMEN), true);
751
		getPreferenceStore().setDefault(createPreferenceString(SHOW_TAXONNODE_WIZARD), true);
752
		getPreferenceStore().setDefault(createPreferenceString(SHOW_NAME_IN_SOURCE), true);
753
		getPreferenceStore().setDefault(createPreferenceString(SHOW_ID_IN_SOURCE), true);
754
		getPreferenceStore().setDefault(createPreferenceString(DISABLE_MULTI_CLASSIFICATION), false);
755
		getPreferenceStore().setDefault(createPreferenceString(DISABLE_SEC), false);
756
		getPreferenceStore().setDefault(createPreferenceString(SHOW_SEC_DETAILS), true);
757
	}
758

    
759
	/**
760
	 * <p>
761
	 * checkNomenclaturalCode
762
	 * </p>
763
	 */
764
	public static void checkNomenclaturalCode() {
765
		// First time Editor is opened, no nomenclatural code has been set
766
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
767
			PreferencesUtil.setPreferredNomenclaturalCode(getPreferenceKey(NomenclaturalCode.ICNAFP), true);
768
		}
769

    
770

    
771

    
772
	}
773
	public static void setNomenclaturalCodePreferences(){
774
	    ICdmRepository controller;
775
        controller = CdmStore.getCurrentApplicationConfiguration();
776
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
777
        CdmPreference preference = null;
778
        if (controller == null){
779
            return ;
780
        }
781
        preference = controller.getPreferenceService().find(key);
782
        if (preference == null){
783
            return;
784
        }
785
        setBooleanValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
786

    
787
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
788
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
789
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
790

    
791
        setStringValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
792
                getPreferenceKey(preferredCode));
793

    
794
	}
795

    
796
	public static void checkDefaultLanguage(){
797
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
798
	       Shell shell = AbstractUtility.getShell();
799
	       int open = new DefaultLanguageDialog(shell).open();
800
	       if(open == Window.OK){
801
	           PlatformUI.getWorkbench().restart();
802
	       }
803
	    }else{
804
	        //TODO:In case of a reinstall, the config.ini will be overwritten
805
	        //     here you create config.ini with the stored key from preferences
806
	    }
807
	}
808

    
809
	/**
810
	 * <p>
811
	 * getMapServiceAccessPoint
812
	 * </p>
813
	 *
814
	 * @return a {@link java.lang.String} object.
815
	 */
816
	public static String getMapServiceAccessPoint() {
817
		return getStringValue(EDIT_MAP_SERVICE_ACCES_POINT);
818
	}
819

    
820
	/**
821
	 * <p>
822
	 * shouldConnectAtStartUp
823
	 * </p>
824
	 *
825
	 * @return a boolean.
826
	 */
827
	public static boolean shouldConnectAtStartUp() {
828
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
829
		//return getBooleanValue(SHOULD_CONNECT_AT_STARTUP);
830
		return false;
831
	}
832

    
833
	/**
834
	 * <p>
835
	 * getDefaultFeatureTreeForTextualDescription
836
	 * </p>
837
	 *
838
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
839
	 */
840
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
841
		String uuidString = getStringValue(
842
				FEATURE_TREE_DEFAULT_TEXT);
843
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
844
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
845
	}
846

    
847
	/**
848
	 * <p>
849
	 * getDefaultFeatureTreeForStructuredDescription
850
	 * </p>
851
	 *
852
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
853
	 */
854
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
855
		String uuidString = getStringValue(
856
				FEATURE_TREE_DEFAULT_STRUCTURE);
857
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
858
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
859
	}
860

    
861
	/**
862
	 * <p>
863
	 * setSortRanksHierarchichally
864
	 * </p>
865
	 *
866
	 * @param selection
867
	 *            a boolean.
868
	 */
869
	public static void setSortRanksHierarchichally(boolean selection) {
870
		setBooleanValue(SORT_RANKS_HIERARCHICHALLY, selection);
871
	}
872

    
873
	/**
874
	 * <p>
875
	 * getSortRanksHierarchichally
876
	 * </p>
877
	 *
878
	 * @return a boolean.
879
	 */
880
	public static boolean getSortRanksHierarchichally() {
881
		return getBooleanValue(SORT_RANKS_HIERARCHICHALLY);
882
	}
883

    
884
	public static boolean isMultilanguageTextEditingCapability() {
885
		return getBooleanValue(
886
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
887
	}
888

    
889
	public static Language getGlobalLanguage() {
890

    
891

    
892
		String languageUuidString = getStringValue(
893
				GLOBAL_LANGUAGE_UUID);
894

    
895
		if(!CdmStore.isActive()) {
896
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
897
            return null;
898
        }
899

    
900
		if (CdmUtils.isBlank(languageUuidString)) {
901
			return Language.getDefaultLanguage();
902
		}
903

    
904
		UUID languageUuid = UUID.fromString(languageUuidString);
905
		return (Language) CdmStore.getService(ITermService.class).load(
906
				languageUuid);
907
	}
908

    
909
	public static void setGlobalLanguage(Language language) {
910
	    if(language != null) {
911
	        setStringValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
912
	        CdmStore.setDefaultLanguage(language);
913
	    }
914

    
915
	}
916

    
917
	/**
918
	 * @return
919
	 */
920
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
921
		List<MarkerType> markerTypes = CdmStore.getTermManager()
922
				.getPreferredTerms(MarkerType.class);
923

    
924
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
925

    
926
		for (MarkerType markerType : markerTypes) {
927
			String name = getMarkerTypeEditingPreferenceKey(markerType);
928
			Boolean value = getBooleanValue(name);
929

    
930
			result.put(markerType, value);
931
		}
932

    
933
		return result;
934
	}
935

    
936
	/**
937
	 * @param markerTypeEditingMap
938
	 */
939
	public static void setEditMarkerTypePreferences(
940
			Map<MarkerType, Boolean> markerTypeEditingMap) {
941
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
942
			String name = getMarkerTypeEditingPreferenceKey(markerType);
943
			setBooleanValue(name,
944
					markerTypeEditingMap.get(markerType));
945
		}
946

    
947
	}
948

    
949
	private static String getMarkerTypeEditingPreferenceKey(
950
			MarkerType markerType) {
951
		markerType = HibernateProxyHelper.deproxy(markerType);
952
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
953
	}
954

    
955
	/**
956
	 * <p>
957
	 * setEditMarkerTypePreference
958
	 * </p>
959
	 *
960
	 * @param input
961
	 *            a {@link org.eclipse.ui.IEditorInput} object.
962
	 * @param markerType
963
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
964
	 * @param edit
965
	 *            a boolean.
966
	 */
967
	public static void setEditMarkerTypePreference(MarkerType markerType,
968
			boolean edit) {
969
		setBooleanValue(
970
				getMarkerTypeEditingPreferenceKey(markerType), edit);
971
	}
972

    
973
	/**
974
	 * @return
975
	 */
976
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
977
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
978
				.NewInstance();
979
		configurator.setMoveDerivedUnitMediaToGallery(true);
980
		configurator.setMoveFieldObjectMediaToGallery(true);
981
		return configurator;
982
	}
983

    
984
	/**
985
	 * This method will write language properties to the config.ini located in the configuration folder
986
	 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
987
	 *
988
	 * @param setLanguage 0 is for german and 1 for english.
989
	 * @throws IOException
990
	 */
991
    public void writePropertyToConfigFile(int setLanguage) throws IOException {
992
        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
993
        //give warning to user if the directory has no write access
994
        if(file == null){
995
            throw new IOException();
996
        }
997
        Properties properties = load(file.getAbsolutePath()+"/config.ini");
998
        switch(setLanguage){
999
        case 0:
1000
            properties.setProperty("osgi.nl", "de");
1001
            setStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
1002
            break;
1003
        case 1:
1004
            properties.setProperty("osgi.nl", "en");
1005
            setStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
1006
            break;
1007
        default:
1008
            break;
1009
        }
1010
        save(file+"/config.ini", properties);
1011
    }
1012

    
1013
    /**
1014
     * This method loads a property from a given file and returns it.
1015
     *
1016
     * @param filename
1017
     * @return
1018
     * @throws IOException
1019
     */
1020
    private Properties load(String filename) throws IOException {
1021
        FileInputStream in = new FileInputStream(filename);
1022
        Properties prop = new Properties();
1023
        prop.load(in);
1024
        in.close();
1025
        return prop;
1026
    }
1027

    
1028
    /**
1029
     * This method saves a property to the specified file.
1030
     *
1031
     * @param filename
1032
     * @param properties
1033
     * @throws IOException
1034
     */
1035
    private void save(String filename, Properties properties) throws IOException{
1036
        FileOutputStream fos =  new FileOutputStream(filename);
1037
        properties.store(fos, "");
1038
        fos.close();
1039
    }
1040

    
1041
    /**
1042
     * Saves a list of P2 Metadata Repositories as string with specified delimiters
1043
     *
1044
     * @param p2Repos
1045
     */
1046
    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
1047
        StringBuilder sb = new StringBuilder();
1048
        for(MetadataRepositoryElement p2Repo : p2Repos) {
1049
            sb.append(P2_REPOSITORIES_DELIM);
1050
            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
1051
                sb.append("-");
1052
            } else {
1053
                sb.append(p2Repo.getName());
1054
            }
1055
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
1056
            sb.append(p2Repo.getLocation().toString());
1057
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
1058
            sb.append(String.valueOf(p2Repo.isEnabled()));
1059
        }
1060
        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
1061
    }
1062

    
1063

    
1064
    /**
1065
     * Retrieves a list of previously saved P2 repositories
1066
     *
1067
     * @return
1068
     */
1069
    public static List<MetadataRepositoryElement> getP2Repositories() {
1070
        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
1071
        String p2ReposPref =  getStringValue(P2_REPOSITORY_LIST);
1072
        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
1073
            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
1074

    
1075
            while(p2ReposPrefST.hasMoreTokens()) {
1076
                String p2RepoStr = p2ReposPrefST.nextToken();
1077
                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
1078
                if(p2ReposStrST.countTokens()==3) {
1079
                    String nickname = p2ReposStrST.nextToken();
1080
                    URI uri = null;
1081
                    try {
1082
                        uri = new URI(p2ReposStrST.nextToken());
1083
                    } catch (URISyntaxException e) {
1084
                        continue;
1085
                    }
1086
                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
1087
                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
1088
                    mre.setNickname(nickname);
1089
                    mre.setEnabled(enabled);
1090
                    p2Repos.add(mre);
1091
                }
1092
            }
1093
        }
1094

    
1095
        return p2Repos;
1096
    }
1097

    
1098
    /**
1099
     * enables/disables nested composite. <br>
1100
     *
1101
     * @param ctrl - Composite to be en-/disabeld
1102
     * @param enabled - boolean
1103
     */
1104
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
1105
        if (ctrl instanceof Composite) {
1106
            Composite comp = (Composite) ctrl;
1107
            for (Control c : comp.getChildren()) {
1108
                recursiveSetEnabled(c, enabled);
1109
            }
1110
        } else {
1111
            ctrl.setEnabled(enabled);
1112
        }
1113
    }
1114

    
1115
    /**
1116
	 * <p>
1117
	 * getSortRanksNaturally
1118
	 * </p>
1119
    	 *
1120
    	 * @return a boolean.
1121
	 */
1122
	public static boolean getSortNodesNaturally() {
1123
		return getBooleanValue(SORT_NODES_NATURALLY);
1124
	}
1125

    
1126
	/**
1127
	 * <p>
1128
	 * setSortRanksNaturally
1129
	 * </p>
1130
	 *
1131
	 * @param selection
1132
	 *            a boolean.
1133
	 */
1134
	public static void setSortNodesNaturally(boolean selection) {
1135
		setBooleanValue(SORT_NODES_NATURALLY, selection);
1136
	}
1137

    
1138

    
1139
	/**
1140
	 * <p>
1141
	 * getSortRanksNaturally
1142
	 * </p>
1143
	 *
1144
	 * @return a boolean.
1145
	 */
1146
	public static boolean getSortNodesStrictlyAlphabetically() {
1147
		return getBooleanValue(SORT_NODES_ALPHABETICALLY);
1148
	}
1149

    
1150
	/**
1151
	 * <p>
1152
	 * setSortRanksNaturally
1153
	 * </p>
1154
	 *
1155
	 * @param selection
1156
	 *            a boolean.
1157
	 */
1158
	public static void setSortNodesStrictlyAlphabetically(boolean selection) {
1159
		setBooleanValue(SORT_NODES_ALPHABETICALLY, selection);
1160
	}
1161

    
1162
	/**
1163
	 * <p>
1164
	 * setStoreNavigatorState
1165
	 * </p>
1166
	 *
1167
	 * @param selection
1168
	 *            a boolean.
1169
	 */
1170
	public static boolean isStoreNavigatorState() {
1171
		return getBooleanValue(RESTORE_NAVIGATOR_STATE);
1172

    
1173
	}
1174

    
1175
	/**
1176
	 * <p>
1177
	 * setStoreNavigatorState
1178
	 * </p>
1179
	 *
1180
	 * @param selection
1181
	 *            a boolean.
1182
	 */
1183
	public static void setStoreNavigatorState(boolean selection) {
1184
		setBooleanValue(RESTORE_NAVIGATOR_STATE, selection);
1185

    
1186
	}
1187

    
1188
    /**
1189
     * @return
1190
     */
1191
    public static boolean isShowUpWidgetIsDisposedMessages() {
1192
       return getBooleanValue(IS_SHOW_UP_WIDGET_IS_DISPOSED);
1193
    }
1194
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
1195
        setBooleanValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
1196
    }
1197

    
1198
    /**
1199
     * @return
1200
     */
1201
    public static boolean isShowIdInVocabularyInChecklistEditor() {
1202
        String area_display = getStringValue(IPreferenceKeys.CHECKLIST_AREA_DISPLAY);
1203
        if (area_display.equals(CHECKLIST_AREA_DISPLAY_ID_IN_VOCABULARY)) {
1204
            return true;
1205
        }else{
1206
            return false;
1207
        }
1208
    }
1209
    public static boolean isShowSymbol1InChecklistEditor() {
1210
        String area_display = getStringValue(IPreferenceKeys.CHECKLIST_AREA_DISPLAY);
1211
        if (area_display.equals(CHECKLIST_AREA_DISPLAY_SYMBOL1)) {
1212
            return true;
1213
        }else{
1214
            return false;
1215
        }
1216
     }
1217
    public static boolean isShowSymbol2InChecklistEditor() {
1218
        String area_display = getStringValue(IPreferenceKeys.CHECKLIST_AREA_DISPLAY);
1219
        if (area_display.equals(CHECKLIST_AREA_DISPLAY_SYMBOL2)) {
1220
            return true;
1221
        }else{
1222
            return false;
1223
        }
1224
     }
1225
    public static void setShowIdInVocabularyInChecklistEditor(String selection) {
1226
        setStringValue(CHECKLIST_AREA_DISPLAY, selection);
1227
    }
1228

    
1229
    /**
1230
     * @return
1231
     */
1232
    public static boolean isShowSymbolInChecklistEditor() {
1233
       return getBooleanValue(IPreferenceKeys.CHECKLIST_SYMBOL);
1234
    }
1235
    public static void setShowSymbolInChecklistEditor(boolean selection) {
1236
        setBooleanValue(CHECKLIST_SYMBOL, selection);
1237
    }
1238

    
1239
    /**
1240
     * @return
1241
     */
1242
    public static boolean isShowRankInChecklistEditor() {
1243
        return getBooleanValue(IPreferenceKeys.CHECKLIST_SHOW_RANK);
1244
    }
1245
    public static void setShowRankInChecklistEditor(boolean selection) {
1246
       setBooleanValue(CHECKLIST_SHOW_RANK, selection);
1247
    }
1248

    
1249
    /**
1250
     * @param object
1251
     * @param b
1252
     * @return
1253
     */
1254
    public static NameDetailsConfigurator getPreferredNameDetailsConfiguration( boolean local) {
1255
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1256

    
1257
        CdmPreference preference = null;
1258

    
1259
        if (!local) {
1260
            preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1261
            if (preference == null){
1262
                return null;
1263
            }
1264

    
1265
            setBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1266

    
1267
            //the preference value is build like this:
1268
            //<section1>:true;<section2>:false....
1269

    
1270
            String value = preference.getValue();
1271
            String [] sections = value.split(";");
1272
            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1273
            String[] sectionValues;
1274
            for (String sectionValue: sections){
1275
                sectionValues = sectionValue.split(":");
1276
                sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1277
            }
1278
            config.setAllowOverride(preference.isAllowOverride());
1279
            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1280

    
1281
    //        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1282
    //                (getValue(sectionMap, "taxon")));
1283
            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1284

    
1285
            config.setSecDetailsActivated(getValue(sectionMap, "taxon.SecEnabled"));
1286
            config.setSecEnabled(getValue(sectionMap, "taxon.SecDetails"));
1287

    
1288
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1289
    //                (getValue(sectionMap, "lsid")));
1290
            config.setLSIDActivated(getValue(sectionMap, "lsid"));
1291

    
1292
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1293
    //                (getValue(sectionMap, "nc")));
1294
            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1295

    
1296
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1297
    //                (getValue(sectionMap, "ap")));
1298
            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1299

    
1300
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1301
    //                (getValue(sectionMap, "rank")));
1302
            config.setRankActivated(getValue(sectionMap, "rank"));
1303

    
1304

    
1305
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1306
    //                (getValue(sectionMap, "atomisedEpithets")));
1307
            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1308

    
1309
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1310
    //                (getValue(sectionMap,"author")));
1311
            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1312

    
1313
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1314
    //                (getValue(sectionMap, "nomRef")));
1315
            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1316

    
1317
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1318
    //                (getValue(sectionMap, "nomStat")));
1319
            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1320

    
1321

    
1322
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1323
    //                (getValue(sectionMap,"protologue")));
1324
            config.setProtologueActivated(getValue(sectionMap,"protologue"));
1325

    
1326
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1327
    //                (getValue(sectionMap,"typeDes")));
1328
            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1329

    
1330
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1331
    //                (getValue(sectionMap,"nameRelation")));
1332
            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1333

    
1334
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1335
    //                (getValue(sectionMap, "hybrid")));
1336
            config.setHybridActivated(getValue(sectionMap,"hybrid"));
1337
        }else{
1338
            config.setSimpleDetailsViewActivated(getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION));
1339
            config.setTaxonSectionActivated(getBooleanValue(SHOW_SIMPLE_NAME_DETAILS_TAXON));
1340
            config.setSecDetailsActivated(getBooleanValue(SHOW_SEC_DETAILS));
1341
            config.setSecEnabled(getBooleanValue(DISABLE_SEC));
1342
            config.setLSIDActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_LSID));
1343
            config.setNomenclaturalCodeActived(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1344
            config.setAppendedPhraseActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1345
            config.setRankActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_RANK));
1346
            config.setAtomisedEpithetsActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1347
            config.setAuthorshipSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1348
            config.setNomenclaturalReferenceSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1349
            config.setNomenclaturalStatusSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1350
            config.setProtologueActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1351
            config.setTypeDesignationSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1352
            config.setNameRelationsSectionActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1353
            config.setHybridActivated(getBooleanValue(SHOW_NAME_DETAILS_SECTION_HYBRID));
1354
        }
1355
        return config;
1356
    }
1357

    
1358
    /**
1359
     * @param object
1360
     * @param b
1361
     * @return
1362
     */
1363
    public static void setPreferredNameDetailsConfiguration(NameDetailsConfigurator config, boolean local) {
1364
        CdmPreference preference = null;
1365

    
1366
        if (!local) {
1367
            preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NameDetailsView, config.toString());
1368

    
1369
            setPreferenceToDB(preference);
1370
        }
1371
        //also add to local preferences
1372
        setBooleanValue(SHOW_SIMPLE_NAME_DETAILS_SECTION, config.isSimpleDetailsViewActivated());
1373
        setBooleanValue(SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1374
        setBooleanValue(SHOW_SEC_DETAILS, config.isSecDetailsActivated());
1375
        setBooleanValue(DISABLE_SEC, config.isSecEnabled());
1376
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1377
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, config.isNomenclaturalCodeActived());
1378
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE, config.isAppendedPhraseActivated());
1379
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1380
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, config.isAtomisedEpithetsActivated());
1381
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, config.isAuthorshipSectionActivated());
1382
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, config.isNomenclaturalReferenceSectionActivated());
1383
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, config.isNomenclaturalStatusSectionActivated());
1384
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, config.isProtologueActivated());
1385
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, config.isTypeDesignationSectionActivated());
1386
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, config.isNameRelationsSectionActivated());
1387
        setBooleanValue(SHOW_NAME_DETAILS_SECTION_HYBRID, config.isHybridActivated());
1388

    
1389
    }
1390

    
1391
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1392
		if (sectionMap.containsKey(string)){
1393
			return sectionMap.get(string);
1394
		}else{
1395
			return true;
1396
		}
1397

    
1398
	}
1399

    
1400

    
1401

    
1402
    /**
1403
     * <p>
1404
     * setAbcdConfigurator
1405
     * </p>
1406
     *
1407
     * @param preferredConfiguration
1408
     *            a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1409
     *            object.
1410
     */
1411
    public static Abcd206ImportConfigurator getAbcdImportConfigurationPreference() {
1412

    
1413
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1414
        ICdmRepository controller;
1415
        controller = CdmStore.getCurrentApplicationConfiguration();
1416
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
1417
        CdmPreference preference = null;
1418
        if (controller == null){
1419
            return null;
1420
        }
1421
        preference = controller.getPreferenceService().find(key);
1422
        boolean allowOverride = true;
1423
        if (preference != null && !preference.isAllowOverride()){
1424
            allowOverride = false;
1425
        }
1426
        if (getBooleanValue(IPreferenceKeys.ABCD_IMPORT_OVERRIDE) && allowOverride){
1427

    
1428
            config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1429

    
1430
            config.setAddMediaAsMediaSpecimen(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1431

    
1432
            config.setAllowReuseOtherClassifications(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1433
            config.setDeduplicateClassifications(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1434
            config.setDeduplicateReferences(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1435

    
1436
            config.setGetSiblings(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1437
            config.setIgnoreAuthorship(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1438
            config.setIgnoreImportOfExistingSpecimen(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1439
            config.setMapUnitIdToAccessionNumber(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1440
            config.setMapUnitIdToBarcode(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1441
            config.setMapUnitIdToCatalogNumber(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1442
            config.setMoveNewTaxaToDefaultClassification(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1443
            config.setOverwriteExistingSpecimens(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1444
            config.setReuseExistingDescriptiveGroups(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1445
            config.setReuseExistingMetaData(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1446
            config.setReuseExistingTaxaWhenPossible(getBooleanValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1447
            config.setNomenclaturalCode(NomenclaturalCode.getByKey(getStringValue(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE)));
1448
            return config;
1449

    
1450
        }
1451

    
1452
            if (preference == null){
1453
                return config;
1454
             } else{
1455
             String configString = preference.getValue();
1456
             String[] configArray = configString.split(";");
1457

    
1458
             for (String configItem: configArray){
1459
                 String[] keyValue = configItem.split(":");
1460
                 String keyString = keyValue[0];
1461
                 String valueString = keyValue[1];
1462
                 if (keyString.equals("ignoreImportOfExistingSpecimen")){
1463
                     config.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
1464
                 }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){
1465
                     config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
1466
                 }else if (keyString.equals("reuseExistingTaxaWhenPossible")){
1467
                     config.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
1468
                 }else if (keyString.equals("ignoreAuthorship")){
1469
                     config.setIgnoreAuthorship(Boolean.valueOf(valueString));
1470
                 }else if (keyString.equals("addMediaAsMediaSpecimen")){
1471
                     config.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
1472
                 }else if (keyString.equals("reuseExistingMetaData")){
1473
                     config.setReuseExistingMetaData(Boolean.valueOf(valueString));
1474
                 }else if (keyString.equals("reuseExistingDescriptiveGroups")){
1475
                     config.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
1476
                 }else if (keyString.equals("allowReuseOtherClassifications")){
1477
                     config.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
1478
                 }else if (keyString.equals("deduplicateReferences")){
1479
                     config.setDeduplicateReferences(Boolean.valueOf(valueString));
1480
                 }else if (keyString.equals("deduplicateClassifications")){
1481
                     config.setDeduplicateClassifications(Boolean.valueOf(valueString));
1482
                 }else if (keyString.equals("moveNewTaxaToDefaultClassification")){
1483
                     config.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
1484
                 }else if (keyString.equals("mapUnitIdToCatalogNumber")){
1485
                     config.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
1486
                 }else if (keyString.equals("mapUnitIdToAccessionNumber")){
1487
                     config.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
1488
                 }else if (keyString.equals("mapUnitIdToBarcode")){
1489
                     config.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
1490
                 }else if (keyString.equals("overwriteExistingSpecimens")){
1491
                     config.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
1492
                 }else if (keyString.equals("nomenclaturalCode")){
1493
                         config.setNomenclaturalCode(NomenclaturalCode.fromString(valueString));
1494
                 }else{
1495
                     logger.debug("This key of the abcd configurator needs to be added to the transformer: " + keyString);
1496
                 }
1497

    
1498
            }
1499
        }
1500
        return config;
1501
    }
1502

    
1503

    
1504
    public static void updateAbcdImportConfigurationPreference() {
1505

    
1506
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1507
        ICdmRepository controller;
1508
        controller = CdmStore.getCurrentApplicationConfiguration();
1509
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
1510
        CdmPreference preference = null;
1511
        if (controller == null){
1512
            return ;
1513
        }
1514
        preference = controller.getPreferenceService().find(key);
1515
        boolean allowOverride = true;
1516
        if (preference != null && !preference.isAllowOverride()){
1517
            allowOverride = false;
1518
        }
1519
        if (!getBooleanValue(IPreferenceKeys.ABCD_IMPORT_OVERRIDE) || !allowOverride){
1520

    
1521
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS, config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
1522
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN, config.isAddMediaAsMediaSpecimen());
1523

    
1524
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS, config.isAllowReuseOtherClassifications());
1525
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS, config.isDeduplicateClassifications());
1526
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES, config.isDeduplicateReferences());
1527

    
1528
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS, config.isGetSiblings());
1529
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP, config.isIgnoreAuthorship());
1530
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN, config.isIgnoreImportOfExistingSpecimen());
1531
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER, config.isMapUnitIdToAccessionNumber());
1532
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE, config.isMapUnitIdToBarcode());
1533
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER, config.isMapUnitIdToCatalogNumber());
1534
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION, config.isMoveNewTaxaToDefaultClassification());
1535
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN, config.isOverwriteExistingSpecimens());
1536
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS, config.isReuseExistingDescriptiveGroups());
1537
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA, config.isReuseExistingMetaData());
1538
            setBooleanValue(ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE, config.isReuseExistingTaxaWhenPossible());
1539
            if (config.getNomenclaturalCode() != null){
1540
                setStringValue(ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE, config.getNomenclaturalCode().getKey());
1541
            }
1542
        }
1543
    }
1544

    
1545

    
1546
    /**
1547
    *
1548
    */
1549
   public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1550
       NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1551
       if (local){
1552
          config.setSimpleDetailsViewActivated(getBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1553
          config.setAppendedPhraseActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1554
          config.setAtomisedEpithetsActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1555
          config.setAuthorshipSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1556
          config.setLSIDActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1557
          config.setNameApprobiationActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1558
          config.setNameCacheActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1559
          config.setNameRelationsSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1560
          config.setNomenclaturalCodeActived(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1561
          config.setNomenclaturalStatusSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1562
          config.setNomenclaturalReferenceSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1563
          config.setProtologueActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1564
          config.setRankActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1565
          config.setTaxonSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1566
          config.setTypeDesignationSectionActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1567
          config.setHybridActivated(getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1568
       }else{
1569

    
1570
       }
1571

    
1572
      return config;
1573
   }
1574

    
1575

    
1576
   public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1577
       setBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1578
               config.isSimpleDetailsViewActivated());
1579
       setBooleanValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1580
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1581
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1582
               config.isNomenclaturalCodeActived());
1583
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1584
               config.isNameCacheActivated());
1585
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1586
               config.isAppendedPhraseActivated());
1587
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1588
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1589
               config.isAtomisedEpithetsActivated());
1590
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1591
               config.isAuthorCacheActivated());
1592
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1593
               config.isAuthorshipSectionActivated());
1594
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1595
               config.isNomenclaturalReferenceSectionActivated());
1596
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1597
               config.isNomenclaturalStatusSectionActivated());
1598
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1599
               config.isProtologueActivated());
1600
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1601
               config.isTypeDesignationSectionActivated());
1602
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1603
               config.isNameRelationsSectionActivated());
1604
       setBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1605
               config.isHybridActivated());
1606

    
1607
   }
1608

    
1609
/**
1610
 * @return
1611
 */
1612
public static boolean isSortTaxaByRankAndName() {
1613

    
1614
    return getBooleanValue(IPreferenceKeys.SORT_TAXA_BY_RANK_AND_NAME);
1615
}
1616

    
1617
/**
1618
 * @return
1619
 */
1620
public static boolean isSortNamedAreaByOrderInVocabulary() {
1621

    
1622
    return getBooleanValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER);
1623
}
1624

    
1625
public static void setSortNamedAreasByOrderInVocabulary(boolean isSortByVocabularyOrder) {
1626
    setBooleanValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER, isSortByVocabularyOrder);
1627

    
1628
}
1629

    
1630
/**
1631
 * <p>
1632
 * setPreferredNamedAreasForDistributionEditor
1633
 * </p>
1634
 *
1635
 * @param saveCheckedElements
1636
 * @param saveGrayedElements
1637
 */
1638
public static void setLastSelectedReference(
1639
        List<String> lastSelectedReferences) {
1640

    
1641
        setStringValue(PreferencesUtil.LAST_SELECTED_REFERENCES, lastSelectedReferences.toString());
1642
    }
1643

    
1644
/**
1645
 * <p>
1646
 * setPreferredNamedAreasForDistributionEditor
1647
 * </p>
1648
 *
1649
 * @param saveCheckedElements
1650
 * @param saveGrayedElements
1651
 */
1652
public static List<String> getLastSelectedReferences() {
1653

    
1654
        //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1655
        String lastSelected = getStringValue(PreferencesUtil.LAST_SELECTED_REFERENCES);
1656
        List<String> result = new ArrayList<>();
1657
        if (!StringUtils.isBlank(lastSelected)){
1658
            Collections.addAll(result, lastSelected.substring(1,lastSelected.length()-1).split(", "));
1659
        }
1660
        return result;
1661
    }
1662

    
1663

    
1664
/**
1665
 * <p>
1666
 * setPreferredNamedAreasForDistributionEditor
1667
 * </p>
1668
 *
1669
 * @param saveCheckedElements
1670
 * @param saveGrayedElements
1671
 */
1672
public static void setPreferredNamedAreasForDistributionEditor(
1673
        String saveCheckedElements, String saveGrayedElements, boolean local) {
1674
    if (local){
1675
        setStringValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS, saveCheckedElements);
1676
        setStringValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS_GRAYED, saveGrayedElements);
1677

    
1678
    }
1679
    else{
1680
        CdmPreference preference = null;
1681

    
1682
        if (saveCheckedElements == null){
1683
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaTerms);
1684

    
1685
            if (preference == null){
1686
                return ;
1687
            } else{
1688
                setStringValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1689
                        saveCheckedElements);
1690
                preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1691
                setPreferenceToDB(preference);
1692

    
1693
            }
1694
        } else{
1695
           preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1696
           setPreferenceToDB(preference);
1697
           setStringValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1698
                    saveCheckedElements);
1699

    
1700
        }
1701
    }
1702

    
1703
}
1704

    
1705
/**
1706
 * @param saveCheckedElements
1707
 * @param saveCheckedElements2
1708
 * @param b
1709
 */
1710
public static void setPreferredVocabulariesForDistributionEditor(String saveCheckedElements,
1711
        boolean local) {
1712
    if (local){
1713
      setStringValue(PreferencesUtil.DISTRIBUTION_VOCABULARIES, saveCheckedElements);
1714
    }
1715
    else{
1716
        ICdmRepository controller;
1717
        CdmPreference preference = null;
1718

    
1719
        if (saveCheckedElements == null){
1720
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1721

    
1722
            if (preference == null){
1723
                return ;
1724
            } else{
1725
                setStringValue(DISTRIBUTION_VOCABULARIES,
1726
                        saveCheckedElements);
1727
                preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1728
                setPreferenceToDB(preference);
1729
            }
1730
        } else{
1731
            preference = CdmPreference.NewInstance(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1732
            setPreferenceToDB(preference);
1733
            setStringValue(DISTRIBUTION_VOCABULARIES,
1734
                    saveCheckedElements);
1735

    
1736
        }
1737
    }
1738
}
1739

    
1740

    
1741

    
1742

    
1743
/**
1744
 * @param saveCheckedElements
1745
 * @param saveCheckedElements2
1746
 * @param b
1747
 */
1748
public static String getPreferredVocabulariesForDistributionEditor(boolean local) {
1749
    if (local){
1750

    
1751
        String pref = getStringValue(DISTRIBUTION_VOCABULARIES);
1752
        return pref;
1753
    }
1754
    else{
1755
        CdmPreference preference = null;
1756
        preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1757
        if (preference == null){
1758
            return null;
1759
        } else{
1760
            return preference.getValue();
1761
        }
1762

    
1763
    }
1764

    
1765

    
1766

    
1767
}
1768

    
1769
public static boolean getFilterCommonNameReferences(){
1770
    return getBooleanValue(PreferencesUtil.FILTER_COMMON_NAME_REFERENCES);
1771
}
1772

    
1773
/**
1774
 *
1775
 */
1776
public static void updateDBPreferences() {
1777
    CdmPreference preference = null;
1778
    IPreferenceStore prefStore = getPreferenceStore();
1779

    
1780
    //ABCD Configurator
1781

    
1782
    updateAbcdImportConfigurationPreference();
1783

    
1784
    preference = getPreferenceFromDB(PreferencePredicate.ShowImportExportMenu);
1785
    if (preference != null){
1786
        if(!getBooleanValue(SHOW_IO_MENU) ||  !preference.isAllowOverride()){
1787
           setBooleanValue(SHOW_IO_MENU, Boolean.valueOf(preference.getValue()));
1788
        }
1789
    }else{
1790
        setBooleanValue(SHOW_IO_MENU, true);
1791
    }
1792

    
1793
    preference = getPreferenceFromDB(PreferencePredicate.ShowMediaView);
1794
    if (preference != null){
1795
        if(!getBooleanValue(SHOW_MEDIA) ||  !preference.isAllowOverride()){
1796
            setBooleanValue(SHOW_MEDIA, Boolean.valueOf(preference.getValue()));
1797
        }
1798
    }else{
1799
        setBooleanValue(SHOW_MEDIA, true);
1800
    }
1801

    
1802
    preference = getPreferenceFromDB(PreferencePredicate.ShowChecklistPerspective);
1803
    if (preference != null){
1804
        if(!getBooleanValue(SHOW_CHECKLIST_PERSPECTIVE) ||  !preference.isAllowOverride()){
1805
            setBooleanValue(SHOW_CHECKLIST_PERSPECTIVE, Boolean.valueOf(preference.getValue()));
1806
        }
1807
    }else{
1808
        setBooleanValue(SHOW_CHECKLIST_PERSPECTIVE, false);
1809
    }
1810

    
1811
    //Specimen Details
1812
    preference = getPreferenceFromDB(PreferencePredicate.ShowSpecimen);
1813
    if (preference != null){
1814
        if(!getBooleanValue(SHOW_SPECIMEN) ||  !preference.isAllowOverride()){
1815
            setBooleanValue(SHOW_SPECIMEN, Boolean.valueOf(preference.getValue()));
1816
        }
1817
    }else{
1818
        setBooleanValue(SHOW_SPECIMEN, true);
1819
    }
1820

    
1821
    preference = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
1822
    if (preference != null){
1823
        if(!getBooleanValue(SHOW_TAXON_ASSOCIATIONS_OVERRIDE) ||  !preference.isAllowOverride()){
1824
            setBooleanValue(SHOW_TAXON_ASSOCIATIONS, Boolean.valueOf(preference.getValue()));
1825
        }
1826
    }
1827
    preference = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
1828
    if (preference != null){
1829
        if(!getBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION_OVERRIDE) ||  !preference.isAllowOverride()){
1830
            setBooleanValue(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION, Boolean.valueOf(preference.getValue()));
1831
        }
1832
    }
1833
    preference = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
1834
    if (preference != null){
1835
        if(!getBooleanValue(SHOW_LIFE_FORM_OVERRIDE) ||  !preference.isAllowOverride()){
1836
            setBooleanValue(SHOW_LIFE_FORM, Boolean.valueOf(preference.getValue()));
1837
        }
1838
    }
1839
    preference = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
1840
    if (preference != null){
1841
        if(!getBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS_OVERRIDE) ||  !preference.isAllowOverride()){
1842
            setBooleanValue(DETERMINATION_ONLY_FOR_FIELD_UNITS, Boolean.valueOf(preference.getValue()));
1843
        }
1844
    }
1845

    
1846

    
1847
    //Name Details
1848
    NameDetailsConfigurator config = getPreferredNameDetailsConfiguration(false);
1849
    if (config != null ){
1850
        if (!getBooleanValue(OVERRIDE_NAME_DETAILS) ||  !getBooleanValue(ALLOW_OVERRIDE_NAME_DETAILS)){
1851
            setPreferredNameDetailsConfiguration(config, false);
1852
        }
1853
    }
1854

    
1855
    //Distribution Editor
1856
    preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1857
    if (preference != null){
1858
        if (!getBooleanValue(DISTRIBUTION_VOCABULARIES_OVERRIDE) ||  !preference.isAllowOverride()){
1859
            setStringValue(DISTRIBUTION_VOCABULARIES, preference.getValue());
1860
            setBooleanValue(DISTRIBUTION_VOCABULARIES_ALLOW_OVERRIDE, preference.isAllowOverride());
1861
        }
1862
    }else{
1863
        setBooleanValue(DISTRIBUTION_VOCABULARIES_ALLOW_OVERRIDE,true);
1864
        setStringValue(DISTRIBUTION_VOCABULARIES, "");
1865
    }
1866

    
1867
    preference = getPreferenceFromDB(PreferencePredicate.DistributionEditorActivated);
1868
    if (preference != null){
1869
        if (!getBooleanValue(DISTRIBUTION_AREA_PREFRENCES_ACTIVE_OVERRIDE) || !preference.isAllowOverride()){
1870
            setBooleanValue(DISTRIBUTION_AREA_PREFRENCES_ACTIVE, Boolean.valueOf(preference.getValue()));
1871
        }
1872
    }
1873

    
1874
    preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionStatus);
1875
    if (preference != null){
1876
        if (!getBooleanValue(DISTRIBUTION_STATUS_OVERRIDE) || !preference.isAllowOverride()){
1877
            //get terms for the uuids... and add them to the termManager as preferred terms
1878
            ITermService termService = CdmStore.getService(ITermService.class);
1879
            String[] uuidArray = preference.getValue().split(";");
1880
            List<UUID> uuidList = new ArrayList();
1881
            for (String uuidString:uuidArray){
1882
                uuidList.add(UUID.fromString(uuidString));
1883
            }
1884

    
1885
            List<DefinedTermBase> definedTermBases = termService.load(uuidList, null);
1886
            CdmStore.getTermManager().setPreferredTerms(definedTermBases, TermStore.getTerms(TermType.PresenceAbsenceTerm, null));
1887
        }
1888
    }
1889

    
1890
    preference = getPreferenceFromDB(PreferencePredicate.CommonNameAreaVocabularies);
1891
    if (preference != null){
1892
        if (!getBooleanValue(COMMON_NAME_AREA_VOCABULARIES) ||  !preference.isAllowOverride()){
1893
            setStringValue(COMMON_NAME_AREA_VOCABULARIES, preference.getValue());
1894
            setBooleanValue(COMMON_NAME_AREA_VOCABULARIES_ALLOW_OVERRIDE, preference.isAllowOverride());
1895
        }
1896
    }else{
1897
        setStringValue(COMMON_NAME_AREA_VOCABULARIES, "");
1898
        setBooleanValue(COMMON_NAME_AREA_VOCABULARIES_ALLOW_OVERRIDE, true);
1899
    }
1900

    
1901
    preference = getPreferenceFromDB(PreferencePredicate.CommonNameReferencesWithMarker);
1902
    if (preference != null){
1903
        if (!getBooleanValue(FILTER_COMMON_NAME_REFERENCES) ||  !preference.isAllowOverride()){
1904
            setBooleanValue(FILTER_COMMON_NAME_REFERENCES, Boolean.valueOf(preference.getValue()));
1905
        }
1906
    }else{
1907
        setBooleanValue(FILTER_COMMON_NAME_REFERENCES, false);
1908
    }
1909

    
1910

    
1911
    preference = getPreferenceFromDB(PreferencePredicate.ShowTaxonNodeWizard);
1912
    if (preference != null){
1913
        if (!getBooleanValue(SHOW_TAXONNODE_WIZARD) ||  !preference.isAllowOverride()){
1914
            setBooleanValue(SHOW_TAXONNODE_WIZARD, Boolean.valueOf(preference.getValue()));
1915
        }
1916
    }else{
1917
        setBooleanValue(SHOW_TAXONNODE_WIZARD, true);
1918
    }
1919

    
1920
    preference = getPreferenceFromDB(PreferencePredicate.ShowIdInSource);
1921
    if (preference != null){
1922
        if (!getBooleanValue(SHOW_ID_IN_SOURCE) ||  !preference.isAllowOverride()){
1923
            setBooleanValue(SHOW_ID_IN_SOURCE, Boolean.valueOf(preference.getValue()));
1924
        }
1925
    }else{
1926
        setBooleanValue(SHOW_ID_IN_SOURCE, true);
1927
    }
1928

    
1929
    preference = getPreferenceFromDB(PreferencePredicate.ShowNameInSource);
1930
    if (preference != null){
1931
        if (!getBooleanValue(SHOW_NAME_IN_SOURCE) ||  !preference.isAllowOverride()){
1932
            setBooleanValue(SHOW_NAME_IN_SOURCE, Boolean.valueOf(preference.getValue()));
1933
        }
1934
    }else{
1935
        setBooleanValue(SHOW_NAME_IN_SOURCE, true);
1936
    }
1937

    
1938
    preference = getPreferenceFromDB(PreferencePredicate.DisableMultiClassification);
1939
    if (preference != null){
1940
        if (!getBooleanValue(DISABLE_MULTI_CLASSIFICATION) ||  !preference.isAllowOverride()){
1941
            setBooleanValue(DISABLE_MULTI_CLASSIFICATION, Boolean.valueOf(preference.getValue()));
1942
        }
1943
    }else{
1944
        setBooleanValue(DISABLE_MULTI_CLASSIFICATION, false);
1945
    }
1946

    
1947
}
1948

    
1949

    
1950

    
1951

    
1952

    
1953

    
1954

    
1955
}
(19-19/25)