]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/UserTransactionHandler.java
158fae24de987e46b6cd827ae4321f66aed4bfc9
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / 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.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.eclipse.core.commands.AbstractHandler;
24 import org.eclipse.core.commands.ExecutionEvent;
25 import org.eclipse.core.commands.ExecutionException;
26
27 /** Manage the transaction that is bound to the current perspective */
28 public class UserTransactionHandler extends AbstractHandler {
29 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
30 + ".userTransactionHandler";
31
32 public final static String PARAM_COMMAND_ID = "param.commandId";
33
34 public final static String TRANSACTION_BEGIN = "transaction.begin";
35 public final static String TRANSACTION_COMMIT = "transaction.commit";
36 public final static String TRANSACTION_ROLLBACK = "transaction.rollback";
37
38 private UserTransaction userTransaction;
39
40 public Object execute(ExecutionEvent event) throws ExecutionException {
41
42 String commandId = event.getParameter(PARAM_COMMAND_ID);
43 try {
44 if (TRANSACTION_BEGIN.equals(commandId)) {
45 if (userTransaction.getStatus() != Status.STATUS_NO_TRANSACTION)
46 throw new ArgeoException("A transaction already exists");
47 else
48 userTransaction.begin();
49 } else if (TRANSACTION_COMMIT.equals(commandId)) {
50 if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
51 throw new ArgeoException("No transaction.");
52 else
53 userTransaction.commit();
54 } else if (TRANSACTION_ROLLBACK.equals(commandId)) {
55 if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
56 throw new ArgeoException("No transaction to rollback.");
57 else
58 userTransaction.rollback();
59 }
60 } catch (ArgeoException e) {
61 throw e;
62 } catch (Exception e) {
63 throw new ArgeoException("Unable to call " + commandId + " on "
64 + userTransaction, e);
65 }
66 //
67 // IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
68 // if (iww == null)
69 // return null;
70 // IWorkbenchPage activePage = iww.getActivePage();
71 // IWorkbenchPart part = activePage.getActivePart();
72 // if (part instanceof UsersView)
73 // ((UsersView) part).refresh();
74 // else if (part instanceof GroupsView)
75 // ((GroupsView) part).refresh();
76 return null;
77 }
78
79 public void setUserTransaction(UserTransaction userTransaction) {
80 this.userTransaction = userTransaction;
81 }
82 }