]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/UserTransactionHandler.java
Re-add org.argeo.cms.util.useradmin
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / commands / UserTransactionHandler.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.security.ui.admin.internal.commands;
17
18 import javax.transaction.Status;
19 import javax.transaction.UserTransaction;
20
21 import org.argeo.ArgeoException;
22 import org.argeo.security.ui.admin.SecurityAdminPlugin;
23 import org.argeo.security.ui.admin.internal.UiAdminUtils;
24 import org.argeo.security.ui.admin.internal.UserAdminWrapper;
25 import org.eclipse.core.commands.AbstractHandler;
26 import org.eclipse.core.commands.ExecutionEvent;
27 import org.eclipse.core.commands.ExecutionException;
28 import org.eclipse.ui.handlers.HandlerUtil;
29 import org.osgi.service.useradmin.UserAdminEvent;
30
31 /** Manage the transaction that is bound to the current perspective */
32 public class UserTransactionHandler extends AbstractHandler {
33 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
34 + ".userTransactionHandler";
35
36 public final static String PARAM_COMMAND_ID = "param.commandId";
37
38 public final static String TRANSACTION_BEGIN = "transaction.begin";
39 public final static String TRANSACTION_COMMIT = "transaction.commit";
40 public final static String TRANSACTION_ROLLBACK = "transaction.rollback";
41
42 /* DEPENDENCY INJECTION */
43 private UserAdminWrapper userAdminWrapper;
44
45 public Object execute(ExecutionEvent event) throws ExecutionException {
46 String commandId = event.getParameter(PARAM_COMMAND_ID);
47 final UserTransaction userTransaction = userAdminWrapper
48 .getUserTransaction();
49 try {
50 if (TRANSACTION_BEGIN.equals(commandId)) {
51 if (userTransaction.getStatus() != Status.STATUS_NO_TRANSACTION)
52 throw new ArgeoException("A transaction already exists");
53 else
54 userTransaction.begin();
55 } else if (TRANSACTION_COMMIT.equals(commandId)) {
56 if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
57 throw new ArgeoException("No transaction.");
58 else
59 userTransaction.commit();
60 } else if (TRANSACTION_ROLLBACK.equals(commandId)) {
61 if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
62 throw new ArgeoException("No transaction to rollback.");
63 else {
64 userTransaction.rollback();
65 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
66 UserAdminEvent.ROLE_CHANGED, null));
67 }
68 }
69
70 // Try to remove invalid thread access errors when managing users.
71 HandlerUtil.getActivePart(event).getSite().getShell().getDisplay()
72 .asyncExec(new Runnable() {
73 @Override
74 public void run() {
75 UiAdminUtils
76 .notifyTransactionStateChange(userTransaction);
77 }
78 });
79
80 } catch (ArgeoException e) {
81 throw e;
82 } catch (Exception e) {
83 throw new ArgeoException("Unable to call " + commandId + " on "
84 + userTransaction, e);
85 }
86 return null;
87 }
88
89 /* DEPENDENCY INJECTION */
90 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
91 this.userAdminWrapper = userAdminWrapper;
92 }
93 }