(no commit message)
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / CdmStoreConnector.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.store;
12
13 import java.sql.SQLException;
14 import java.util.concurrent.CancellationException;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.swt.widgets.Display;
21 import org.springframework.core.io.Resource;
22
23 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
24 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
25 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
26 import eu.etaxonomy.cdm.config.CdmSourceException;
27 import eu.etaxonomy.cdm.config.ICdmSource;
28 import eu.etaxonomy.cdm.database.DbSchemaValidation;
29 import eu.etaxonomy.cdm.database.ICdmDataSource;
30 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
31 import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
34 import eu.etaxonomy.taxeditor.ui.dialog.LoginDialog;
35 import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
36
37 /**
38 * @author n.hoffmann
39 * @created Dec 8, 2010
40 * @version 1.0
41 */
42 class CdmStoreConnector extends Job {
43 private final Display display;
44 private final ICdmSource cdmSource;
45 private DbSchemaValidation dbSchemaValidation;
46 private final Resource applicationContextBean;
47
48 /**
49 * @param datasource
50 * @param dbSchemaValidation
51 * @param applicationContextBean
52 */
53 public CdmStoreConnector(Display display, ICdmSource cdmSource,
54 DbSchemaValidation dbSchemaValidation,
55 Resource applicationContextBean) {
56 super("Connecting to datasource: " + cdmSource);
57 this.display = display;
58 this.cdmSource = cdmSource;
59 this.dbSchemaValidation = dbSchemaValidation;
60 this.applicationContextBean = applicationContextBean;
61 }
62
63 @Override
64 public IStatus run(final IProgressMonitor monitor) {
65
66 monitor.beginTask(getConnectionMessage(), 10);
67
68 // check if database is up and running
69 checkDatabaseReachable(monitor);
70
71 if (!monitor.isCanceled()) {
72 // check if the datasource actually holds data
73 checkIsNonEmptyCdmDatabase(monitor);
74 }
75
76 if (dbSchemaValidation != DbSchemaValidation.CREATE
77 && !monitor.isCanceled()) {
78 // if we do not create the datasource, we want to check if the
79 // datasource is compatible with this editor
80 checkDbSchemaVersionCompatibility(monitor);
81 }
82
83 // we are done with our low level checking and will free resources now
84 cdmSource.closeOpenConnections();
85
86 if (!monitor.isCanceled()) {
87 CdmStore.close(monitor);
88 }
89
90 ICdmApplicationConfiguration applicationController = null;
91
92 if (!monitor.isCanceled()) {
93 CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter
94 .CreateSubMonitor(monitor, 7);
95 // This is where we instantiate the application controller
96 try {
97
98 applicationController = getApplicationController(cdmSource,subprogressMonitor);
99
100 } catch (Exception e) {
101 if(! causeIsCancelationExceptionRecursive(e)){
102 return new Status(IStatus.ERROR, "Could not connect to CDM Store", "An error occurred while trying to connect to datasource: " + cdmSource.getName(), e);
103 }
104 } finally {
105 monitor.done();
106 }
107 }
108
109
110
111 if (!monitor.isCanceled()) {
112 CdmStore.setInstance(applicationController, cdmSource);
113
114 display.asyncExec(new Runnable() {
115 /*
116 * (non-Javadoc)
117 *
118 * @see java.lang.Runnable#run()
119 */
120 @Override
121 public void run() {
122 authenticate();
123
124 startContext();
125 }
126 });
127
128 MessagingUtils.info("Application context initialized.");
129 return Status.OK_STATUS;
130 } else {
131 // Show datasource view if not shown yet
132 display.asyncExec(new Runnable() {
133 /*
134 * (non-Javadoc)
135 *
136 * @see java.lang.Runnable#run()
137 */
138 @Override
139 public void run() {
140 StoreUtil.showView(CdmDataSourceViewPart.ID);
141 }
142 });
143 return Status.CANCEL_STATUS;
144 }
145
146 }
147
148 private ICdmApplicationConfiguration getApplicationController(ICdmSource cdmSource, CdmProgressMonitorAdapter subprogressMonitor) {
149 if(cdmSource instanceof ICdmDataSource) {
150 return CdmApplicationController.NewInstance(applicationContextBean,
151 (ICdmDataSource)cdmSource,
152 dbSchemaValidation,
153 false,
154 subprogressMonitor);
155 } else if(cdmSource instanceof ICdmRemoteSource) {
156 return CdmApplicationRemoteController.NewInstance((ICdmRemoteSource)cdmSource,
157 false,
158 subprogressMonitor,
159 null);
160 } else {
161 throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource.getName());
162 }
163 }
164 private void authenticate() {
165 LoginDialog loginDialog = new LoginDialog(StoreUtil.getShell());
166 loginDialog.open();
167 }
168
169 private void startContext() {
170 CdmStore.getContextManager().notifyContextStart();
171 }
172
173 /**
174 * @return
175 */
176 private String getConnectionMessage() {
177 return cdmSource.getConnectionMessage();
178 }
179
180 /**
181 * @return
182 * @throws SQLException
183 */
184 private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor) {
185 monitor.subTask("Checking if datasource is compatible with this editor.");
186 String dbSchemaVersion;
187 boolean result = false;
188 try {
189 dbSchemaVersion = cdmSource.getDbSchemaVersion();
190 // we assume that empty dbSchemaVersion means an empty database and
191 // skip version checking
192 result = dbSchemaVersion == null ? true : CdmMetaData
193 .isDbSchemaVersionCompatible(dbSchemaVersion);
194 monitor.worked(1);
195 } catch (CdmSourceException e) {
196 //
197 }
198
199 if (!result) {
200 // Show an error message
201 MessagingUtils
202 .messageDialog(
203 "DatabaseCompatibilityCheck failed",
204 this,
205 "The database schema for the chosen "
206 + "datasource '"
207 + cdmSource
208 + "' \n is not valid for this version of the taxonomic editor. \n"
209 + "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.",
210 null);
211
212 monitor.setCanceled(true);
213 }
214
215 }
216
217 private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor) {
218 monitor.subTask("Checking if datasource is a non empty CDM database.");
219 boolean isDbEmpty = false;
220 try {
221 isDbEmpty = cdmSource.isDbEmpty();
222 } catch (CdmSourceException e) {
223 isDbEmpty = true;
224 }
225 if(isDbEmpty) {
226 dbSchemaValidation = DbSchemaValidation.CREATE;
227 }
228 }
229
230 private boolean causeIsCancelationExceptionRecursive(Throwable throwable){
231 if(throwable == null){
232 return false;
233 }else if(throwable instanceof CancellationException){
234 return true;
235 }else{
236 return causeIsCancelationExceptionRecursive(throwable.getCause());
237 }
238 }
239
240 private void checkDatabaseReachable(IProgressMonitor monitor) {
241 try {
242 monitor.subTask("Checking if datasource is reachable.");
243 cdmSource.checkConnection();
244 monitor.worked(1);
245 } catch (CdmSourceException e) {
246 MessagingUtils.messageDialog("Could not connect to chosen datasource",
247 this, "Reason: " + e.getMessage(), e);
248 monitor.setCanceled(true);
249 }
250 }
251 }