Project

General

Profile

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

    
10
package eu.etaxonomy.taxeditor.preference;
11

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

    
25
import org.apache.commons.lang.StringUtils;
26
import org.apache.log4j.Logger;
27
import org.eclipse.jface.preference.IPreferenceStore;
28
import org.eclipse.jface.window.Window;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.Control;
31
import org.eclipse.swt.widgets.Shell;
32
import org.eclipse.ui.PlatformUI;
33

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

    
66
/**
67
 * <p>
68
 * PreferencesUtil class.
69
 * </p>
70
 *
71
 * @author p.ciardelli
72
 * @author n.hoffmann
73
 * @created 05.12.2008
74
 * @version 1.0
75
 */
76
public class PreferencesUtil implements IPreferenceKeys {
77

    
78
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
79

    
80
	private final static Logger logger = Logger.getLogger(PreferencesUtil.class);
81

    
82
	/**
83
	 * <p>
84
	 * getPreferenceStore
85
	 * </p>
86
	 *
87
	 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
88
	 */
89
	public static IPreferenceStore getPreferenceStore() {
90
		return TaxeditorStorePlugin.getDefault().getPreferenceStore();
91
	}
92

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

    
130
    				getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
131
    				        preferenceKey);
132
    				getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,preference.isAllowOverride());
133
    				return preference;
134
    			}
135
    		} else{
136
    			preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferenceKey);
137
    			controller.getPreferenceService().set(preference);
138

    
139
    		}
140
	    }
141
        return null;
142

    
143

    
144

    
145
	}
146

    
147
	public static NomenclaturalCode getPreferredNomenclaturalCode(){
148
	    if (getPreferenceStore().getBoolean(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
149
            return getPreferredNomenclaturalCode(true);
150
        } else{
151
            return getPreferredNomenclaturalCode(false);
152
        }
153
	}
154

    
155
	/**
156
	 * <p>
157
	 * getPreferredNomenclaturalCode
158
	 * </p>
159
	 *
160
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
161
	 */
162
	public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
163

    
164
		CdmPreference pref = null;
165
		if (!useLocal){
166
		    pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
167
		}
168

    
169
	    String preferredCode;
170
	    if(pref == null){
171
	    	preferredCode = getPreferenceStore().getString(
172
					PREFERRED_NOMENCLATURAL_CODE_KEY);
173

    
174
	    }else{
175
	    	preferredCode = pref.getValue();
176
	    }
177

    
178
	    return getPreferredNomenclaturalCode(preferredCode);
179

    
180
	}
181

    
182
	/**
183
     * <p>
184
     * getPreferredNomenclaturalCode
185
     * </p>
186
     *
187
     * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
188
     */
189
    public static NomenclaturalCode getPreferredNomenclaturalCode(String preferenceKeyNomenclaturalCode) {
190

    
191
        for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
192
            if (getPreferenceKey(code).equals(preferenceKeyNomenclaturalCode)) {
193
                return code;
194
            }
195
        }
196
        return null;
197
    }
198

    
199
	public static boolean isShowTaxonAssociations(){
200
        if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
201
            return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
202
        } else{
203
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
204

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

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

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

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

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

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

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

    
239
	public static boolean isShowSimpleDetailsView(){
240
        if (getPreferenceStore().getBoolean(OVERRIDE_NAME_DETAILS) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_NAME_DETAILS)) {
241
            return getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION);
242
        } else{
243
            CdmPreference pref = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
244

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

    
249

    
250

    
251
    public static CdmPreference getPreferenceFromDB(PreferencePredicate predicate){
252
        ICdmRepository controller;
253
        CdmPreference pref = null;
254

    
255
        try{
256
            if(CdmStore.isActive()){
257
                controller = CdmStore.getCurrentApplicationConfiguration();
258
                PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), predicate);
259
                pref = controller.getPreferenceService().find(key);
260
            }
261
        }catch(Exception e){
262
            e.printStackTrace();
263
        }
264

    
265
        return pref;
266

    
267
    }
