Project

General

Profile

Download (54.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.preference;
11

    
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.net.URI;
17
import java.net.URISyntaxException;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Properties;
24
import java.util.Set;
25
import java.util.StringTokenizer;
26
import java.util.UUID;
27

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

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

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

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

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

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

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

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

    
140
    				getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
141
    				        preferenceKey);
142
    				getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,preference.isAllowOverride());
143
    				return preference;
144
    			}
145
    		} else{
146
    			preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferenceKey);
147
    			controller.getPreferenceService().set(preference);
148

    
149
    		}
150
	    }
151
        return null;
152

    
153

    
154

    
155
	}
156

    
157
	public static NomenclaturalCode getPreferredNomenclaturalCode(){
158
	    if (getPreferenceStore().getBoolean(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
159
            return getPreferredNomenclaturalCode(true);
160
        } else{
161
            return getPreferredNomenclaturalCode(false);
162
        }
163
	}
164

    
165
	/**
166
	 * <p>
167
	 * getPreferredNomenclaturalCode
168
	 * </p>
169
	 *
170
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
171
	 */
172
	public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
173

    
174
		CdmPreference pref = null;
175
		if (!useLocal){
176
		    pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
177
		}
178

    
179
	    String preferredCode;
180
	    if(pref == null){
181
	    	preferredCode = getPreferenceStore().getString(
182
					PREFERRED_NOMENCLATURAL_CODE_KEY);
183

    
184
	    }else{
185
	    	preferredCode = pref.getValue();
186
	    }
187

    
188
	    return getPreferredNomenclaturalCode(preferredCode);
189

    
190
	}
191

    
192
	/**
193
     * <p>
194
     * getPreferredNomenclaturalCode
195
     * </p>
196
     *
197
     * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
198
     */
199
    public static NomenclaturalCode getPreferredNomenclaturalCode(String preferenceKeyNomenclaturalCode) {
200

    
201
        for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
202
            if (getPreferenceKey(code).equals(preferenceKeyNomenclaturalCode)) {
203
                return code;
204
            }
205
        }
206
        return null;
207
    }
208

    
209
	public static boolean isShowTaxonAssociations(){
210
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
211
            return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
212
        } else{
213
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
214

    
215
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
216
        }
217
    }
218

    
219
	public static boolean isShowLifeForm(){
220
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
221
            return getPreferenceStore().getBoolean(SHOW_LIFE_FORM);
222
        } else{
223
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
224

    
225
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
226
        }
227
    }
228

    
229
	public static boolean isDeterminationOnlyForFieldUnits(){
230
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
231
            return getPreferenceStore().getBoolean(DETERMINATION_ONLY_FOR_FIELD_UNITS);
232
        } else{
233
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
234

    
235
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
236
        }
237
    }
238

    
239
	public static boolean isCollectingAreaInGeneralSection(){
240
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
241
            return getPreferenceStore().getBoolean(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION);
242
        } else{
243
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
244

    
245
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
246
        }
247
    }
248

    
249
	public static boolean isShowSimpleDetailsView(){
250
        if (getPreferenceStore().getBoolean(OVERRIDE_NAME_DETAILS) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_NAME_DETAILS)) {
251
            return getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION);
252
        } else{
253
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
254

    
255
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
256
        }
257
    }
258

    
259

    
260

    
261
    public static CdmPreference getPreferenceFromDB(PreferencePredicate predicate){
262
        ICdmRepository controller;
263
        CdmPreference pref = null;
264

    
265
        try{
266
            if(CdmStore.isActive()){
267
                controller = CdmStore.getCurrentApplicationConfiguration();
268
                PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), predicate);
269
                pref = controller.getPreferenceService().find(key);
270
            }
271
        }catch(Exception e){
272
            e.printStackTrace();
273
        }
274

    
275
        return pref;
276

    
277
    }
278

    
279
    public static void setPreferenceToDB(CdmPreference preference){
280
        ICdmRepository controller;
281
        try{
282
            if(CdmStore.isActive()){
283
                controller = CdmStore.getCurrentApplicationConfiguration();
284
                controller.getPreferenceService().set(preference);
285
            }
286
        }catch(Exception e){
287
            e.printStackTrace();
288
        }
289

    
290
    }
291

    
292

    
293

    
294
	public static String getPreferredDefaultLangugae(){
295
	    String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
296
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
297
	        return preferredLanguage;
298
	    }
