3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.store
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.sql
.SQLException
;
15 import java
.util
.concurrent
.CancellationException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.core
.runtime
.jobs
.Job
;
21 import org
.eclipse
.jface
.dialogs
.ProgressMonitorDialog
;
22 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.springframework
.core
.io
.Resource
;
26 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
27 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
28 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
29 import eu
.etaxonomy
.cdm
.config
.CdmSourceException
;
30 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
31 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
32 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
33 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
35 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
36 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
37 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
38 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
39 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.LoginDialog
;
40 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.RemotingLoginDialog
;
41 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
46 * @created Dec 8, 2010
49 class CdmStoreConnector
extends Job
{
50 private final Display display
;
51 private final ICdmSource cdmSource
;
52 private DbSchemaValidation dbSchemaValidation
;
53 private final Resource applicationContextBean
;
58 * @param dbSchemaValidation
59 * @param applicationContextBean
61 public CdmStoreConnector(Display display
,
63 DbSchemaValidation dbSchemaValidation
,
64 Resource applicationContextBean
) {
65 super("Connecting to datasource: " + cdmSource
);
66 this.display
= display
;
67 this.cdmSource
= cdmSource
;
68 this.dbSchemaValidation
= dbSchemaValidation
;
69 this.applicationContextBean
= applicationContextBean
;
75 public IStatus
run(final IProgressMonitor monitor
) {
77 monitor
.beginTask(getConnectionMessage(), 10);
79 // check if database is up and running
80 checkDatabaseReachable(monitor
);
82 if (!monitor
.isCanceled()) {
83 // check if the datasource actually holds data
84 checkIsNonEmptyCdmDatabase(monitor
);
87 if (dbSchemaValidation
!= DbSchemaValidation
.CREATE
88 && !monitor
.isCanceled()) {
89 // if we do not create the datasource, we want to check if the
90 // datasource is compatible with this editor
91 checkDbSchemaVersionCompatibility(monitor
);
94 // we are done with our low level checking and will free resources now
95 cdmSource
.closeOpenConnections();
97 if (!monitor
.isCanceled()) {
98 CdmStore
.close(monitor
);
101 ICdmApplicationConfiguration applicationController
= null;
103 if (!monitor
.isCanceled()) {
104 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
105 .CreateSubMonitor(monitor
, 7);
106 // This is where we instantiate the application controller
107 int oldPriority
= Thread
.currentThread().getPriority();
109 Thread
.currentThread().setPriority(10);
110 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
111 } catch (Exception e
) {
112 if(! causeIsCancelationExceptionRecursive(e
)){
113 return new Status(IStatus
.ERROR
, "Could not connect to CDM Store", "An error occurred while trying to connect to datasource: " + cdmSource
.getName(), e
);
117 Thread
.currentThread().setPriority(oldPriority
);
123 if (!monitor
.isCanceled()) {
124 CdmStore
.setInstance(applicationController
, cdmSource
);
126 display
.asyncExec(new Runnable() {
130 * @see java.lang.Runnable#run()
140 MessagingUtils
.info("Application context initialized.");
141 return Status
.OK_STATUS
;
143 // Show datasource view if not shown yet
144 display
.asyncExec(new Runnable() {
148 * @see java.lang.Runnable#run()
152 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
155 return Status
.CANCEL_STATUS
;
160 public void start(final RemotingLoginDialog loginDialog
) {
161 // hide login dialog and start connection dialog
162 loginDialog
.setMessage(null);
163 loginDialog
.hide(true);
166 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
169 dialog
.run(true, true, new IRunnableWithProgress() {
171 public void run(final IProgressMonitor monitor
) {
173 monitor
.beginTask(getConnectionMessage(), 7);
175 // check if database is up and running
176 checkDatabaseReachable(monitor
);
178 // check if the datasource actually holds data
179 checkIsNonEmptyCdmDatabase(monitor
);
181 if (dbSchemaValidation
!= DbSchemaValidation
.CREATE
) {
182 // if we do not create the datasource, we want to check if the
183 // datasource is compatible with this editor
184 checkDbSchemaVersionCompatibility(monitor
);
187 // we are done with our low level checking and will free resources now
188 cdmSource
.closeOpenConnections();
190 display
.syncExec(new Runnable() {
194 * @see java.lang.Runnable#run()
198 // close the current context
199 CdmStore
.close(monitor
, false);
203 ICdmApplicationConfiguration applicationController
= null;
205 if (!monitor
.isCanceled()) {
206 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
207 .CreateSubMonitor(monitor
, 3);
208 // genrerate new application controller
209 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
213 if (!monitor
.isCanceled()) {
214 CdmStore
.setInstance(applicationController
, cdmSource
);
215 monitor
.subTask("Authenticating user");
216 display
.syncExec(new Runnable() {
220 * @see java.lang.Runnable#run()
226 // create new security context
227 CdmStore
.getLoginManager().doAuthenticate(loginDialog
.getUsername(), loginDialog
.getPassword());
228 loginDialog
.onComplete();
229 CdmStore
.getContextManager().notifyContextStart();
230 Rank
.initDefaultTerms();
231 NomenclaturalStatusType
.initDefaultTerms();
232 } catch(CdmAuthenticationException cae
) {
233 loginDialog
.hide(false);
234 loginDialog
.setMessage(cae
.getMessage());
240 throw new RuntimeException("Login cancelled");
247 } catch (InvocationTargetException e
) {
248 loginDialog
.hide(false);
249 loginDialog
.setMessage(e
.getMessage());
250 } catch (InterruptedException e
) {
251 loginDialog
.hide(false);
252 loginDialog
.setMessage(e
.getMessage());
257 private ICdmApplicationConfiguration
getApplicationController(ICdmSource cdmSource
, CdmProgressMonitorAdapter subprogressMonitor
) {
258 if(cdmSource
instanceof ICdmDataSource
) {
259 return CdmApplicationController
.NewInstance(applicationContextBean
,
260 (ICdmDataSource
)cdmSource
,
264 } else if(cdmSource
instanceof ICdmRemoteSource
) {
265 return CdmApplicationRemoteController
.NewInstance((ICdmRemoteSource
)cdmSource
,
269 throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource
.getName());
273 private void authenticate() {
274 LoginDialog saloginDialog
= new LoginDialog(StoreUtil
.getShell());
275 saloginDialog
.open();
278 private void startContext() {
279 CdmStore
.getContextManager().notifyContextStart();
285 private String
getConnectionMessage() {
286 return cdmSource
.getConnectionMessage();
291 * @throws SQLException
293 private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor
) {
294 monitor
.subTask("Checking if datasource is compatible with this editor.");
295 String dbSchemaVersion
;
297 String message
= null;
299 dbSchemaVersion
= cdmSource
.getDbSchemaVersion();
300 // we assume that empty dbSchemaVersion means an empty database and
301 // skip version checking
303 if(dbSchemaVersion
!= null) {
304 int compareVersion
= CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
305 // if the datasource version is greater than the taxeditor compatible version then the taxeditor needs to
306 // be updated else the datasource needs to be updated
307 if(compareVersion
> 0) {
308 message
= "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible datasource";
309 } else if (compareVersion
< 0) {
310 message
= "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.";
314 } catch (CdmSourceException e
) {
318 if (message
!= null) {
319 // Show an error message
322 "Datasource Compatibility Check failed",
324 "The database schema for the chosen "
327 + "' \n is not compatible for this version of the taxonomic editor. \n\n"
331 monitor
.setCanceled(true);
336 private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor
) {
337 monitor
.subTask("Checking if datasource is a non empty CDM database.");
338 boolean isDbEmpty
= false;
340 isDbEmpty
= cdmSource
.isDbEmpty();
341 } catch (CdmSourceException e
) {
345 dbSchemaValidation
= DbSchemaValidation
.CREATE
;
349 private boolean causeIsCancelationExceptionRecursive(Throwable throwable
){
350 if(throwable
== null){
352 }else if(throwable
instanceof CancellationException
){
355 return causeIsCancelationExceptionRecursive(throwable
.getCause());
359 private void checkDatabaseReachable(IProgressMonitor monitor
) {
361 monitor
.subTask("Checking if datasource is reachable.");
362 cdmSource
.checkConnection();
364 } catch (CdmSourceException e
) {
365 MessagingUtils
.messageDialog("Could not connect to chosen datasource",
366 this, "Reason: " + e
.getMessage(), e
);
367 monitor
.setCanceled(true);