]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DeleteGroups.java
16128b8a4af10766bc4e60b182337a8f1ff29e2c
[lgpl/argeo-commons.git] / DeleteGroups.java
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.cms.ui.workbench.internal.useradmin.commands;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
23 import org.argeo.cms.ui.workbench.internal.useradmin.UserAdminWrapper;
24 import org.argeo.cms.ui.workbench.internal.useradmin.parts.UserEditorInput;
25 import org.argeo.cms.util.useradmin.UserAdminUtils;
26 import org.eclipse.core.commands.AbstractHandler;
27 import org.eclipse.core.commands.ExecutionEvent;
28 import org.eclipse.core.commands.ExecutionException;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.handlers.HandlerUtil;
35 import org.osgi.service.useradmin.Group;
36 import org.osgi.service.useradmin.UserAdmin;
37 import org.osgi.service.useradmin.UserAdminEvent;
38
39 /** Delete the selected groups */
40 public class DeleteGroups extends AbstractHandler {
41 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID
42 + ".deleteGroups";
43
44 /* DEPENDENCY INJECTION */
45 private UserAdminWrapper userAdminWrapper;
46
47 @SuppressWarnings("unchecked")
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49 ISelection selection = HandlerUtil.getCurrentSelection(event);
50 if (selection.isEmpty())
51 return null;
52
53 List<Group> groups = new ArrayList<Group>();
54 Iterator<Group> it = ((IStructuredSelection) selection).iterator();
55 StringBuilder builder = new StringBuilder();
56 while (it.hasNext()) {
57 Group currGroup = it.next();
58 String groupName = UserAdminUtils.getUsername(currGroup);
59 // TODO add checks
60 builder.append(groupName).append("; ");
61 groups.add(currGroup);
62 }
63
64 if (!MessageDialog.openQuestion(HandlerUtil.getActiveShell(event),
65 "Delete Groups",
66 "Are you sure that you " + "want to delete these groups?\n"
67 + builder.substring(0, builder.length() - 2)))
68 return null;
69
70 userAdminWrapper.beginTransactionIfNeeded();
71 UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
72 IWorkbenchPage iwp = HandlerUtil.getActiveWorkbenchWindow(event)
73 .getActivePage();
74 for (Group group : groups) {
75 String groupName = group.getName();
76 // TODO find a way to close the editor cleanly if opened. Cannot be
77 // done through the UserAdminListeners, it causes a
78 // java.util.ConcurrentModificationException because disposing the
79 // editor unregisters and disposes the listener
80 IEditorPart part = iwp.findEditor(new UserEditorInput(groupName));
81 if (part != null)
82 iwp.closeEditor(part, false);
83 userAdmin.removeRole(groupName);
84 }
85 userAdminWrapper.commitOrNotifyTransactionStateChange();
86
87 // Update the view
88 for (Group group : groups) {
89 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
90 UserAdminEvent.ROLE_REMOVED, group));
91 }
92
93 return null;
94 }
95
96 /* DEPENDENCY INJECTION */
97 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
98 this.userAdminWrapper = userAdminWrapper;
99 }
100 }