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
.lang
.reflect
.InvocationTargetException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.jface
.dialogs
.ProgressMonitorDialog
;
19 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
21 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
22 import eu
.etaxonomy
.cdm
.database
.CdmDataSource
;
23 import eu
.etaxonomy
.cdm
.database
.CdmPersistentDataSource
;
24 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
25 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
26 import eu
.etaxonomy
.cdm
.model
.common
.init
.TermNotFoundException
;
27 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
28 import eu
.etaxonomy
.taxeditor
.controller
.EditorController
;
29 import eu
.etaxonomy
.taxeditor
.controller
.TreeController
;
30 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
31 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
32 import eu
.etaxonomy
.taxeditor
.model
.CdmUtil
;
34 public class CdmDataSourceRepository
implements ICdmDataSourceRepository
{
35 private static final Logger logger
= Logger
36 .getLogger(CdmDataSourceRepository
.class);
38 private static ICdmDataSource currentDataSource
;
39 private static ICdmDataSourceRepository repository
;
40 private List
<ICdmDataSource
> dataSources
;
41 private CdmApplicationController cdmAppController
;
43 public static ICdmDataSourceRepository
getDefault() {
44 if (repository
== null) {
45 repository
= new CdmDataSourceRepository();
47 // Create datasource for local H2 database
48 ICdmDataSource h2DataSource
= CdmDataSource
.NewH2EmbeddedInstance(
50 repository
.add(h2DataSource
);
52 // Add stored datasources to repository
53 for (ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()) {
54 repository
.add(dataSource
);
57 // Get default datasource
58 ICdmDataSource defaultDataSource
= CdmPersistentDataSource
.NewDefaultInstance();
60 // If none specified, use local H2 database
61 if (defaultDataSource
== null) {
62 defaultDataSource
= h2DataSource
;
65 // Set repository's current datasource
66 repository
.setCurrentDataSource(defaultDataSource
);
68 // defaultDataSource = CdmPersistentDataSource.save("mysql_cichorieae", DatabaseTypeEnum.MySQL, "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");
69 // CdmDataSource mySqlDataSource = CdmDataSource.NewMySqlInstance(
70 // "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");
76 public boolean add(ICdmDataSource dataSource
) {
77 return getDataSources().add(dataSource
);
82 public boolean delete(CdmPersistentDataSource dataSource
) {
83 CdmPersistentDataSource
.delete(dataSource
);
84 return remove(dataSource
);
88 public boolean remove(ICdmDataSource dataSource
) {
89 return getDataSources().remove(dataSource
);
93 public List
<ICdmDataSource
> getAll() {
94 return getDataSources();
98 public ICdmDataSource
getCurrentDataSource() {
99 if (currentDataSource
== null) {
100 throw new IllegalStateException("Current data source not set.");
102 return currentDataSource
;
106 public boolean setCurrentDataSource(ICdmDataSource dataSource
) {
107 if (currentDataSource
!= null) {
108 if (!changeDataSource(dataSource
)) {
112 currentDataSource
= dataSource
;
117 public void setCdmApplicationController(
118 CdmApplicationController cdmAppController
) {
119 this.cdmAppController
= cdmAppController
;
122 private CdmApplicationController
getCdmAppController() {
123 if (cdmAppController
== null) {
124 throw new IllegalStateException("CdmApplicationController not set.");
126 return cdmAppController
;
129 private List
<ICdmDataSource
> getDataSources() {
130 if (dataSources
== null) {
131 dataSources
= new ArrayList
<ICdmDataSource
>();
136 private boolean changeDataSource(final ICdmDataSource dataSource
) {
138 // Close all open editors before showing progress monitor
139 if (!EditorController
.closeAll()) {
141 // User has canceled operation
146 IRunnableWithProgress op
= new IRunnableWithProgress() {
149 public void run(IProgressMonitor monitor
)
150 throws InvocationTargetException
, InterruptedException
{
152 // Start the progress bar with 25 steps
153 monitor
.beginTask("Changing to a new datasource ...", 25);
155 // Clear all session variables
156 CdmSessionDataRepository
.getDefault().clearNonTaxonData();
159 // Clear session taxa
160 CdmSessionDataRepository
.getDefault().clearTaxonData();
163 CdmTransactionController
.commitTransaction();
166 // Set new data source
168 getCdmAppController().changeDataSource(dataSource
);
169 } catch (TermNotFoundException e
) {
171 throw new InterruptedException();
175 CdmTransactionController
.startTransaction();
178 // Get session root taxa
179 // TODO get listener in tree content provider to take care of this
180 TreeController
.getTreeViewer().setInput(
181 CdmSessionDataRepository
.getDefault().getRootTaxa());
189 new ProgressMonitorDialog(GlobalController
.getShell()).run(false, true, op
);
190 } catch (InvocationTargetException e
) {
193 } catch (InterruptedException e
) {
201 public CdmPersistentDataSource
save(String dataSourceName
, DatabaseTypeEnum databaseType
,
202 String server
, String database
, int port
, String username
,
204 CdmPersistentDataSource dataSource
= CdmPersistentDataSource
.save(
205 dataSourceName
, databaseType
, server
, database
, port
, username
, password
);
211 public CdmPersistentDataSource
save(String dataSourceName
, DatabaseTypeEnum databaseType
,
212 String server
, String database
, String username
, String password
) {
213 CdmPersistentDataSource dataSource
= CdmPersistentDataSource
.save(
214 dataSourceName
, databaseType
, server
, database
, username
, password
);