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  import javax.jcr.RepositoryException;
24  import javax.jcr.Session;
25  
26  import org.argeo.slc.SlcException;
27  import org.argeo.slc.client.ui.ClientUiPlugin;
28  import org.argeo.slc.client.ui.model.ResultFolder;
29  import org.argeo.slc.client.ui.model.ResultParent;
30  import org.argeo.slc.client.ui.model.ResultParentUtils;
31  import org.argeo.slc.client.ui.model.SingleResultNode;
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.dialogs.MessageDialog;
40  import org.eclipse.jface.resource.ImageDescriptor;
41  import org.eclipse.jface.viewers.ISelection;
42  import org.eclipse.jface.viewers.IStructuredSelection;
43  import org.eclipse.ui.handlers.HandlerUtil;
44  
45  /** Deletes one or many results */
46  public class DeleteItems extends AbstractHandler {
47  	public final static String ID = ClientUiPlugin.ID + ".deleteItems";
48  	public final static ImageDescriptor DEFAULT_IMG_DESCRIPTOR = ClientUiPlugin
49  			.getImageDescriptor("icons/removeAll.png");
50  	public final static String DEFAULT_LABEL = "Delete selected item(s)";
51  
52  	public Object execute(final ExecutionEvent event) throws ExecutionException {
53  		final ISelection selection = HandlerUtil
54  				.getActiveWorkbenchWindow(event).getActivePage().getSelection();
55  
56  		// confirmation
57  		StringBuilder buf = new StringBuilder("");
58  		Iterator<?> lst = ((IStructuredSelection) selection).iterator();
59  		while (lst.hasNext()) {
60  			Object obj = lst.next();
61  			if (obj instanceof ResultParent) {
62  				ResultParent rp = ((ResultParent) obj);
63  				buf.append(rp.getName()).append(", ");
64  			}
65  		}
66  
67  		String msg = "Nothing to delete";
68  		// remove last separator
69  		if (buf.lastIndexOf(", ") > -1) {
70  			msg = "Do you want to delete following objects (and their children): "
71  					+ buf.substring(0, buf.lastIndexOf(", ")) + "?";
72  		}
73  		Boolean ok = MessageDialog.openConfirm(
74  				HandlerUtil.getActiveShell(event), "Confirm deletion", msg);
75  
76  		if (!ok)
77  			return null;
78  
79  		Job job = new Job("Delete results") {
80  			@Override
81  			protected IStatus run(IProgressMonitor monitor) {
82  				if (selection != null
83  						&& selection instanceof IStructuredSelection) {
84  					Map<String, Node> nodes = new HashMap<String, Node>();
85  					Iterator<?> it = ((IStructuredSelection) selection)
86  							.iterator();
87  					Object obj = null;
88  					try {
89  
90  						while (it.hasNext()) {
91  							obj = it.next();
92  							if (obj instanceof ResultFolder) {
93  								Node node = ((ResultFolder) obj).getNode();
94  								nodes.put(node.getPath(), node);
95  							} else if (obj instanceof SingleResultNode) {
96  								Node node = ((SingleResultNode) obj).getNode();
97  								nodes.put(node.getPath(), node);
98  							}
99  						}
100 						if (!nodes.isEmpty()) {
101 							Session session = null;
102 							monitor.beginTask("Delete results", nodes.size());
103 							for (String path : nodes.keySet()) {
104 								if (session == null)
105 									session = nodes.get(path).getSession();
106 
107 								// check if the item has not already been
108 								// deleted while deleting one of its ancestor
109 								if (session.itemExists(path)) {
110 									Node parent = nodes.get(path).getParent();
111 									nodes.get(path).remove();
112 									ResultParentUtils.updatePassedStatus(
113 											parent, true);
114 								}
115 								monitor.worked(1);
116 							}
117 							session.save();
118 						}
119 
120 					} catch (RepositoryException e) {
121 						throw new SlcException(
122 								"Unexpected error while deleteting node(s)", e);
123 					}
124 					monitor.done();
125 				}
126 				return Status.OK_STATUS;
127 			}
128 
129 		};
130 		job.setUser(true);
131 		job.schedule();
132 		return null;
133 	}
134 }