p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / 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.lang.reflect.InvocationTargetException;
13 import java.util.ArrayList;
14 import java.util.List;
15
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;
20
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.DbSchemaValidation;
26 import eu.etaxonomy.cdm.database.ICdmDataSource;
27 import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
28 import eu.etaxonomy.taxeditor.controller.EditorController;
29 import eu.etaxonomy.taxeditor.controller.GlobalController;
30 import eu.etaxonomy.taxeditor.controller.TreeController;
31 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
32
33 public class CdmDataSourceRepository implements ICdmDataSourceRepository {
34 private static final Logger logger = Logger
35 .getLogger(CdmDataSourceRepository.class);
36
37 private static ICdmDataSource currentDataSource;
38 private static ICdmDataSourceRepository repository;
39 private List<ICdmDataSource> dataSources;
40 private CdmApplicationController cdmAppController;
41
42 public static ICdmDataSourceRepository getDefault() {
43 if (repository == null) {
44 repository = new CdmDataSourceRepository();
45
46 // Create datasource for local H2 database
47 ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
48 "cdm", "sa", "");
49 repository.add(h2DataSource);
50
51 // Add stored datasources to repository
52 for (ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()) {
53 repository.add(dataSource);
54 }
55
56 // Get default datasource
57 ICdmDataSource defaultDataSource = CdmPersistentDataSource.NewDefaultInstance();
58
59 // If none specified, use local H2 database
60 if (defaultDataSource == null) {
61 defaultDataSource = h2DataSource;
62 }
63
64 // Set repository's current datasource
65 repository.setCurrentDataSource(defaultDataSource);
66
67 // defaultDataSource = CdmPersistentDataSource.save("mysql_cichorieae", DatabaseTypeEnum.MySQL, "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");
68 // CdmDataSource mySqlDataSource = CdmDataSource.NewMySqlInstance(
69 // "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");
70 }
71 return repository;
72 }
73
74
75 public boolean add(ICdmDataSource dataSource) {
76 return getDataSources().add(dataSource);
77 }
78
79
80
81 public boolean delete(CdmPersistentDataSource dataSource) {
82 CdmPersistentDataSource.delete(dataSource);
83 return remove(dataSource);
84 }
85
86
87 public boolean remove(ICdmDataSource dataSource) {
88 return getDataSources().remove(dataSource);
89 }
90
91
92 public List<ICdmDataSource> getAll() {
93 return getDataSources();
94 }
95
96
97 public ICdmDataSource getCurrentDataSource() {
98 if (currentDataSource == null) {
99 throw new IllegalStateException("Current data source not set.");
100 }
101 return currentDataSource;
102 }
103
104
105 public boolean setCurrentDataSource(ICdmDataSource dataSource) {
106 if (currentDataSource != null) {
107 if (!changeDataSource(dataSource)) {
108 return false;
109 }
110 }
111 currentDataSource = dataSource;
112 return true;
113 }
114
115
116 public void setCdmApplicationController(
117 CdmApplicationController cdmAppController) {
118 this.cdmAppController = cdmAppController;
119 }
120
121 private CdmApplicationController getCdmAppController() {
122 if (cdmAppController == null) {
123 throw new IllegalStateException("CdmApplicationController not set.");
124 }
125 return cdmAppController;
126 }
127
128 private List<ICdmDataSource> getDataSources() {
129 if (dataSources == null) {
130 dataSources = new ArrayList<ICdmDataSource>();
131 }
132 return dataSources;
133 }
134
135 private boolean changeDataSource(final ICdmDataSource dataSource) {
136
137 // Close all open editors before showing progress monitor
138 if (!EditorController.closeAll()) {
139
140 // User has canceled operation
141 return false;
142 }
143
144 try {
145 IRunnableWithProgress op = new IRunnableWithProgress() {
146
147
148 public void run(IProgressMonitor monitor)
149 throws InvocationTargetException, InterruptedException {
150
151 // Start the progress bar with 25 steps
152 monitor.beginTask("Changing to a new datasource ...", 25);
153
154 // Clear all session variables
155 CdmSessionDataRepository.getDefault().clearNonTaxonData();
156 monitor.worked(1);
157
158 // Clear session taxa
159 CdmSessionDataRepository.getDefault().clearTaxonData();
160 monitor.worked(1);
161
162 CdmTransactionController.commitTransaction();
163 monitor.worked(1);
164
165 try {
166 // getCdmAppController().changeDataSource(dataSource);
167 getCdmAppController().changeDataSource(dataSource, DbSchemaValidation.UPDATE);
168 } catch (TermNotFoundException e) {
169 // TODO Auto-generated catch block
170 e.printStackTrace();
171 }
172 monitor.worked(10);
173
174 CdmTransactionController.startTransaction();
175 monitor.worked(1);
176
177 // Get session root taxa
178 // TODO get listener in tree content provider to take care of this
179 TreeController.getTreeViewer().setInput(
180 CdmSessionDataRepository.getDefault().getRootTaxa());
181
182 monitor.worked(11);
183
184 monitor.done();
185 }
186
187 };
188 new ProgressMonitorDialog(GlobalController.getShell()).run(false, true, op);
189 } catch (InvocationTargetException e) {
190 e.printStackTrace();
191 return false;
192 } catch (InterruptedException e) {
193 e.printStackTrace();
194 return false;
195 }
196 return true;
197 }
198
199
200 public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,
201 String server, String database, int port, String username,
202 String password) {
203 CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(
204 dataSourceName, databaseType, server, database, port, username, password);
205 add(dataSource);
206 return dataSource;
207 }
208
209
210 public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,
211 String server, String database, String username, String password) {
212 CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(
213 dataSourceName, databaseType, server, database, username, password);
214 add(dataSource);
215 return dataSource;
216 }
217 }