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
|
}
|