2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.datasource
;
12 import java
.io
.BufferedReader
;
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
;
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
;
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
;
38 public class CdmDataSourceRepository
{
39 private static final Logger logger
= Logger
40 .getLogger(CdmDataSourceRepository
.class);
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";
47 private static ICdmDataSource currentDataSource
;
48 private static CdmDataSourceRepository repository
;
49 private XMLMemento memento
;
50 private String lastUsedDataSourceName
;
52 public CdmDataSourceRepository(){
54 memento
= readMemento();
55 lastUsedDataSourceName
= memento
!= null ? memento
.getString(CURRENT_DATASOURCE
) : DEFAULT_DATASOURCE_NAME
;
58 public static CdmDataSourceRepository
getDefault() {
59 if (repository
== null) {
60 repository
= new CdmDataSourceRepository();
65 public boolean delete(CdmPersistentDataSource dataSource
) {
66 CdmPersistentDataSource
.delete(dataSource
);
71 public List
<ICdmDataSource
> getAll() {
72 List
<ICdmDataSource
> dataSources
= new ArrayList
<ICdmDataSource
>();
74 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
75 dataSources
.add(dataSource
);
85 public ICdmDataSource
getCurrentDataSource() {
86 if (currentDataSource
== null) {
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
);
97 return currentDataSource
;
102 * @return the last used DataSource
104 public String
getLastUsedDataSourceName() {
105 return lastUsedDataSourceName
;
113 public boolean setCurrentDataSource(ICdmDataSource dataSource
) {
114 if (currentDataSource
!= null) {
115 if (!changeDataSource(dataSource
)) {
119 currentDataSource
= dataSource
;
128 private boolean changeDataSource(final ICdmDataSource dataSource
) {
130 // TODO Close all open editors before showing progress monitor
131 if (!StoreUtil
.closeAll()) {
132 // User has canceled operation
136 memento
.putString(CURRENT_DATASOURCE
, dataSource
.getName());
137 saveMementoToFile(memento
);
138 PlatformUI
.getWorkbench().restart();
147 public ICdmDataSource
save(String dataSourceName
, ICdmDataSource dataSource
) {
149 DatabaseTypeEnum databaseType
= dataSource
.getDatabaseType();
151 if(databaseType
.equals(DatabaseTypeEnum
.H2
)){
152 return CdmPersistentDataSource
.saveLocalH2(
154 dataSource
.getFilePath(),
155 dataSource
.getDatabase(),
156 dataSource
.getUsername(),
157 dataSource
.getPassword(),
158 dataSource
.getMode());
160 return CdmPersistentDataSource
.save(
162 dataSource
.getDatabaseType(),
163 dataSource
.getServer(),
164 dataSource
.getDatabase(),
165 dataSource
.getPort(),
166 dataSource
.getUsername(),
167 dataSource
.getPassword()
173 public CdmPersistentDataSource
save(String dataSourceName
, DatabaseTypeEnum databaseType
,
174 String server
, int port
, String database
, String username
,
176 CdmPersistentDataSource dataSource
= CdmPersistentDataSource
.save(
177 dataSourceName
, databaseType
, server
, database
, port
, username
, password
);
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
);
189 /*********************************************************
191 *********************************************************/
194 * Answer the workbench state file.
196 private File
getDataSourceStateFile() {
197 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
201 path
= path
.append(DEFAULT_DATASOURCE_STATE_FILENAME
);
202 return path
.toFile();
205 private XMLMemento
readMemento(){
206 File stateFile
= getDataSourceStateFile();
207 FileInputStream input
;
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();
217 // TODO Auto-generated catch block
218 // e.printStackTrace();
219 } catch (WorkbenchException e
) {
220 // TODO Auto-generated catch block
222 } catch (UnsupportedEncodingException e
) {
223 // TODO Auto-generated catch block
233 private XMLMemento
initializeMemento() {
235 XMLMemento memento
= XMLMemento
.createWriteRoot(TAG_DATASOURCE
);
236 memento
.putString(CURRENT_DATASOURCE
, DEFAULT_DATASOURCE_NAME
);
237 saveMementoToFile(memento
);
239 return readMemento();
243 * Save the workbench UI in a persistence file.
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) {
253 FileOutputStream stream
= new FileOutputStream(stateFile
);
254 OutputStreamWriter writer
= new OutputStreamWriter(stream
, "utf-8"); //$NON-NLS-1$
255 memento
.save(writer
);
257 } catch (IOException e
) {
259 logger
.error("Could not save datasource state");