View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.commands;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import javax.jcr.Node;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.argeo.slc.BasicNameVersion;
27  import org.argeo.slc.NameVersion;
28  import org.argeo.slc.SlcException;
29  import org.argeo.slc.SlcNames;
30  import org.argeo.slc.SlcTypes;
31  import org.argeo.slc.deploy.ModulesManager;
32  import org.eclipse.core.commands.AbstractHandler;
33  import org.eclipse.core.commands.ExecutionEvent;
34  import org.eclipse.core.commands.ExecutionException;
35  import org.eclipse.core.runtime.IProgressMonitor;
36  import org.eclipse.core.runtime.IStatus;
37  import org.eclipse.core.runtime.Status;
38  import org.eclipse.core.runtime.jobs.Job;
39  import org.eclipse.jface.viewers.ISelection;
40  import org.eclipse.jface.viewers.IStructuredSelection;
41  import org.eclipse.ui.handlers.HandlerUtil;
42  
43  /** Deletes one or many results */
44  public class UpdateModule extends AbstractHandler {
45  	private final static Log log = LogFactory.getLog(UpdateModule.class);
46  
47  	private ModulesManager modulesManager;
48  
49  	public Object execute(ExecutionEvent event) throws ExecutionException {
50  		final ISelection selection = HandlerUtil
51  				.getActiveWorkbenchWindow(event).getActivePage().getSelection();
52  		if (selection != null && selection instanceof IStructuredSelection) {
53  			UpdateJob job = new UpdateJob(selection);
54  			job.setUser(true);
55  			job.schedule();
56  		}
57  		return null;
58  	}
59  
60  	private class UpdateJob extends Job {
61  		private final IStructuredSelection selection;
62  
63  		public UpdateJob(ISelection selection) {
64  			super("Update modules");
65  			this.selection = ((IStructuredSelection) selection);
66  		}
67  
68  		@Override
69  		protected IStatus run(IProgressMonitor monitor) {
70  			Iterator<?> it = selection.iterator();
71  			Object obj = null;
72  			try {
73  				Map<String, Node> nodes = new HashMap<String, Node>();
74  				nodes: while (it.hasNext()) {
75  					obj = it.next();
76  					if (obj instanceof Node) {
77  						Node node = (Node) obj;
78  						Node executionModuleNode = null;
79  						while (executionModuleNode == null) {
80  							if (node.isNodeType(SlcTypes.SLC_EXECUTION_MODULE)) {
81  								executionModuleNode = node;
82  							}
83  							node = node.getParent();
84  							if (node.getPath().equals("/"))// root
85  								continue nodes;
86  						}
87  
88  						if (!nodes.containsKey(executionModuleNode.getPath()))
89  							nodes.put(executionModuleNode.getPath(),
90  									executionModuleNode);
91  					}
92  				}
93  
94  				monitor.beginTask("Update modules", nodes.size());
95  				for (Node executionModuleNode : nodes.values()) {
96  					monitor.subTask("Update " + executionModuleNode.getName());
97  					NameVersion nameVersion = new BasicNameVersion(
98  							executionModuleNode.getProperty(SlcNames.SLC_NAME)
99  									.getString(), executionModuleNode
100 									.getProperty(SlcNames.SLC_VERSION)
101 									.getString());
102 					modulesManager.upgrade(nameVersion);
103 					monitor.worked(1);
104 					log.info("Module " + nameVersion + " updated");
105 					if (monitor.isCanceled())
106 						return Status.CANCEL_STATUS;
107 				}
108 				return Status.OK_STATUS;
109 			} catch (Exception e) {
110 				throw new SlcException("Cannot update module " + obj, e);
111 				// return Status.CANCEL_STATUS;
112 			}
113 		}
114 
115 		@Override
116 		protected void canceling() {
117 			getThread().interrupt();
118 			super.canceling();
119 		}
120 
121 	}
122 
123 	public void setModulesManager(ModulesManager modulesManager) {
124 		this.modulesManager = modulesManager;
125 	}
126 
127 }