Project

General

Profile

Download (12.8 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
package eu.etaxonomy.taxeditor.local.server;
10

    
11
import java.lang.reflect.InvocationTargetException;
12
import java.util.concurrent.CancellationException;
13

    
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.core.runtime.Status;
17
import org.eclipse.core.runtime.jobs.Job;
18
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
19
import org.eclipse.jface.operation.IRunnableWithProgress;
20
import org.eclipse.swt.widgets.Display;
21
import org.springframework.core.io.Resource;
22

    
23
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
24
import eu.etaxonomy.cdm.api.application.ICdmRepository;
25
import eu.etaxonomy.cdm.config.CdmSourceException;
26
import eu.etaxonomy.cdm.config.ICdmSource;
27
import eu.etaxonomy.cdm.database.DbSchemaValidation;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
30
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.term.init.TermLoader;
33
import eu.etaxonomy.taxeditor.event.EventUtility;
34
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
35
import eu.etaxonomy.taxeditor.l10n.Messages;
36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
37
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
38
import eu.etaxonomy.taxeditor.model.MessagingUtils;
39
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
40
import eu.etaxonomy.taxeditor.store.CdmAuthenticationException;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42
import eu.etaxonomy.taxeditor.store.LoginManager;
43
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
44

    
45
/**
46
 * @author n.hoffmann
47
 * @created Dec 8, 2010
48
 */
49
public class CdmStoreConnectorLocal extends Job {
50
    private final Display display;
51
    private final ICdmSource cdmSource;
52
    private DbSchemaValidation dbSchemaValidation;
53
    private final Resource applicationContextBean;
54

    
55
    public CdmStoreConnectorLocal(Display display,
56
            ICdmSource cdmSource,
57
            DbSchemaValidation dbSchemaValidation,
58
            Resource applicationContextBean) {
59

    
60
        super(String.format(Messages.CdmStoreConnector_CREATING_DATAMODEL, cdmSource));
61
        this.display = display;
62
        this.cdmSource = cdmSource;
63
        this.dbSchemaValidation = dbSchemaValidation;
64
        this.applicationContextBean = applicationContextBean;
65
    }
66

    
67
    @Override
68
    public IStatus run(final IProgressMonitor monitor) {
69

    
70
        monitor.beginTask(getConnectionMessage(), 10);
71

    
72
        // check if database is up and running
73
        checkDatabaseReachable(monitor);
74

    
75
        if (!monitor.isCanceled()) {
76
            // check if the datasource actually holds data
77
            checkIsNonEmptyCdmDatabase(monitor);
78
        }
79

    
80
        if (dbSchemaValidation != DbSchemaValidation.CREATE
81
                && !monitor.isCanceled()) {
82
            // if we do not create the datasource, we want to check if the
83
            // datasource is compatible with this editor
84
            checkDbSchemaVersionCompatibility(monitor);
85
        }
86

    
87
        // we are done with our low level checking and will free resources now
88
        cdmSource.closeOpenConnections();
89

    
90
        if (!monitor.isCanceled()) {
91
            CdmStore.close(monitor);
92
        }
93

    
94
        ICdmRepository applicationController = null;
95

    
96
        if (!monitor.isCanceled()) {
97
            CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter
98
                    .CreateSubMonitor(monitor, 7);
99
            // This is where we instantiate the application controller
100
            int oldPriority = Thread.currentThread().getPriority();
101
            try {
102
                Thread.currentThread().setPriority(10);
103
                applicationController = getApplicationController(cdmSource,subprogressMonitor);
104
                MessagingUtils.informationDialog(Messages.CdmStoreConnector_SUCCESS, Messages.CdmStoreConnector_DATA_MODEL_CREATION_SUCCESSFUL);
105
                //FIXME E4 migrate or delete because data source view is not used anymore
106
//                CdmDataSourceViewPartE4 dataSourceView = (CdmDataSourceViewPartE4) WorkbenchUtility.getE4WrappedPart(AbstractUtility.getView("eu.etaxonomy.taxeditor.view.datasource", false));
107
//                if(dataSourceView!=null){
108
//                    dataSourceView.refresh();
109
//                }
110
                EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DATASOURCE, true);
111
                return Status.OK_STATUS;
112
            } catch (Exception e) {
113
                if(! causeIsCancelationExceptionRecursive(e)){
114
                    MessagingUtils.errorDialog(Messages.CdmStoreConnector_COULD_NOT_CREATE_DATAMODEL, CdmStoreConnectorLocal.class,
115
                    		String.format(Messages.CdmStoreConnector_ERROR_DURING_DATAMODEL_CREATION, cdmSource.getName()), TaxeditorStorePlugin.PLUGIN_ID, e, true);
116
                    return Status.CANCEL_STATUS;
117
                }
118
            } finally {
119
                monitor.done();
120
                Thread.currentThread().setPriority(oldPriority);
121
            }
122
        }
123
        return Status.CANCEL_STATUS;
124
    }
125

    
126
    public void start(final RemotingLoginDialogLocal loginDialog) {
127
        // hide login dialog and start connection dialog
128
        loginDialog.setMessage(null);
129
        loginDialog.hide(true);
130

    
131
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(AbstractUtility.getShell());
132

    
133
        try {
134
            dialog.run(true, true, new IRunnableWithProgress() {
135

    
136
                @Override
137
                public void run(final IProgressMonitor monitor) {
138
                    try {
139
                        monitor.beginTask(getConnectionMessage(), 7);
140

    
141
                        // check if database is up and running
142
                        checkDatabaseReachable(monitor);
143

    
144
                        // check if the datasource actually holds data
145
                        checkIsNonEmptyCdmDatabase(monitor);
146

    
147
                        if (dbSchemaValidation != DbSchemaValidation.CREATE) {
148
                            // if we do not create the datasource, we want to check if the
149
                            // datasource is compatible with this editor
150
                            checkDbSchemaVersionCompatibility(monitor);
151
                        }
152

    
153
                        // we are done with our low level checking and will free resources now
154
                        cdmSource.closeOpenConnections();
155

    
156
                        display.syncExec(new Runnable() {
157
                            @Override
158
                            public void run() {
159
                                // close the current context
160
                                CdmStore.close(monitor, false);
161
                            }
162
                        });
163

    
164
                        CdmApplicationRemoteController applicationController = null;
165

    
166
                        if (!monitor.isCanceled()) {
167
                            CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter
168
                                    .CreateSubMonitor(monitor, 3);
169
                            // genrerate new application controller
170
                            applicationController = getApplicationController(cdmSource, subprogressMonitor);
171
                        }
172

    
173
                        if (!monitor.isCanceled()) {
174
                            CdmStore.setInstance(applicationController, cdmSource);
175
                            CdmStore.getTermManager().reset();
176
                            monitor.subTask(Messages.CdmStoreConnector_AUTHENTICATING_USER);
177
                            display.syncExec(()-> {
178
                                try {
179
                                    // create new security context
180
                                    CdmStore.getLoginManager().doAuthenticate(loginDialog.getUsername(), loginDialog.getPassword());
181
                                    //loginDialog.onComplete();
182
                                    CdmStore.getContextManager().notifyContextStart();
183
                                    loginDialog.onComplete();
184
                                    //TODO AM: is this necessary to be done on display thread?
185
                                    new TermLoader().unloadAllTerms();
186
                                    Rank.initDefaultTerms();
187
                                    NomenclaturalStatusType.initDefaultTerms();
188
                                    Language.getDefaultLanguage();
189
                                } catch(CdmAuthenticationException cae) {
190
                                    loginDialog.hide(false);
191
                                    loginDialog.setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
192
                                }
193
                            });
194
                        } else {
195
                            throw new RuntimeException("Login cancelled");
196
                        }
197
                    } finally {
198
                        monitor.done();
199
                    }
200
                }
201
            });
202
        } catch (InvocationTargetException e) {
203
            loginDialog.hide(false);
204
            loginDialog.setMessage(e.getMessage());
205
        } catch (InterruptedException e) {
206
            loginDialog.hide(false);
207
            loginDialog.setMessage(e.getMessage());
208
        }
209
    }
210

    
211
    private CdmApplicationRemoteController getApplicationController(ICdmSource cdmSource, CdmProgressMonitorAdapter subprogressMonitor) {
212
        if(cdmSource instanceof ICdmRemoteSource) {
213
            return CdmApplicationRemoteController.NewInstance((ICdmRemoteSource)cdmSource,
214
                    subprogressMonitor,
215
                    null);
216
        } else {
217
            throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource.getName());
218
        }
219
    }
