Project

General

Profile

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