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