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
|
* @author n.hoffmann
|
10
|
*/
|
11
|
package eu.etaxonomy.taxeditor.datasource;
|
12
|
|
13
|
import java.io.File;
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
|
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.config.CdmPersistentSourceUtils;
|
23
|
import eu.etaxonomy.cdm.config.ICdmPersistentSource;
|
24
|
import eu.etaxonomy.cdm.config.ICdmSource;
|
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.taxeditor.model.MementoHelper;
|
30
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
31
|
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
|
32
|
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
|
33
|
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
|
34
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
35
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
36
|
public class CdmDataSourceRepository{
|
37
|
|
38
|
/** Constant <code>TAG_DATASOURCE="tagDataSource"</code> */
|
39
|
public static final String TAG_DATASOURCE = "tagDataSource";
|
40
|
private static final String CURRENT_DATASOURCE = "currentDataSource";
|
41
|
private static final String CURRENT_DATASOURCE_POSTFIX = "currentDataSourcePostFix";
|
42
|
private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml";
|
43
|
|
44
|
private static final String DEFAULT_DATASOURCE_NAME = "cdm";
|
45
|
private static final String DEFAULT_DATASOURCE_POSTFIX = CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX;
|
46
|
|
47
|
private static ICdmSource currentCdmSource;
|
48
|
private static IMemento memento;
|
49
|
|
50
|
private static String lastUsedCdmSourceName;
|
51
|
private static String lastUsedCdmSourcePostFix;
|
52
|
|
53
|
/**
|
54
|
* <p>Getter for the field <code>lastUsedDataSourceName</code>.</p>
|
55
|
*
|
56
|
* @return a {@link java.lang.String} object.
|
57
|
*/
|
58
|
public static void updateLastUsedDataSource(){
|
59
|
if(lastUsedCdmSourceName == null){
|
60
|
memento = readMemento();
|
61
|
lastUsedCdmSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME;
|
62
|
lastUsedCdmSourcePostFix = memento != null ? memento.getString(CURRENT_DATASOURCE_POSTFIX) : DEFAULT_DATASOURCE_POSTFIX;
|
63
|
|
64
|
}
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* <p>delete</p>
|
69
|
*
|
70
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
|
71
|
* @return a boolean.
|
72
|
*/
|
73
|
public static boolean delete(ICdmPersistentSource cdmPersistentSource) {
|
74
|
CdmPersistentSourceUtils.delete(cdmPersistentSource);
|
75
|
return true;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* <p>getAll</p>
|
80
|
*
|
81
|
* @return a {@link java.util.List} object.
|
82
|
*/
|
83
|
public static List<ICdmSource> getAll() {
|
84
|
List<ICdmSource> remoteSources = new ArrayList<>();
|
85
|
|
86
|
if (CdmPersistentDataSource.getAllDataSources() == null){
|
87
|
MessagingUtils.errorDialog("Could not read root element", CdmPersistentDataSource.class,
|
88
|
"Could not read element in datasources.xml in .cdmLibrary folder. Maybe your datasources.xml file is broken.",
|
89
|
TaxeditorStorePlugin.PLUGIN_ID, null, true);
|
90
|
return remoteSources;
|
91
|
}
|
92
|
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
|
93
|
try {
|
94
|
remoteSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName()));
|
95
|
} catch (DataSourceNotFoundException e) {
|
96
|
MessagingUtils.error(CdmDataSourceRepository.class, "Could not find dataSource", e);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
try {
|
101
|
for(ICdmRemoteSource remoteSource : CdmPersistentRemoteSource.getAllRemoteSources()){
|
102
|
remoteSources.add(remoteSource);
|
103
|
}
|
104
|
} catch (CdmRemoteSourceException e) {
|
105
|
MessagingUtils.error(CdmDataSourceRepository.class, "Error retrieving remote sources", e);
|
106
|
}
|
107
|
|
108
|
// TODO sort by database name
|
109
|
|
110
|
return remoteSources;
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* <p>getDataSource</p>
|
115
|
*
|
116
|
* @param name a {@link java.lang.String} object.
|
117
|
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
118
|
*/
|
119
|
public static ICdmDataSource getDataSource(String name){
|
120
|
|
121
|
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
|
122
|
try {
|
123
|
if(dataSource.getName().equals(name)){
|
124
|
return CdmPersistentDataSource.NewInstance(dataSource.getName());
|
125
|
}
|
126
|
} catch (DataSourceNotFoundException e) {
|
127
|
MessagingUtils.error(CdmDataSourceRepository.class, "Could not find dataSource", e);
|
128
|
}
|
129
|
}
|
130
|
|
131
|
return null;
|
132
|
}
|
133
|
|
134
|
|
135
|
/**
|
136
|
* <p>Getter for the field <code>currentDataSource</code>.</p>
|
137
|
*
|
138
|
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
139
|
* @throws CdmRemoteSourceException
|
140
|
*/
|
141
|
public static ICdmSource getCurrentCdmSource() throws CdmRemoteSourceException {
|
142
|
|
143
|
if (currentCdmSource == null) {
|
144
|
updateLastUsedDataSource();
|
145
|
|
146
|
if(lastUsedCdmSourceName == null) {
|
147
|
return null;
|
148
|
}
|
149
|
// for post fix = null , source is by default a data source
|
150
|
if(lastUsedCdmSourcePostFix == null || lastUsedCdmSourcePostFix.equals(CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX)) {
|
151
|
try {
|
152
|
currentCdmSource = CdmPersistentDataSource.NewInstance(lastUsedCdmSourceName);
|
153
|
} catch (DataSourceNotFoundException e) {
|
154
|
// fallback creates a new default
|
155
|
setCurrentCdmSource(createDefaultH2DataSource());
|
156
|
}
|
157
|
} else if (lastUsedCdmSourcePostFix.equals(CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX)) {
|
158
|
currentCdmSource = CdmPersistentRemoteSource.NewInstance(lastUsedCdmSourceName);
|
159
|
} else {
|
160
|
throw new CdmRemoteSourceException("Unkown Cdm Source Type");
|
161
|
}
|
162
|
}
|
163
|
return currentCdmSource;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Creates a default H2 CDM Data Source
|
168
|
*
|
169
|
* @return the newly created data source
|
170
|
*/
|
171
|
public static ICdmDataSource createDefaultH2DataSource() {
|
172
|
ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
|
173
|
DEFAULT_DATASOURCE_NAME, "sa", "");
|
174
|
save(h2DataSource.getName(), h2DataSource);
|
175
|
return h2DataSource;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* <p>Setter for the field <code>currentDataSource</code>.</p>
|
180
|
*
|
181
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
182
|
* @return a boolean.
|
183
|
*/
|
184
|
public static boolean setCurrentCdmSource(ICdmSource cdmSource) {
|
185
|
currentCdmSource = cdmSource;
|
186
|
// NomenclaturalCode dataSourceNomenclaturalCode = cdmSource.getNomenclaturalCode();
|
187
|
// NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode(true);
|
188
|
//
|
189
|
// if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){
|
190
|
// PreferencesUtil.setPreferredNomenclaturalCode(dataSourceNomenclaturalCode, true);
|
191
|
// MessagingUtils.informationDialog("Nomenclatural Code Change", "The Datasource that was just " +
|
192
|
// "loaded has a different nomenclatural code than the one stored in Preferences. " +
|
193
|
// "The nomenclatural code was changed in the application.");
|
194
|
// }
|
195
|
return true;
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* <p>changeDataSource</p>
|
200
|
*
|
201
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
202
|
* @return a boolean.
|
203
|
*/
|
204
|
public static boolean changeDataSource(final ICdmSource cdmSource) {
|
205
|
saveAsCurrentDatabaseToMemento(cdmSource);
|
206
|
CdmStore.connect(cdmSource);
|
207
|
|
208
|
return true;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* <p>save</p>
|
213
|
*
|
214
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
215
|
* @param strDataSourceName a {@link java.lang.String} object.
|
216
|
* @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
|
217
|
* @throws CdmRemoteSourceException
|
218
|
*/
|
219
|
public static ICdmPersistentSource save(String strCdmSourceName, ICdmRemoteSource cdmSource) throws CdmRemoteSourceException {
|
220
|
return CdmPersistentRemoteSource.save(strCdmSourceName, cdmSource);
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* <p>save</p>
|
225
|
*
|
226
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
227
|
* @param strDataSourceName a {@link java.lang.String} object.
|
228
|
* @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
|
229
|
* @throws CdmRemoteSourceException
|
230
|
*/
|
231
|
public static ICdmPersistentSource save(String strCdmSourceName, ICdmDataSource cdmSource) {
|
232
|
return CdmPersistentDataSource.save(strCdmSourceName, cdmSource);
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* <p>update</p>
|
237
|
*
|
238
|
* @param strDataSourceName a {@link java.lang.String} object.
|
239
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
240
|
* @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
|
241
|
*/
|
242
|
public static ICdmPersistentSource update(String strCdmSourceName, ICdmDataSource cdmSource){
|
243
|
try {
|
244
|
return CdmPersistentDataSource.update(strCdmSourceName, cdmSource);
|
245
|
} catch (Exception e) {
|
246
|
MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
|
247
|
}
|
248
|
return null;
|
249
|
}
|
250
|
|
251
|
public static ICdmPersistentSource replace(String strCdmSourceName, ICdmDataSource cdmSource){
|
252
|
try {
|
253
|
return CdmPersistentDataSource.replace(strCdmSourceName, cdmSource);
|
254
|
} catch (Exception e) {
|
255
|
MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
|
256
|
}
|
257
|
return null;
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* <p>update</p>
|
262
|
*
|
263
|
* @param strDataSourceName a {@link java.lang.String} object.
|
264
|
* @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
265
|
* @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object.
|
266
|
*/
|
267
|
public static ICdmPersistentSource update(String strCdmSourceName, ICdmRemoteSource cdmSource){
|
268
|
try {
|
269
|
return CdmPersistentRemoteSource.update(strCdmSourceName, cdmSource);
|
270
|
} catch (Exception e) {
|
271
|
MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
|
272
|
|
273
|
}
|
274
|
return null;
|
275
|
}
|
276
|
|
277
|
/*********************************************************
|
278
|
* Memento Handling *
|
279
|
*********************************************************/
|
280
|
|
281
|
private static void saveAsCurrentDatabaseToMemento(ICdmSource cdmSource){
|
282
|
if(memento == null) {
|
283
|
memento = readMemento();
|
284
|
}
|
285
|
memento.putString(CURRENT_DATASOURCE, cdmSource.getName());
|
286
|
|
287
|
// Set postfix to distinguish between data and remote sources
|
288
|
if(cdmSource instanceof CdmPersistentRemoteSource) {
|
289
|
memento.putString(CURRENT_DATASOURCE_POSTFIX, CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX);
|
290
|
} else {
|
291
|
memento.putString(CURRENT_DATASOURCE_POSTFIX, CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX);
|
292
|
}
|
293
|
|
294
|
|
295
|
saveMementoToFile(memento);
|
296
|
}
|
297
|
|
298
|
/*
|
299
|
* Answer the workbench state file.
|
300
|
*/
|
301
|
private static File getCdmSourceStateFile() {
|
302
|
IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
|
303
|
if (path == null) {
|
304
|
return null;
|
305
|
}
|
306
|
path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME);
|
307
|
return path.toFile();
|
308
|
}
|
309
|
|
310
|
private static IMemento readMemento(){
|
311
|
try {
|
312
|
return MementoHelper.readMementoFromFile(getCdmSourceStateFile());
|
313
|
} catch (FileNotFoundException e) {
|
314
|
return initializeMemento();
|
315
|
}
|
316
|
}
|
317
|
|
318
|
|
319
|
private static IMemento initializeMemento() {
|
320
|
|
321
|
XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE);
|
322
|
memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME);
|
323
|
saveMementoToFile(memento);
|
324
|
|
325
|
return readMemento();
|
326
|
}
|
327
|
|
328
|
/*
|
329
|
* Save the workbench UI in a persistence file.
|
330
|
*/
|
331
|
private static IMemento saveMementoToFile(IMemento memento) {
|
332
|
return MementoHelper.saveMementoToFile(memento, getCdmSourceStateFile());
|
333
|
}
|
334
|
}
|