Project

General

Profile

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

    
10
package eu.etaxonomy.taxeditor.preference;
11

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

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

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

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

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

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

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

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

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

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

    
144
    		}
145
	    }
146
        return null;
147

    
148

    
149

    
150
	}
151

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

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

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

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

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

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

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

    
197
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
198
        }
199
    }
200

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

    
207
            return pref!=null?Boolean.valueOf(pref.getValue()):false;
208
        }
209
    }
210

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

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

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

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

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

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

    
241

    
242

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

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

    
257
        return pref;
258

    
259
    }
260

    
261

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

    
270
	public static boolean isShowMediaPreview(){
271
        boolean isShowMediaPreview = getPreferenceStore().getBoolean(SHOW_MEDIA_PREVIEW);
272
        return isShowMediaPreview;
273
    }
274

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

    
288
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
289
				String matchModeName = getPreferenceStore().getString(
290
						getMatchStrategyFieldName(className, fieldName));
291
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
292
				try {
293
					matchStrategy.setMatchMode(fieldName, matchMode);
294
				} catch (MatchException e) {
295
					MessagingUtils.error(PreferencesUtil.class, e);
296
					throw new RuntimeException(e);
297
				}
298
			}
299

    
300
			return matchStrategy;
301
		}
302
		return getDefaultMatchStrategy(clazz);
303
	}
304

    
305
	/**
306
	 * Stores a matchStrategy into the preference store.
307
	 *
308
	 * @param matchStrategy
309
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
310
	 *            object.
311
	 */
312
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
313
		String className = matchStrategy.getMatchClass().getName();
314
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
315

    
316
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
317

    
318
		for (String fieldName : matchFields) {
319
			getPreferenceStore().setValue(
320
					getMatchStrategyFieldName(className, fieldName),
321
					matchStrategy.getMatchMode(fieldName).name());
322
		}
323
	}
324

    
325
	/**
326
	 * Helper method to create the preference property for a match field.
327
	 *
328
	 * @param className
329
	 * @param fieldName
330
	 * @return
331
	 */
332
	private static String getMatchStrategyFieldName(String className,
333
			String fieldName) {
334
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
335
	}
336

    
337
	/**
338
	 * Returns the default match strategy for a given class.
339
	 *
340
	 * @param clazz
341
	 *            a {@link java.lang.Class} object.
342
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
343
	 */
344
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
345
		return DefaultMatchStrategy.NewInstance(clazz);
346
	}
347

    
348
	/**
349
	 * <p>
350
	 * getDateFormatPattern
351
	 * </p>
352
	 *
353
	 * @return a {@link java.lang.String} object.
354
	 */
355
	public static String getDateFormatPattern() {
356
		// TODO make this configurable in properties
357
		String pattern = "Y-M-d H:m";
358
		return pattern;
359
	}
360

    
361
	/**
362
	 * <p>
363
	 * addTermToPreferredTerms
364
	 * </p>
365
	 *
366
	 * @param term
367
	 *            a T object.
368
	 * @param <T>
369
	 *            a T object.
370
	 */
371
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
372

    
373
		// VocabularyEnum vocabulary =
374
		// VocabularyEnum.getVocabularyEnum(term.getClass());
375
		//
376
		// getPreferenceStore().setValue(getPreferenceKey(term),
377
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
378
		//
379
		// firePreferencesChanged(term.getClass());
380
	}
381

    
382
	/**
383
	 * Construct a unique key using the CdmBase object's uuid
384
	 *
385
	 * @param cdmBase
386
	 * @return
387
	 */
388
	private static String getPreferenceKey(ICdmBase cdmBase) {
389
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
390

    
391
		String key = cdmBase.getClass().getName().concat(".")
392
				.concat(cdmBase.getUuid().toString());
393
		if (key.contains("javassist")) {
394
			MessagingUtils.info("proxy");
395
		}
396
		return key;
397
	}
398

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

    
416

    
417

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

    
435
	/**
436
	 * Retrieves search preferences from the preference store
437
	 *
438
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
439
	 */
