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