299
	    return null;
300
	}
301

    
302
	public static boolean isShowMediaPreview(){
303
        boolean isShowMediaPreview = getPreferenceStore().getBoolean(SHOW_MEDIA_PREVIEW);
304
        return isShowMediaPreview;
305
    }
306

    
307
	/**
308
	 * Get the match strategy for the given class that was stored in preferences
309
	 * or the default strategy if it was not stored in preferences
310
	 *
311
	 * @param clazz
312
	 *            a {@link java.lang.Class} object.
313
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
314
	 */
315
	public static IMatchStrategy getMatchStrategy(Class clazz) {
316
		String className = clazz.getName();
317
		if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
318
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
319

    
320
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
321
				String matchModeName = getPreferenceStore().getString(
322
						getMatchStrategyFieldName(className, fieldName));
323
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
324
				try {
325
					matchStrategy.setMatchMode(fieldName, matchMode);
326
				} catch (MatchException e) {
327
					MessagingUtils.error(PreferencesUtil.class, e);
328
					throw new RuntimeException(e);
329
				}
330
			}
331

    
332
			return matchStrategy;
333
		}
334
		return getDefaultMatchStrategy(clazz);
335
	}
336

    
337
	/**
338
	 * Stores a matchStrategy into the preference store.
339
	 *
340
	 * @param matchStrategy
341
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
342
	 *            object.
343
	 */
344
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
345
		String className = matchStrategy.getMatchClass().getName();
346
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
347

    
348
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
349

    
350
		for (String fieldName : matchFields) {
351
			getPreferenceStore().setValue(
352
					getMatchStrategyFieldName(className, fieldName),
353
					matchStrategy.getMatchMode(fieldName).name());
354
		}
355
	}
356

    
357
	/**
358
	 * Helper method to create the preference property for a match field.
359
	 *
360
	 * @param className
361
	 * @param fieldName
362
	 * @return
363
	 */
364
	private static String getMatchStrategyFieldName(String className,
365
			String fieldName) {
366
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
367
	}
368

    
369
	/**
370
	 * Returns the default match strategy for a given class.
371
	 *
372
	 * @param clazz
373
	 *            a {@link java.lang.Class} object.
374
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
375
	 */
376
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
377
		return DefaultMatchStrategy.NewInstance(clazz);
378
	}
379

    
380
	/**
381
	 * <p>
382
	 * getDateFormatPattern
383
	 * </p>
384
	 *
385
	 * @return a {@link java.lang.String} object.
386
	 */
387
	public static String getDateFormatPattern() {
388
		// TODO make this configurable in properties
389
		String pattern = "Y-M-d H:m";
390
		return pattern;
391
	}
392

    
393
	/**
394
	 * <p>
395
	 * addTermToPreferredTerms
396
	 * </p>
397
	 *
398
	 * @param term
399
	 *            a T object.
400
	 * @param <T>
401
	 *            a T object.
402
	 */
403
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
404

    
405
		// VocabularyEnum vocabulary =
406
		// VocabularyEnum.getVocabularyEnum(term.getClass());
407
		//
408
		// getPreferenceStore().setValue(getPreferenceKey(term),
409
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
410
		//
411
		// firePreferencesChanged(term.getClass());
412
	}
413

    
414
	/**
415
	 * Construct a unique key using the CdmBase object's uuid
416
	 *
417
	 * @param cdmBase
418
	 * @return
419
	 */
420
	private static String getPreferenceKey(ICdmBase cdmBase) {
421
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
422

    
423
		String key = cdmBase.getClass().getName().concat(".")
424
				.concat(cdmBase.getUuid().toString());
425
		if (key.contains("javassist")) {
426
			MessagingUtils.info("proxy");
427
		}
428
		return key;
429
	}
430

    
431
	/**
432
	 * Construct a unique key using the CdmBase object's uuid
433
	 *
434
	 * @param cdmBase
435
	 * @return
436
	 */
437
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
438
		simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
439
		String key = simpleTerm.getClass().getName().concat(".")
440
				.concat(simpleTerm.getUuid().toString());
441
		if (key.contains("javassist")) {
442
			MessagingUtils.warn(PreferencesUtil.class,
443
					"Trying to persist a preference based on a proxy class.");
444
		}
445
		return key;
446
	}