440
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
441
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
442

    
443
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
444
				TAXON_SERVICE_CONFIGURATOR_TAXA));
445
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
446
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
447
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
448
				TAXON_SERVICE_CONFIGURATOR_NAMES));
449
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
450
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
451
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
452

    
453
		return configurator;
454
	}
455

    
456
	/**
457
	 * create new preferences, setting all search options to true
458
	 *
459
	 * @return a
460
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
461
	 *         object.
462
	 */
463
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
464
		IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
465

    
466
		configurator.setDoTaxa(true);
467
		configurator.setDoSynonyms(true);
468
		configurator.setDoNamesWithoutTaxa(true);
469
		configurator.setDoTaxaByCommonNames(true);
470

    
471
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
472
				"name", "name.$", "relationsFromThisTaxon.$"));
473

    
474
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
475
				"name", "name.$", "synonymRelations.relatedTo.*"));
476

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

    
487
		return configurator;
488
	}
489

    
490
	/**
491
	 * Store search preferences
492
	 *
493
	 * @param configurator
494
	 *            a
495
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
496
	 *            object.
497
	 */
498
	public static void setSearchConfigurator(
499
			IFindTaxaAndNamesConfigurator configurator) {
500
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
501
				configurator.isDoTaxa());
502
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
503
				configurator.isDoSynonyms());
504
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
505
				configurator.isDoNamesWithoutTaxa());
506
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
507
				configurator.isDoTaxaByCommonNames());
508
	}
509

    
510
	/**
511
	 * <p>
512
	 * firePreferencesChanged
513
	 * </p>
514
	 *
515
	 * @param clazz
516
	 *            a {@link java.lang.Class} object.
517
	 */
518
	public static void firePreferencesChanged(Class clazz) {
519
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
520
				null, clazz);
521
	}
522

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

    
557
	}
558

    
559
	/**
560
	 * <p>
561
	 * checkNomenclaturalCode
562
	 * </p>
563
	 */
564
	public static void checkNomenclaturalCode() {
565
		// First time Editor is opened, no nomenclatural code has been set
566

    
567

    
568
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
569
			PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP, true);
570
		}
571

    
572

    
573

    
574
	}
575
	public static void setNomenclaturalCodePreferences(){
576
	    ICdmRepository controller;
577
        controller = CdmStore.getCurrentApplicationConfiguration();
578
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
579
        CdmPreference preference = null;
580
        if (controller == null){
581
            return ;
582
        }
583
        preference = controller.getPreferenceService().find(key);
584
        if (preference == null){
585
            return;
586
        }
587
        getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
588

    
589
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
590
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
591
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
592

    
593
        getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
594
                getPreferenceKey(preferredCode));
595

    
596
        if (!preference.isAllowOverride()){
597
            getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
598
                    getPreferenceKey(preferredCode));
599
        }
600
	}
601

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

    
615
	/**
616
	 * <p>
617
	 * getMapServiceAccessPoint
618
	 * </p>
619
	 *
620
	 * @return a {@link java.lang.String} object.
621
	 */
622
	public static String getMapServiceAccessPoint() {
623
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
624
	}
625

    
626
	/**
627
	 * <p>
628
	 * shouldConnectAtStartUp
629
	 * </p>
630
	 *
631
	 * @return a boolean.
632
	 */
633
	public static boolean shouldConnectAtStartUp() {
634
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
635
		//return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
636
		return false;
637
	}
638

    
639
	/**
640
	 * <p>
641
	 * getDefaultFeatureTreeForTextualDescription
642
	 * </p>
643
	 *
644
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
645
	 */
646
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
647
		String uuidString = getPreferenceStore().getString(
648
				FEATURE_TREE_DEFAULT_TEXT);
649
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
650
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
651
	}
652

    
653
	/**
654
	 * <p>
655
	 * getDefaultFeatureTreeForStructuredDescription
656
	 * </p>
657
	 *
658
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
659
	 */
660
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
661
		String uuidString = getPreferenceStore().getString(
662
				FEATURE_TREE_DEFAULT_STRUCTURE);