268

    
269
    public static void setPreferenceToDB(CdmPreference preference){
270
        ICdmRepository controller;
271
        try{
272
            if(CdmStore.isActive()){
273
                controller = CdmStore.getCurrentApplicationConfiguration();
274
                controller.getPreferenceService().set(preference);
275
            }
276
        }catch(Exception e){
277
            e.printStackTrace();
278
        }
279

    
280
    }
281

    
282

    
283

    
284
	public static String getPreferredDefaultLangugae(){
285
	    String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
286
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
287
	        return preferredLanguage;
288
	    }
289
	    return null;
290
	}
291

    
292
	public static boolean isShowMediaPreview(){
293
        boolean isShowMediaPreview = getPreferenceStore().getBoolean(SHOW_MEDIA_PREVIEW);
294
        return isShowMediaPreview;
295
    }
296

    
297
	/**
298
	 * Get the match strategy for the given class that was stored in preferences
299
	 * or the default strategy if it was not stored in preferences
300
	 *
301
	 * @param clazz
302
	 *            a {@link java.lang.Class} object.
303
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
304
	 */
305
	public static IMatchStrategy getMatchStrategy(Class clazz) {
306
		String className = clazz.getName();
307
		if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
308
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
309

    
310
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
311
				String matchModeName = getPreferenceStore().getString(
312
						getMatchStrategyFieldName(className, fieldName));
313
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
314
				try {
315
					matchStrategy.setMatchMode(fieldName, matchMode);
316
				} catch (MatchException e) {
317
					MessagingUtils.error(PreferencesUtil.class, e);
318
					throw new RuntimeException(e);
319
				}
320
			}
321

    
322
			return matchStrategy;
323
		}
324
		return getDefaultMatchStrategy(clazz);
325
	}
326

    
327
	/**
328
	 * Stores a matchStrategy into the preference store.
329
	 *
330
	 * @param matchStrategy
331
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
332
	 *            object.
333
	 */
334
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
335
		String className = matchStrategy.getMatchClass().getName();
336
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
337

    
338
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
339

    
340
		for (String fieldName : matchFields) {
341
			getPreferenceStore().setValue(
342
					getMatchStrategyFieldName(className, fieldName),
343
					matchStrategy.getMatchMode(fieldName).name());
344
		}
345
	}
346

    
347
	/**
348
	 * Helper method to create the preference property for a match field.
349
	 *
350
	 * @param className
351
	 * @param fieldName
352
	 * @return
353
	 */
354
	private static String getMatchStrategyFieldName(String className,
355
			String fieldName) {
356
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
357
	}
358

    
359
	/**
360
	 * Returns the default match strategy for a given class.
361
	 *
362
	 * @param clazz
363
	 *            a {@link java.lang.Class} object.
364
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
365
	 */
366
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
367
		return DefaultMatchStrategy.NewInstance(clazz);
368
	}
369

    
370
	/**
371
	 * <p>
372
	 * getDateFormatPattern
373
	 * </p>
374
	 *
375
	 * @return a {@link java.lang.String} object.
376
	 */
377
	public static String getDateFormatPattern() {
378
		// TODO make this configurable in properties
379
		String pattern = "Y-M-d H:m";
380
		return pattern;
381
	}
382

    
383
	/**
384
	 * <p>
385
	 * addTermToPreferredTerms
386
	 * </p>
387
	 *
388
	 * @param term
389
	 *            a T object.
390
	 * @param <T>
391
	 *            a T object.
392
	 */
393
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
394

    
395
		// VocabularyEnum vocabulary =
396
		// VocabularyEnum.getVocabularyEnum(term.getClass());
397
		//
398
		// getPreferenceStore().setValue(getPreferenceKey(term),
399
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
400
		//
401
		// firePreferencesChanged(term.getClass());
402
	}
403

    
404
	/**
405
	 * Construct a unique key using the CdmBase object's uuid
406
	 *
407
	 * @param cdmBase
408
	 * @return
409
	 */
410
	private static String getPreferenceKey(ICdmBase cdmBase) {
411
		cdmBase = HibernateProxyHelper.deproxy(cdmBase);
412

    
413
		String key = cdmBase.getClass().getName().concat(".")
414
				.concat(cdmBase.getUuid().toString());
415
		if (key.contains("javassist")) {
416
			MessagingUtils.info("proxy");
417
		}
418
		return key;
419
	}