447

    
448

    
449

    
450
	/**
451
	 * Construct a unique key using the CdmBase object's uuid
452
	 *
453
	 * @param cdmBase
454
	 * @return
455
	 */
456
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
457
		definedTerm = HibernateProxyHelper.deproxy(definedTerm);
458
		String key = definedTerm.getClass().getName().concat(".")
459
				.concat(definedTerm.getUuid().toString());
460
		if (key.contains("javassist")) {
461
			MessagingUtils.warn(PreferencesUtil.class,
462
					"Trying to persist a preference based on a proxy class.");
463
		}
464
		return key;
465
	}
466

    
467
	/**
468
	 * Retrieves search preferences from the preference store
469
	 *
470
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
471
	 */
472
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
473
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
474

    
475
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
476
				TAXON_SERVICE_CONFIGURATOR_TAXA));
477
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
478
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
479
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
480
				TAXON_SERVICE_CONFIGURATOR_NAMES));
481
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
482
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
483
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
484

    
485
		return configurator;
486
	}
487

    
488
	/**
489
	 * create new preferences, setting all search options to true
490
	 *
491
	 * @return a
492
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
493
	 *         object.
494
	 */
495
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
496
		IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
497

    
498
		configurator.setDoTaxa(true);
499
		configurator.setDoSynonyms(true);
500
		configurator.setDoNamesWithoutTaxa(true);
501
		configurator.setDoTaxaByCommonNames(true);
502

    
503
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
504
				"name", "name.$", "relationsFromThisTaxon.$"));
505

    
506
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
507
				"name", "name.$", "synonyms.relatedTo.*"));
508

    
509
		// DEFAULT VALUES
510
		// match mode is a simple like, actually all other match modes are kind
511
		// of bogus
512
		configurator
513
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
514
		// we set page number and size here as this should always be unlimited
515
		configurator.setPageNumber(0);
516
		// TODO currently limit results to 10000
517
		configurator.setPageSize(10000);
518

    
519
		return configurator;
520
	}
521

    
522
	/**
523
	 * Store search preferences
524
	 *
525
	 * @param configurator
526
	 *            a
527
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
528
	 *            object.
529
	 */
530
	public static void setSearchConfigurator(
531
			IFindTaxaAndNamesConfigurator configurator) {
532
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
533
				configurator.isDoTaxa());
534
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
535
				configurator.isDoSynonyms());
536
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
537
				configurator.isDoNamesWithoutTaxa());
538
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
539
				configurator.isDoTaxaByCommonNames());
540
	}
541

    
542
	/**
543
	 * <p>
544
	 * firePreferencesChanged
545
	 * </p>
546
	 *
547
	 * @param clazz
548
	 *            a {@link java.lang.Class} object.
549
	 */
550
	public static void firePreferencesChanged(Class clazz) {
551
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
552
				null, clazz);
553
	}
554

    
555
	/**
556
	 * Set default values for preferences
557
	 */
558
	public static void setDefaults() {
559
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
560
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
561
				true);
562
		getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
563
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
564
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
565
		getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
566
		getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
567
				"http://www.biodiversitylibrary.org/openurl");
568
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
569
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
570
		getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, false);
571
		getPreferenceStore().setDefault(CHECKLIST_ID_IN_VOCABULARY, true);
572
		getPreferenceStore().setDefault(CHECKLIST_SYMBOL, false);
573
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, true);
574
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE, true);
575
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, true);
576
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_HYBRID, true);
577
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_LSID, true);
578
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, true);
579
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAMECACHE, true);
580
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, true);
581
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, true);
582
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, true);
583
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, true);
584
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_RANK, true);
585
		getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, true);
586
		getPreferenceStore().setDefault(SORT_NODES_NATURALLY, false);
587
		getPreferenceStore().setDefault(SORT_NODES_ALPHABETICALLY, false);
588
		getPreferenceStore().setDefault(SORT_TAXA_BY_RANK_AND_NAME, true);
589

    
590
	}
591

    
592
	/**
593
	 * <p>
594
	 * checkNomenclaturalCode
595
	 * </p>
596
	 */
597
	public static void checkNomenclaturalCode() {
598
		// First time Editor is opened, no nomenclatural code has been set
599

    
600

    
601
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
602
			PreferencesUtil.setPreferredNomenclaturalCode(getPreferenceKey(NomenclaturalCode.ICNAFP), true);
603
		}
604

    
605

    
606

    
607
	}