220

    
221
    private String getConnectionMessage() {
222
        return cdmSource.getConnectionMessage();
223
    }
224

    
225
    private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor) {
226
        monitor.subTask(Messages.CdmStoreConnector_CHECK_IF_EDITOR_IS_COMPATIBLE);
227
        String dbSchemaVersion;
228

    
229
        String message = null;
230
        try {
231
            dbSchemaVersion = cdmSource.getDbSchemaVersion();
232
            // we assume that empty dbSchemaVersion means an empty database and
233
            // skip version checking
234

    
235
            if(dbSchemaVersion != null) {
236
                int compareVersion = CdmMetaData.compareVersion(dbSchemaVersion, CdmMetaData.getDbSchemaVersion(), 3, null);
237
                // if the datasource version is greater than the taxeditor compatible version then the taxeditor needs to
238
                // be updated else the datasource needs to be updated
239
                if(compareVersion > 0) {
240
                    message = Messages.CdmStoreConnector_UPDATE_EDITOR_OR_CHOOSE_COMPATIBLE_DATASOURCE;
241
                } else if (compareVersion < 0) {
242
                    message = Messages.CdmStoreConnector_UPDATE_DATASOUREC_OR_CHOOSE_NEW_DATASOURCE;
243
                }
244
            }
245
            monitor.worked(1);
246
        } catch (CdmSourceException e) {
247
            //
248
        }
249

    
250
        if (message != null) {
251
            // Show an error message
252
            MessagingUtils
253
            .messageDialog(
254
                    Messages.CdmStoreConnector_COMPATIBILITY_CHECK_FAILED,
255
                    this,
256
                    String.format(Messages.CdmStoreConnector_SCHEME_NOT_COMPATIBLE, cdmSource, message),
257
                            null);
258

    
259
            monitor.setCanceled(true);
260
        }
261
    }