663
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
664
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
665
	}
666

    
667
	/**
668
	 * <p>
669
	 * setSortRanksHierarchichally
670
	 * </p>
671
	 *
672
	 * @param selection
673
	 *            a boolean.
674
	 */
675
	public static void setSortRanksHierarchichally(boolean selection) {
676
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
677
	}
678

    
679
	/**
680
	 * <p>
681
	 * getSortRanksHierarchichally
682
	 * </p>
683
	 *
684
	 * @return a boolean.
685
	 */
686
	public static boolean getSortRanksHierarchichally() {
687
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
688
	}
689

    
690
	public static boolean isMultilanguageTextEditingCapability() {
691
		return getPreferenceStore().getBoolean(
692
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
693
	}
694

    
695
	public static Language getGlobalLanguage() {
696

    
697

    
698
		String languageUuidString = getPreferenceStore().getString(
699
				GLOBAL_LANGUAGE_UUID);
700

    
701
		if(!CdmStore.isActive()) {
702
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
703
            return null;
704
        }
705

    
706
		if (CdmUtils.isBlank(languageUuidString)) {
707
			return Language.getDefaultLanguage();
708
		}
709

    
710
		UUID languageUuid = UUID.fromString(languageUuidString);
711
		return (Language) CdmStore.getService(ITermService.class).load(
712
				languageUuid);
713
	}
714

    
715
	public static void setGlobalLanguage(Language language) {
716
	    if(language != null) {
717
	        getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
718
	        CdmStore.setDefaultLanguage(language);
719
	    }
720

    
721
	}
722

    
723
	/**
724
	 * @return
725
	 */
726
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
727
		List<MarkerType> markerTypes = CdmStore.getTermManager()
728
				.getPreferredTerms(MarkerType.class);
729

    
730
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
731

    
732
		for (MarkerType markerType : markerTypes) {
733
			String name = getMarkerTypeEditingPreferenceKey(markerType);
734
			Boolean value = getPreferenceStore().getBoolean(name);
735

    
736
			result.put(markerType, value);
737
		}
738

    
739
		return result;
740
	}
741

    
742
	/**
743
	 * @param markerTypeEditingMap
744
	 */
745
	public static void setEditMarkerTypePreferences(
746
			Map<MarkerType, Boolean> markerTypeEditingMap) {
747
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
748
			String name = getMarkerTypeEditingPreferenceKey(markerType);
749
			getPreferenceStore().setValue(name,
750
					markerTypeEditingMap.get(markerType));
751
		}
752

    
753
	}
754

    
755
	private static String getMarkerTypeEditingPreferenceKey(
756
			MarkerType markerType) {
757
		markerType = HibernateProxyHelper.deproxy(markerType);
758
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
759
	}
760

    
761
	/**
762
	 * <p>
763
	 * setEditMarkerTypePreference
764
	 * </p>
765
	 *
766
	 * @param input
767
	 *            a {@link org.eclipse.ui.IEditorInput} object.
768
	 * @param markerType
769
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
770
	 * @param edit
771
	 *            a boolean.
772
	 */
773
	public static void setEditMarkerTypePreference(MarkerType markerType,
774
			boolean edit) {
775
		getPreferenceStore().setValue(
776
				getMarkerTypeEditingPreferenceKey(markerType), edit);
777
	}
778

    
779
	/**
780
	 * @return
781
	 */
782
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
783
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
784
				.NewInstance();
785
		configurator.setMoveDerivedUnitMediaToGallery(true);
786
		configurator.setMoveFieldObjectMediaToGallery(true);
787
		return configurator;
788
	}
789

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

    
819
    /**
820
     * This method loads a property from a given file and returns it.
821
     *
822
     * @param filename
823
     * @return
824
     * @throws IOException
825
     */
826
    private Properties load(String filename) throws IOException {
827
        FileInputStream in = new FileInputStream(filename);
828
        Properties prop = new Properties();
829
        prop.load(in);
830
        in.close();
831
        return prop;
832
    }