608
	public static void setNomenclaturalCodePreferences(){
609
	    ICdmRepository controller;
610
        controller = CdmStore.getCurrentApplicationConfiguration();
611
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
612
        CdmPreference preference = null;
613
        if (controller == null){
614
            return ;
615
        }
616
        preference = controller.getPreferenceService().find(key);
617
        if (preference == null){
618
            return;
619
        }
620
        getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
621

    
622
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
623
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
624
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
625

    
626
        getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
627
                getPreferenceKey(preferredCode));
628

    
629
        if (!preference.isAllowOverride()){
630
            getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
631
                    getPreferenceKey(preferredCode));
632
        }
633
	}
634

    
635
	public static void checkDefaultLanguage(){
636
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
637
	       Shell shell = AbstractUtility.getShell();
638
	       int open = new DefaultLanguageDialog(shell).open();
639
	       if(open == Window.OK){
640
	           PlatformUI.getWorkbench().restart();
641
	       }
642
	    }else{
643
	        //TODO:In case of a reinstall, the config.ini will be overwritten
644
	        //     here you create config.ini with the stored key from preferences
645
	    }
646
	}
647

    
648
	/**
649
	 * <p>
650
	 * getMapServiceAccessPoint
651
	 * </p>
652
	 *
653
	 * @return a {@link java.lang.String} object.
654
	 */
655
	public static String getMapServiceAccessPoint() {
656
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
657
	}
658

    
659
	/**
660
	 * <p>
661
	 * shouldConnectAtStartUp
662
	 * </p>
663
	 *
664
	 * @return a boolean.
665
	 */
666
	public static boolean shouldConnectAtStartUp() {
667
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
668
		//return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
669
		return false;
670
	}
671

    
672
	/**
673
	 * <p>
674
	 * getDefaultFeatureTreeForTextualDescription
675
	 * </p>
676
	 *
677
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
678
	 */
679
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
680
		String uuidString = getPreferenceStore().getString(
681
				FEATURE_TREE_DEFAULT_TEXT);
682
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
683
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
684
	}
685

    
686
	/**
687
	 * <p>
688
	 * getDefaultFeatureTreeForStructuredDescription
689
	 * </p>
690
	 *
691
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
692
	 */
693
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
694
		String uuidString = getPreferenceStore().getString(
695
				FEATURE_TREE_DEFAULT_STRUCTURE);
696
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
697
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
698
	}
699

    
700
	/**
701
	 * <p>
702
	 * setSortRanksHierarchichally
703
	 * </p>
704
	 *
705
	 * @param selection
706
	 *            a boolean.
707
	 */
708
	public static void setSortRanksHierarchichally(boolean selection) {
709
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
710
	}
711

    
712
	/**
713
	 * <p>
714
	 * getSortRanksHierarchichally
715
	 * </p>
716
	 *
717
	 * @return a boolean.
718
	 */
719
	public static boolean getSortRanksHierarchichally() {
720
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
721
	}
722

    
723
	public static boolean isMultilanguageTextEditingCapability() {
724
		return getPreferenceStore().getBoolean(
725
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
726
	}
727

    
728
	public static Language getGlobalLanguage() {
729

    
730

    
731
		String languageUuidString = getPreferenceStore().getString(
732
				GLOBAL_LANGUAGE_UUID);
733

    
734
		if(!CdmStore.isActive()) {
735
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
736
            return null;
737
        }
738

    
739
		if (CdmUtils.isBlank(languageUuidString)) {
740
			return Language.getDefaultLanguage();
741
		}
742

    
743
		UUID languageUuid = UUID.fromString(languageUuidString);
744
		return (Language) CdmStore.getService(ITermService.class).load(
745
				languageUuid);
746
	}
747

    
748
	public static void setGlobalLanguage(Language language) {
749
	    if(language != null) {
750
	        getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
751
	        CdmStore.setDefaultLanguage(language);
752
	    }
753

    
754
	}
755

    
756
	/**
757
	 * @return
758
	 */
759
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
760
		List<MarkerType> markerTypes = CdmStore.getTermManager()
761
				.getPreferredTerms(MarkerType.class);
762

    
763
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
764

    
765
		for (MarkerType markerType : markerTypes) {
766
			String name = getMarkerTypeEditingPreferenceKey(markerType);
767
			Boolean value = getPreferenceStore().getBoolean(name);
768

    
769
			result.put(markerType, value);
770
		}
771

    
772
		return result;
773
	}
