2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.store
;
12 import java
.lang
.reflect
.Constructor
;
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
.ICdmRepository
;
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
.common
.DefinedTermBase
;
34 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
36 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
37 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
38 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
39 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
40 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
43 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
44 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.LoginDialog
;
45 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.RemotingLoginDialog
;
50 * @created Dec 8, 2010
53 class CdmStoreConnector
extends Job
{
54 private final Display display
;
55 private final ICdmSource cdmSource
;
56 private DbSchemaValidation dbSchemaValidation
;
57 private final Resource applicationContextBean
;
62 * @param dbSchemaValidation
63 * @param applicationContextBean
65 public CdmStoreConnector(Display display
,
67 DbSchemaValidation dbSchemaValidation
,
68 Resource applicationContextBean
) {
69 super(String
.format(Messages
.CdmStoreConnector_CREATING_DATAMODEL
, cdmSource
));
70 this.display
= display
;
71 this.cdmSource
= cdmSource
;
72 this.dbSchemaValidation
= dbSchemaValidation
;
73 this.applicationContextBean
= applicationContextBean
;
79 public IStatus
run(final IProgressMonitor monitor
) {
81 monitor
.beginTask(getConnectionMessage(), 10);
83 // check if database is up and running
84 checkDatabaseReachable(monitor
);
86 if (!monitor
.isCanceled()) {
87 // check if the datasource actually holds data
88 checkIsNonEmptyCdmDatabase(monitor
);
91 if (dbSchemaValidation
!= DbSchemaValidation
.CREATE
92 && !monitor
.isCanceled()) {
93 // if we do not create the datasource, we want to check if the
94 // datasource is compatible with this editor
95 checkDbSchemaVersionCompatibility(monitor
);
98 // we are done with our low level checking and will free resources now
99 cdmSource
.closeOpenConnections();
101 if (!monitor
.isCanceled()) {
102 CdmStore
.close(monitor
);
105 ICdmRepository applicationController
= null;
107 if (!monitor
.isCanceled()) {
108 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
109 .CreateSubMonitor(monitor
, 7);
110 // This is where we instantiate the application controller
111 int oldPriority
= Thread
.currentThread().getPriority();
113 Thread
.currentThread().setPriority(10);
114 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
115 MessagingUtils
.informationDialog(Messages
.CdmStoreConnector_SUCCESS
, Messages
.CdmStoreConnector_DATA_MODEL_CREATION_SUCCESSFUL
);
116 //FIXME E4 migrate or delete because data source view is not used anymore
117 // CdmDataSourceViewPartE4 dataSourceView = (CdmDataSourceViewPartE4) WorkbenchUtility.getE4WrappedPart(AbstractUtility.getView("eu.etaxonomy.taxeditor.view.datasource", false));
118 // if(dataSourceView!=null){
119 // dataSourceView.refresh();
121 EventUtility
.postEvent(WorkbenchEventConstants
.REFRESH_DATASOURCE
, true);
122 return Status
.OK_STATUS
;
123 } catch (Exception e
) {
124 if(! causeIsCancelationExceptionRecursive(e
)){
125 MessagingUtils
.errorDialog(Messages
.CdmStoreConnector_COULD_NOT_CREATE_DATAMODEL
, CdmStoreConnector
.class,
126 String
.format(Messages
.CdmStoreConnector_ERROR_DURING_DATAMODEL_CREATION
, cdmSource
.getName()), TaxeditorStorePlugin
.PLUGIN_ID
, e
, true);
127 return Status
.CANCEL_STATUS
;
131 Thread
.currentThread().setPriority(oldPriority
);
134 return Status
.CANCEL_STATUS
;
138 public void start(final RemotingLoginDialog loginDialog
) {
139 // hide login dialog and start connection dialog
140 loginDialog
.setMessage(null);
141 loginDialog
.hide(true);
144 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
147 dialog
.run(true, true, new IRunnableWithProgress() {
149 public void run(final IProgressMonitor monitor
) {
151 monitor
.beginTask(getConnectionMessage(), 7);
153 // check if database is up and running
154 checkDatabaseReachable(monitor
);
156 // check if the datasource actually holds data
157 checkIsNonEmptyCdmDatabase(monitor
);
159 if (dbSchemaValidation
!= DbSchemaValidation
.CREATE
) {
160 // if we do not create the datasource, we want to check if the
161 // datasource is compatible with this editor
162 checkDbSchemaVersionCompatibility(monitor
);
165 // we are done with our low level checking and will free resources now
166 cdmSource
.closeOpenConnections();
168 display
.syncExec(new Runnable() {
172 * @see java.lang.Runnable#run()
176 // close the current context
177 CdmStore
.close(monitor
, false);
181 ICdmRepository applicationController
= null;
183 if (!monitor
.isCanceled()) {
184 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
185 .CreateSubMonitor(monitor
, 3);
186 // genrerate new application controller
187 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
191 if (!monitor
.isCanceled()) {
192 CdmStore
.setInstance(applicationController
, cdmSource
);
193 monitor
.subTask(Messages
.CdmStoreConnector_AUTHENTICATING_USER
);
194 display
.syncExec(new Runnable() {
198 * @see java.lang.Runnable#run()
204 // create new security context
205 CdmStore
.getLoginManager().doAuthenticate(loginDialog
.getUsername(), loginDialog
.getPassword());
206 //loginDialog.onComplete();
207 CdmStore
.getContextManager().notifyContextStart();
208 loginDialog
.onComplete();
209 getInstance(Rank
.class).resetTerms();
210 getInstance(NomenclaturalStatusType
.class).resetTerms();
211 Rank
.initDefaultTerms();
212 NomenclaturalStatusType
.initDefaultTerms();
213 } catch(CdmAuthenticationException cae
) {
214 loginDialog
.hide(false);
215 loginDialog
.setMessage("Your credentials are valid \nbut you are not permitted to use \nthe TaxEditor with the selected data source");
222 throw new RuntimeException("Login cancelled");
229 } catch (InvocationTargetException e
) {
230 loginDialog
.hide(false);
231 loginDialog
.setMessage(e
.getMessage());
232 } catch (InterruptedException e
) {
233 loginDialog
.hide(false);
234 loginDialog
.setMessage(e
.getMessage());
239 * Returns a new instance for the given class by using the default constructor.
240 * The constructor must be declared but can be unaccessible (e.g. private)
244 private <T
extends DefinedTermBase
> T
getInstance(Class
<?
extends DefinedTermBase
> termClass
) {
246 Constructor
<T
> c
= ((Class
<T
>)termClass
).getDeclaredConstructor();
247 c
.setAccessible(true);
248 T termInstance
= c
.newInstance();
250 } catch (Exception e
) {
251 throw new RuntimeException(e
);
256 private ICdmRepository
getApplicationController(ICdmSource cdmSource
, CdmProgressMonitorAdapter subprogressMonitor
) {
257 if(cdmSource
instanceof ICdmDataSource
) {
258 return CdmApplicationController
.NewInstance(applicationContextBean
,
259 (ICdmDataSource
)cdmSource
,
263 } else if(cdmSource
instanceof ICdmRemoteSource
) {
264 return CdmApplicationRemoteController
.NewInstance((ICdmRemoteSource
)cdmSource
,
268 throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource
.getName());
272 private void authenticate() {
273 LoginDialog saloginDialog
= new LoginDialog(StoreUtil
.getShell());
274 saloginDialog
.open();
277 private void startContext() {
278 CdmStore
.getContextManager().notifyContextStart();
284 private String
getConnectionMessage() {
285 return cdmSource
.getConnectionMessage();
290 * @throws SQLException
292 private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor
) {
293 monitor
.subTask(Messages
.CdmStoreConnector_CHECK_IF_EDITOR_IS_COMPATIBLE
);
294 String dbSchemaVersion
;
296 String message
= null;
298 dbSchemaVersion
= cdmSource
.getDbSchemaVersion();
299 // we assume that empty dbSchemaVersion means an empty database and
300 // skip version checking
302 if(dbSchemaVersion
!= null) {
303 int compareVersion
= CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
304 // if the datasource version is greater than the taxeditor compatible version then the taxeditor needs to
305 // be updated else the datasource needs to be updated
306 if(compareVersion
> 0) {
307 message
= Messages
.CdmStoreConnector_UPDATE_EDITOR_OR_CHOOSE_COMPATIBLE_DATASOURCE
;
308 } else if (compareVersion
< 0) {
309 message
= Messages
.CdmStoreConnector_UPDATE_DATASOUREC_OR_CHOOSE_NEW_DATASOURCE
;
313 } catch (CdmSourceException e
) {
317 if (message
!= null) {
318 // Show an error message
321 Messages
.CdmStoreConnector_COMPATIBILITY_CHECK_FAILED
,
323 String
.format(Messages
.CdmStoreConnector_SCHEME_NOT_COMPATIBLE
, cdmSource
, message
),
326 monitor
.setCanceled(true);
331 private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor
) {
332 monitor
.subTask(Messages
.CdmStoreConnector_CHECK_IF_NON_EMPTY
);
333 boolean isDbEmpty
= false;
335 isDbEmpty
= cdmSource
.isDbEmpty();
336 } catch (CdmSourceException e
) {
340 dbSchemaValidation
= DbSchemaValidation
.CREATE
;
344 private boolean causeIsCancelationExceptionRecursive(Throwable throwable
){
345 if(throwable
== null){
347 }else if(throwable
instanceof CancellationException
){
350 return causeIsCancelationExceptionRecursive(throwable
.getCause());
354 private void checkDatabaseReachable(IProgressMonitor monitor
) {
356 monitor
.subTask(Messages
.CdmStoreConnector_CHECK_IF_REACHABLE
);
357 cdmSource
.checkConnection();
359 } catch (CdmSourceException e
) {
360 MessagingUtils
.messageDialog(Messages
.CdmStoreConnector_COULD_NOT_CONNECT_TO_CHOSEN_DATASOURCE
,
361 this, Messages
.CdmStoreConnector_REASON
+ e
.getMessage(), e
);
362 monitor
.setCanceled(true);