Project

General

Profile

Download (4.81 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.view.datasource.e4.handler;
11

    
12
import javax.inject.Named;
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.e4.core.di.annotations.CanExecute;
19
import org.eclipse.e4.core.di.annotations.Optional;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.ui.progress.IProgressConstants;
28

    
29
import eu.etaxonomy.cdm.config.ICdmSource;
30
import eu.etaxonomy.cdm.database.ICdmDataSource;
31
import eu.etaxonomy.cdm.database.update.CdmUpdater;
32
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
33
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
34
import eu.etaxonomy.taxeditor.model.MessagingUtils;
35
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
36
import eu.etaxonomy.taxeditor.view.datasource.CdmMetaDataAwareDataSourceContainer;
37
import eu.etaxonomy.taxeditor.view.datasource.e4.CdmDataSourceViewPartE4;
38

    
39
/**
40
 *
41
 * @author pplitzner
42
 * @date 22.08.2017
43
 *
44
 */
45
public class UpdateDataSourceHandlerE4 extends AbstractDataSourceHandlerE4 {
46

    
47
    /** {@inheritDoc} */
48
    @Override
49
    public boolean specificExecute(CdmDataSourceViewPartE4 dataSourceViewPart,
50
            CdmMetaDataAwareDataSourceContainer container, Shell shell) {
51
        if(! MessageDialog.open(MessageDialog.CONFIRM, shell, "Update Datasource", "WARNING!\n\n" +
52
                "This will change your data base structure! Be sure you have an up-to-date backup of your data before running the update! \n\n " +
53
                "Are you sure you want to update the datasource?", SWT.NONE)){
54
            return false;
55
        }
56
        Job job = new Job("Updating datasource " + container.getCdmSource().getName()){
57

    
58
            @Override
59
            protected IStatus run(IProgressMonitor monitor) {
60
                CdmUpdater updater = new CdmUpdater();
61
                IStatus status = null;
62
                ICdmSource cdmSource = container.getCdmSource();
63
                //FIXME:Remoting we need another updater for ICdmRemoteSource
64
                if(cdmSource instanceof ICdmDataSource) {
65
                    try{
66
                        SchemaUpdateResult result = updater.updateToCurrentVersion(
67
                                (ICdmDataSource)cdmSource, CdmProgressMonitorAdapter.CreateMonitor(monitor));
68
                        if(result.isSuccess()){
69
                           sync.asyncExec(()->{
70
                                    if(dataSourceViewPart!= null){
71
                                        container.getMetaDataFromDataSource();
72
                                        dataSourceViewPart.getViewer().update(new CdmMetaDataAwareDataSourceContainer[]{container}, null);
73
                                    }
74
                                }
75
                            );
76
                            status = Status.OK_STATUS;
77
                        }else{
78
                            throw new RuntimeException("An error occurred during the update.");
79
                        }
80
                    }catch(Exception e){
81
                        status = new Status(IStatus.ERROR, TaxeditorStorePlugin.PLUGIN_ID, e.getMessage(), e);
82
                        MessagingUtils.errorDialog("Could not complete updater", updater, status.getMessage(), status.getPlugin(), e, true);
83
                    }
84
                }
85

    
86
                return status;
87
            }
88

    
89
        };
90

    
91
        job.setPriority(Job.BUILD);
92
        job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
93
        job.schedule();
94

    
95
        return true;
96
    }
97

    
98

    
99
    @Override
100
    @CanExecute
101
    public boolean canExecute(
102
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
103
            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
104
            MHandledMenuItem menuItem) {
105
        boolean canExecute = false;
106
        canExecute = thisPart.getObject() instanceof CdmDataSourceViewPartE4
107
                && selection!=null
108
                && selection.size()==1
109
                && selection.getFirstElement() instanceof CdmMetaDataAwareDataSourceContainer
110
                && !((CdmMetaDataAwareDataSourceContainer) selection.getFirstElement()).isDataSourceCompatible();
111
        menuItem.setVisible(canExecute);
112
        return canExecute;
113
    }
114
}
(7-7/7)