420

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

    
438

    
439

    
440
	/**
441
	 * Construct a unique key using the CdmBase object's uuid
442
	 *
443
	 * @param cdmBase
444
	 * @return
445
	 */
446
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
447
		definedTerm = HibernateProxyHelper.deproxy(definedTerm);
448
		String key = definedTerm.getClass().getName().concat(".")
449
				.concat(definedTerm.getUuid().toString());
450
		if (key.contains("javassist")) {
451
			MessagingUtils.warn(PreferencesUtil.class,
452
					"Trying to persist a preference based on a proxy class.");
453
		}
454
		return key;
455
	}
456

    
457
	/**
458
	 * Retrieves search preferences from the preference store
459
	 *
460
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
461
	 */
462
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
463
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
464

    
465
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
466
				TAXON_SERVICE_CONFIGURATOR_TAXA));
467
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
468
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
469
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
470
				TAXON_SERVICE_CONFIGURATOR_NAMES));
471
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
472
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
473
		//configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
474

    
475
		return configurator;
476
	}
477

    
478
	/**
479
	 * create new preferences, setting all search options to true
480
	 *
481
	 * @return a
482
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
483
	 *         object.
484
	 */
485
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
486
		IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
487

    
488
		configurator.setDoTaxa(true);
489
		configurator.setDoSynonyms(true);
490
		configurator.setDoNamesWithoutTaxa(true);
491
		configurator.setDoTaxaByCommonNames(true);
492

    
493
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
494
				"name", "name.$", "relationsFromThisTaxon.$"));
495

    
496
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
497
				"name", "name.$", "synonyms.relatedTo.*"));
498

    
499
		// DEFAULT VALUES
500
		// match mode is a simple like, actually all other match modes are kind
501
		// of bogus
502
		configurator
503
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
504
		// we set page number and size here as this should always be unlimited
505
		configurator.setPageNumber(0);
506
		// TODO currently limit results to 10000
507
		configurator.setPageSize(10000);
508

    
509
		return configurator;
510
	}
511

    
512
	/**
513
	 * Store search preferences
514
	 *
515
	 * @param configurator
516
	 *            a
517
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
518
	 *            object.
519
	 */
520
	public static void setSearchConfigurator(
521
			IFindTaxaAndNamesConfigurator configurator) {
522
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
523
				configurator.isDoTaxa());
524
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
525
				configurator.isDoSynonyms());
526
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
527
				configurator.isDoNamesWithoutTaxa());
528
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
529
				configurator.isDoTaxaByCommonNames());
530
	}
531

    
532
	/**
533
	 * <p>
534
	 * firePreferencesChanged
535
	 * </p>
536
	 *
537
	 * @param clazz
538
	 *            a {@link java.lang.Class} object.
539
	 */
540
	public static void firePreferencesChanged(Class clazz) {
541
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
542
				null, clazz);
543
	}
544

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

    
580
	}
581

    
582
	/**
583
	 * <p>
584
	 * checkNomenclaturalCode
585
	 * </p>
586
	 */
587
	public static void checkNomenclaturalCode() {
588
		// First time Editor is opened, no nomenclatural code has been set
589

    
590

    
591
		if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
592
			PreferencesUtil.setPreferredNomenclaturalCode(getPreferenceKey(NomenclaturalCode.ICNAFP), true);
593
		}
594

    
595

    
596

    
597
	}
598
	public static void setNomenclaturalCodePreferences(){
599
	    ICdmRepository controller;
600
        controller = CdmStore.getCurrentApplicationConfiguration();
601
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
602
        CdmPreference preference = null;
603
        if (controller == null){
604
            return ;
605
        }
606
        preference = controller.getPreferenceService().find(key);
607
        if (preference == null){
608
            return;
609
        }
610
        getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
611

    
612
        int index = StringUtils.lastIndexOf(preference.getValue(), ".");
613
        UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
614
        NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
615

    
616
        getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
617
                getPreferenceKey(preferredCode));
