Project

General

Profile

Download (14.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.store;
11

    
12
import java.lang.reflect.Constructor;
13
import java.lang.reflect.InvocationTargetException;
14
import java.sql.SQLException;
15
import java.util.concurrent.CancellationException;
16

    
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;
25

    
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;
46

    
47

    
48
/**
49
 * @author n.hoffmann
50
 * @created Dec 8, 2010
51
 * @version 1.0
52
 */
53
class CdmStoreConnector extends Job {
54
    private final Display display;
55
    private final ICdmSource cdmSource;
56
    private DbSchemaValidation dbSchemaValidation;
57
    private final Resource applicationContextBean;
58

    
59

    
60
    /**
61
     * @param datasource
62
     * @param dbSchemaValidation
63
     * @param applicationContextBean
64
     */
65
    public CdmStoreConnector(Display display,
66
            ICdmSource cdmSource,
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;
74
    }
75

    
76

    
77

    
78
    @Override
79
    public IStatus run(final IProgressMonitor monitor) {
80

    
81
        monitor.beginTask(getConnectionMessage(), 10);
82

    
83
        // check if database is up and running
84
        checkDatabaseReachable(monitor);
85

    
86
        if (!monitor.isCanceled()) {
87
            // check if the datasource actually holds data
88
            checkIsNonEmptyCdmDatabase(monitor);
89
        }
90

    
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);
96
        }
97

    
98
        // we are done with our low level checking and will free resources now
99
        cdmSource.closeOpenConnections();
100

    
101
        if (!monitor.isCanceled()) {
102
            CdmStore.close(monitor);
103
        }
104

    
105
        ICdmRepository applicationController = null;
106

    
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();
112
            try {
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();
120
//                }
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;
128
                }
129
            } finally {
130
                monitor.done();
131
                Thread.currentThread().setPriority(oldPriority);
132
            }
133
        }
134
        return Status.CANCEL_STATUS;
135

    
136
    }
137

    
138
    public void start(final RemotingLoginDialog loginDialog) {
139
        // hide login dialog and start connection dialog
140
        loginDialog.setMessage(null);
141
        loginDialog.hide(true);
142

    
143

    
144
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
145

    
146
        try {
147
            dialog.run(true, true, new IRunnableWithProgress() {
148
                @Override
149
                public void run(final IProgressMonitor monitor) {
150
                    try {
151
                        monitor.beginTask(getConnectionMessage(), 7);
152

    
153
                        // check if database is up and running
154
                        checkDatabaseReachable(monitor);
155

    
156
                        // check if the datasource actually holds data
157
                        checkIsNonEmptyCdmDatabase(monitor);
158

    
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);
163
                        }
164

    
165
                        // we are done with our low level checking and will free resources now
166
                        cdmSource.closeOpenConnections();
167

    
168
                        display.syncExec(new Runnable() {
169
                            /*
170
                             * (non-Javadoc)
171
                             *
172
                             * @see java.lang.Runnable#run()
173
                             */
174
                            @Override
175
                            public void run() {
176
                                // close the current context
177
                                CdmStore.close(monitor, false);
178
                            }
179
                        });
180

    
181
                        ICdmRepository applicationController = null;
182

    
183
                        if (!monitor.isCanceled()) {
184
                            CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter
185
                                    .CreateSubMonitor(monitor, 3);
186
                            // genrerate new application controller
187
                            applicationController = getApplicationController(cdmSource,subprogressMonitor);
188
                        }
189

    
190

    
191
                        if (!monitor.isCanceled()) {
192
                            CdmStore.setInstance(applicationController, cdmSource);
193
                            monitor.subTask(Messages.CdmStoreConnector_AUTHENTICATING_USER);
194
                            display.syncExec(new Runnable() {
195
                                /*
196
                                 * (non-Javadoc)
197
                                 *
198
                                 * @see java.lang.Runnable#run()
199
                                 */
200
                                @Override
201
                                public void run() {
202

    
203
                                    try {
204
                                        // create new security context
205
                                        CdmStore.getLoginManager().doAuthenticate(loginDialog.getUsername(), loginDialog.getPassword());
206
                                        loginDialog.onComplete();
207
                                        CdmStore.getContextManager().notifyContextStart();
208
                                        getInstance(Rank.class).resetTerms();
209
                                        getInstance(NomenclaturalStatusType.class).resetTerms();
210
                                        Rank.initDefaultTerms();
211
                                        NomenclaturalStatusType.initDefaultTerms();
212
                                    } catch(CdmAuthenticationException cae) {
213
                                        loginDialog.hide(false);
214
                                        loginDialog.setMessage(cae.getMessage());
215
                                    }
216

    
217
                                }
218
                            });
219
                        } else {
220
                            throw new RuntimeException("Login cancelled");
221
                        }
222
                    } finally {
223
                        monitor.done();
224
                    }
225
                }
226
            });
227
        } catch (InvocationTargetException e) {
228
            loginDialog.hide(false);
229
            loginDialog.setMessage(e.getMessage());
230
        } catch (InterruptedException e) {
231
            loginDialog.hide(false);
232
            loginDialog.setMessage(e.getMessage());
233
        }