774

    
775
	/**
776
	 * @param markerTypeEditingMap
777
	 */
778
	public static void setEditMarkerTypePreferences(
779
			Map<MarkerType, Boolean> markerTypeEditingMap) {
780
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
781
			String name = getMarkerTypeEditingPreferenceKey(markerType);
782
			getPreferenceStore().setValue(name,
783
					markerTypeEditingMap.get(markerType));
784
		}
785

    
786
	}
787

    
788
	private static String getMarkerTypeEditingPreferenceKey(
789
			MarkerType markerType) {
790
		markerType = HibernateProxyHelper.deproxy(markerType);
791
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
792
	}
793

    
794
	/**
795
	 * <p>
796
	 * setEditMarkerTypePreference
797
	 * </p>
798
	 *
799
	 * @param input
800
	 *            a {@link org.eclipse.ui.IEditorInput} object.
801
	 * @param markerType
802
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
803
	 * @param edit
804
	 *            a boolean.
805
	 */
806
	public static void setEditMarkerTypePreference(MarkerType markerType,
807
			boolean edit) {
808
		getPreferenceStore().setValue(
809
				getMarkerTypeEditingPreferenceKey(markerType), edit);
810
	}
811

    
812
	/**
813
	 * @return
814
	 */
815
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
816
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
817
				.NewInstance();
818
		configurator.setMoveDerivedUnitMediaToGallery(true);
819
		configurator.setMoveFieldObjectMediaToGallery(true);
820
		return configurator;
821
	}
822

    
823
	/**
824
	 * This method will write language properties to the config.ini located in the configuration folder
825
	 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
826
	 *
827
	 * @param setLanguage 0 is for german and 1 for english.
828
	 * @throws IOException
829
	 */
830
    public void writePropertyToConfigFile(int setLanguage) throws IOException {
831
        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
832
        //give warning to user if the directory has no write access
833
        if(file == null){
834
            throw new IOException();
835
        }
836
        Properties properties = load(file.getAbsolutePath()+"/config.ini");
837
        switch(setLanguage){
838
        case 0:
839
            properties.setProperty("osgi.nl", "de");
840
            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
841
            break;
842
        case 1:
843
            properties.setProperty("osgi.nl", "en");
844
            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
845
            break;
846
        default:
847
            break;
848
        }
849
        save(file+"/config.ini", properties);
850
    }
851

    
852
    /**
853
     * This method loads a property from a given file and returns it.
854
     *
855
     * @param filename
856
     * @return
857
     * @throws IOException
858
     */
859
    private Properties load(String filename) throws IOException {
860
        FileInputStream in = new FileInputStream(filename);
861
        Properties prop = new Properties();
862
        prop.load(in);
863
        in.close();
864
        return prop;
865
    }
866

    
867
    /**
868
     * This method saves a property to the specified file.
869
     *
870
     * @param filename
871
     * @param properties
872
     * @throws IOException
873
     */
874
    private void save(String filename, Properties properties) throws IOException{
875
        FileOutputStream fos =  new FileOutputStream(filename);
876
        properties.store(fos, "");
877
        fos.close();
878
    }
879

    
880
    /**
881
     * Saves a list of P2 Metadata Repositories as string with specified delimiters
882
     *
883
     * @param p2Repos
884
     */
885
    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
886
        StringBuilder sb = new StringBuilder();
887
        for(MetadataRepositoryElement p2Repo : p2Repos) {
888
            sb.append(P2_REPOSITORIES_DELIM);
889
            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
890
                sb.append("-");
891
            } else {
892
                sb.append(p2Repo.getName());
893
            }
894
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
895
            sb.append(p2Repo.getLocation().toString());
896
            sb.append(P2_REPOSITORY_FIELDS_DELIM);
897
            sb.append(String.valueOf(p2Repo.isEnabled()));
898
        }
899
        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
900
    }
901

    
902

    
903
    /**
904
     * Retrieves a list of previously saved P2 repositories
905
     *
906
     * @return
907
     */