618

    
619
        if (!preference.isAllowOverride()){
620
            getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
621
                    getPreferenceKey(preferredCode));
622
        }
623
	}
624

    
625
	public static void checkDefaultLanguage(){
626
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
627
	       Shell shell = AbstractUtility.getShell();
628
	       int open = new DefaultLanguageDialog(shell).open();
629
	       if(open == Window.OK){
630
	           PlatformUI.getWorkbench().restart();
631
	       }
632
	    }else{
633
	        //TODO:In case of a reinstall, the config.ini will be overwritten
634
	        //     here you create config.ini with the stored key from preferences
635
	    }
636
	}
637

    
638
	/**
639
	 * <p>
640
	 * getMapServiceAccessPoint
641
	 * </p>
642
	 *
643
	 * @return a {@link java.lang.String} object.
644
	 */
645
	public static String getMapServiceAccessPoint() {
646
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
647
	}
648

    
649
	/**
650
	 * <p>
651
	 * shouldConnectAtStartUp
652
	 * </p>
653
	 *
654
	 * @return a boolean.
655
	 */
656
	public static boolean shouldConnectAtStartUp() {
657
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
658
		//return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
659
		return false;
660
	}
661

    
662
	/**
663
	 * <p>
664
	 * getDefaultFeatureTreeForTextualDescription
665
	 * </p>
666
	 *
667
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
668
	 */
669
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
670
		String uuidString = getPreferenceStore().getString(
671
				FEATURE_TREE_DEFAULT_TEXT);
672
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
673
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
674
	}
675

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

    
690
	/**
691
	 * <p>
692
	 * setSortRanksHierarchichally
693
	 * </p>
694
	 *
695
	 * @param selection
696
	 *            a boolean.
697
	 */
698
	public static void setSortRanksHierarchichally(boolean selection) {
699
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
700
	}
701

    
702
	/**
703
	 * <p>
704
	 * getSortRanksHierarchichally
705
	 * </p>
706
	 *
707
	 * @return a boolean.
708
	 */
709
	public static boolean getSortRanksHierarchichally() {
710
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
711
	}
712

    
713
	public static boolean isMultilanguageTextEditingCapability() {
714
		return getPreferenceStore().getBoolean(
715
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
716
	}
717

    
718
	public static Language getGlobalLanguage() {
719

    
720

    
721
		String languageUuidString = getPreferenceStore().getString(
722
				GLOBAL_LANGUAGE_UUID);
723

    
724
		if(!CdmStore.isActive()) {
725
            MessagingUtils.noDataSourceWarningDialog(languageUuidString);
726
            return null;
727
        }
728

    
729
		if (CdmUtils.isBlank(languageUuidString)) {
730
			return Language.getDefaultLanguage();
731
		}
732

    
733
		UUID languageUuid = UUID.fromString(languageUuidString);
734
		return (Language) CdmStore.getService(ITermService.class).load(
735
				languageUuid);
736
	}
737

    
738
	public static void setGlobalLanguage(Language language) {
739
	    if(language != null) {
740
	        getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
741
	        CdmStore.setDefaultLanguage(language);
742
	    }
743

    
744
	}
745

    
746
	/**
747
	 * @return
748
	 */
749
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
750
		List<MarkerType> markerTypes = CdmStore.getTermManager()
751
				.getPreferredTerms(MarkerType.class);
752

    
753
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
754

    
755
		for (MarkerType markerType : markerTypes) {
756
			String name = getMarkerTypeEditingPreferenceKey(markerType);
757
			Boolean value = getPreferenceStore().getBoolean(name);
758

    
759
			result.put(markerType, value);
760
		}
761

    
762
		return result;
763
	}
764

    
765
	/**
766
	 * @param markerTypeEditingMap
767
	 */
768
	public static void setEditMarkerTypePreferences(
769
			Map<MarkerType, Boolean> markerTypeEditingMap) {
770
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
771
			String name = getMarkerTypeEditingPreferenceKey(markerType);
772
			getPreferenceStore().setValue(name,
773
					markerTypeEditingMap.get(markerType));
774
		}
