avoid NPE in getLastSelectedReferences
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.preference;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.Set;
26 import java.util.StringTokenizer;
27 import java.util.UUID;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.log4j.Logger;
31 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.eclipse.jface.window.Window;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.ui.PlatformUI;
38
39 import eu.etaxonomy.cdm.api.application.ICdmRepository;
40 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
41 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
42 import eu.etaxonomy.cdm.api.service.ITermService;
43 import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
44 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
45 import eu.etaxonomy.cdm.common.CdmUtils;
46 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
47 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
48 import eu.etaxonomy.cdm.model.common.ICdmBase;
49 import eu.etaxonomy.cdm.model.common.IDefinedTerm;
50 import eu.etaxonomy.cdm.model.common.ISimpleTerm;
51 import eu.etaxonomy.cdm.model.common.Language;
52 import eu.etaxonomy.cdm.model.common.MarkerType;
53 import eu.etaxonomy.cdm.model.common.TermBase;
54 import eu.etaxonomy.cdm.model.description.FeatureTree;
55 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
56 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
57 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
58 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
59 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
60 import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
61 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
62 import eu.etaxonomy.cdm.strategy.match.MatchException;
63 import eu.etaxonomy.cdm.strategy.match.MatchMode;
64 import eu.etaxonomy.taxeditor.model.AbstractUtility;
65 import eu.etaxonomy.taxeditor.model.MessagingUtils;
66 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
67 import eu.etaxonomy.taxeditor.store.CdmStore;
68 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
69 import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
70
71 /**
72 * <p>
73 * PreferencesUtil class.
74 * </p>
75 *
76 * @author p.ciardelli
77 * @author n.hoffmann
78 * @created 05.12.2008
79 * @version 1.0
80 */
81 public class PreferencesUtil implements IPreferenceKeys {
82
83 /**
84 *
85 */
86 public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
87
88 public static final String P2_REPOSITORIES_DELIM = ",";
89 public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
90
91 private final static Logger logger = Logger.getLogger(PreferencesUtil.class);
92
93
94
95 /**
96 * <p>
97 * getPreferenceStore
98 * </p>
99 *
100 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
101 */
102 public static IPreferenceStore getPreferenceStore() {
103 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
104 }
105
106 /**
107 * <p>
108 * setPreferredNomenclaturalCode
109 * </p>
110 *
111 * @param preferredCode
112 * a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
113 * object.
114 */
115 public static CdmPreference setPreferredNomenclaturalCode(
116 String preferenceKey, boolean local) {
117 if (local){
118 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
119 preferenceKey);
120 }
121 else{
122 ICdmRepository controller;
123 controller = CdmStore.getCurrentApplicationConfiguration();
124 PrefKey key = null;
125 try{
126 key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
127 }catch (Exception e){
128 System.out.println(e.getStackTrace());
129 }
130 CdmPreference preference = null;
131 if (controller == null){
132 return null;
133 }
134 if (preferenceKey == null){
135 preference = controller.getPreferenceService().find(key);
136 if (preference == null){
137 return null;
138 } else{
139 // int index = StringUtils.lastIndexOf(preference.getValue(), ".");
140 // UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
141 // preferredCode = NomenclaturalCode.getByUuid(uuid);
142
143 getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
144 preferenceKey);
145 getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,preference.isAllowOverride());
146 return preference;
147 }
148 } else{
149 preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferenceKey);
150 controller.getPreferenceService().set(preference);
151
152 }
153 }
154 return null;
155
156
157
158 }
159
160 public static NomenclaturalCode getPreferredNomenclaturalCode(){
161 if (getPreferenceStore().getBoolean(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
162 return getPreferredNomenclaturalCode(true);
163 } else{
164 return getPreferredNomenclaturalCode(false);
165 }
166 }
167
168 /**
169 * <p>
170 * getPreferredNomenclaturalCode
171 * </p>
172 *
173 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
174 */
175 public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
176
177 CdmPreference pref = null;
178 if (!useLocal){
179 pref = getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
180 }
181
182 String preferredCode;
183 if(pref == null){
184 preferredCode = getPreferenceStore().getString(
185 PREFERRED_NOMENCLATURAL_CODE_KEY);
186
187 }else{
188 preferredCode = pref.getValue();
189 }
190
191 return getPreferredNomenclaturalCode(preferredCode);
192
193 }
194
195 /**
196 * <p>
197 * getPreferredNomenclaturalCode
198 * </p>
199 *
200 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
201 */
202 public static NomenclaturalCode getPreferredNomenclaturalCode(String preferenceKeyNomenclaturalCode) {
203
204 for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
205 if (getPreferenceKey(code).equals(preferenceKeyNomenclaturalCode)) {
206 return code;
207 }
208 }
209 return null;
210 }
211
212 public static boolean isShowTaxonAssociations(){
213 if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
214 return getPreferenceStore().getBoolean(SHOW_TAXON_ASSOCIATIONS);
215 } else{
216 CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowTaxonAssociations);
217
218 return pref!=null?Boolean.valueOf(pref.getValue()):false;
219 }
220 }
221
222 public static boolean isShowLifeForm(){
223 if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
224 return getPreferenceStore().getBoolean(SHOW_LIFE_FORM);
225 } else{
226 CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowLifeForm);
227
228 return pref!=null?Boolean.valueOf(pref.getValue()):false;
229 }
230 }
231
232 public static boolean isDeterminationOnlyForFieldUnits(){
233 if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
234 return getPreferenceStore().getBoolean(DETERMINATION_ONLY_FOR_FIELD_UNITS);
235 } else{
236 CdmPreference pref = getPreferenceFromDB(PreferencePredicate.DeterminationOnlyForFieldUnits);
237
238 return pref!=null?Boolean.valueOf(pref.getValue()):false;
239 }
240 }
241
242 public static boolean isCollectingAreaInGeneralSection(){
243 if (getPreferenceStore().getBoolean(OVERRIDE_SPECIMEN_PREF) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_SPECIMEN_PREF)) {
244 return getPreferenceStore().getBoolean(SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION);
245 } else{
246 CdmPreference pref = getPreferenceFromDB(PreferencePredicate.ShowCollectingAreasInGeneralSection);
247
248 return pref!=null?Boolean.valueOf(pref.getValue()):false;
249 }
250 }
251
252 public static boolean isShowSimpleDetailsView(){
253 if (getPreferenceStore().getBoolean(OVERRIDE_NAME_DETAILS) && getPreferenceStore().getBoolean(ALLOW_OVERRIDE_NAME_DETAILS)) {
254 return getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION);
255 } else{
256 CdmPreference pref = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
257
258 return pref!=null?Boolean.valueOf(pref.getValue()):false;
259 }
260 }
261
262
263
264 public static CdmPreference getPreferenceFromDB(PreferencePredicate predicate){
265 ICdmRepository controller;
266 CdmPreference pref = null;
267
268 try{
269 if(CdmStore.isActive()){
270 controller = CdmStore.getCurrentApplicationConfiguration();
271 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), predicate);
272 pref = controller.getPreferenceService().find(key);
273 }
274 }catch(Exception e){
275 e.printStackTrace();
276 }
277
278 return pref;
279
280 }
281
282 public static void setPreferenceToDB(CdmPreference preference){
283 ICdmRepository controller;
284 try{
285 if(CdmStore.isActive()){
286 controller = CdmStore.getCurrentApplicationConfiguration();
287 controller.getPreferenceService().set(preference);
288 }
289 }catch(Exception e){
290 e.printStackTrace();
291 }
292
293 }
294
295
296
297 public static String getPreferredDefaultLangugae(){
298 String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
299 if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
300 return preferredLanguage;
301 }
302 return null;
303 }
304
305 public static boolean isShowMediaPreview(){
306 boolean isShowMediaPreview = getPreferenceStore().getBoolean(SHOW_MEDIA_PREVIEW);
307 return isShowMediaPreview;
308 }
309
310 /**
311 * Get the match strategy for the given class that was stored in preferences
312 * or the default strategy if it was not stored in preferences
313 *
314 * @param clazz
315 * a {@link java.lang.Class} object.
316 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
317 */
318 public static IMatchStrategy getMatchStrategy(Class clazz) {
319 String className = clazz.getName();
320 if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
321 IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
322
323 for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
324 String matchModeName = getPreferenceStore().getString(
325 getMatchStrategyFieldName(className, fieldName));
326 MatchMode matchMode = MatchMode.valueOf(matchModeName);
327 try {
328 matchStrategy.setMatchMode(fieldName, matchMode);
329 } catch (MatchException e) {
330 MessagingUtils.error(PreferencesUtil.class, e);
331 throw new RuntimeException(e);
332 }
333 }
334
335 return matchStrategy;
336 }
337 return getDefaultMatchStrategy(clazz);
338 }
339
340 /**
341 * Stores a matchStrategy into the preference store.
342 *
343 * @param matchStrategy
344 * a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
345 * object.
346 */
347 public static void setMatchStrategy(IMatchStrategy matchStrategy) {
348 String className = matchStrategy.getMatchClass().getName();
349 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
350
351 Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
352
353 for (String fieldName : matchFields) {
354 getPreferenceStore().setValue(
355 getMatchStrategyFieldName(className, fieldName),
356 matchStrategy.getMatchMode(fieldName).name());
357 }
358 }
359
360 /**
361 * Helper method to create the preference property for a match field.
362 *
363 * @param className
364 * @param fieldName
365 * @return
366 */
367 private static String getMatchStrategyFieldName(String className,
368 String fieldName) {
369 return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
370 }
371
372 /**
373 * Returns the default match strategy for a given class.
374 *
375 * @param clazz
376 * a {@link java.lang.Class} object.
377 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
378 */
379 public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
380 return DefaultMatchStrategy.NewInstance(clazz);
381 }
382
383 /**
384 * <p>
385 * getDateFormatPattern
386 * </p>
387 *
388 * @return a {@link java.lang.String} object.
389 */
390 public static String getDateFormatPattern() {
391 // TODO make this configurable in properties
392 String pattern = "Y-M-d H:m";
393 return pattern;
394 }
395
396 /**
397 * <p>
398 * addTermToPreferredTerms
399 * </p>
400 *
401 * @param term
402 * a T object.
403 * @param <T>
404 * a T object.
405 */
406 public static <T extends TermBase> void addTermToPreferredTerms(T term) {
407
408 // VocabularyEnum vocabulary =
409 // VocabularyEnum.getVocabularyEnum(term.getClass());
410 //
411 // getPreferenceStore().setValue(getPreferenceKey(term),
412 // VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
413 //
414 // firePreferencesChanged(term.getClass());
415 }
416
417 /**
418 * Construct a unique key using the CdmBase object's uuid
419 *
420 * @param cdmBase
421 * @return
422 */
423 private static String getPreferenceKey(ICdmBase cdmBase) {
424 cdmBase = HibernateProxyHelper.deproxy(cdmBase);
425
426 String key = cdmBase.getClass().getName().concat(".")
427 .concat(cdmBase.getUuid().toString());
428 if (key.contains("javassist")) {
429 MessagingUtils.info("proxy");
430 }
431 return key;
432 }
433
434 /**
435 * Construct a unique key using the CdmBase object's uuid
436 *
437 * @param cdmBase
438 * @return
439 */
440 public static String getPreferenceKey(ISimpleTerm simpleTerm) {
441 simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
442 String key = simpleTerm.getClass().getName().concat(".")
443 .concat(simpleTerm.getUuid().toString());
444 if (key.contains("javassist")) {
445 MessagingUtils.warn(PreferencesUtil.class,
446 "Trying to persist a preference based on a proxy class.");
447 }
448 return key;
449 }
450
451
452
453 /**
454 * Construct a unique key using the CdmBase object's uuid
455 *
456 * @param cdmBase
457 * @return
458 */
459 public static String getPreferenceKey(IDefinedTerm definedTerm) {
460 definedTerm = HibernateProxyHelper.deproxy(definedTerm);
461 String key = definedTerm.getClass().getName().concat(".")
462 .concat(definedTerm.getUuid().toString());
463 if (key.contains("javassist")) {
464 MessagingUtils.warn(PreferencesUtil.class,
465 "Trying to persist a preference based on a proxy class.");
466 }
467 return key;
468 }
469
470 /**
471 * Retrieves search preferences from the preference store
472 *
473 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
474 */
475 public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
476 IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
477
478 configurator.setDoTaxa(getPreferenceStore().getBoolean(
479 TAXON_SERVICE_CONFIGURATOR_TAXA));
480 configurator.setDoSynonyms(getPreferenceStore().getBoolean(
481 TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
482 configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
483 TAXON_SERVICE_CONFIGURATOR_NAMES));
484 configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
485 TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
486 //configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
487
488 return configurator;
489 }
490
491 /**
492 * create new preferences, setting all search options to true
493 *
494 * @return a
495 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
496 * object.
497 */
498 public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
499 IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
500
501 configurator.setDoTaxa(true);
502 configurator.setDoSynonyms(true);
503 configurator.setDoNamesWithoutTaxa(true);
504 configurator.setDoTaxaByCommonNames(true);
505
506 configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
507 "name", "name.$", "relationsFromThisTaxon.$"));
508
509 configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
510 "name", "name.$", "synonyms.relatedTo.*"));
511
512 // DEFAULT VALUES
513 // match mode is a simple like, actually all other match modes are kind
514 // of bogus
515 configurator
516 .setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
517 // we set page number and size here as this should always be unlimited
518 configurator.setPageNumber(0);
519 // TODO currently limit results to 10000
520 configurator.setPageSize(10000);
521
522 return configurator;
523 }
524
525 /**
526 * Store search preferences
527 *
528 * @param configurator
529 * a
530 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
531 * object.
532 */
533 public static void setSearchConfigurator(
534 IFindTaxaAndNamesConfigurator configurator) {
535 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
536 configurator.isDoTaxa());
537 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
538 configurator.isDoSynonyms());
539 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
540 configurator.isDoNamesWithoutTaxa());
541 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
542 configurator.isDoTaxaByCommonNames());
543 }
544
545 /**
546 * <p>
547 * firePreferencesChanged
548 * </p>
549 *
550 * @param clazz
551 * a {@link java.lang.Class} object.
552 */
553 public static void firePreferencesChanged(Class clazz) {
554 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
555 null, clazz);
556 }
557
558 /**
559 * Set default values for preferences
560 */
561 public static void setDefaults() {
562 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
563 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
564 true);
565 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
566 "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
567 //FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
568 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
569 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
570 "http://www.biodiversitylibrary.org/openurl");
571 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
572 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
573 getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, false);
574 getPreferenceStore().setDefault(CHECKLIST_ID_IN_VOCABULARY, true);
575 getPreferenceStore().setDefault(CHECKLIST_SYMBOL, false);
576 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, true);
577 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE, true);
578 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, true);
579 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_HYBRID, true);
580 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_LSID, true);
581 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, true);
582 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAMECACHE, true);
583 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, true);
584 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, true);
585 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, true);
586 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, true);
587 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_RANK, true);
588 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, true);
589 getPreferenceStore().setDefault(SORT_NODES_NATURALLY, false);
590 getPreferenceStore().setDefault(SORT_NODES_ALPHABETICALLY, false);
591 getPreferenceStore().setDefault(SORT_TAXA_BY_RANK_AND_NAME, true);
592
593 }
594
595 /**
596 * <p>
597 * checkNomenclaturalCode
598 * </p>
599 */
600 public static void checkNomenclaturalCode() {
601 // First time Editor is opened, no nomenclatural code has been set
602
603
604 if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
605 PreferencesUtil.setPreferredNomenclaturalCode(getPreferenceKey(NomenclaturalCode.ICNAFP), true);
606 }
607
608
609
610 }
611 public static void setNomenclaturalCodePreferences(){
612 ICdmRepository controller;
613 controller = CdmStore.getCurrentApplicationConfiguration();
614 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
615 CdmPreference preference = null;
616 if (controller == null){
617 return ;
618 }
619 preference = controller.getPreferenceService().find(key);
620 if (preference == null){
621 return;
622 }
623 getPreferenceStore().setValue(ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, preference.isAllowOverride());
624
625 int index = StringUtils.lastIndexOf(preference.getValue(), ".");
626 UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
627 NomenclaturalCode preferredCode = NomenclaturalCode.getByUuid(uuid);
628
629 getPreferenceStore().setValue(CDM_NOMENCLATURAL_CODE_KEY,
630 getPreferenceKey(preferredCode));
631
632 if (!preference.isAllowOverride()){
633 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
634 getPreferenceKey(preferredCode));
635 }
636 }
637
638 public static void checkDefaultLanguage(){
639 if(PreferencesUtil.getPreferredDefaultLangugae() == null){
640 Shell shell = AbstractUtility.getShell();
641 int open = new DefaultLanguageDialog(shell).open();
642 if(open == Window.OK){
643 PlatformUI.getWorkbench().restart();
644 }
645 }else{
646 //TODO:In case of a reinstall, the config.ini will be overwritten
647 // here you create config.ini with the stored key from preferences
648 }
649 }
650
651 /**
652 * <p>
653 * getMapServiceAccessPoint
654 * </p>
655 *
656 * @return a {@link java.lang.String} object.
657 */
658 public static String getMapServiceAccessPoint() {
659 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
660 }
661
662 /**
663 * <p>
664 * shouldConnectAtStartUp
665 * </p>
666 *
667 * @return a boolean.
668 */
669 public static boolean shouldConnectAtStartUp() {
670 //FIXME : force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
671 //return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
672 return false;
673 }
674
675 /**
676 * <p>
677 * getDefaultFeatureTreeForTextualDescription
678 * </p>
679 *
680 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
681 */
682 public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
683 String uuidString = getPreferenceStore().getString(
684 FEATURE_TREE_DEFAULT_TEXT);
685 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
686 IFeatureTreeService.class).load(UUID.fromString(uuidString));
687 }
688
689 /**
690 * <p>
691 * getDefaultFeatureTreeForStructuredDescription
692 * </p>
693 *
694 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
695 */
696 public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
697 String uuidString = getPreferenceStore().getString(
698 FEATURE_TREE_DEFAULT_STRUCTURE);
699 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
700 IFeatureTreeService.class).load(UUID.fromString(uuidString));
701 }
702
703 /**
704 * <p>
705 * setSortRanksHierarchichally
706 * </p>
707 *
708 * @param selection
709 * a boolean.
710 */
711 public static void setSortRanksHierarchichally(boolean selection) {
712 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
713 }
714
715 /**
716 * <p>
717 * getSortRanksHierarchichally
718 * </p>
719 *
720 * @return a boolean.
721 */
722 public static boolean getSortRanksHierarchichally() {
723 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
724 }
725
726 public static boolean isMultilanguageTextEditingCapability() {
727 return getPreferenceStore().getBoolean(
728 MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
729 }
730
731 public static Language getGlobalLanguage() {
732
733
734 String languageUuidString = getPreferenceStore().getString(
735 GLOBAL_LANGUAGE_UUID);
736
737 if(!CdmStore.isActive()) {
738 MessagingUtils.noDataSourceWarningDialog(languageUuidString);
739 return null;
740 }
741
742 if (CdmUtils.isBlank(languageUuidString)) {
743 return Language.getDefaultLanguage();
744 }
745
746 UUID languageUuid = UUID.fromString(languageUuidString);
747 return (Language) CdmStore.getService(ITermService.class).load(
748 languageUuid);
749 }
750
751 public static void setGlobalLanguage(Language language) {
752 if(language != null) {
753 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
754 CdmStore.setDefaultLanguage(language);
755 }
756
757 }
758
759 /**
760 * @return
761 */
762 public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
763 List<MarkerType> markerTypes = CdmStore.getTermManager()
764 .getPreferredTerms(MarkerType.class);
765
766 Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
767
768 for (MarkerType markerType : markerTypes) {
769 String name = getMarkerTypeEditingPreferenceKey(markerType);
770 Boolean value = getPreferenceStore().getBoolean(name);
771
772 result.put(markerType, value);
773 }
774
775 return result;
776 }
777
778 /**
779 * @param markerTypeEditingMap
780 */
781 public static void setEditMarkerTypePreferences(
782 Map<MarkerType, Boolean> markerTypeEditingMap) {
783 for (MarkerType markerType : markerTypeEditingMap.keySet()) {
784 String name = getMarkerTypeEditingPreferenceKey(markerType);
785 getPreferenceStore().setValue(name,
786 markerTypeEditingMap.get(markerType));
787 }
788
789 }
790
791 private static String getMarkerTypeEditingPreferenceKey(
792 MarkerType markerType) {
793 markerType = HibernateProxyHelper.deproxy(markerType);
794 return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
795 }
796
797 /**
798 * <p>
799 * setEditMarkerTypePreference
800 * </p>
801 *
802 * @param input
803 * a {@link org.eclipse.ui.IEditorInput} object.
804 * @param markerType
805 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
806 * @param edit
807 * a boolean.
808 */
809 public static void setEditMarkerTypePreference(MarkerType markerType,
810 boolean edit) {
811 getPreferenceStore().setValue(
812 getMarkerTypeEditingPreferenceKey(markerType), edit);
813 }
814
815 /**
816 * @return
817 */
818 public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
819 DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
820 .NewInstance();
821 configurator.setMoveDerivedUnitMediaToGallery(true);
822 configurator.setMoveFieldObjectMediaToGallery(true);
823 return configurator;
824 }
825
826 /**
827 * This method will write language properties to the config.ini located in the configuration folder
828 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
829 *
830 * @param setLanguage 0 is for german and 1 for english.
831 * @throws IOException
832 */
833 public void writePropertyToConfigFile(int setLanguage) throws IOException {
834 File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
835 //give warning to user if the directory has no write access
836 if(file == null){
837 throw new IOException();
838 }
839 Properties properties = load(file.getAbsolutePath()+"/config.ini");
840 switch(setLanguage){
841 case 0:
842 properties.setProperty("osgi.nl", "de");
843 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
844 break;
845 case 1:
846 properties.setProperty("osgi.nl", "en");
847 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
848 break;
849 default:
850 break;
851 }
852 save(file+"/config.ini", properties);
853 }
854
855 /**
856 * This method loads a property from a given file and returns it.
857 *
858 * @param filename
859 * @return
860 * @throws IOException
861 */
862 private Properties load(String filename) throws IOException {
863 FileInputStream in = new FileInputStream(filename);
864 Properties prop = new Properties();
865 prop.load(in);
866 in.close();
867 return prop;
868 }
869
870 /**
871 * This method saves a property to the specified file.
872 *
873 * @param filename
874 * @param properties
875 * @throws IOException
876 */
877 private void save(String filename, Properties properties) throws IOException{
878 FileOutputStream fos = new FileOutputStream(filename);
879 properties.store(fos, "");
880 fos.close();
881 }
882
883 /**
884 * Saves a list of P2 Metadata Repositories as string with specified delimiters
885 *
886 * @param p2Repos
887 */
888 public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
889 StringBuilder sb = new StringBuilder();
890 for(MetadataRepositoryElement p2Repo : p2Repos) {
891 sb.append(P2_REPOSITORIES_DELIM);
892 if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
893 sb.append("-");
894 } else {
895 sb.append(p2Repo.getName());
896 }
897 sb.append(P2_REPOSITORY_FIELDS_DELIM);
898 sb.append(p2Repo.getLocation().toString());
899 sb.append(P2_REPOSITORY_FIELDS_DELIM);
900 sb.append(String.valueOf(p2Repo.isEnabled()));
901 }
902 getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
903 }
904
905
906 /**
907 * Retrieves a list of previously saved P2 repositories
908 *
909 * @return
910 */
911 public static List<MetadataRepositoryElement> getP2Repositories() {
912 List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
913 String p2ReposPref = getPreferenceStore().getString(P2_REPOSITORY_LIST);
914 if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
915 StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
916
917 while(p2ReposPrefST.hasMoreTokens()) {
918 String p2RepoStr = p2ReposPrefST.nextToken();
919 StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
920 if(p2ReposStrST.countTokens()==3) {
921 String nickname = p2ReposStrST.nextToken();
922 URI uri = null;
923 try {
924 uri = new URI(p2ReposStrST.nextToken());
925 } catch (URISyntaxException e) {
926 continue;
927 }
928 boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
929 MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
930 mre.setNickname(nickname);
931 mre.setEnabled(enabled);
932 p2Repos.add(mre);
933 }
934 }
935 }
936
937 return p2Repos;
938 }
939
940 /**
941 * enables/disables nested composite. <br>
942 *
943 * @param ctrl - Composite to be en-/disabeld
944 * @param enabled - boolean
945 */
946 public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
947 if (ctrl instanceof Composite) {
948 Composite comp = (Composite) ctrl;
949 for (Control c : comp.getChildren()) {
950 recursiveSetEnabled(c, enabled);
951 }
952 } else {
953 ctrl.setEnabled(enabled);
954 }
955 }
956
957 /**
958 * <p>
959 * getSortRanksNaturally
960 * </p>
961 *
962 * @return a boolean.
963 */
964 public static boolean getSortNodesNaturally() {
965 return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
966 }
967
968 /**
969 * <p>
970 * setSortRanksNaturally
971 * </p>
972 *
973 * @param selection
974 * a boolean.
975 */
976 public static void setSortNodesNaturally(boolean selection) {
977 getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
978 }
979
980
981 /**
982 * <p>
983 * getSortRanksNaturally
984 * </p>
985 *
986 * @return a boolean.
987 */
988 public static boolean getSortNodesStrictlyAlphabetically() {
989 return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
990 }
991
992 /**
993 * <p>
994 * setSortRanksNaturally
995 * </p>
996 *
997 * @param selection
998 * a boolean.
999 */
1000 public static void setSortNodesStrictlyAlphabetically(boolean selection) {
1001 getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
1002 }
1003
1004 /**
1005 * <p>
1006 * setStoreNavigatorState
1007 * </p>
1008 *
1009 * @param selection
1010 * a boolean.
1011 */
1012 public static boolean isStoreNavigatorState() {
1013 return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
1014
1015 }
1016
1017 /**
1018 * <p>
1019 * setStoreNavigatorState
1020 * </p>
1021 *
1022 * @param selection
1023 * a boolean.
1024 */
1025 public static void setStoreNavigatorState(boolean selection) {
1026 getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
1027
1028 }
1029
1030 /**
1031 * @return
1032 */
1033 public static boolean isShowUpWidgetIsDisposedMessages() {
1034 return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
1035 }
1036 public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
1037 getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
1038 }
1039
1040 /**
1041 * @return
1042 */
1043 public static boolean isShowIdInVocabularyInChecklistEditor() {
1044 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
1045 }
1046 public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
1047 getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
1048 }
1049
1050 /**
1051 * @return
1052 */
1053 public static boolean isShowSymbolInChecklistEditor() {
1054 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SYMBOL);
1055 }
1056 public static void setShowSymbolInChecklistEditor(boolean selection) {
1057 getPreferenceStore().setValue(CHECKLIST_SYMBOL, selection);
1058 }
1059
1060 /**
1061 * @return
1062 */
1063 public static boolean isShowRankInChecklistEditor() {
1064 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
1065 }
1066 public static void setShowRankInChecklistEditor(boolean selection) {
1067 getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
1068 }
1069
1070 /**
1071 * @param object
1072 * @param b
1073 * @return
1074 */
1075 public static NameDetailsConfigurator setPreferredNameDetailsConfiguration( boolean local) {
1076 NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1077
1078 CdmPreference preference = null;
1079
1080 if (!local) {
1081 preference = getPreferenceFromDB(PreferencePredicate.NameDetailsView);
1082 if (preference == null){
1083 return null;
1084 }
1085
1086 getPreferenceStore().setValue(ALLOW_OVERRIDE_NAME_DETAILS, preference.isAllowOverride());
1087
1088 //the preference value is build like this:
1089 //<section1>:true;<section2>:false....
1090
1091 String value = preference.getValue();
1092 String [] sections = value.split(";");
1093 Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
1094 String[] sectionValues;
1095 for (String sectionValue: sections){
1096 sectionValues = sectionValue.split(":");
1097 sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
1098 }
1099 config.setAllowOverride(preference.isAllowOverride());
1100 config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
1101
1102 // getPreferenceStore().setValue(SHOW_SIMPLE_NAME_DETAILS_TAXON,
1103 // (getValue(sectionMap, "taxon")));
1104 config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
1105
1106 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_LSID,
1107 // (getValue(sectionMap, "lsid")));
1108 config.setLSIDActivated(getValue(sectionMap, "lsid"));
1109
1110 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1111 // (getValue(sectionMap, "nc")));
1112 config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
1113
1114 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1115 // (getValue(sectionMap, "ap")));
1116 config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
1117
1118 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_RANK,
1119 // (getValue(sectionMap, "rank")));
1120 config.setRankActivated(getValue(sectionMap, "rank"));
1121
1122
1123 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1124 // (getValue(sectionMap, "atomisedEpithets")));
1125 config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
1126
1127 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1128 // (getValue(sectionMap,"author")));
1129 config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
1130
1131 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1132 // (getValue(sectionMap, "nomRef")));
1133 config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
1134
1135 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1136 // (getValue(sectionMap, "nomStat")));
1137 config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
1138
1139
1140 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1141 // (getValue(sectionMap,"protologue")));
1142 config.setProtologueActivated(getValue(sectionMap,"protologue"));
1143
1144 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1145 // (getValue(sectionMap,"typeDes")));
1146 config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
1147
1148 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1149 // (getValue(sectionMap,"nameRelation")));
1150 config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
1151
1152 // getPreferenceStore().setValue(SHOW_NAME_DETAILS_SECTION_HYBRID,
1153 // (getValue(sectionMap, "hybrid")));
1154 config.setHybridActivated(getValue(sectionMap,"hybrid"));
1155 }else{
1156 config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_SECTION));
1157 config.setTaxonSectionActivated(getPreferenceStore().getBoolean(SHOW_SIMPLE_NAME_DETAILS_TAXON));
1158 config.setLSIDActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_LSID));
1159 config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1160 config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1161 config.setRankActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_RANK));
1162 config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1163 config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1164 config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1165 config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1166 config.setProtologueActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1167 config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1168 config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1169 config.setHybridActivated(getPreferenceStore().getBoolean(SHOW_NAME_DETAILS_SECTION_HYBRID));
1170 }
1171 return config;
1172 }
1173
1174 private static Boolean getValue(Map<String, Boolean> sectionMap, String string) {
1175 if (sectionMap.containsKey(string)){
1176 return sectionMap.get(string);
1177 }else{
1178 return true;
1179 }
1180
1181 }
1182
1183
1184
1185 /**
1186 * <p>
1187 * setAbcdConfigurator
1188 * </p>
1189 *
1190 * @param preferredConfiguration
1191 * a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
1192 * object.
1193 */
1194 public static Abcd206ImportConfigurator getAbcdImportConfigurationPreference(boolean useLocal) {
1195
1196 Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
1197
1198 if (useLocal){
1199 config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_INDIVIDUALS_ASSOCIATIONS_SUCH_AS_SPECIMEN_AND_OBSERVATIONS));
1200
1201 config.setAddMediaAsMediaSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ADD_MEDIA_AS_MEDIASPECIMEN));
1202
1203 config.setAllowReuseOtherClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_ALLOW_REUSE_OTHER_CLASSIFICATIONS));
1204 config.setDeduplicateClassifications(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_CLASSIFICATIONS));
1205 config.setDeduplicateReferences(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DEDUPLICATE_REFERENCES));
1206
1207 config.setGetSiblings(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_DO_SIBLINGS));
1208 config.setIgnoreAuthorship(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_AUTHORSHIP));
1209 config.setIgnoreImportOfExistingSpecimen(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_IGNORE_IMPORT_OF_EXISTING_SPECIMEN));
1210 config.setMapUnitIdToAccessionNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_ACCESSION_NUMBER));
1211 config.setMapUnitIdToBarcode(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TO_BARCODE));
1212 config.setMapUnitIdToCatalogNumber(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MAP_UNIT_ID_TOCATALOG_NUMBER));
1213 config.setMoveNewTaxaToDefaultClassification(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_MOVE_NEW_TAXA_TO_DEFAULT_CLASSIFICATION));
1214 config.setOverwriteExistingSpecimens(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_OVERWRITE_EXISTING_SPECIMEN));
1215 config.setReuseExistingDescriptiveGroups(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_DESCRIPTIVE_GROUPS));
1216 config.setReuseExistingMetaData(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_META_DATA));
1217 config.setReuseExistingTaxaWhenPossible(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_REUSE_EXISTING_TAXA_WHEN_POSSIBLE));
1218 config.setNomenclaturalCode(NomenclaturalCode.getByKey(PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.ABCD_IMPORT_CONFIGURATOR_NOMENCLATURAL_CODE)));
1219 return config;
1220
1221 }
1222 ICdmRepository controller;
1223 controller = CdmStore.getCurrentApplicationConfiguration();
1224 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AbcdImportConfig);
1225 CdmPreference preference = null;
1226 if (controller == null){
1227 return null;
1228 }
1229 preference = controller.getPreferenceService().find(key);
1230 if (preference == null){
1231 return config;
1232 } else{
1233 String configString = preference.getValue();
1234 String[] configArray = configString.split(";");
1235
1236 for (String configItem: configArray){
1237 String[] keyValue = configItem.split(":");
1238 String keyString = keyValue[0];
1239 String valueString = keyValue[1];
1240 if (keyString.equals("ignoreImportOfExistingSpecimen")){
1241 config.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
1242 }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){
1243 config.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
1244 }else if (keyString.equals("reuseExistingTaxaWhenPossible")){
1245 config.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
1246 }else if (keyString.equals("ignoreAuthorship")){
1247 config.setIgnoreAuthorship(Boolean.valueOf(valueString));
1248 }else if (keyString.equals("addMediaAsMediaSpecimen")){
1249 config.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
1250 }else if (keyString.equals("reuseExistingMetaData")){
1251 config.setReuseExistingMetaData(Boolean.valueOf(valueString));
1252 }else if (keyString.equals("reuseExistingDescriptiveGroups")){
1253 config.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
1254 }else if (keyString.equals("allowReuseOtherClassifications")){
1255 config.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
1256 }else if (keyString.equals("deduplicateReferences")){
1257 config.setDeduplicateReferences(Boolean.valueOf(valueString));
1258 }else if (keyString.equals("deduplicateClassifications")){
1259 config.setDeduplicateClassifications(Boolean.valueOf(valueString));
1260 }else if (keyString.equals("moveNewTaxaToDefaultClassification")){
1261 config.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
1262 }else if (keyString.equals("mapUnitIdToCatalogNumber")){
1263 config.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
1264 }else if (keyString.equals("mapUnitIdToAccessionNumber")){
1265 config.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
1266 }else if (keyString.equals("mapUnitIdToBarcode")){
1267 config.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
1268 }else if (keyString.equals("overwriteExistingSpecimens")){
1269 config.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
1270 }else if (keyString.equals("nomenclaturalCode")){
1271 config.setNomenclaturalCode(NomenclaturalCode.fromString(valueString));
1272 }else{
1273 logger.debug("This key of the abcd configurator needs to be added to the transformer: " + keyString);
1274 }
1275
1276 }
1277 }
1278 return config;
1279
1280
1281
1282 }
1283
1284 /**
1285 *
1286 */
1287 public NameDetailsConfigurator createLocalNameDetailsViewConfig(boolean local) {
1288 NameDetailsConfigurator config = new NameDetailsConfigurator(true);
1289 if (local){
1290 config.setSimpleDetailsViewActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION));
1291 config.setAppendedPhraseActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE));
1292 config.setAtomisedEpithetsActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS));
1293 config.setAuthorshipSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP));
1294 config.setLSIDActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID));
1295 config.setNameApprobiationActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_APPROBATION));
1296 config.setNameCacheActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_CACHE));
1297 config.setNameRelationsSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP));
1298 config.setNomenclaturalCodeActived(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE));
1299 config.setNomenclaturalStatusSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS));
1300 config.setNomenclaturalReferenceSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE));
1301 config.setProtologueActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE));
1302 config.setRankActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK));
1303 config.setTaxonSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON));
1304 config.setTypeDesignationSectionActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION));
1305 config.setHybridActivated(getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID));
1306 }else{
1307
1308 }
1309
1310 return config;
1311 }
1312
1313
1314 public static void saveConfigToPrefernceStore(NameDetailsConfigurator config) {
1315 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION,
1316 config.isSimpleDetailsViewActivated());
1317 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_TAXON, config.isTaxonSectionActivated());
1318 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_LSID, config.isLSIDActivated());
1319 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE,
1320 config.isNomenclaturalCodeActived());
1321 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE,
1322 config.isNameCacheActivated());
1323 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE,
1324 config.isAppendedPhraseActivated());
1325 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK, config.isRankActivated());
1326 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS,
1327 config.isAtomisedEpithetsActivated());
1328 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP_CACHE,
1329 config.isAuthorCacheActivated());
1330 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP,
1331 config.isAuthorshipSectionActivated());
1332 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE,
1333 config.isNomenclaturalReferenceSectionActivated());
1334 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS,
1335 config.isNomenclaturalStatusSectionActivated());
1336 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_PROTOLOGUE,
1337 config.isProtologueActivated());
1338 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION,
1339 config.isTypeDesignationSectionActivated());
1340 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP,
1341 config.isNameRelationsSectionActivated());
1342 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_HYBRID,
1343 config.isHybridActivated());
1344
1345 }
1346
1347 /**
1348 * @return
1349 */
1350 public static boolean isSortTaxaByRankAndName() {
1351
1352 return getPreferenceStore().getBoolean(IPreferenceKeys.SORT_TAXA_BY_RANK_AND_NAME);
1353 }
1354
1355 /**
1356 * @return
1357 */
1358 public static boolean isSortNamedAreaByOrderInVocabulary() {
1359
1360 return getPreferenceStore().getBoolean(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER);
1361 }
1362
1363 public static void setSortNamedAreasByOrderInVocabulary(boolean isSortByVocabularyOrder) {
1364 getPreferenceStore().setValue(IPreferenceKeys.SORT_NAMED_AREA_BY_VOCABULARY_ORDER, isSortByVocabularyOrder);
1365
1366 }
1367
1368 /**
1369 * <p>
1370 * setPreferredNamedAreasForDistributionEditor
1371 * </p>
1372 *
1373 * @param saveCheckedElements
1374 * @param saveGrayedElements
1375 */
1376 public static void setLastSelectedReference(
1377 List<String> lastSelectedReferences) {
1378
1379 IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1380 preferenceStore.setValue(PreferencesUtil.LAST_SELECTED_REFERENCES, lastSelectedReferences.toString());
1381 }
1382
1383 /**
1384 * <p>
1385 * setPreferredNamedAreasForDistributionEditor
1386 * </p>
1387 *
1388 * @param saveCheckedElements
1389 * @param saveGrayedElements
1390 */
1391 public static List<String> getLastSelectedReferences() {
1392
1393 IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1394 String lastSelected = preferenceStore.getString(PreferencesUtil.LAST_SELECTED_REFERENCES);
1395 List<String> result = new ArrayList<>();
1396 if (!StringUtils.isBlank(lastSelected)){
1397 Collections.addAll(result, lastSelected.substring(1,lastSelected.length()-1).split(", "));
1398 }
1399 return result;
1400 }
1401
1402
1403 /**
1404 * <p>
1405 * setPreferredNamedAreasForDistributionEditor
1406 * </p>
1407 *
1408 * @param saveCheckedElements
1409 * @param saveGrayedElements
1410 */
1411 public static void setPreferredNamedAreasForDistributionEditor(
1412 String saveCheckedElements, String saveGrayedElements, boolean local) {
1413 if (local){
1414 IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1415 preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS, saveCheckedElements);
1416 preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS_GRAYED, saveGrayedElements);
1417
1418 }
1419 else{
1420 // ICdmRepository controller;
1421
1422 // PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms);
1423 CdmPreference preference = null;
1424
1425 if (saveCheckedElements == null){
1426 preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaTerms);
1427
1428 if (preference == null){
1429 return ;
1430 } else{
1431 getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1432 saveCheckedElements);
1433 preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1434 setPreferenceToDB(preference);
1435
1436 }
1437 } else{
1438 preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaTerms, saveCheckedElements);
1439 setPreferenceToDB(preference);
1440 getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1441 saveCheckedElements);
1442
1443 }
1444 }
1445
1446 }
1447
1448 /**
1449 * @param saveCheckedElements
1450 * @param saveCheckedElements2
1451 * @param b
1452 */
1453 public static void setPreferredVocabulariesForDistributionEditor(String saveCheckedElements,
1454 boolean local) {
1455 if (local){
1456 IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
1457 preferenceStore.setValue(PreferencesUtil.DISTRIBUTION_VOCABULARIES, saveCheckedElements);
1458
1459
1460 }
1461 else{
1462 ICdmRepository controller;
1463 CdmPreference preference = null;
1464
1465 if (saveCheckedElements == null){
1466 preference = getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies);
1467
1468 if (preference == null){
1469 return ;
1470 } else{
1471 getPreferenceStore().setValue(DISTRIBUTION_VOCABULARIES,
1472 saveCheckedElements);
1473 preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1474 setPreferenceToDB(preference);
1475
1476
1477 }
1478 } else{
1479 preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.AvailableDistributionAreaVocabularies, saveCheckedElements);
1480 setPreferenceToDB(preference);
1481 getPreferenceStore().setValue(DISTRIBUTION_AREA_OCCURENCE_STATUS,
1482 saveCheckedElements);
1483
1484 }
1485 }
1486
1487
1488
1489 }
1490
1491
1492
1493
1494
1495 }