833

    
834
    /**
835
     * This method saves a property to the specified file.
836
     *
837
     * @param filename
838
     * @param properties
839
     * @throws IOException
840
     */
841
    private void save(String filename, Properties properties) throws IOException{
842
        FileOutputStream fos =  new FileOutputStream(filename);
843
        properties.store(fos, "");
844
        fos.close();
845
    }
846

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

    
869

    
870
    /**
871
     * Retrieves a list of previously saved P2 repositories
872
     *
873
     * @return
874
     */
875
    public static List<MetadataRepositoryElement> getP2Repositories() {
876
        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
877
        String p2ReposPref =  getPreferenceStore().getString(P2_REPOSITORY_LIST);
878
        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
879
            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
880

    
881
            while(p2ReposPrefST.hasMoreTokens()) {
882
                String p2RepoStr = p2ReposPrefST.nextToken();
883
                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
884
                if(p2ReposStrST.countTokens()==3) {
885
                    String nickname = p2ReposStrST.nextToken();
886
                    URI uri = null;
887
                    try {
888
                        uri = new URI(p2ReposStrST.nextToken());
889
                    } catch (URISyntaxException e) {
890
                        continue;
891
                    }
892
                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
893
                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
894
                    mre.setNickname(nickname);
895
                    mre.setEnabled(enabled);
896
                    p2Repos.add(mre);
897
                }
898
            }
899
        }
900

    
901
        return p2Repos;
902
    }
903

    
904
    /**
905
     * enables/disables nested composite. <br>
906
     *
907
     * @param ctrl - Composite to be en-/disabeld
908
     * @param enabled - boolean
909
     */
910
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
911
        if (ctrl instanceof Composite) {
912
            Composite comp = (Composite) ctrl;
913
            for (Control c : comp.getChildren()) {
914
                recursiveSetEnabled(c, enabled);
915
            }
916
        } else {
917
            ctrl.setEnabled(enabled);
918
        }
919
    }
920

    
921
    /**
922
	 * <p>
923
	 * getSortRanksNaturally
924
	 * </p>
925
    	 *
926
    	 * @return a boolean.
927
	 */
928
	public static boolean getSortNodesNaturally() {
929
		return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
930
	}
931

    
932
	/**
933
	 * <p>
934
	 * setSortRanksNaturally
935
	 * </p>
936
	 *
937
	 * @param selection
938
	 *            a boolean.
939
	 */
940
	public static void setSortNodesNaturally(boolean selection) {
941
		getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
942
	}
943

    
944

    
945
	/**
946
	 * <p>
947
	 * getSortRanksNaturally
948
	 * </p>
949
	 *
950
	 * @return a boolean.
951
	 */
952
	public static boolean getSortNodesStrictlyAlphabetically() {
953
		return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
954
	}
955

    
956
	/**
957
	 * <p>
958
	 * setSortRanksNaturally
959
	 * </p>
960
	 *
961
	 * @param selection
962
	 *            a boolean.
963
	 */
964
	public static void setSortNodesStrictlyAlphabetically(boolean selection) {
965
		getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
966
	}
967

    
968
	/**
969
	 * <p>
970
	 * setStoreNavigatorState
971
	 * </p>
972
	 *
973
	 * @param selection
974
	 *            a boolean.
975
	 */
976
	public static boolean isStoreNavigatorState() {
977
		return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
978

    
979
	}
980

    
981
	/**
982
	 * <p>
983
	 * setStoreNavigatorState
984
	 * </p>
985
	 *
986
	 * @param selection
987
	 *            a boolean.
988
	 */
989
	public static void setStoreNavigatorState(boolean selection) {
990
		getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
991

    
992
	}
993

    
994
    /**
995
     * @return
996
     */
997
    public static boolean isShowUpWidgetIsDisposedMessages() {
998
       return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
999
    }
1000
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
1001
        getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
1002
    }
1003

    
1004
    /**
1005
     * @return
1006
     */
1007
    public static boolean isShowIdInVocabularyInChecklistEditor() {
1008
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
1009
    }