908
    public static List<MetadataRepositoryElement> getP2Repositories() {
909
        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
910
        String p2ReposPref =  getPreferenceStore().getString(P2_REPOSITORY_LIST);
911
        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
912
            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
913

    
914
            while(p2ReposPrefST.hasMoreTokens()) {
915
                String p2RepoStr = p2ReposPrefST.nextToken();
916
                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
917
                if(p2ReposStrST.countTokens()==3) {
918
                    String nickname = p2ReposStrST.nextToken();
919
                    URI uri = null;
920
                    try {
921
                        uri = new URI(p2ReposStrST.nextToken());
922
                    } catch (URISyntaxException e) {
923
                        continue;
924
                    }
925
                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
926
                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
927
                    mre.setNickname(nickname);
928
                    mre.setEnabled(enabled);
929
                    p2Repos.add(mre);
930
                }
931
            }
932
        }
933

    
934
        return p2Repos;
935
    }
936

    
937
    /**
938
     * enables/disables nested composite. <br>
939
     *
940
     * @param ctrl - Composite to be en-/disabeld
941
     * @param enabled - boolean
942
     */
943
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
944
        if (ctrl instanceof Composite) {
945
            Composite comp = (Composite) ctrl;
946
            for (Control c : comp.getChildren()) {
947
                recursiveSetEnabled(c, enabled);
948
            }
949
        } else {
950
            ctrl.setEnabled(enabled);
951
        }
952
    }
953

    
954
    /**
955
	 * <p>
956
	 * getSortRanksNaturally
957
	 * </p>
958
    	 *
959
    	 * @return a boolean.
960
	 */
961
	public static boolean getSortNodesNaturally() {
962
		return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
963
	}
964

    
965
	/**
966
	 * <p>
967
	 * setSortRanksNaturally
968
	 * </p>
969
	 *
970
	 * @param selection
971
	 *            a boolean.
972
	 */
973
	public static void setSortNodesNaturally(boolean selection) {
974
		getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
975
	}
976

    
977

    
978
	/**
979
	 * <p>
980
	 * getSortRanksNaturally
981
	 * </p>
982
	 *
983
	 * @return a boolean.
984
	 */
985
	public static boolean getSortNodesStrictlyAlphabetically() {
986
		return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
987
	}
988

    
989
	/**
990
	 * <p>
991
	 * setSortRanksNaturally
992
	 * </p>
993
	 *
994
	 * @param selection
995
	 *            a boolean.
996
	 */
997
	public static void setSortNodesStrictlyAlphabetically(boolean selection) {
998
		getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
999
	}
1000

    
1001
	/**
1002
	 * <p>
1003
	 * setStoreNavigatorState
1004
	 * </p>
1005
	 *
1006
	 * @param selection
1007
	 *            a boolean.
1008
	 */
1009
	public static boolean isStoreNavigatorState() {
1010
		return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
1011

    
1012
	}
1013

    
1014
	/**
1015
	 * <p>
1016
	 * setStoreNavigatorState
1017
	 * </p>
1018
	 *
1019
	 * @param selection
1020
	 *            a boolean.
1021
	 */
1022
	public static void setStoreNavigatorState(boolean selection) {
1023
		getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
1024

    
1025
	}
1026

    
1027
    /**
1028
     * @return
1029
     */
1030
    public static boolean isShowUpWidgetIsDisposedMessages() {
1031
       return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
1032
    }
1033
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
1034
        getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
1035
    }
1036

    
1037
    /**
1038
     * @return
1039
     */
1040
    public static boolean isShowIdInVocabularyInChecklistEditor() {
1041
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
1042
    }
1043
    public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
1044
        getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
1045
    }
1046

    
1047
    /**
1048
     * @return
1049
     */
1050
    public static boolean isShowSymbolInChecklistEditor() {
1051
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SYMBOL);
1052
    }
1053
    public static void setShowSymbolInChecklistEditor(boolean selection) {
1054
        getPreferenceStore().setValue(CHECKLIST_SYMBOL, selection);
1055
    }
1056

    
1057
    /**
1058
     * @return
1059
     */
1060
    public static boolean isShowRankInChecklistEditor() {
1061
        return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
1062
    }
1063
    public static void setShowRankInChecklistEditor(boolean selection) {
1064
        getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
1065
    }
1066

    
1067
    /**
1068
     * @param object
1069
     * @param b
1070
     * @return
1071
     */
