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
.sql
.SQLException
;
14 import java
.util
.concurrent
.CancellationException
;
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
;
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
.cdm
.remote
.ICdmRemoteSource
;
32 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.LoginDialog
;
35 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
39 * @created Dec 8, 2010
42 class CdmStoreConnector
extends Job
{
43 private final Display display
;
44 private final ICdmSource cdmSource
;
45 private DbSchemaValidation dbSchemaValidation
;
46 private final Resource applicationContextBean
;
50 * @param dbSchemaValidation
51 * @param applicationContextBean
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
;
64 public IStatus
run(final IProgressMonitor monitor
) {
66 monitor
.beginTask(getConnectionMessage(), 10);
68 // check if database is up and running
69 checkDatabaseReachable(monitor
);
71 if (!monitor
.isCanceled()) {
72 // check if the datasource actually holds data
73 checkIsNonEmptyCdmDatabase(monitor
);
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
);
83 // we are done with our low level checking and will free resources now
84 cdmSource
.closeOpenConnections();
86 if (!monitor
.isCanceled()) {
87 CdmStore
.close(monitor
);
90 ICdmApplicationConfiguration applicationController
= null;
92 if (!monitor
.isCanceled()) {
93 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
94 .CreateSubMonitor(monitor
, 7);
95 // This is where we instantiate the application controller
96 int oldPriority
= Thread
.currentThread().getPriority();
98 Thread
.currentThread().setPriority(10);
99 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
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
);
106 Thread
.currentThread().setPriority(oldPriority
);
112 if (!monitor
.isCanceled()) {
113 CdmStore
.setInstance(applicationController
, cdmSource
);
115 display
.asyncExec(new Runnable() {
119 * @see java.lang.Runnable#run()
129 MessagingUtils
.info("Application context initialized.");
130 return Status
.OK_STATUS
;
132 // Show datasource view if not shown yet
133 display
.asyncExec(new Runnable() {
137 * @see java.lang.Runnable#run()
141 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
144 return Status
.CANCEL_STATUS
;
149 private ICdmApplicationConfiguration
getApplicationController(ICdmSource cdmSource
, CdmProgressMonitorAdapter subprogressMonitor
) {
150 if(cdmSource
instanceof ICdmDataSource
) {
151 return CdmApplicationController
.NewInstance(applicationContextBean
,
152 (ICdmDataSource
)cdmSource
,
156 } else if(cdmSource
instanceof ICdmRemoteSource
) {
157 return CdmApplicationRemoteController
.NewInstance((ICdmRemoteSource
)cdmSource
,
162 throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource
.getName());
165 private void authenticate() {
166 LoginDialog loginDialog
= new LoginDialog(StoreUtil
.getShell());
170 private void startContext() {
171 CdmStore
.getContextManager().notifyContextStart();
177 private String
getConnectionMessage() {
178 return cdmSource
.getConnectionMessage();
183 * @throws SQLException
185 private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor
) {
186 monitor
.subTask("Checking if datasource is compatible with this editor.");
187 String dbSchemaVersion
;
188 boolean result
= false;
190 dbSchemaVersion
= cdmSource
.getDbSchemaVersion();
191 // we assume that empty dbSchemaVersion means an empty database and
192 // skip version checking
193 result
= dbSchemaVersion
== null ?
true : CdmMetaData
194 .isDbSchemaVersionCompatible(dbSchemaVersion
);
196 } catch (CdmSourceException e
) {
201 // Show an error message
204 "DatabaseCompatibilityCheck failed",
206 "The database schema for the chosen "
209 + "' \n is not valid for this version of the taxonomic editor. \n"
210 + "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.",
213 monitor
.setCanceled(true);
218 private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor
) {
219 monitor
.subTask("Checking if datasource is a non empty CDM database.");
220 boolean isDbEmpty
= false;
222 isDbEmpty
= cdmSource
.isDbEmpty();
223 } catch (CdmSourceException e
) {
227 dbSchemaValidation
= DbSchemaValidation
.CREATE
;
231 private boolean causeIsCancelationExceptionRecursive(Throwable throwable
){
232 if(throwable
== null){
234 }else if(throwable
instanceof CancellationException
){
237 return causeIsCancelationExceptionRecursive(throwable
.getCause());
241 private void checkDatabaseReachable(IProgressMonitor monitor
) {
243 monitor
.subTask("Checking if datasource is reachable.");
244 cdmSource
.checkConnection();
246 } catch (CdmSourceException e
) {
247 MessagingUtils
.messageDialog("Could not connect to chosen datasource",
248 this, "Reason: " + e
.getMessage(), e
);
249 monitor
.setCanceled(true);