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.cms.e4.users.handlers;
17  
18  import java.util.List;
19  
20  import javax.inject.Inject;
21  import javax.inject.Named;
22  
23  import org.argeo.cms.auth.UserAdminUtils;
24  import org.argeo.cms.e4.users.GroupsView;
25  import org.argeo.cms.e4.users.UserAdminWrapper;
26  import org.eclipse.e4.core.di.annotations.CanExecute;
27  import org.eclipse.e4.core.di.annotations.Execute;
28  import org.eclipse.e4.ui.model.application.ui.basic.MPart;
29  import org.eclipse.e4.ui.services.IServiceConstants;
30  import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
31  import org.eclipse.jface.dialogs.MessageDialog;
32  import org.eclipse.swt.widgets.Display;
33  import org.osgi.service.useradmin.Group;
34  import org.osgi.service.useradmin.UserAdmin;
35  import org.osgi.service.useradmin.UserAdminEvent;
36  
37  /** Delete the selected groups */
38  public class DeleteGroups {
39  	// public final static String ID = WorkbenchUiPlugin.PLUGIN_ID +
40  	// ".deleteGroups";
41  
42  	/* DEPENDENCY INJECTION */
43  	@Inject
44  	private UserAdminWrapper userAdminWrapper;
45  
46  	@Inject
47  	ESelectionService selectionService;
48  
49  	@SuppressWarnings("unchecked")
50  	@Execute
51  	public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
52  		// ISelection selection = null;// HandlerUtil.getCurrentSelection(event);
53  		// if (selection.isEmpty())
54  		// return null;
55  		//
56  		// List<Group> groups = new ArrayList<Group>();
57  		// Iterator<Group> it = ((IStructuredSelection) selection).iterator();
58  
59  		List<Group> selection = (List<Group>) selectionService.getSelection();
60  		if (selection == null)
61  			return;
62  
63  		StringBuilder builder = new StringBuilder();
64  		for (Group group : selection) {
65  			Group currGroup = group;
66  			String groupName = UserAdminUtils.getUserLocalId(currGroup.getName());
67  			// TODO add checks
68  			builder.append(groupName).append("; ");
69  			// groups.add(currGroup);
70  		}
71  
72  		if (!MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), "Delete Groups", "Are you sure that you "
73  				+ "want to delete these groups?\n" + builder.substring(0, builder.length() - 2)))
74  			return;
75  
76  		userAdminWrapper.beginTransactionIfNeeded();
77  		UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
78  		// IWorkbenchPage iwp =
79  		// HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
80  		for (Group group : selection) {
81  			String groupName = group.getName();
82  			// TODO find a way to close the editor cleanly if opened. Cannot be
83  			// done through the UserAdminListeners, it causes a
84  			// java.util.ConcurrentModificationException because disposing the
85  			// editor unregisters and disposes the listener
86  			// IEditorPart part = iwp.findEditor(new UserEditorInput(groupName));
87  			// if (part != null)
88  			// iwp.closeEditor(part, false);
89  			userAdmin.removeRole(groupName);
90  		}
91  		userAdminWrapper.commitOrNotifyTransactionStateChange();
92  
93  		// Update the view
94  		for (Group group : selection) {
95  			userAdminWrapper.notifyListeners(new UserAdminEvent(null, UserAdminEvent.ROLE_REMOVED, group));
96  		}
97  
98  		// return null;
99  	}
100 
101 	@CanExecute
102 	public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
103 		return part.getObject() instanceof GroupsView && selectionService.getSelection() != null;
104 	}
105 
106 	/* DEPENDENCY INJECTION */
107 	// public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
108 	// this.userAdminWrapper = userAdminWrapper;
109 	// }
110 }