262

    
263
    private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor) {
264
        monitor.subTask(Messages.CdmStoreConnector_CHECK_IF_NON_EMPTY);
265
        boolean isDbEmpty = false;
266
        try {
267
            isDbEmpty = cdmSource.isDbEmpty();
268
        } catch (CdmSourceException e) {
269
            isDbEmpty = true;
270
        }
271
        if(isDbEmpty) {
272
            dbSchemaValidation = DbSchemaValidation.CREATE;
273
        }
274
    }
275

    
276
    private boolean causeIsCancelationExceptionRecursive(Throwable throwable){
277
        if(throwable == null){
278
            return false;
279
        }else if(throwable instanceof CancellationException){
280
            return true;
281
        }else{
282
            return causeIsCancelationExceptionRecursive(throwable.getCause());
283
        }
284
    }
285

    
286
    private void checkDatabaseReachable(IProgressMonitor monitor) {
287
        try {
288
            monitor.subTask(Messages.CdmStoreConnector_CHECK_IF_REACHABLE);
289
            cdmSource.checkConnection();
290
            monitor.worked(1);
291
        } catch (CdmSourceException e) {
292
            MessagingUtils.messageDialog(Messages.CdmStoreConnector_COULD_NOT_CONNECT_TO_CHOSEN_DATASOURCE,
293
                    this, Messages.CdmStoreConnector_REASON + e.getMessage(), e);
294
            monitor.setCanceled(true);
295
        }
296
    }
297
}
(1-1/3)