234
    }
235

    
236
    /**
237
     * Returns a new instance for the given class by using the default constructor.
238
     * The constructor must be declared but can be unaccessible (e.g. private)
239
     * @param termClass
240
     * @return
241
     */
242
    private  <T extends DefinedTermBase> T getInstance(Class<? extends DefinedTermBase> termClass) {
243
        try {
244
            Constructor<T> c = ((Class<T>)termClass).getDeclaredConstructor();
245
            c.setAccessible(true);
246
            T termInstance = c.newInstance();
247
            return termInstance;
248
        } catch (Exception e) {
249
            throw new RuntimeException(e);
250
        }
251
    }
252

    
253

    
254
    private ICdmRepository getApplicationController(ICdmSource cdmSource, CdmProgressMonitorAdapter subprogressMonitor) {
255
        if(cdmSource instanceof ICdmDataSource) {
256
            return  CdmApplicationController.NewInstance(applicationContextBean,
257
                    (ICdmDataSource)cdmSource,
258
                    dbSchemaValidation,
259
                    false,
260
                    subprogressMonitor);
261
        } else if(cdmSource instanceof ICdmRemoteSource) {
262
            return CdmApplicationRemoteController.NewInstance((ICdmRemoteSource)cdmSource,
263
                    subprogressMonitor,
264
                    null);
265
        } else {
266
            throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource.getName());
267
        }
268
    }
269

    
270
    private void authenticate() {
271
        LoginDialog saloginDialog = new LoginDialog(StoreUtil.getShell());
272
        saloginDialog.open();
273
    }
274

    
275
    private void startContext() {
276
        CdmStore.getContextManager().notifyContextStart();
277
    }
278

    
279
    /**
280
     * @return
281
     */
282
    private String getConnectionMessage() {
283
        return cdmSource.getConnectionMessage();
284
    }
285

    
286
    /**
287
     * @return
288
     * @throws SQLException
289
     */
290
    private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor) {
291
        monitor.subTask(Messages.CdmStoreConnector_CHECK_IF_EDITOR_IS_COMPATIBLE);
292
        String dbSchemaVersion;
293

    
294
        String message = null;
295
        try {
296
            dbSchemaVersion = cdmSource.getDbSchemaVersion();
297
            // we assume that empty dbSchemaVersion means an empty database and
298
            // skip version checking
299

    
300
            if(dbSchemaVersion != null) {
301
                int compareVersion = CdmMetaData.compareVersion(dbSchemaVersion, CdmMetaData.getDbSchemaVersion(), 3, null);
302
                // if the datasource version is greater than the taxeditor compatible version then the taxeditor needs to
303
                // be updated else the datasource needs to be updated
304
                if(compareVersion > 0) {
305
                    message = Messages.CdmStoreConnector_UPDATE_EDITOR_OR_CHOOSE_COMPATIBLE_DATASOURCE;
306
                } else if (compareVersion < 0) {
307
                    message = Messages.CdmStoreConnector_UPDATE_DATASOUREC_OR_CHOOSE_NEW_DATASOURCE;
308
                }
309
            }
310
            monitor.worked(1);
311
        } catch (CdmSourceException e) {
312
            //
313
        }
314

    
315
        if (message != null) {
316
            // Show an error message
317
            MessagingUtils
318
            .messageDialog(
319
                    Messages.CdmStoreConnector_COMPATIBILITY_CHECK_FAILED,
320
                    this,
321
                    String.format(Messages.CdmStoreConnector_SCHEME_NOT_COMPATIBLE, cdmSource, message),
322
                            null);
323

    
324
            monitor.setCanceled(true);
325
        }
326

    
327
    }
328

    
329
    private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor) {
330
        monitor.subTask(Messages.CdmStoreConnector_CHECK_IF_NON_EMPTY);
331
        boolean isDbEmpty = false;
332
        try {
333
            isDbEmpty = cdmSource.isDbEmpty();
334
        } catch (CdmSourceException e) {
335
            isDbEmpty = true;
336
        }
337
        if(isDbEmpty) {
338
            dbSchemaValidation = DbSchemaValidation.CREATE;
339
        }
340
    }
341

    
342
    private boolean causeIsCancelationExceptionRecursive(Throwable throwable){
343
        if(throwable == null){
344
            return false;
345
        }else if(throwable instanceof CancellationException){
346
            return true;
347
        }else{
348
            return causeIsCancelationExceptionRecursive(throwable.getCause());
349
        }
350
    }
351

    
352
    private void checkDatabaseReachable(IProgressMonitor monitor) {
353
        try {
354
            monitor.subTask(Messages.CdmStoreConnector_CHECK_IF_REACHABLE);
355
            cdmSource.checkConnection();
356
            monitor.worked(1);
357
        } catch (CdmSourceException e) {
358
            MessagingUtils.messageDialog(Messages.CdmStoreConnector_COULD_NOT_CONNECT_TO_CHOSEN_DATASOURCE,
359
                    this, Messages.CdmStoreConnector_REASON + e.getMessage(), e);
360
            monitor.setCanceled(true);
361
        }
362
    }
363

    
364

    
365
}
(4-4/14)