1072
    public static NameDetailsConfigurator setPreferredNameDetailsConfiguration( boolean local) {
1073
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1074

    
1075
        CdmPreference preference = null;
1076

    
1077
        if (!local) {
1078
            preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1079
            if (preference == null){
1080
                return null;
1081
            }
1082

    
1083
            getPreferenceStore().setValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1084

    
1085
            //the preference value is build like this:
1086
            //<section1>:true;<section2>:false....
1087

    
1088
            String value = preference.getValue();
1089
            String [] sections = value.split(";");
1090
            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1091
            String[] sectionValues;
1092
            for (String sectionValue: sections){
1093
                sectionValues = sectionValue.split(":");
1094
                sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1095
            }
1096
            config.setAllowOverride(preference.isAllowOverride());
1097
            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1098

    
1099
    //        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1100
    //                (getValue(sectionMap, "taxon")));
1101
            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1102

    
1103
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1104
    //                (getValue(sectionMap, "lsid")));
1105
            config.setLSIDActivated(getValue(sectionMap, "lsid"));
1106

    
1107
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1108
    //                (getValue(sectionMap, "nc")));
1109
            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1110

    
1111
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1112
    //                (getValue(sectionMap, "ap")));
1113
            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1114

    
1115
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1116
    //                (getValue(sectionMap, "rank")));
1117
            config.setRankActivated(getValue(sectionMap, "rank"));
1118

    
1119

    
1120
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1121
    //                (getValue(sectionMap, "atomisedEpithets")));
1122
            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1123

    
1124
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1125
    //                (getValue(sectionMap,"author")));
1126
            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1127

    
1128
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1129
    //                (getValue(sectionMap, "nomRef")));
1130
            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1131

    
1132
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1133
    //                (getValue(sectionMap, "nomStat")));
1134
            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1135

    
1136

    
1137
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1138
    //                (getValue(sectionMap,"protologue")));
1139
            config.setProtologueActivated(getValue(sectionMap,"protologue"));
1140

    
1141
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1142
    //                (getValue(sectionMap,"typeDes")));
1143
            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1144

    
1145
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1146
    //                (getValue(sectionMap,"nameRelation")));
1147
            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1148

    
1149
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1150
    //                (getValue(sectionMap, "hybrid")));
1151
            config.setHybridActivated(getValue(sectionMap,"hybrid"));
1152
        }else{
1153
            config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION));
1154
            config.setTaxonSectionActivated(getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_TAXON));
1155
            config.setLSIDActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_LSID));
1156
            config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1157
            config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1158
            config.setRankActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_RANK));
1159
            config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1160
            config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1161
            config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1162
            config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1163
            config.setProtologueActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1164
            config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1165
            config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1166
            config.setHybridActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_HYBRID));
1167
        }
1168
        return config;
1169
    }
1170

    
1171
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1172
		if (sectionMap.containsKey(string)){
1173
			return sectionMap.get(string);
1174
		}else{
1175
			return true;
1176
		}
1177

    
1178
	}
1179

    
1180

    
1181

    
1182
    /**
1183
     * <p>
1184
     * setAbcdConfigurator
1185
     * </p>
1186
     *
1187
     * @param preferredConfiguration
1188
     *            a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1189
     *            object.
1190
     */