775

    
776
	}
777

    
778
	private static String getMarkerTypeEditingPreferenceKey(
779
			MarkerType markerType) {
780
		markerType = HibernateProxyHelper.deproxy(markerType);
781
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
782
	}
783

    
784
	/**
785
	 * <p>
786
	 * setEditMarkerTypePreference
787
	 * </p>
788
	 *
789
	 * @param input
790
	 *            a {@link org.eclipse.ui.IEditorInput} object.
791
	 * @param markerType
792
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
793
	 * @param edit
794
	 *            a boolean.
795
	 */
796
	public static void setEditMarkerTypePreference(MarkerType markerType,
797
			boolean edit) {
798
		getPreferenceStore().setValue(
799
				getMarkerTypeEditingPreferenceKey(markerType), edit);
800
	}
801

    
802
	/**
803
	 * @return
804
	 */
805
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
806
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
807
				.NewInstance();
808
		configurator.setMoveDerivedUnitMediaToGallery(true);
809
		configurator.setMoveFieldObjectMediaToGallery(true);
810
		return configurator;
811
	}
812

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

    
842
    /**
843
     * This method loads a property from a given file and returns it.
844
     *
845
     * @param filename
846
     * @return
847
     * @throws IOException
848
     */
849
    private Properties load(String filename) throws IOException {
850
        FileInputStream in = new FileInputStream(filename);
851
        Properties prop = new Properties();
852
        prop.load(in);
853
        in.close();
854
        return prop;
855
    }
856

    
857
    /**
858
     * This method saves a property to the specified file.
859
     *
860
     * @param filename
861
     * @param properties
862
     * @throws IOException
863
     */
864
    private void save(String filename, Properties properties) throws IOException{
865
        FileOutputStream fos =  new FileOutputStream(filename);
866
        properties.store(fos, "");
867
        fos.close();
868
    }
869

    
870

    
871
    /**
872
     * enables/disables nested composite. <br>
873
     *
874
     * @param ctrl - Composite to be en-/disabeld
875
     * @param enabled - boolean
876
     */
877
    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
878
        if (ctrl instanceof Composite) {
879
            Composite comp = (Composite) ctrl;
880
            for (Control c : comp.getChildren()) {
881
                recursiveSetEnabled(c, enabled);
882
            }
883
        } else {
884
            ctrl.setEnabled(enabled);
885
        }
886
    }
887

    
888
    /**
889
	 * <p>
890
	 * getSortRanksNaturally
891
	 * </p>
892
    	 *
893
    	 * @return a boolean.
894
	 */
895
	public static boolean getSortNodesNaturally() {
896
		return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
897
	}
898

    
899
	/**
900
	 * <p>
901
	 * setSortRanksNaturally
902
	 * </p>
903
	 *
904
	 * @param selection
905
	 *            a boolean.
906
	 */
907
	public static void setSortNodesNaturally(boolean selection) {
908
		getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
909
	}
910

    
911

    
912
	/**
913
	 * <p>
914
	 * getSortRanksNaturally
915
	 * </p>
916
	 *
917
	 * @return a boolean.
918
	 */
919
	public static boolean getSortNodesStrictlyAlphabetically() {
920
		return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
921
	}
922

    
923
	/**
924
	 * <p>
925
	 * setSortRanksNaturally
926
	 * </p>
927
	 *
928
	 * @param selection
929
	 *            a boolean.
930
	 */
931
	public static void setSortNodesStrictlyAlphabetically(boolean selection) {
932
		getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
933
	}
934

    
935
	/**
936
	 * <p>
937
	 * setStoreNavigatorState
938
	 * </p>
939
	 *
940
	 * @param selection
941
	 *            a boolean.
942
	 */
943
	public static boolean isStoreNavigatorState() {
944
		return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
945

    
946
	}
947

    
948
	/**
949
	 * <p>
950
	 * setStoreNavigatorState
951
	 * </p>
952
	 *
953
	 * @param selection
954
	 *            a boolean.
955
	 */
956
	public static void setStoreNavigatorState(boolean selection) {
957
		getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
958

    
959
	}
