Merge branch 'release/4.7.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / P2Util.java
index 9e1731c4cf7cdcc7c73745e5bcf39b1d8ef705b1..dcc0a844975d89f94012e46936682899cc187cc3 100644 (file)
@@ -5,38 +5,18 @@ import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.jobs.IJobChangeEvent;
-import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.core.runtime.jobs.JobChangeAdapter;
 import org.eclipse.equinox.internal.p2.ui.ProvUI;
 import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
 import org.eclipse.equinox.p2.repository.IRepository;
 import org.eclipse.equinox.p2.repository.IRepositoryManager;
-import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
-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.ProvisioningSession;
-import org.eclipse.equinox.p2.operations.RepositoryTracker;
-import org.eclipse.equinox.p2.operations.Update;
-import org.eclipse.equinox.p2.operations.UpdateOperation;
 import org.eclipse.equinox.p2.ui.ProvisioningUI;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.PlatformUI;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
 
-import eu.etaxonomy.taxeditor.ApplicationUtil;
 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.util.ApplicationUtil;
 
 /**
  * This class is a utility class for updating the editor from a p2 update site,
@@ -99,7 +79,7 @@ public class P2Util {
         repoElements.addAll(savedRepoElements);
 
         ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
-                .toArray(new MetadataRepositoryElement[]{} ), null);
+                .toArray(new MetadataRepositoryElement[]{} ));
 
     }
 
@@ -140,141 +120,4 @@ public class P2Util {
         }
         PreferencesUtil.setP2Repositories(repoElements);
     }
-
-    /**
-     *
-     *
-     */
-    public static void checkForUpdates() {
-        // the main job which performs the update
-        Job updateJob = new Job("Update Job") {
-            @Override
-            public IStatus run(IProgressMonitor monitor) {
-                return doCheckForUpdates(monitor);
-            }
-        };
-        updateJob.schedule();
-    }
-
-    /**
-     * @param monitor
-     * @return
-     */
-    private static IStatus doCheckForUpdates(IProgressMonitor monitor) {
-
-        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;
-        }
-
-        final IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
-        IStatus updateStatus;
-        try {
-            updateStatus = P2Util.checkForUpdates(agent, monitor);
-            MessagingUtils.info(updateStatus);
-        } finally {
-            bundleContext.ungetService(reference);
-        }
-        return updateStatus;
-    }
-
-    /**
-     * @param agent
-     * @param monitor
-     * @return
-     * @throws OperationCanceledException
-     */
-    static IStatus checkForUpdates(IProvisioningAgent agent, final IProgressMonitor monitor) {
-        ProvisioningSession session = new ProvisioningSession(agent);
-        // the default update operation looks for updates to the currently
-        // running profile, using the default profile root marker. To change
-        // which installable units are being updated, use the more detailed
-        // constructors.
-        final UpdateOperation operation = new UpdateOperation(session);
-//        try {
-//            setUpdateRepositories(operation);
-//        } catch (URISyntaxException e) {
-//            MessagingUtils.errorDialog("Invalid update site URI",
-//                    operation,
-//                    "The update site URI has an invalid syntax",
-//                    TaxonomicEditorPlugin.PLUGIN_ID,
-//                    e,
-//                    false);
-//            return null;
-//        }
-
-        final IStatus status = operation.resolveModal(monitor);
-
-        if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
-            return status;
-        }
-
-        if (status.isOK() && status.getSeverity() != IStatus.ERROR) {
-            // We need this block of code to be in async execution
-            // since the confirm dialogs work only on the UI thread
-            Display.getDefault().asyncExec(new Runnable() {
-                @Override
-                public void run() {
-                    String updates = "";
-                    Update[] possibleUpdates = operation
-                            .getPossibleUpdates();
-                    for (Update update : possibleUpdates) {
-                        updates += update + "\n";
-                    }
-
-                    boolean doInstall = MessagingUtils.confirmDialog("Updates available", "Do you want to install the available updates ?");
-                    // We may need to think whether we still run in async mode once
-                    // the user agrees to update. Maybe be reasonable to change to blocking
-                    // from this point until the update is complete.
-
-                    // More complex status handling might include showing the user what
-                    // updates are available if there are multiples, differentiating
-                    // patches vs. updates, etc. In this example, we simply update as
-                    // suggested by the operation.
-                    if(doInstall) {
-                        ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
-                        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 {
-                            // register a job change listener to track
-                            // installation progress and notify user upon success
-                            provisioningJob
-                            .addJobChangeListener(new JobChangeAdapter() {
-                                @Override
-                                public void done(IJobChangeEvent event) {
-                                    if (event.getResult().isOK()) {
-                                        // We need this block of code to be in async execution
-                                        // since the confirm dialogs work only on the UI thread
-                                        Display.getDefault().asyncExec(new Runnable() {
-                                            @Override
-                                            public void run() {
-                                                boolean restart = MessagingUtils.confirmDialog(
-                                                        "Updates installed, restart?",
-                                                        "Updates have been installed successfully, do you want to restart?");
-                                                if (restart) {
-                                                    PlatformUI.getWorkbench().restart();
-                                                }
-                                            }
-                                        });
-                                    }
-                                    super.done(event);
-                                }
-                            });
-                            provisioningJob.schedule();
-                        }
-                    }
-                }
-            });
-        }
-        return status;
-    }
 }