Implemented some of the import/export functionality. Major refactoring of datasource...
[taxeditor.git] / 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.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.jface.preference.IPreferenceStore;
18
19 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
20 import eu.etaxonomy.cdm.model.common.TermBase;
21 import eu.etaxonomy.cdm.model.description.Feature;
22 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
23 import eu.etaxonomy.cdm.model.name.BotanicalName;
24 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
25 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
26 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
27 import eu.etaxonomy.cdm.model.name.NonViralName;
28 import eu.etaxonomy.cdm.model.name.Rank;
29 import eu.etaxonomy.cdm.model.name.ZoologicalName;
30 import eu.etaxonomy.taxeditor.model.Resources;
31 import eu.etaxonomy.taxeditor.store.VocabularyStore;
32 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
33
34 /**
35 * @author p.ciardelli
36 * @created 05.12.2008
37 * @version 1.0
38 * @author n.hoffmann
39 */
40 public class PreferencesUtil {
41 private static final Logger logger = Logger
42 .getLogger(PreferencesUtil.class);
43
44 /**
45 *
46 * @return
47 */
48 public static IPreferenceStore getPreferenceStore() {
49 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
50 }
51
52 /**
53 *
54 * @return
55 */
56 private static String getNameCodePreference() {
57 try{
58 return getPreferenceStore().getString(Resources.CODE_PREFERENCE);
59 }catch(NullPointerException e){
60 logger.warn("PreferenceStore was not available. That is OK if you are running a unit test. Setting ICBN as default code.");
61 return Resources.CODE_PREFERENCE_ICBN;
62 }
63 }
64
65 /**
66 *
67 * @param preferredCode
68 */
69 public static void setNomenclaturalCode(String preferredCode) {
70 getPreferenceStore().setValue(Resources.CODE_PREFERENCE, preferredCode);
71 }
72
73 /**
74 *
75 */
76 public static void setDefaultNomenclaturalCode() {
77 setNomenclaturalCode(Resources.DEFAULT_CODE_PREFERENCE);
78 }
79
80 /**
81 *
82 * @return
83 */
84 public static String getPreferredNomenclaturalCodeAsString() {
85 String nameCodePreference = getNameCodePreference();
86
87 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
88 return "International Code of Botanical Nomenclature (ICBN)";
89 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
90 return "International Code of Zoological Nomenclature (ICZN)";
91 }
92 return null;
93 }
94
95 /**
96 * @return
97 */
98 public static NomenclaturalCode getPreferredNomenclaturalCode() {
99
100 String nameCodePreference = getNameCodePreference();
101
102 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
103 return NomenclaturalCode.ICBN;
104 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
105 return NomenclaturalCode.ICZN;
106 }
107 return null;
108 }
109
110
111 /**
112 * Returns a new name object that conforms to the nomenclatural
113 * code specified in user preferences.
114 *
115 * @return
116 */
117 public static NonViralName<?> getInstanceOfPreferredNameClass() {
118 NomenclaturalCode code = getPreferredNomenclaturalCode();
119
120 // Check whether name code preference needs to be initialized
121 if (code == null) {
122 setDefaultNomenclaturalCode();
123 }
124
125 if (code.equals(NomenclaturalCode.ICBN)) {
126 return BotanicalName.NewInstance(null);
127 } else if (code.equals(NomenclaturalCode.ICZN)) {
128 return ZoologicalName.NewInstance(null);
129 }
130 return NonViralName.NewInstance(null);
131 }
132
133 /**
134 * Generic method to get term preferences for a term vocabulary
135 *
136 * @param <T>
137 * @param initialFeatures
138 * @return
139 */
140 public static <T extends DefinedTermBase> List<T> getPreferredTerms(Set<T> initialFeatures){
141 List<T> preferredTerms = new ArrayList<T>();
142
143 for (T term : initialFeatures){
144 if(getPreferenceStore().getBoolean(getPreferenceKey(term))){
145 preferredTerms.add(term);
146 }
147 }
148
149 return preferredTerms;
150 }
151
152 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
153 public static List<Feature> getPreferredFeatures() { return getPreferredTerms(VocabularyStore.getFeatures());}
154
155 /** @return a <code>List</code> containing user preferred <code>Rank</code> terms */
156 public static List<Rank> getPreferredRanks() { return getPreferredTerms(VocabularyStore.getRanks());}
157
158 /** @return a <code>List</code> containing user preferred <code>PresenceAbsenceTermBase</code> terms */
159 public static List<PresenceAbsenceTermBase<PresenceAbsenceTermBase<?>>> getPreferredPresenceAbsenceTerms(){ return getPreferredTerms(VocabularyStore.getPresenceAbsenceTerms());}
160
161 /** @return a <code>List</code> containing user preferred <code>NomenclaturalStatusType</code> terms */
162 public static List<NomenclaturalStatusType> getPreferredNomenclaturalStatusTypes(){ return getPreferredTerms(VocabularyStore.getNomenclaturalStatusTypes());}
163
164 /** @return a <code>List</code> containing user preferred <code>NameRelationshipType</code> terms */
165 public static List<NameRelationshipType> getPreferredNameRelationshipTypes(){ return getPreferredTerms(VocabularyStore.getNameRelationshipTypes());}
166
167
168 /**
169 * Generic method to set term preferences
170 *
171 * @param preferredTerms
172 * @param initalTerms
173 */
174 public static <T extends DefinedTermBase> void setPreferredTerms(List<T> preferredTerms, Set<T> initalTerms){
175 for(TermBase term : initalTerms){
176 getPreferenceStore().setValue(getPreferenceKey(term), preferredTerms.contains(term));
177 }
178 }
179
180 /**
181 * Construct a unique key using the terms uuid
182 *
183 * @param term
184 * @return
185 */
186 private static String getPreferenceKey(TermBase term) {
187 return term.getClass().getName()
188 . concat(".")
189 . concat(term.getUuid().toString());
190 }
191 }