154dc12b923f49b450759a692ee94939534b5550
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / datasource / handler / UpdateDataSourceHandler.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.view.datasource.handler;
12
13 import org.eclipse.core.commands.ExecutionEvent;
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.MessageDialog;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.ui.handlers.HandlerUtil;
22 import org.eclipse.ui.progress.IProgressConstants;
23
24 import eu.etaxonomy.cdm.database.update.CdmUpdater;
25 import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
26 import eu.etaxonomy.taxeditor.store.StoreUtil;
27 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
28 import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
29 import eu.etaxonomy.taxeditor.view.datasource.CdmMetaDataAwareDataSourceContainer;
30
31 /**
32 * <p>UpdateDataSourceHandler class.</p>
33 *
34 * @author n.hoffmann
35 * @created Sep 22, 2010
36 * @version 1.0
37 */
38 public class UpdateDataSourceHandler extends AbstractDataSourceHandler {
39
40 /* (non-Javadoc)
41 * @see eu.etaxonomy.taxeditor.datasource.handler.AbstractDataSourceHandler#specificExecute(org.eclipse.core.commands.ExecutionEvent)
42 */
43 /** {@inheritDoc} */
44 @Override
45 public boolean specificExecute(ExecutionEvent event) {
46 final CdmMetaDataAwareDataSourceContainer container = getSelectedDataSourceContainer(event);
47
48 final Display display = Display.getCurrent();
49 if(! MessageDialog.open(MessageDialog.CONFIRM, HandlerUtil.getActiveShell(event), "Update Datasource", "WARNING!\n\n" +
50 "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 " +
51 "Are you sure you want to update the datasource?", SWT.NONE)){
52 return false;
53 }
54 Job job = new Job("Updating datasource " + container.getDataSource().getName()){
55
56 @Override
57 protected IStatus run(IProgressMonitor monitor) {
58 CdmUpdater updater = new CdmUpdater();
59 IStatus status = null;
60
61 try{
62 if(updater.updateToCurrentVersion(container.getDataSource(), CdmProgressMonitorAdapter.CreateMonitor(monitor))){
63 display.asyncExec(new Runnable(){
64
65 @Override
66 public void run() {
67
68 CdmDataSourceViewPart view = (CdmDataSourceViewPart) StoreUtil.getView(CdmDataSourceViewPart.ID, false);
69 if(view != null){
70 view.getViewer().update(new CdmMetaDataAwareDataSourceContainer[]{container}, null);
71 }
72
73 }
74 });
75 status = Status.OK_STATUS;
76 }
77 }catch(Exception e){
78 status = new Status(IStatus.ERROR, TaxeditorStorePlugin.PLUGIN_ID, e.getMessage(), e);
79 StoreUtil.errorDialog("Could not run updater", getClass(), status);
80 }
81
82
83 return status;
84 }
85
86 };
87
88 job.setPriority(Job.BUILD);
89 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
90 job.schedule();
91
92 return false;
93 }
94 }