960

    
961
    /**
962
     * @return
963
     */
964
    public static boolean isShowUpWidgetIsDisposedMessages() {
965
       return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
966
    }
967
    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
968
        getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
969
    }
970

    
971
    /**
972
     * @return
973
     */
974
    public static boolean isShowIdInVocabularyInChecklistEditor() {
975
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
976
    }
977
    public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
978
        getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
979
    }
980

    
981
    /**
982
     * @return
983
     */
984
    public static boolean isShowSymbolInChecklistEditor() {
985
       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SYMBOL);
986
    }
987
    public static void setShowSymbolInChecklistEditor(boolean selection) {
988
        getPreferenceStore().setValue(CHECKLIST_SYMBOL, selection);
989
    }
990

    
991
    /**
992
     * @return
993
     */
994
    public static boolean isShowRankInChecklistEditor() {
995
        return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
996
    }
997
    public static void setShowRankInChecklistEditor(boolean selection) {
998
        getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
999
    }
1000

    
1001
    /**
1002
     * @param object
1003
     * @param b
1004
     * @return
1005
     */
1006
    public static NameDetailsConfigurator setPreferredNameDetailsConfiguration( boolean local) {
1007
        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1008

    
1009
        CdmPreference preference = null;
1010

    
1011
        if (!local) {
1012
            preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1013
            if (preference == null){
1014
                return null;
1015
            }
1016

    
1017
            getPreferenceStore().setValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1018

    
1019
            //the preference value is build like this:
1020
            //<section1>:true;<section2>:false....
1021

    
1022
            String value = preference.getValue();
1023
            String [] sections = value.split(";");
1024
            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1025
            String[] sectionValues;
1026
            for (String sectionValue: sections){
1027
                sectionValues = sectionValue.split(":");
1028
                sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1029
            }
1030
            config.setAllowOverride(preference.isAllowOverride());
1031
            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1032

    
1033
    //        getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1034
    //                (getValue(sectionMap, "taxon")));
1035
            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1036

    
1037
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1038
    //                (getValue(sectionMap, "lsid")));
1039
            config.setLSIDActivated(getValue(sectionMap, "lsid"));
1040

    
1041
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1042
    //                (getValue(sectionMap, "nc")));
1043
            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1044

    
1045
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1046
    //                (getValue(sectionMap, "ap")));
1047
            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1048

    
1049
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1050
    //                (getValue(sectionMap, "rank")));
1051
            config.setRankActivated(getValue(sectionMap, "rank"));
1052

    
1053

    
1054
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1055
    //                (getValue(sectionMap, "atomisedEpithets")));
1056
            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1057

    
1058
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1059
    //                (getValue(sectionMap,"author")));
1060
            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1061

    
1062
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1063
    //                (getValue(sectionMap, "nomRef")));
1064
            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1065

    
1066
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1067
    //                (getValue(sectionMap, "nomStat")));
1068
            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1069

    
1070

    
1071
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1072
    //                (getValue(sectionMap,"protologue")));
1073
            config.setProtologueActivated(getValue(sectionMap,"protologue"));
1074

    
1075
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1076
    //                (getValue(sectionMap,"typeDes")));
1077
            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1078

    
1079
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1080
    //                (getValue(sectionMap,"nameRelation")));
1081
            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1082

    
1083
    //        getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1084
    //                (getValue(sectionMap, "hybrid")));
1085
            config.setHybridActivated(getValue(sectionMap,"hybrid"));
1086
        }else{
1087
            config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION));
1088
            config.setTaxonSectionActivated(getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_TAXON));
1089
            config.setLSIDActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_LSID));
1090
            config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1091
            config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1092
            config.setRankActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_RANK));
1093
            config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1094
            config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1095
            config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1096
            config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1097
            config.setProtologueActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1098
            config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1099
            config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1100
            config.setHybridActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_HYBRID));
1101
        }
1102
        return config;
1103
    }
1104

    
1105
    private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1106
		if (sectionMap.containsKey(string)){
1107
			return sectionMap.get(string);
1108
		}else{
1109
			return true;
1110
		}
1111

    
1112
	}
