Always commit user transactions on save when modifying the security model via the UI
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / UserAdminWrapper.java
index 8306e155234aedd1f6abc9c1643e40117922b65e..d443a3d47ce3c1a4b4e3c8109d7cc02334585796 100644 (file)
@@ -10,24 +10,27 @@ import org.argeo.cms.CmsException;
 import org.osgi.service.useradmin.UserAdminEvent;
 import org.osgi.service.useradmin.UserAdminListener;
 
-/** Centralize interaction with the UserAdmin in this bundle */
+/** Centralise interaction with the UserAdmin in this bundle */
 public class UserAdminWrapper extends
                org.argeo.cms.util.useradmin.UserAdminWrapper {
-       // private Log log = LogFactory.getLog(UserAdminWrapper.class);
+
+       // First effort to simplify UX while managing users and groups
+       public final static boolean COMMIT_ON_SAVE = true;
 
        // Registered listeners
        List<UserAdminListener> listeners = new ArrayList<UserAdminListener>();
 
        /**
-        * Overwrite the normal begin transaction behaviour to also notify the UI.
-        * Must be called from the UI Thread.
+        * Starts a transaction if necessary. Should always been called together
+        * with {@link UserAdminWrapper#commitOrNotifyTransactionStateChange()} once
+        * the security model changes have been performed.
         */
        public UserTransaction beginTransactionIfNeeded() {
                try {
                        UserTransaction userTransaction = getUserTransaction();
                        if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
                                userTransaction.begin();
-                               UiAdminUtils.notifyTransactionStateChange(userTransaction);
+                               // UiAdminUtils.notifyTransactionStateChange(userTransaction);
                        }
                        return userTransaction;
                } catch (Exception e) {
@@ -35,13 +38,33 @@ public class UserAdminWrapper extends
                }
        }
 
+       /**
+        * Depending on the current application configuration, it will either commit
+        * the current transaction or throw a notification that the transaction
+        * state has changed (In the later case, it must be called from the UI
+        * thread).
+        */
+       public void commitOrNotifyTransactionStateChange() {
+               try {
+                       UserTransaction userTransaction = getUserTransaction();
+                       if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
+                               return;
+
+                       if (UserAdminWrapper.COMMIT_ON_SAVE)
+                               userTransaction.commit();
+                       else
+                               UiAdminUtils.notifyTransactionStateChange(userTransaction);
+               } catch (Exception e) {
+                       throw new CmsException("Unable to clean transaction", e);
+               }
+       }
+
        // TODO implement safer mechanism
        public void addListener(UserAdminListener userAdminListener) {
                if (!listeners.contains(userAdminListener))
                        listeners.add(userAdminListener);
        }
 
-       // Expose this?
        public void removeListener(UserAdminListener userAdminListener) {
                if (listeners.contains(userAdminListener))
                        listeners.remove(userAdminListener);