Project

General

Profile

Download (6.6 KB) Statistics
| Branch: | Tag: | Revision:
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

    
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.core.runtime.jobs.Job;
19
import org.eclipse.swt.widgets.Display;
20
import org.springframework.core.io.Resource;
21

    
22
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
23
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
24
import eu.etaxonomy.cdm.api.application.ICdmApplicationRemoteConfiguration;
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.common.CdmMetaData;
29
import eu.etaxonomy.cdm.model.common.CdmMetaData.MetaDataPropertyName;
30
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
31
import eu.etaxonomy.taxeditor.ui.dialogs.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
		ICdmApplicationRemoteConfiguration applicationController = null;
88

    
89
		if (!monitor.isCanceled()) {
90
			CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter
91
					.CreateSubMonitor(monitor, IProgressMonitor.UNKNOWN);
92
			// This is where we instantiate the application controller
93
			try {
94
//				applicationController = CdmApplicationController.NewInstance(
95
//						applicationContextBean, dataSource, dbSchemaValidation,
96
//						false, subprogressMonitor);
97
				applicationController = CdmApplicationRemoteController.NewInstance(applicationContextBean, subprogressMonitor);
98
			} catch (Exception e) {
99
				StoreUtil.error(this.getClass(), e.getMessage(), e);
100
			}
101
		}
102

    
103
		monitor.done();
104

    
105
		if (!monitor.isCanceled()) {
106
			CdmStore.setInstance(applicationController, dataSource);
107

    
108
			display.asyncExec(new Runnable() {
109
				/*
110
				 * (non-Javadoc)
111
				 * 
112
				 * @see java.lang.Runnable#run()
113
				 */
114
				@Override
115
				public void run() {
116
					authenticate();
117

    
118
					startContext();
119
				}
120
			});
121

    
122
			StoreUtil.info("Application context initialized.");
123
			return Status.OK_STATUS;
124
		} else {
125
			// Show datasource view if not shown yet
126
			display.asyncExec(new Runnable() {
127
				/*
128
				 * (non-Javadoc)
129
				 * 
130
				 * @see java.lang.Runnable#run()
131
				 */
132
				@Override
133
				public void run() {
134
					StoreUtil.showView(CdmDataSourceViewPart.ID);
135
				}
136
			});
137
			return Status.CANCEL_STATUS;
138
		}
139

    
140
	}
141

    
142
	private void authenticate() {
143
		LoginDialog loginDialog = new LoginDialog(StoreUtil.getShell());
144
		loginDialog.open();
145
	}
146

    
147
	private void startContext() {
148
		CdmStore.getContextManager().notifyContextStart();
149
	}
150

    
151
	/**
152
	 * @return
153
	 */
154
	private String getConnectionMessage() {
155
		String message = "";
156
		if (dataSource.getDatabaseType().equals(DatabaseTypeEnum.H2)) {
157
			message = " local CDM Store ";
158
		} else {
159
			message = " CDM Community Store ";
160
		}
161
		message += "'" + dataSource.getName() + "'";
162

    
163
		message = "Connecting to" + message + ".";
164

    
165
		return message;
166
	}
167

    
168
	/**
169
	 * @return
170
	 * @throws SQLException
171
	 */
172
	private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor) {
173
		monitor.subTask("Checking if datasource is compatible with this editor.");
174
		String dbSchemaVersion;
175
		boolean result = false;
176
		try {
177
			dbSchemaVersion = (String) dataSource
178
					.getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION
179
							.getSqlQuery());
180
			// we assume that empty dbSchemaVersion means an empty database and
181
			// skip version checking
182
			result = dbSchemaVersion == null ? true : CdmMetaData
183
					.isDbSchemaVersionCompatible(dbSchemaVersion);
184
			monitor.worked(1);
185
		} catch (SQLException e) {
186
			//
187
		}
188

    
189
		if (!result) {
190
			// Show an error message
191
			StoreUtil
192
					.errorDialog(
193
							"DatabaseCompatibilityCheck failed",
194
							this,
195
							"The database schema for the chosen "
196
									+ "datasource '"
197
									+ dataSource
198
									+ "' \n is not valid for this version of the taxonomic editor. \n"
199
									+ "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.",
200
							null);
201

    
202
			monitor.setCanceled(true);
203
		}
204

    
205
	}
206

    
207
	private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor) {
208
		monitor.subTask("Checking if datasource is a non empty CDM database.");
209

    
210
		try {
211
			dataSource.getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION
212
					.getSqlQuery());
213
		} catch (SQLException e1) {
214
			dbSchemaValidation = DbSchemaValidation.CREATE;
215
		}
216
	}
217

    
218
	private void checkDatabaseReachable(IProgressMonitor monitor) {
219
		try {
220
			monitor.subTask("Checking if datasource is reachable.");
221
			dataSource.testConnection();
222
			monitor.worked(1);
223
		} catch (ClassNotFoundException e) {
224
			StoreUtil.errorDialog("Could not connect to chosen datasource",
225
					this, "Reason: " + e.getMessage(), e);
226
			monitor.setCanceled(true);
227
		} catch (SQLException e) {
228
			StoreUtil.errorDialog("Could not connect to chosen datasource",
229
					this, "Reason: " + e.getMessage(), e);
230
			monitor.setCanceled(true);
231
		}
232

    
233
	}
234
}
(2-2/9)