1113

    
1114

    
1115

    
1116
    /**
1117
     * <p>
1118
     * setAbcdConfigurator
1119
     * </p>
1120
     *
1121
     * @param preferredConfiguration
1122
     *            a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1123
     *            object.
1124
     */
1125
    public static Abcd206ImportConfigurator getAbcdImportConfigurationPreference(boolean useLocal) {
1126

    
1127
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1128

    
1129
        if (useLocal){
1130
            config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1131

    
1132
            config.setAddMediaAsMediaSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1133

    
1134
            config.setAllowReuseOtherClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1135
            config.setDeduplicateClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1136
            config.setDeduplicateReferences(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1137

    
1138
            config.setGetSiblings(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1139
            config.setIgnoreAuthorship(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1140
            config.setIgnoreImportOfExistingSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1141
            config.setMapUnitIdToAccessionNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1142
            config.setMapUnitIdToBarcode(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1143
            config.setMapUnitIdToCatalogNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1144
            config.setMoveNewTaxaToDefaultClassification(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1145
            config.setOverwriteExistingSpecimens(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1146
            config.setReuseExistingDescriptiveGroups(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1147
            config.setReuseExistingMetaData(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1148
            config.setReuseExistingTaxaWhenPossible(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1149
            config.setNomenclaturalCode(NomenclaturalCode.getByKey(PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE)));
1150
            return config;
1151

    
1152
        }
1153
            ICdmRepository controller;
1154
            controller = CdmStore.getCurrentApplicationConfiguration();
1155
            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AbcdImportConfig);
1156
            CdmPreference preference = null;
1157
            if (controller == null){
1158
                return null;
1159
            }
1160
            preference = controller.getPreferenceService().find(key);
1161
            if (preference == null){
1162
                return config;
1163
             } else{
1164
             String configString = preference.getValue();
1165
             String[] configArray = configString.split(";");
1166

    
1167
             for (String configItem: configArray){
1168
                 String[] keyValue = configItem.split(":");
1169
                 String keyString = keyValue[0];
1170
                 String valueString = keyValue[1];
1171
                 if (keyString.equals("ignoreImportOfExistingSpecimen")){
1172
                     config.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
1173
                 }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){
1174
                     config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
1175
                 }else if (keyString.equals("reuseExistingTaxaWhenPossible")){
1176
                     config.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
1177
                 }else if (keyString.equals("ignoreAuthorship")){
1178
                     config.setIgnoreAuthorship(Boolean.valueOf(valueString));
1179
                 }else if (keyString.equals("addMediaAsMediaSpecimen")){
1180
                     config.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
1181
                 }else if (keyString.equals("reuseExistingMetaData")){
1182
                     config.setReuseExistingMetaData(Boolean.valueOf(valueString));
1183
                 }else if (keyString.equals("reuseExistingDescriptiveGroups")){
1184
                     config.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
1185
                 }else if (keyString.equals("allowReuseOtherClassifications")){
1186
                     config.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
1187
                 }else if (keyString.equals("deduplicateReferences")){
1188
                     config.setDeduplicateReferences(Boolean.valueOf(valueString));
1189
                 }else if (keyString.equals("deduplicateClassifications")){
1190
                     config.setDeduplicateClassifications(Boolean.valueOf(valueString));
1191
                 }else if (keyString.equals("moveNewTaxaToDefaultClassification")){
1192
                     config.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
1193
                 }else if (keyString.equals("mapUnitIdToCatalogNumber")){
1194
                     config.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
1195
                 }else if (keyString.equals("mapUnitIdToAccessionNumber")){
1196
                     config.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
1197
                 }else if (keyString.equals("mapUnitIdToBarcode")){
1198
                     config.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
1199
                 }else if (keyString.equals("overwriteExistingSpecimens")){
1200
                     config.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
1201
                 }else if (keyString.equals("nomenclaturalCode")){
1202
                         config.setNomenclaturalCode(NomenclaturalCode.fromString(valueString));
1203
                 }else{
1204
                     logger.debug("This key of the abcd configurator needs to be added to the transformer: " + keyString);
1205
                 }
1206

    
1207
            }
1208
        }
1209
        return config;
1210

    
1211

    
1212

    
1213
    }
1214

    
1215
    /**
1216
    *
1217
    */
1218
   public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1219
       NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1220
       if (local){
1221
          config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1222
          config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1223
          config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1224
          config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1225
          config.setLSIDActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1226
          config.setNameApprobiationActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1227
          config.setNameCacheActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1228
          config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1229
          config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1230
          config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1231
          config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1232
          config.setProtologueActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1233
          config.setRankActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1234
          config.setTaxonSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1235
          config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1236
          config.setHybridActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1237
       }else{
1238

    
1239
       }
1240

    
1241
      return config;
1242
   }
1243

    
1244

    
1245
   public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1246
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1247
               config.isSimpleDetailsViewActivated());
1248
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1249
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1250
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1251
               config.isNomenclaturalCodeActived());
1252
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1253
               config.isNameCacheActivated());
1254
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1255
               config.isAppendedPhraseActivated());
1256
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1257
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1258
               config.isAtomisedEpithetsActivated());
