Revision 3be6ef3e
Added by Niels Hoffmann over 13 years ago
taxeditor-store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.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.datasource; |
|
11 |
|
|
12 |
import java.io.File; |
|
13 |
import java.io.FileNotFoundException; |
|
14 |
import java.util.ArrayList; |
|
15 |
import java.util.List; |
|
16 |
|
|
17 |
import org.apache.log4j.Logger; |
|
18 |
import org.eclipse.core.runtime.IPath; |
|
19 |
import org.eclipse.ui.IMemento; |
|
20 |
import org.eclipse.ui.XMLMemento; |
|
21 |
|
|
22 |
import eu.etaxonomy.cdm.database.CdmDataSource; |
|
23 |
import eu.etaxonomy.cdm.database.CdmPersistentDataSource; |
|
24 |
import eu.etaxonomy.cdm.database.DataSourceNotFoundException; |
|
25 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
|
26 |
import eu.etaxonomy.cdm.model.name.NomenclaturalCode; |
|
27 |
import eu.etaxonomy.taxeditor.model.MementoHelper; |
|
28 |
import eu.etaxonomy.taxeditor.preference.PreferencesUtil; |
|
29 |
import eu.etaxonomy.taxeditor.store.CdmStore; |
|
30 |
import eu.etaxonomy.taxeditor.store.StoreUtil; |
|
31 |
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin; |
|
32 |
|
|
33 |
public class CdmDataSourceRepository{; |
|
34 |
|
|
35 |
public static final String TAG_DATASOURCE = "tagDataSource"; |
|
36 |
private static final String CURRENT_DATASOURCE = "currentDataSource"; |
|
37 |
private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml"; |
|
38 |
private static final String DEFAULT_DATASOURCE_NAME = "cdm"; |
|
39 |
|
|
40 |
private static ICdmDataSource currentDataSource; |
|
41 |
private static IMemento memento; |
|
42 |
private static String lastUsedDataSourceName; |
|
43 |
public static String getLastUsedDataSourceName(){ |
|
44 |
if(lastUsedDataSourceName == null){ |
|
45 |
memento = readMemento(); |
|
46 |
lastUsedDataSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME; |
|
47 |
} |
|
48 |
return lastUsedDataSourceName; |
|
49 |
} |
|
50 |
|
|
51 |
public static boolean delete(CdmPersistentDataSource dataSource) { |
|
52 |
CdmPersistentDataSource.delete(dataSource); |
|
53 |
return true; |
|
54 |
} |
|
55 |
|
|
56 |
|
|
57 |
public static List<ICdmDataSource> getAll() { |
|
58 |
List<ICdmDataSource> dataSources = new ArrayList<ICdmDataSource>(); |
|
59 |
|
|
60 |
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){ |
|
61 |
try { |
|
62 |
dataSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName())); |
|
63 |
} catch (DataSourceNotFoundException e) { |
|
64 |
StoreUtil.error(CdmDataSourceRepository.class, "Could not find dataSource", e); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
// TODO sort by database name |
|
69 |
|
|
70 |
return dataSources; |
|
71 |
} |
|
72 |
|
|
73 |
public static ICdmDataSource getDataSource(String name){ |
|
74 |
|
|
75 |
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){ |
|
76 |
try { |
|
77 |
if(dataSource.getName().equals(name)){ |
|
78 |
return CdmPersistentDataSource.NewInstance(dataSource.getName()); |
|
79 |
} |
|
80 |
} catch (DataSourceNotFoundException e) { |
|
81 |
StoreUtil.error(CdmDataSourceRepository.class, "Could not find dataSource", e); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
return null; |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* |
|
90 |
* @return |
|
91 |
*/ |
|
92 |
public static ICdmDataSource getCurrentDataSource() { |
|
93 |
if (currentDataSource == null) { |
|
94 |
try { |
|
95 |
currentDataSource = CdmPersistentDataSource.NewInstance(getLastUsedDataSourceName()); |
|
96 |
} catch (DataSourceNotFoundException e) { |
|
97 |
// fallback creates a new default |
|
98 |
ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance( |
|
99 |
DEFAULT_DATASOURCE_NAME, "sa", "", PreferencesUtil.getPreferredNomenclaturalCode()); |
|
100 |
save(h2DataSource.getName(), h2DataSource); |
|
101 |
setCurrentDataSource(h2DataSource); |
|
102 |
} |
|
103 |
} |
|
104 |
return currentDataSource; |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* |
|
109 |
* @param dataSource |
|
110 |
* @return |
|
111 |
*/ |
|
112 |
public static boolean setCurrentDataSource(ICdmDataSource dataSource) { |
|
113 |
currentDataSource = dataSource; |
|
114 |
NomenclaturalCode dataSourceNomenclaturalCode = dataSource.getNomenclaturalCode(); |
|
115 |
NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode(); |
|
116 |
|
|
117 |
if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){ |
|
118 |
PreferencesUtil.setPreferredNomenclaturalCode(dataSourceNomenclaturalCode); |
|
119 |
StoreUtil.informationDialog("Nomenclatural Code Change", "The Datasource that was just " + |
|
120 |
"loaded has a different nomenclatural code than the one stored in Preferences. " + |
|
121 |
"The nomenclatural code was changed in the application."); |
|
122 |
} |
|
123 |
return true; |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* |
|
128 |
* @param dataSource |
|
129 |
* @return |
|
130 |
*/ |
|
131 |
public static boolean changeDataSource(final ICdmDataSource dataSource) { |
|
132 |
saveAsCurrentDatabaseToMemento(dataSource); |
|
133 |
|
|
134 |
CdmStore.connect(dataSource); |
|
135 |
|
|
136 |
return true; |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* @param dataSource |
|
141 |
* @return |
|
142 |
*/ |
|
143 |
public static CdmPersistentDataSource save(String strDataSourceName, ICdmDataSource dataSource) { |
|
144 |
return CdmPersistentDataSource.save(strDataSourceName, dataSource); |
|
145 |
} |
|
146 |
|
|
147 |
public static CdmPersistentDataSource update(String strDataSourceName, ICdmDataSource dataSource){ |
|
148 |
try { |
|
149 |
return CdmPersistentDataSource.update(strDataSourceName, dataSource); |
|
150 |
} catch (Exception e) { |
|
151 |
StoreUtil.error(CdmDataSourceRepository.class, "Error updating datasource", e); |
|
152 |
} |
|
153 |
return null; |
|
154 |
} |
|
155 |
|
|
156 |
/********************************************************* |
|
157 |
* Memento Handling * |
|
158 |
*********************************************************/ |
|
159 |
|
|
160 |
private static void saveAsCurrentDatabaseToMemento(ICdmDataSource dataSource){ |
|
161 |
if(memento == null) |
|
162 |
memento = readMemento(); |
|
163 |
memento.putString(CURRENT_DATASOURCE, dataSource.getName()); |
|
164 |
saveMementoToFile(memento); |
|
165 |
} |
|
166 |
|
|
167 |
/* |
|
168 |
* Answer the workbench state file. |
|
169 |
*/ |
|
170 |
private static File getDataSourceStateFile() { |
|
171 |
IPath path = TaxeditorStorePlugin.getDefault().getStateLocation(); |
|
172 |
if (path == null) { |
|
173 |
return null; |
|
174 |
} |
|
175 |
path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME); |
|
176 |
return path.toFile(); |
|
177 |
} |
|
178 |
|
|
179 |
private static IMemento readMemento(){ |
|
180 |
try { |
|
181 |
return MementoHelper.readMementoFromFile(getDataSourceStateFile()); |
|
182 |
} catch (FileNotFoundException e) { |
|
183 |
return initializeMemento(); |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* |
|
189 |
*/ |
|
190 |
private static IMemento initializeMemento() { |
|
191 |
|
|
192 |
XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE); |
|
193 |
memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME); |
|
194 |
saveMementoToFile(memento); |
|
195 |
|
|
196 |
return readMemento(); |
|
197 |
} |
|
198 |
|
|
199 |
/* |
|
200 |
* Save the workbench UI in a persistence file. |
|
201 |
*/ |
|
202 |
private static IMemento saveMementoToFile(IMemento memento) { |
|
203 |
return MementoHelper.saveMementoToFile(memento, getDataSourceStateFile()); |
|
204 |
} |
|
205 |
} |
|
1 |
|
|
2 |
/** |
|
3 |
* Copyright (C) 2007 EDIT |
|
4 |
* European Distributed Institute of Taxonomy |
|
5 |
* http://www.e-taxonomy.eu |
|
6 |
* |
|
7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
9 |
* |
|
10 |
* @author n.hoffmann |
|
11 |
* @version $Id: $ |
|
12 |
*/ |
|
13 |
|
|
14 |
package eu.etaxonomy.taxeditor.datasource; |
|
15 |
|
|
16 |
import java.io.File; |
|
17 |
import java.io.FileNotFoundException; |
|
18 |
import java.util.ArrayList; |
|
19 |
import java.util.List; |
|
20 |
|
|
21 |
import org.eclipse.core.runtime.IPath; |
|
22 |
import org.eclipse.ui.IMemento; |
|
23 |
import org.eclipse.ui.XMLMemento; |
|
24 |
|
|
25 |
import eu.etaxonomy.cdm.database.CdmDataSource; |
|
26 |
import eu.etaxonomy.cdm.database.CdmPersistentDataSource; |
|
27 |
import eu.etaxonomy.cdm.database.DataSourceNotFoundException; |
|
28 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
|
29 |
import eu.etaxonomy.cdm.model.name.NomenclaturalCode; |
|
30 |
import eu.etaxonomy.taxeditor.model.MementoHelper; |
|
31 |
import eu.etaxonomy.taxeditor.preference.PreferencesUtil; |
|
32 |
import eu.etaxonomy.taxeditor.store.CdmStore; |
|
33 |
import eu.etaxonomy.taxeditor.store.StoreUtil; |
|
34 |
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin; |
|
35 |
public class CdmDataSourceRepository{; |
|
36 |
|
|
37 |
/** Constant <code>TAG_DATASOURCE="tagDataSource"</code> */ |
|
38 |
public static final String TAG_DATASOURCE = "tagDataSource"; |
|
39 |
private static final String CURRENT_DATASOURCE = "currentDataSource"; |
|
40 |
private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml"; |
|
41 |
private static final String DEFAULT_DATASOURCE_NAME = "cdm"; |
|
42 |
|
|
43 |
private static ICdmDataSource currentDataSource; |
|
44 |
private static IMemento memento; |
|
45 |
private static String lastUsedDataSourceName; |
|
46 |
|
|
47 |
/** |
|
48 |
* <p>Getter for the field <code>lastUsedDataSourceName</code>.</p> |
|
49 |
* |
|
50 |
* @return a {@link java.lang.String} object. |
|
51 |
*/ |
|
52 |
public static String getLastUsedDataSourceName(){ |
|
53 |
if(lastUsedDataSourceName == null){ |
|
54 |
memento = readMemento(); |
|
55 |
lastUsedDataSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME; |
|
56 |
} |
|
57 |
return lastUsedDataSourceName; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* <p>delete</p> |
|
62 |
* |
|
63 |
* @param dataSource a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. |
|
64 |
* @return a boolean. |
|
65 |
*/ |
|
66 |
public static boolean delete(CdmPersistentDataSource dataSource) { |
|
67 |
CdmPersistentDataSource.delete(dataSource); |
|
68 |
return true; |
|
69 |
} |
|
70 |
|
|
71 |
|
|
72 |
/** |
|
73 |
* <p>getAll</p> |
|
74 |
* |
|
75 |
* @return a {@link java.util.List} object. |
|
76 |
*/ |
|
77 |
public static List<ICdmDataSource> getAll() { |
|
78 |
List<ICdmDataSource> dataSources = new ArrayList<ICdmDataSource>(); |
|
79 |
|
|
80 |
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){ |
|
81 |
try { |
|
82 |
dataSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName())); |
|
83 |
} catch (DataSourceNotFoundException e) { |
|
84 |
StoreUtil.error(CdmDataSourceRepository.class, "Could not find dataSource", e); |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
// TODO sort by database name |
|
89 |
|
|
90 |
return dataSources; |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* <p>getDataSource</p> |
|
95 |
* |
|
96 |
* @param name a {@link java.lang.String} object. |
|
97 |
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. |
|
98 |
*/ |
|
99 |
public static ICdmDataSource getDataSource(String name){ |
|
100 |
|
|
101 |
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){ |
|
102 |
try { |
|
103 |
if(dataSource.getName().equals(name)){ |
|
104 |
return CdmPersistentDataSource.NewInstance(dataSource.getName()); |
|
105 |
} |
|
106 |
} catch (DataSourceNotFoundException e) { |
|
107 |
StoreUtil.error(CdmDataSourceRepository.class, "Could not find dataSource", e); |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
return null; |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* <p>Getter for the field <code>currentDataSource</code>.</p> |
|
116 |
* |
|
117 |
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. |
|
118 |
*/ |
|
119 |
public static ICdmDataSource getCurrentDataSource() { |
|
120 |
if (currentDataSource == null) { |
|
121 |
try { |
|
122 |
currentDataSource = CdmPersistentDataSource.NewInstance(getLastUsedDataSourceName()); |
|
123 |
} catch (DataSourceNotFoundException e) { |
|
124 |
// fallback creates a new default |
|
125 |
ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance( |
|
126 |
DEFAULT_DATASOURCE_NAME, "sa", "", PreferencesUtil.getPreferredNomenclaturalCode()); |
|
127 |
save(h2DataSource.getName(), h2DataSource); |
|
128 |
setCurrentDataSource(h2DataSource); |
|
129 |
} |
|
130 |
} |
|
131 |
return currentDataSource; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* <p>Setter for the field <code>currentDataSource</code>.</p> |
|
136 |
* |
|
137 |
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. |
|
138 |
* @return a boolean. |
|
139 |
*/ |
|
140 |
public static boolean setCurrentDataSource(ICdmDataSource dataSource) { |
|
141 |
currentDataSource = dataSource; |
|
142 |
NomenclaturalCode dataSourceNomenclaturalCode = dataSource.getNomenclaturalCode(); |
|
143 |
NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode(); |
|
144 |
|
|
145 |
if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){ |
|
146 |
PreferencesUtil.setPreferredNomenclaturalCode(dataSourceNomenclaturalCode); |
|
147 |
StoreUtil.informationDialog("Nomenclatural Code Change", "The Datasource that was just " + |
|
148 |
"loaded has a different nomenclatural code than the one stored in Preferences. " + |
|
149 |
"The nomenclatural code was changed in the application."); |
|
150 |
} |
|
151 |
return true; |
|
152 |
} |
|
153 |
|
|
154 |
/** |
|
155 |
* <p>changeDataSource</p> |
|
156 |
* |
|
157 |
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. |
|
158 |
* @return a boolean. |
|
159 |
*/ |
|
160 |
public static boolean changeDataSource(final ICdmDataSource dataSource) { |
|
161 |
saveAsCurrentDatabaseToMemento(dataSource); |
|
162 |
|
|
163 |
CdmStore.connect(dataSource); |
|
164 |
|
|
165 |
return true; |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* <p>save</p> |
|
170 |
* |
|
171 |
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. |
|
172 |
* @param strDataSourceName a {@link java.lang.String} object. |
|
173 |
* @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. |
|
174 |
*/ |
|
175 |
public static CdmPersistentDataSource save(String strDataSourceName, ICdmDataSource dataSource) { |
|
176 |
return CdmPersistentDataSource.save(strDataSourceName, dataSource); |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* <p>update</p> |
|
181 |
* |
|
182 |
* @param strDataSourceName a {@link java.lang.String} object. |
|
183 |
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. |
|
184 |
* @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. |
|
185 |
*/ |
|
186 |
public static CdmPersistentDataSource update(String strDataSourceName, ICdmDataSource dataSource){ |
|
187 |
try { |
|
188 |
return CdmPersistentDataSource.update(strDataSourceName, dataSource); |
|
189 |
} catch (Exception e) { |
|
190 |
StoreUtil.error(CdmDataSourceRepository.class, "Error updating datasource", e); |
|
191 |
} |
|
192 |
return null; |
|
193 |
} |
|
194 |
|
|
195 |
/********************************************************* |
|
196 |
* Memento Handling * |
|
197 |
*********************************************************/ |
|
198 |
|
|
199 |
private static void saveAsCurrentDatabaseToMemento(ICdmDataSource dataSource){ |
|
200 |
if(memento == null) |
|
201 |
memento = readMemento(); |
|
202 |
memento.putString(CURRENT_DATASOURCE, dataSource.getName()); |
|
203 |
saveMementoToFile(memento); |
|
204 |
} |
|
205 |
|
|
206 |
/* |
|
207 |
* Answer the workbench state file. |
|
208 |
*/ |
|
209 |
private static File getDataSourceStateFile() { |
|
210 |
IPath path = TaxeditorStorePlugin.getDefault().getStateLocation(); |
|
211 |
if (path == null) { |
|
212 |
return null; |
|
213 |
} |
|
214 |
path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME); |
|
215 |
return path.toFile(); |
|
216 |
} |
|
217 |
|
|
218 |
private static IMemento readMemento(){ |
|
219 |
try { |
|
220 |
return MementoHelper.readMementoFromFile(getDataSourceStateFile()); |
|
221 |
} catch (FileNotFoundException e) { |
|
222 |
return initializeMemento(); |
|
223 |
} |
|
224 |
} |
|
225 |
|
|
226 |
|
|
227 |
private static IMemento initializeMemento() { |
|
228 |
|
|
229 |
XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE); |
|
230 |
memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME); |
|
231 |
saveMementoToFile(memento); |
|
232 |
|
|
233 |
return readMemento(); |
|
234 |
} |
|
235 |
|
|
236 |
/* |
|
237 |
* Save the workbench UI in a persistence file. |
|
238 |
*/ |
|
239 |
private static IMemento saveMementoToFile(IMemento memento) { |
|
240 |
return MementoHelper.saveMementoToFile(memento, getDataSourceStateFile()); |
|
241 |
} |
|
242 |
} |
Also available in: Unified diff
performed javacscript:fix and worked on documentation