list specimen-taxon associations separately in specimen details view
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / UpdateHandler.java
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.update;
10
11 import java.lang.reflect.InvocationTargetException;
12 import java.net.URI;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.OperationCanceledException;
20 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
21 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
22 import org.eclipse.equinox.internal.p2.ui.ProvUI;
23 import org.eclipse.equinox.p2.core.IProvisioningAgent;
24 import org.eclipse.equinox.p2.core.ProvisionException;
25 import org.eclipse.equinox.p2.operations.ProvisioningJob;
26 import org.eclipse.equinox.p2.operations.ProvisioningSession;
27 import org.eclipse.equinox.p2.operations.UpdateOperation;
28 import org.eclipse.equinox.p2.repository.IRepositoryManager;
29 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
30 import org.eclipse.equinox.p2.ui.ProvisioningUI;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
33 import org.eclipse.jface.operation.IRunnableWithProgress;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.ui.IWorkbench;
37 import org.eclipse.ui.PlatformUI;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.ServiceReference;
40
41 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
42 import eu.etaxonomy.taxeditor.model.MessagingUtils;
43
44 /**
45 * UpdateHandler invokes the check for updates UI
46 *
47 */
48 public class UpdateHandler extends AbstractHandler {
49
50 private static final String ERROR_PERFORMING_UPDATES = "Error performing updates";
51 /**
52 * {@inheritDoc}
53 */
54 @Override
55 public Object execute(ExecutionEvent event) throws ExecutionException {
56 // update using a progress monitor
57 IRunnableWithProgress runnable = new IRunnableWithProgress() {
58 @Override
59 public void run(IProgressMonitor monitor) throws InvocationTargetException,
60 InterruptedException {
61 doExecute(monitor);
62 }
63 };
64
65 try {
66 new ProgressMonitorDialog(null).run(true, true, runnable);
67 } catch (InvocationTargetException | InterruptedException e) {
68 showError(e);
69 }
70 return null;
71 }
72
73 private void doExecute(IProgressMonitor monitor) {
74
75 //UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
76
77 BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
78 ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
79 if (reference == null) {
80 showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "No provisioning agent found. This application is not set up for updates.");
81 return;
82 }
83
84 final IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
85 final IWorkbench workbench = PlatformUI.getWorkbench();
86
87 ProvisioningSession session = new ProvisioningSession(agent);
88 // update all user-visible installable units
89 UpdateOperation operation = new UpdateOperation(session);
90
91 // force refresh all the repository caches before update
92 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
93 URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
94
95 for(URI repo : repos) {
96 try {
97 metaManager.refreshRepository(repo, null);
98 } catch (ProvisionException | OperationCanceledException e) {
99 showError(e);
100 }
101 }
102
103 // set location of artifact and metadata repo
104 operation.getProvisioningContext().setArtifactRepositories(repos);
105 operation.getProvisioningContext().setMetadataRepositories(repos);
106
107 IStatus status = operation.resolveModal(monitor);
108
109 // if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
110 // if (UpdateSingleIUWizard.validFor(operation)) {
111 // // Special case for only updating a single root
112 // UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
113 // WizardDialog dialog = new WizardDialog(getShell(), wizard);
114 // dialog.create();
115 // dialog.open();
116 // } else {
117 // // Open the normal version of the update wizard
118 // getProvisioningUI().openUpdateWizard(false, operation, job);
119 // }
120 // }
121
122 if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
123 showMessage(MessageDialog.INFORMATION, "Checking for updates", "No updates were found");
124 return;
125 }
126
127 final ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
128 if (provisioningJob != null) {
129 Display.getDefault().syncExec(new Runnable() {
130 @Override
131 public void run() {
132 boolean performUpdate = MessageDialog.openQuestion(
133 null,
134 "Updates available",
135 "There are updates available. Do you want to install them now?");
136 if (performUpdate) {
137 provisioningJob.addJobChangeListener(new JobChangeAdapter() {
138 @Override
139 public void done(IJobChangeEvent event) {
140 if (event.getResult().isOK()) {
141 Display.getDefault().syncExec(new Runnable() {
142 @Override
143 public void run() {
144 boolean restart = MessageDialog.openQuestion(null,
145 "Updates installed, restart?",
146 "Updates have been installed successfully, do you want to restart?");
147 if (restart) {
148 workbench.restart();
149 }
150 }
151 });
152 } else {
153 MessageDialog.openInformation(
154 null,
155 ERROR_PERFORMING_UPDATES,
156 event.getResult().getMessage());
157 }
158 }
159 });
160 provisioningJob.schedule();
161 }
162 }
163 });
164 } else {
165 if (operation.hasResolved()) {
166 showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't get provisioning job: " + operation.getResolutionResult());
167 } else {
168 showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't resolve provisioning job");
169 }
170 }
171 }
172
173 private void showMessage(final int kind, final String title, final String info) {
174 Display.getDefault().syncExec(new Runnable() {
175 @Override
176 public void run() {
177 MessageDialog.open(kind, null, title, info, SWT.NONE);
178 }
179 });
180 }
181
182 private void showError(final Throwable t) {
183 Display.getDefault().syncExec(new Runnable() {
184 @Override
185 public void run() {
186 MessagingUtils.errorDialog(ERROR_PERFORMING_UPDATES,
187 this,
188 t.getMessage(),
189 TaxonomicEditorPlugin.PLUGIN_ID,
190 t,
191 true);
192 }
193 });
194 }
195
196
197 }