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