1259
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1260
               config.isAuthorCacheActivated());
1261
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1262
               config.isAuthorshipSectionActivated());
1263
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1264
               config.isNomenclaturalReferenceSectionActivated());
1265
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1266
               config.isNomenclaturalStatusSectionActivated());
1267
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1268
               config.isProtologueActivated());
1269
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1270
               config.isTypeDesignationSectionActivated());
1271
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1272
               config.isNameRelationsSectionActivated());
1273
       PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1274
               config.isHybridActivated());
1275

    
1276
   }
1277

    
1278
/**
1279
 * @return
1280
 */
1281
public static boolean isSortTaxaByRankAndName() {
1282

    
1283
    return getPreferenceStore().getBoolean(IPreferenceKeys.SORT_TAXA_BY_RANK_AND_NAME);
1284
}
1285

    
1286
/**
1287
 * @return
1288
 */
1289
public static boolean isSortNamedAreaByOrderInVocabulary() {
1290

    
1291
    return getPreferenceStore().getBoolean(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER);
1292
}
1293

    
1294
public static void setSortNamedAreasByOrderInVocabulary(boolean isSortByVocabularyOrder) {
1295
    getPreferenceStore().setValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER, isSortByVocabularyOrder);
1296

    
1297
}
1298

    
1299
/**
1300
 * <p>
1301
 * setPreferredNomenclaturalCode
1302
 * </p>
1303
 *
1304
 * @param preferredCode
1305
 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
1306
 *            object.
1307
 */
1308
public static CdmPreference setPreferredNamedAreasForDistributionEditor(
1309
        String saveCheckedElements, String saveGrayedElements, boolean local) {
1310
    if (local){
1311
        IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1312
        preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS, saveCheckedElements);
1313
        preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS_GRAYED, saveGrayedElements);
1314

    
1315
    }
1316
    else{
1317
        ICdmRepository controller;
1318

    
1319
//        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms);
1320
        CdmPreference preference = null;
1321

    
1322
        if (saveCheckedElements == null){
1323
            preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaTerms);
1324
//            preference = controller.getPreferenceService().find(key);
1325
            if (preference == null){
1326
                return null;
1327
            } else{
1328
                String[] uuids = StringUtils.splitByWholeSeparator(saveCheckedElements, ",");
1329
                List<UUID> uuidList = new ArrayList();
1330
                for (String uuidString: uuids){
1331
                    uuidList.add(UUID.fromString(uuidString.trim()));
1332
                }
1333
                getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1334
                        saveCheckedElements);
1335

    
1336
                return preference;
1337
            }
1338
        } else{
1339
            controller = CdmStore.getCurrentApplicationConfiguration();
1340
            preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1341
            controller.getPreferenceService().set(preference);
1342
            getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1343
                    saveCheckedElements);
1344

    
1345
        }
1346
    }
1347
    return null;
1348

    
1349

    
1350

    
1351
}
1352

    
1353

    
1354

    
1355

    
1356

    
1357
}
(19-19/24)