fixes #805 and started to work on #835
[taxeditor.git] / 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.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 lastUsedDataSourceName;
51
52 public CdmDataSourceRepository(){
53
54 memento = readMemento();
55 lastUsedDataSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME;
56 }
57
58 public static CdmDataSourceRepository getDefault() {
59 if (repository == null) {
60 repository = new CdmDataSourceRepository();
61 }
62 return repository;
63 }
64
65 public boolean delete(CdmPersistentDataSource dataSource) {
66 CdmPersistentDataSource.delete(dataSource);
67 return true;
68 }
69
70
71 public List<ICdmDataSource> getAll() {
72 List<ICdmDataSource> dataSources = new ArrayList<ICdmDataSource>();
73
74 for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
75 dataSources.add(dataSource);
76 }
77
78 return dataSources;
79 }
80
81 /**
82 *
83 * @return
84 */
85 public ICdmDataSource getCurrentDataSource() {
86 if (currentDataSource == null) {
87 try {
88 currentDataSource = CdmPersistentDataSource.NewInstance(lastUsedDataSourceName);
89 } catch (DataSourceNotFoundException e) {
90 // fallback creates a new default
91 ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
92 DEFAULT_DATASOURCE_NAME, "sa", "");
93 save(h2DataSource.getName(), h2DataSource);
94 setCurrentDataSource(h2DataSource);
95 }
96 }
97 return currentDataSource;
98 }
99
100
101 /**
102 * @return the last used DataSource
103 */
104 public String getLastUsedDataSourceName() {
105 return lastUsedDataSourceName;
106 }
107
108 /**
109 *
110 * @param dataSource
111 * @return
112 */
113 public boolean setCurrentDataSource(ICdmDataSource dataSource) {
114 if (currentDataSource != null) {
115 if (!changeDataSource(dataSource)) {
116 return false;
117 }
118 }
119 currentDataSource = dataSource;
120 return true;
121 }
122
123 /**
124 *
125 * @param dataSource
126 * @return
127 */
128 private boolean changeDataSource(final ICdmDataSource dataSource) {
129
130 // TODO Close all open editors before showing progress monitor
131 if (!StoreUtil.closeAll()) {
132 // User has canceled operation
133 return false;
134 }
135
136 memento.putString(CURRENT_DATASOURCE, dataSource.getName());
137 saveMementoToFile(memento);
138 PlatformUI.getWorkbench().restart();
139
140 return true;
141 }
142
143 /**
144 * @param dataSource
145 * @return
146 */
147 public ICdmDataSource save(String dataSourceName, ICdmDataSource dataSource) {
148
149 DatabaseTypeEnum databaseType = dataSource.getDatabaseType();
150
151 if(databaseType.equals(DatabaseTypeEnum.H2)){
152 return CdmPersistentDataSource.saveLocalH2(
153 dataSourceName,
154 dataSource.getFilePath(),
155 dataSource.getDatabase(),
156 dataSource.getUsername(),
157 dataSource.getPassword(),
158 dataSource.getMode());
159 }else{
160 return CdmPersistentDataSource.save(
161 dataSourceName,
162 dataSource.getDatabaseType(),
163 dataSource.getServer(),
164 dataSource.getDatabase(),
165 dataSource.getPort(),
166 dataSource.getUsername(),
167 dataSource.getPassword()
168 );
169 }
170 }
171
172 @Deprecated
173 public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,
174 String server, int port, String database, String username,
175 String password) {
176 CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(
177 dataSourceName, databaseType, server, database, port, username, password);
178 return dataSource;
179 }
180
181 @Deprecated
182 public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,
183 String server, String database, String username, String password) {
184 CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(
185 dataSourceName, databaseType, server, database, username, password);
186 return dataSource;
187 }
188
189 /*********************************************************
190 * Memento Handling *
191 *********************************************************/
192
193 /*
194 * Answer the workbench state file.
195 */
196 private File getDataSourceStateFile() {
197 IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
198 if (path == null) {
199 return null;
200 }
201 path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME);
202 return path.toFile();
203 }
204
205 private XMLMemento readMemento(){
206 File stateFile = getDataSourceStateFile();
207 FileInputStream input;
208 try {
209 input = new FileInputStream(stateFile);
210 BufferedReader reader = new BufferedReader(
211 new InputStreamReader(input, "utf-8")); //$NON-NLS-1$
212 return XMLMemento.createReadRoot(reader);
213 } catch (FileNotFoundException e) {
214 // first start the file is not there
215 return initializeMemento();
216
217 // TODO Auto-generated catch block
218 // e.printStackTrace();
219 } catch (WorkbenchException e) {
220 // TODO Auto-generated catch block
221 e.printStackTrace();
222 } catch (UnsupportedEncodingException e) {
223 // TODO Auto-generated catch block
224 e.printStackTrace();
225 }
226
227 return null;
228 }
229
230 /**
231 *
232 */
233 private XMLMemento initializeMemento() {
234
235 XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE);
236 memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME);
237 saveMementoToFile(memento);
238
239 return readMemento();
240 }
241
242 /*
243 * Save the workbench UI in a persistence file.
244 */
245 private boolean saveMementoToFile(XMLMemento memento) {
246 // Save it to a file.
247 // XXX: nobody currently checks the return value of this method.
248 File stateFile = getDataSourceStateFile();
249 if (stateFile == null) {
250 return false;
251 }
252 try {
253 FileOutputStream stream = new FileOutputStream(stateFile);
254 OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
255 memento.save(writer);
256 writer.close();
257 } catch (IOException e) {
258 stateFile.delete();
259 logger.error("Could not save datasource state");
260 return false;
261 }
262
263 // Success !
264 return true;
265 }
266 }