1191
    public static Abcd206ImportConfigurator getAbcdImportConfigurationPreference(boolean useLocal) {
1192

    
1193
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1194

    
1195
        if (useLocal){
1196
            config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1197

    
1198
            config.setAddMediaAsMediaSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1199

    
1200
            config.setAllowReuseOtherClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1201
            config.setDeduplicateClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1202
            config.setDeduplicateReferences(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1203

    
1204
            config.setGetSiblings(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1205
            config.setIgnoreAuthorship(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1206
            config.setIgnoreImportOfExistingSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1207
            config.setMapUnitIdToAccessionNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1208
            config.setMapUnitIdToBarcode(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1209
            config.setMapUnitIdToCatalogNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1210
            config.setMoveNewTaxaToDefaultClassification(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1211
            config.setOverwriteExistingSpecimens(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1212
            config.setReuseExistingDescriptiveGroups(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1213
            config.setReuseExistingMetaData(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1214
            config.setReuseExistingTaxaWhenPossible(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1215
            config.setNomenclaturalCode(NomenclaturalCode.getByKey(PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE)));
1216
            return config;
1217

    
1218
        }
1219
            ICdmRepository controller;
1220
            controller = CdmStore.getCurrentApplicationConfiguration();
1221
            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AbcdImportConfig);
1222
            CdmPreference preference = null;
1223
            if (controller == null){
1224
                return null;
1225
            }
1226
            preference = controller.getPreferenceService().find(key);
1227
            if (preference == null){
1228
                return config;
1229
             } else{
1230
             String configString = preference.getValue();
1231
             String[] configArray = configString.split(";");
1232

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

    
1273
            }
1274
        }
1275
        return config;
1276

    
1277

    
1278

    
1279
    }
1280

    
1281
    /**
1282
    *
1283
    */
1284
   public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1285
       NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1286
       if (local){
1287
          config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1288
          config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1289
          config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1290
          config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1291
          config.setLSIDActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1292
          config.setNameApprobiationActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1293
          config.setNameCacheActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1294
          config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1295
          config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1296
          config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1297
          config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1298
          config.setProtologueActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1299
          config.setRankActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1300
          config.setTaxonSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1301
          config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1302
          config.setHybridActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1303
       }else{
1304

    
1305
       }
1306

    
1307
      return config;
1308
   }
1309

    
1310

    
1311
   public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1312
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1313
               config.isSimpleDetailsViewActivated());
1314
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1315
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1316
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1317
               config.isNomenclaturalCodeActived());
1318
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1319
               config.isNameCacheActivated());
1320
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1321
               config.isAppendedPhraseActivated());
1322
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1323
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1324
               config.isAtomisedEpithetsActivated());
1325
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1326
               config.isAuthorCacheActivated());
1327
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1328
               config.isAuthorshipSectionActivated());
1329
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1330
               config.isNomenclaturalReferenceSectionActivated());
1331
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1332
               config.isNomenclaturalStatusSectionActivated());
1333
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1334
               config.isProtologueActivated());
1335
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1336
               config.isTypeDesignationSectionActivated());
1337
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1338
               config.isNameRelationsSectionActivated());
1339
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1340
               config.isHybridActivated());
1341

    
1342
   }
1343

    
1344
/**
1345
 * @return
1346
 */
1347
public static boolean isSortTaxaByRankAndName() {
1348

    
1349
    return getPreferenceStore().getBoolean(IPreferenceKeys.SORT_TAXA_BY_RANK_AND_NAME);
1350
}
1351

    
1352
/**
1353
 * @return
1354
 */
1355
public static boolean isSortNamedAreaByOrderInVocabulary() {
1356

    
1357
    return getPreferenceStore().getBoolean(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER);
1358
}
1359

    
1360
public static void setSortNamedAreasByOrderInVocabulary(boolean isSortByVocabularyOrder) {
1361
    getPreferenceStore().setValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER, isSortByVocabularyOrder);
1362

    
1363
}
1364

    
1365
/**
1366
 * <p>
1367
 * setPreferredNomenclaturalCode
1368
 * </p>
1369
 *
1370
 * @param preferredCode
1371
 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
1372
 *            object.
1373
 */
1374
public static CdmPreference setPreferredNamedAreasForDistributionEditor(
1375
        String saveCheckedElements, String saveGrayedElements, boolean local) {
1376
    if (local){
1377
        IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1378
        preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS, saveCheckedElements);
1379
        preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS_GRAYED, saveGrayedElements);
1380

    
1381
    }
1382
    else{
1383
        ICdmRepository controller;
1384

    
1385
//        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms);
1386
        CdmPreference preference = null;
1387

    
1388
        if (saveCheckedElements == null){
1389
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaTerms);
1390
//            preference = controller.getPreferenceService().find(key);
1391
            if (preference == null){
1392
                return null;
1393
            } else{
1394
                String[] uuids = StringUtils.splitByWholeSeparator(saveCheckedElements, ",");
1395
                List<UUID> uuidList = new ArrayList();
1396
                for (String uuidString: uuids){
1397
                    uuidList.add(UUID.fromString(uuidString.trim()));
1398
                }
1399
                getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1400
                        saveCheckedElements);
1401

    
1402
                return preference;
1403
            }
1404
        } else{
1405
            controller = CdmStore.getCurrentApplicationConfiguration();
1406
            preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1407
            controller.getPreferenceService().set(preference);
1408
            getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1409
                    saveCheckedElements);
1410

    
1411
        }
1412
    }
1413
    return null;
1414

    
1415

    
1416

    
1417
}
1418

    
1419

    
1420

    
1421

    
1422

    
1423
}
(19-19/24)