Move update checks from P2Util to UpdateHandler
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / UpdateHandler.java
index bca8c9923b8b59811899f2d03ae56b73559a7953..6a110d40f98a4ef75b271173ea713d0d66aeef0c 100644 (file)
  *******************************************************************************/
 package eu.etaxonomy.taxeditor.update;
 
+import java.net.URI;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.equinox.internal.p2.ui.ProvUI;
 import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard;
+import org.eclipse.equinox.p2.core.IProvisioningAgent;
+import org.eclipse.equinox.p2.core.ProvisionException;
+import org.eclipse.equinox.p2.operations.ProvisioningJob;
 import org.eclipse.equinox.p2.operations.RepositoryTracker;
 import org.eclipse.equinox.p2.operations.UpdateOperation;
+import org.eclipse.equinox.p2.repository.IRepositoryManager;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
 import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
+import org.eclipse.equinox.p2.ui.ProvisioningUI;
 import org.eclipse.jface.wizard.WizardDialog;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
  * UpdateHandler invokes the check for updates UI
@@ -23,40 +40,101 @@ import org.eclipse.jface.wizard.WizardDialog;
  */
 public class UpdateHandler extends PreloadingRepositoryHandler {
 
-       boolean hasNoRepos = false;
+    boolean hasNoRepos = false;
 
-       @Override
+    @Override
     protected void doExecute(LoadMetadataRepositoryJob job) {
-               if (hasNoRepos) {
-                       return;
-               }
-               UpdateOperation operation = getProvisioningUI().getUpdateOperation(null, null);
-
-
-               // check for updates
-               operation.resolveModal(null);
-               if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
-                       if (UpdateSingleIUWizard.validFor(operation)) {
-                               // Special case for only updating a single root
-                               UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
-                               WizardDialog dialog = new WizardDialog(getShell(), wizard);
-                               dialog.create();
-                               dialog.open();
-                       } else {
-                               // Open the normal version of the update wizard
-                               getProvisioningUI().openUpdateWizard(false, operation, job);
-                       }
-               }
-       }
-
-       @Override
+        if (hasNoRepos) {
+            return;
+        }
+        UpdateOperation operation = getProvisioningUI().getUpdateOperation(null, null);
+
+        // check for updates
+        IStatus status = checkForUpdates(operation);
+
+        if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
+            MessagingUtils.info(status);
+            return;
+        }
+
+        if (status.isOK() && status.getSeverity() != IStatus.ERROR) {
+
+            boolean doInstall = MessagingUtils.confirmDialog("Updates available", "Do you want to install the available updates ?");
+            if(doInstall) {
+                ProvisioningJob provisioningJob = operation.getProvisioningJob(null);
+                if (provisioningJob == null) {
+                    MessagingUtils.messageDialog("Error in performing update",
+                            operation,
+                            "ProvisioningJob could not be created." + System.getProperty("line.separator") +
+                            "Either this application does not support p2 software installation or this application has been launched from within the Eclipse IDE",
+                            null,
+                            false);
+
+                } else {
+                    if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
+                        if (UpdateSingleIUWizard.validFor(operation)) {
+                            // Special case for only updating a single root
+                            UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
+                            WizardDialog dialog = new WizardDialog(getShell(), wizard);
+                            dialog.create();
+                            dialog.open();
+                        } else {
+                            // Open the normal version of the update wizard
+                            getProvisioningUI().openUpdateWizard(false, operation, job);
+                        }
+                    }
+                }
+            }
+        }
+
+    }
+
+
+    /**
+     * @param monitor
+     * @return
+     */
+    private IStatus checkForUpdates(UpdateOperation operation) {
+
+        // force refresh all the caches before
+        IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
+        URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
+        for(URI repo : repos) {
+            try {
+                metaManager.refreshRepository(repo, null);
+            } catch (ProvisionException pe) {
+                IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
+                        "Error occured while reloading cache.", pe);
+
+            } catch (OperationCanceledException oce) {
+                IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
+                        "Error occured while reloading cache.", oce);
+            }
+        }
+        BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
+        ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
+        if (reference == null) {
+            IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
+                    "No provisioning agent found.  This application is not set up for updates.");
+            return errorStatus;
+        }
+
+        try {
+            return operation.resolveModal(null);
+        } finally {
+            bundleContext.ungetService(reference);
+        }
+    }
+
+
+    @Override
     protected boolean preloadRepositories() {
-               hasNoRepos = false;
-               RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
-               if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
-                       hasNoRepos = true;
-                       return false;
-               }
-               return super.preloadRepositories();
-       }
+        hasNoRepos = false;
+        RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
+        if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
+            hasNoRepos = true;
+            return false;
+        }
+        return super.preloadRepositories();
+    }
 }