43c6e15bb4537fbae370c9f5984a22d54669986a
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / UpdateHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2007, 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package eu.etaxonomy.taxeditor.update;
12
13 import java.net.URI;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.OperationCanceledException;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.equinox.internal.p2.ui.ProvUI;
19 import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard;
20 import org.eclipse.equinox.p2.core.IProvisioningAgent;
21 import org.eclipse.equinox.p2.core.ProvisionException;
22 import org.eclipse.equinox.p2.operations.ProvisioningJob;
23 import org.eclipse.equinox.p2.operations.RepositoryTracker;
24 import org.eclipse.equinox.p2.operations.UpdateOperation;
25 import org.eclipse.equinox.p2.repository.IRepositoryManager;
26 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
27 import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
28 import org.eclipse.equinox.p2.ui.ProvisioningUI;
29 import org.eclipse.jface.wizard.WizardDialog;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceReference;
32
33 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35
36 /**
37 * UpdateHandler invokes the check for updates UI
38 *
39 * @since 3.4
40 */
41 public class UpdateHandler extends PreloadingRepositoryHandler {
42
43 boolean hasNoRepos = false;
44
45 @Override
46 protected void doExecute(LoadMetadataRepositoryJob job) {
47 if (hasNoRepos) {
48 return;
49 }
50 UpdateOperation operation = getProvisioningUI().getUpdateOperation(null, null);
51
52 // check for updates
53 IStatus status = checkForUpdates(operation);
54
55 if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
56 MessagingUtils.informationDialog("Checking for updates", status);
57 return;
58 }
59
60 if (status.isOK() && status.getSeverity() != IStatus.ERROR) {
61
62 boolean doInstall = MessagingUtils.confirmDialog("Updates available", "Do you want to install the available updates ?");
63 if(doInstall) {
64 ProvisioningJob provisioningJob = operation.getProvisioningJob(null);
65 if (provisioningJob == null) {
66 MessagingUtils.messageDialog("Error in performing update",
67 operation,
68 "ProvisioningJob could not be created." + System.getProperty("line.separator") +
69 "Either this application does not support p2 software installation or this application has been launched from within the Eclipse IDE",
70 null,
71 false);
72
73 } else {
74 if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
75 if (UpdateSingleIUWizard.validFor(operation)) {
76 // Special case for only updating a single root
77 UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
78 WizardDialog dialog = new WizardDialog(getShell(), wizard);
79 dialog.create();
80 dialog.open();
81 } else {
82 // Open the normal version of the update wizard
83 getProvisioningUI().openUpdateWizard(false, operation, job);
84 }
85 }
86 }
87 }
88 }
89 }
90
91
92 /**
93 * @param monitor
94 * @return
95 */
96 private IStatus checkForUpdates(UpdateOperation operation) {
97
98 // force refresh all the caches before
99 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
100 URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
101 for(URI repo : repos) {
102 try {
103 metaManager.refreshRepository(repo, null);
104 } catch (ProvisionException pe) {
105 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
106 "Error occured while reloading cache.", pe);
107
108 } catch (OperationCanceledException oce) {
109 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
110 "Error occured while reloading cache.", oce);
111 }
112 }
113 BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
114 ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
115 if (reference == null) {
116 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
117 "No provisioning agent found. This application is not set up for updates.");
118 return errorStatus;
119 }
120
121 try {
122 return operation.resolveModal(null);
123 } finally {
124 bundleContext.ungetService(reference);
125 }
126 }
127
128
129 @Override
130 protected boolean preloadRepositories() {
131 hasNoRepos = false;
132 RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
133 if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
134 hasNoRepos = true;
135 return false;
136 }
137 return super.preloadRepositories();
138 }
139 }