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