1010
    public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
1011
        getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
1012
    }
1013

    
1014
    /**
1015
     * @return
1016
     */
1017
    public static boolean isShowSymbolInChecklistEditor() {
1018
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SYMBOL);
1019
    }
1020
    public static void setShowSymbolInChecklistEditor(boolean selection) {
1021
        getPreferenceStore().setValue(CHECKLIST_SYMBOL, selection);
1022
    }
1023

    
1024
    /**
1025
     * @return
1026
     */
1027
    public static boolean isShowRankInChecklistEditor() {
1028
        return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
1029
    }
1030
    public static void setShowRankInChecklistEditor(boolean selection) {
1031
        getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
1032
    }
1033

    
1034
    /**
1035
     * @param object
1036
     * @param b
1037
     * @return
1038
     */
1039
    public static NameDetailsConfigurator setPreferredNameDetailsConfiguration( boolean local) {
1040
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1041

    
1042
        CdmPreference preference = null;
1043

    
1044
        if (!local) {
1045
            preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1046
        }
1047
        if (preference == null){
1048
            return null;
1049
        }
1050

    
1051
        getPreferenceStore().setValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1052

    
1053
        //the preference value is build like this:
1054
        //<section1>:true;<section2>:false....
1055

    
1056
        String value = preference.getValue();
1057
        String [] sections = value.split(";");
1058
        Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1059
        String[] sectionValues;
1060
        for (String sectionValue: sections){
1061
            sectionValues = sectionValue.split(":");
1062
            sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1063
        }
1064

    
1065
        config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1066

    
1067
//        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1068
//                (getValue(sectionMap, "taxon")));
1069
        config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1070

    
1071
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1072
//                (getValue(sectionMap, "lsid")));
1073
        config.setLSIDActivated(getValue(sectionMap, "lsid"));
1074

    
1075
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1076
//                (getValue(sectionMap, "nc")));
1077
        config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1078

    
1079
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1080
//                (getValue(sectionMap, "ap")));
1081
        config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1082

    
1083
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1084
//                (getValue(sectionMap, "rank")));
1085
        config.setRankActivated(getValue(sectionMap, "rank"));
1086

    
1087

    
1088
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1089
//                (getValue(sectionMap, "atomisedEpithets")));
1090
        config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1091

    
1092
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1093
//                (getValue(sectionMap,"author")));
1094
        config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1095

    
1096
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1097
//                (getValue(sectionMap, "nomRef")));
1098
        config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1099

    
1100
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1101
//                (getValue(sectionMap, "nomStat")));
1102
        config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1103

    
1104

    
1105
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1106
//                (getValue(sectionMap,"protologue")));
1107
        config.setProtologueActivated(getValue(sectionMap,"protologue"));
1108

    
1109
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1110
//                (getValue(sectionMap,"typeDes")));
1111
        config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1112

    
1113
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1114
//                (getValue(sectionMap,"nameRelation")));
1115
        config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1116

    
1117
//        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1118
//                (getValue(sectionMap, "hybrid")));
1119
        config.setHybridActivated(getValue(sectionMap,"hybrid"));
1120

    
1121
        return config;
1122
    }
1123

    
1124
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1125
		if (sectionMap.containsKey(string)){
1126
			return sectionMap.get(string);
1127
		}else{
1128
			return true;
1129
		}
1130

    
1131
	}
1132

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

    
1169
//    public static boolean getBioCaseProvider(boolean local){
1170
//        CdmPreference preference = null;
1171
//
1172
//        if (!local) {
1173
//            preference = getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
1174
//        }
1175
//        if (preference == null){
1176
//            return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
1177
//        }
1178
//        return Boolean.valueOf(preference.getValue());
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
            return config;
1216

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

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

    
1270
            }
1271
        }
1272
        return config;
1273

    
1274

    
1275

    
1276
    }
1277

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

    
1302
       }
1303

    
1304
      return config;
1305
   }
1306

    
1307

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

    
1339
   }
1340

    
1341

    
1342

    
1343
}
(19-19/25)