6a110d40f98a4ef75b271173ea713d0d66aeef0c
[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.info(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 /**
94 * @param monitor
95 * @return
96 */
97 private IStatus checkForUpdates(UpdateOperation operation) {
98
99 // force refresh all the caches before
100 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
101 URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
102 for(URI repo : repos) {
103 try {
104 metaManager.refreshRepository(repo, null);
105 } catch (ProvisionException pe) {
106 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
107 "Error occured while reloading cache.", pe);
108
109 } catch (OperationCanceledException oce) {
110 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
111 "Error occured while reloading cache.", oce);
112 }
113 }
114 BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
115 ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
116 if (reference == null) {
117 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
118 "No provisioning agent found. This application is not set up for updates.");
119 return errorStatus;
120 }
121
122 try {
123 return operation.resolveModal(null);
124 } finally {
125 bundleContext.ungetService(reference);
126 }
127 }
128
129
130 @Override
131 protected boolean preloadRepositories() {
132 hasNoRepos = false;
133 RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
134 if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
135 hasNoRepos = true;
136 return false;
137 }
138 return super.preloadRepositories();
139 }
140 }