Project

General

Profile

Download (7.27 KB) Statistics
| Branch: | Tag: | Revision:
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.store.datasource;
11

    
12
import java.io.BufferedReader;
13
import java.io.File;
14
import java.io.FileInputStream;
15
import java.io.FileNotFoundException;
16
import java.io.FileOutputStream;
17
import java.io.IOException;
18
import java.io.InputStreamReader;
19
import java.io.OutputStreamWriter;
20
import java.io.UnsupportedEncodingException;
21
import java.util.ArrayList;
22
import java.util.List;
23

    
24
import org.apache.log4j.Logger;
25
import org.eclipse.core.runtime.IPath;
26
import org.eclipse.ui.PlatformUI;
27
import org.eclipse.ui.WorkbenchException;
28
import org.eclipse.ui.XMLMemento;
29

    
30
import eu.etaxonomy.cdm.database.CdmDataSource;
31
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
32
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
33
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
34
import eu.etaxonomy.cdm.database.ICdmDataSource;
35
import eu.etaxonomy.taxeditor.store.StoreUtil;
36
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
37

    
38
public class CdmDataSourceRepository{
39
	private static final Logger logger = Logger
40
			.getLogger(CdmDataSourceRepository.class);
41

    
42
	public static final String TAG_DATASOURCE = "tagDataSource";
43
	private static final String CURRENT_DATASOURCE = "currentDataSource";
44
	private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml";
45
	private static final String DEFAULT_DATASOURCE_NAME = "cdm";
46
	
47
	private static ICdmDataSource currentDataSource;
48
	private static CdmDataSourceRepository repository;
49
	private XMLMemento memento;
50
	private String currentDataSourceName;
51
	
52
	public CdmDataSourceRepository(){
53

    
54
		
55
		memento = readMemento();		
56
		
57
		
58
		try {
59
			currentDataSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME;
60
			currentDataSource = CdmPersistentDataSource.NewInstance(currentDataSourceName);
61
		} catch (DataSourceNotFoundException e) {
62
			// fallback creates a new default
63
			ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
64
					DEFAULT_DATASOURCE_NAME, "sa", "");
65
			save(h2DataSource);
66
			setCurrentDataSource(h2DataSource);
67
		}
68
		
69
	}
70
	
71
	public static CdmDataSourceRepository getDefault() {
72
		if (repository == null) {
73
			repository = new CdmDataSourceRepository();
74
		}
75
		return repository;
76
	}
77

    
78
	public boolean delete(CdmPersistentDataSource dataSource) {
79
		CdmPersistentDataSource.delete(dataSource);
80
		return true;
81
	}
82

    
83
	
84
	public List<ICdmDataSource> getAll() {
85
		List<ICdmDataSource> dataSources = new ArrayList<ICdmDataSource>();
86
		
87
		for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
88
			dataSources.add(dataSource);
89
		}
90
		
91
		return dataSources;
92
	}
93
		
94
	
95
	public ICdmDataSource getCurrentDataSource() {
96
		if (currentDataSource == null) {
97
			throw new IllegalStateException("Current data source not set.");
98
		}
99
		return currentDataSource;
100
	}
101

    
102
	
103
	public boolean setCurrentDataSource(ICdmDataSource dataSource) {
104
		if (currentDataSource != null) {
105
			if (!changeDataSource(dataSource)) {
106
				return false;
107
			}
108
		}
109
		currentDataSource = dataSource;
110
		return true;
111
	}	
112
		
113
	private boolean changeDataSource(final ICdmDataSource dataSource) {
114

    
115
		// TODO Close all open editors before showing progress monitor
116
		if (!StoreUtil.closeAll()) {
117
			// User has canceled operation   
118
			return false;
119
		}
120
		
121
		memento.putString(CURRENT_DATASOURCE, dataSource.getName());
122
		saveMementoToFile(memento);
123
		PlatformUI.getWorkbench().restart();
124
	
125
		return true;
126
	}
127

    
128
	/**
129
	 * @param dataSource
130
	 * @return
131
	 */
132
	public ICdmDataSource save(ICdmDataSource dataSource) {
133
		
134
		DatabaseTypeEnum databaseType = dataSource.getDatabaseType();
135
		
136
		if(databaseType.equals(DatabaseTypeEnum.H2)){
137
			return CdmPersistentDataSource.saveLocalH2(
138
					dataSource.getName(), 
139
					dataSource.getFilePath(), 
140
					dataSource.getDatabase(), 
141
					dataSource.getUsername(), 
142
					dataSource.getPassword(), 
143
					dataSource.getMode());
144
		}else{
145
			return CdmPersistentDataSource.save(
146
					dataSource.getName(), 
147
					dataSource.getDatabaseType(),
148
					dataSource.getServer(),
149
					dataSource.getDatabase(),
150
					dataSource.getPort(),
151
					dataSource.getUsername(),
152
					dataSource.getPassword()
153
			);
154
		}
155
	}
156
	
157
	@Deprecated
158
	public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,
159
			String server, int port, String database,  String username,
160
			String password) {
161
		CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(
162
				dataSourceName, databaseType, server, database, port, username, password);
163
		return dataSource;
164
	}
165
	
166
	@Deprecated
167
	public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,
168
			String server, String database, String username, String password) {
169
		CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(
170
				dataSourceName, databaseType, server, database, username, password);
171
		return dataSource;
172
	}
173
	
174
	/*********************************************************
175
	 * Memento Handling										 *
176
	 *********************************************************/
177
	
178
	/*
179
	 * Answer the workbench state file.
180
	 */
181
	private File getDataSourceStateFile() {
182
		IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
183
		if (path == null) {
184
			return null;
185
		}
186
		path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME);
187
		return path.toFile();
188
	}
189
	
190
	private XMLMemento readMemento(){
191
		File stateFile = getDataSourceStateFile();
192
		FileInputStream input;
193
		try {
194
			input = new FileInputStream(stateFile);
195
			BufferedReader reader = new BufferedReader(
196
					new InputStreamReader(input, "utf-8")); //$NON-NLS-1$
197
			return XMLMemento.createReadRoot(reader);
198
		} catch (FileNotFoundException e) {
199
			// first start the file is not there
200
			return initializeMemento();
201
			
202
			// TODO Auto-generated catch block
203
//			e.printStackTrace();
204
		} catch (WorkbenchException e) {
205
			// TODO Auto-generated catch block
206
			e.printStackTrace();
207
		} catch (UnsupportedEncodingException e) {
208
			// TODO Auto-generated catch block
209
			e.printStackTrace();
210
		}
211

    
212
		return null;
213
	}
214
	
215
	/**
216
	 * 
217
	 */
218
	private XMLMemento initializeMemento() {
219
		
220
		XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE);
221
		memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME);
222
		saveMementoToFile(memento);
223
		
224
		return readMemento();
225
	}
226

    
227
	/*
228
	 * Save the workbench UI in a persistence file.
229
	 */
230
	private boolean saveMementoToFile(XMLMemento memento) {
231
		// Save it to a file.
232
		// XXX: nobody currently checks the return value of this method.
233
		File stateFile = getDataSourceStateFile();
234
		if (stateFile == null) {
235
			return false;
236
		}
237
		try {
238
			FileOutputStream stream = new FileOutputStream(stateFile);
239
			OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
240
			memento.save(writer);
241
			writer.close();
242
		} catch (IOException e) {
243
			stateFile.delete();
244
			logger.error("Could not save datasource state");
245
			return false;
246
		}
247

    
248
		// Success !
249
		return true;
250
	}
251
	
252
}
    (1-1/1)