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
.store
.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 currentDataSourceName
;
52 public CdmDataSourceRepository(){
55 memento
= readMemento();
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", "");
66 setCurrentDataSource(h2DataSource
);
71 public static CdmDataSourceRepository
getDefault() {
72 if (repository
== null) {
73 repository
= new CdmDataSourceRepository();
78 public boolean delete(CdmPersistentDataSource dataSource
) {
79 CdmPersistentDataSource
.delete(dataSource
);
84 public List
<ICdmDataSource
> getAll() {
85 List
<ICdmDataSource
> dataSources
= new ArrayList
<ICdmDataSource
>();
87 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
88 dataSources
.add(dataSource
);
95 public ICdmDataSource
getCurrentDataSource() {
96 if (currentDataSource
== null) {
97 throw new IllegalStateException("Current data source not set.");
99 return currentDataSource
;
103 public boolean setCurrentDataSource(ICdmDataSource dataSource
) {
104 if (currentDataSource
!= null) {
105 if (!changeDataSource(dataSource
)) {
109 currentDataSource
= dataSource
;
113 private boolean changeDataSource(final ICdmDataSource dataSource
) {
115 // TODO Close all open editors before showing progress monitor
116 if (!StoreUtil
.closeAll()) {
117 // User has canceled operation
121 memento
.putString(CURRENT_DATASOURCE
, dataSource
.getName());
122 saveMementoToFile(memento
);
123 PlatformUI
.getWorkbench().restart();
132 public ICdmDataSource
save(ICdmDataSource dataSource
) {
134 DatabaseTypeEnum databaseType
= dataSource
.getDatabaseType();
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());
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()
158 public CdmPersistentDataSource
save(String dataSourceName
, DatabaseTypeEnum databaseType
,
159 String server
, int port
, String database
, String username
,
161 CdmPersistentDataSource dataSource
= CdmPersistentDataSource
.save(
162 dataSourceName
, databaseType
, server
, database
, port
, username
, password
);
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
);
174 /*********************************************************
176 *********************************************************/
179 * Answer the workbench state file.
181 private File
getDataSourceStateFile() {
182 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
186 path
= path
.append(DEFAULT_DATASOURCE_STATE_FILENAME
);
187 return path
.toFile();
190 private XMLMemento
readMemento(){
191 File stateFile
= getDataSourceStateFile();
192 FileInputStream input
;
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();
202 // TODO Auto-generated catch block
203 // e.printStackTrace();
204 } catch (WorkbenchException e
) {
205 // TODO Auto-generated catch block
207 } catch (UnsupportedEncodingException e
) {
208 // TODO Auto-generated catch block
218 private XMLMemento
initializeMemento() {
220 XMLMemento memento
= XMLMemento
.createWriteRoot(TAG_DATASOURCE
);
221 memento
.putString(CURRENT_DATASOURCE
, DEFAULT_DATASOURCE_NAME
);
222 saveMementoToFile(memento
);
224 return readMemento();
228 * Save the workbench UI in a persistence file.
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) {
238 FileOutputStream stream
= new FileOutputStream(stateFile
);
239 OutputStreamWriter writer
= new OutputStreamWriter(stream
, "utf-8"); //$NON-NLS-1$
240 memento
.save(writer
);
242 } catch (IOException e
) {
244 logger
.error("Could not save datasource state");