]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/commands/UpdateModule.java
SLC UI building (except Dist)
[gpl/argeo-slc.git] / org.argeo.slc.client.ui / src / org / argeo / slc / client / ui / commands / UpdateModule.java
diff --git a/org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/commands/UpdateModule.java b/org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/commands/UpdateModule.java
new file mode 100644 (file)
index 0000000..7843dc2
--- /dev/null
@@ -0,0 +1,127 @@
+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.argeo.slc.client.ui.commands;\r
+\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+\r
+import javax.jcr.Node;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.argeo.slc.BasicNameVersion;\r
+import org.argeo.slc.NameVersion;\r
+import org.argeo.slc.SlcException;\r
+import org.argeo.slc.deploy.ModulesManager;\r
+import org.argeo.slc.jcr.SlcNames;\r
+import org.argeo.slc.jcr.SlcTypes;\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+\r
+/** Deletes one or many results */\r
+public class UpdateModule extends AbstractHandler {\r
+       private final static Log log = LogFactory.getLog(UpdateModule.class);\r
+\r
+       private ModulesManager modulesManager;\r
+\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               final ISelection selection = HandlerUtil\r
+                               .getActiveWorkbenchWindow(event).getActivePage().getSelection();\r
+               if (selection != null && selection instanceof IStructuredSelection) {\r
+                       UpdateJob job = new UpdateJob(selection);\r
+                       job.setUser(true);\r
+                       job.schedule();\r
+               }\r
+               return null;\r
+       }\r
+\r
+       private class UpdateJob extends Job {\r
+               private final IStructuredSelection selection;\r
+\r
+               public UpdateJob(ISelection selection) {\r
+                       super("Update modules");\r
+                       this.selection = ((IStructuredSelection) selection);\r
+               }\r
+\r
+               @Override\r
+               protected IStatus run(IProgressMonitor monitor) {\r
+                       Iterator<?> it = selection.iterator();\r
+                       Object obj = null;\r
+                       try {\r
+                               Map<String, Node> nodes = new HashMap<String, Node>();\r
+                               nodes: while (it.hasNext()) {\r
+                                       obj = it.next();\r
+                                       if (obj instanceof Node) {\r
+                                               Node node = (Node) obj;\r
+                                               Node executionModuleNode = null;\r
+                                               while (executionModuleNode == null) {\r
+                                                       if (node.isNodeType(SlcTypes.SLC_EXECUTION_MODULE)) {\r
+                                                               executionModuleNode = node;\r
+                                                       }\r
+                                                       node = node.getParent();\r
+                                                       if (node.getPath().equals("/"))// root\r
+                                                               continue nodes;\r
+                                               }\r
+\r
+                                               if (!nodes.containsKey(executionModuleNode.getPath()))\r
+                                                       nodes.put(executionModuleNode.getPath(),\r
+                                                                       executionModuleNode);\r
+                                       }\r
+                               }\r
+\r
+                               monitor.beginTask("Update modules", nodes.size());\r
+                               for (Node executionModuleNode : nodes.values()) {\r
+                                       monitor.subTask("Update " + executionModuleNode.getName());\r
+                                       NameVersion nameVersion = new BasicNameVersion(\r
+                                                       executionModuleNode.getProperty(SlcNames.SLC_NAME)\r
+                                                                       .getString(), executionModuleNode\r
+                                                                       .getProperty(SlcNames.SLC_VERSION)\r
+                                                                       .getString());\r
+                                       modulesManager.upgrade(nameVersion);\r
+                                       monitor.worked(1);\r
+                                       log.info("Module " + nameVersion + " updated");\r
+                                       if (monitor.isCanceled())\r
+                                               return Status.CANCEL_STATUS;\r
+                               }\r
+                               return Status.OK_STATUS;\r
+                       } catch (Exception e) {\r
+                               throw new SlcException("Cannot update module " + obj, e);\r
+                               // return Status.CANCEL_STATUS;\r
+                       }\r
+               }\r
+\r
+               @Override\r
+               protected void canceling() {\r
+                       getThread().interrupt();\r
+                       super.canceling();\r
+               }\r
+\r
+       }\r
+\r
+       public void setModulesManager(ModulesManager modulesManager) {\r
+               this.modulesManager = modulesManager;\r
+       }\r
+\r
+}\r