You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by tv...@apache.org on 2006/10/08 21:54:08 UTC

svn commit: r454198 [1/2] - in /jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque: basic/ dynamic/ om/ turbine/

Author: tv
Date: Sun Oct  8 12:54:07 2006
New Revision: 454198

URL: http://svn.apache.org/viewvc?view=rev&rev=454198
Log:
Refactored stuff for better code re-use

Modified:
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicGroupManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicUserManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicGroupManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicPermissionManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicRoleManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicUserManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/om/   (props changed)
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineGroupManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbinePermissionManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineRoleManagerImpl.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineUserManagerImpl.java

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicGroupManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicGroupManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicGroupManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicGroupManagerImpl.java Sun Oct  8 12:54:07 2006
@@ -15,7 +15,6 @@
  *  limitations under the License.
  */
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -23,306 +22,29 @@
 import org.apache.fulcrum.security.entity.Group;
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.basic.entity.BasicGroup;
-import org.apache.fulcrum.security.spi.AbstractGroupManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractGroupManager;
 import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroupPeer;
-import org.apache.fulcrum.security.torque.om.TorqueGroup;
-import org.apache.fulcrum.security.torque.om.TorqueGroupPeer;
 import org.apache.fulcrum.security.torque.om.TorqueUser;
 import org.apache.fulcrum.security.torque.om.TorqueUserPeer;
 import org.apache.fulcrum.security.util.DataBackendException;
-import org.apache.fulcrum.security.util.EntityExistsException;
-import org.apache.fulcrum.security.util.GroupSet;
-import org.apache.fulcrum.security.util.UnknownEntityException;
 import org.apache.fulcrum.security.util.UserSet;
-import org.apache.torque.NoRowsException;
 import org.apache.torque.TorqueException;
-import org.apache.torque.om.SimpleKey;
 import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Transaction;
 /**
  * This implementation persists to a database via Torque.
  *
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  * @version $Id:$
  */
-public class TorqueBasicGroupManagerImpl extends AbstractGroupManager
+public class TorqueBasicGroupManagerImpl extends TorqueAbstractGroupManager
 {
     /**
-     * Retrieve a Group object with specified name.
-     *
-     * @param name the name of the Group.
-     * @return an object representing the Group with specified name.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     * @throws UnknownEntityException if the group does not exist.
-     */
-    public Group getGroupByName(String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        Group group = getGroupInstance();
-        List groups = Collections.EMPTY_LIST;
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
-            
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueGroupPeer.GROUP_NAME, name);
-
-            groups = TorqueGroupPeer.doSelect(criteria, con);
-
-            if (groups.size() == 1)
-            {
-                TorqueGroup g = (TorqueGroup) groups.get(0);
-                
-                group.setId(g.getId());
-                group.setName(g.getName());
-                
-                // Add users if they exist
-                ((BasicGroup)group).setUsers(getUsersForGroup(group, con));
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving group information", e);
-        }
-
-        if (groups.size() == 0)
-        {
-            throw new UnknownEntityException("Could not find group" + name);
-        }
-
-        if (groups.size() > 1)
-        {
-            throw new DataBackendException("Multiple Groups with same name '" + name + "'");
-        }
-
-        return group;
-    }
-
-    /**
-     * Retrieves all groups defined in the system.
-     *
-     * @return the names of all groups defined in the system.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     */
-    public GroupSet getAllGroups() throws DataBackendException
-    {
-        GroupSet groupSet = new GroupSet();
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
-
-            List groups = TorqueGroupPeer.doSelect(new Criteria(), con);
-            
-            for (Iterator i = groups.iterator(); i.hasNext();)
-            {
-                Group group = getGroupInstance();
-                TorqueGroup g = (TorqueGroup)i.next();
-                group.setId(g.getId());
-                group.setName(g.getName());
-                
-                // Add users if they exist
-                ((BasicGroup)group).setUsers(getUsersForGroup(group, con));
-
-                groupSet.add(group);
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving group information", e);
-        }
-
-        return groupSet;
-    }
-
-    /**
-        * Removes a Group from the system.
-        *
-        * @param group The object describing the group to be removed.
-        * @throws DataBackendException if there was an error accessing the data
-        *         backend.
-        * @throws UnknownEntityException if the group does not exist.
-        */
-    public synchronized void removeGroup(Group group)
-        throws DataBackendException, UnknownEntityException
-    {
-        try
-        {
-            TorqueGroupPeer.doDelete(SimpleKey.keyFor((Integer)group.getId()));
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Removing Group '" + group + "' failed", e);
-        }
-    }
-
-    /**
-        * Renames an existing Group.
-        *
-        * @param group The object describing the group to be renamed.
-        * @param name the new name for the group.
-        * @throws DataBackendException if there was an error accessing the data
-        *         backend.
-        * @throws UnknownEntityException if the group does not exist.
-        */
-    public synchronized void renameGroup(Group group, String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(group))
-        {
-            group.setName(name);
-
-            try
-            {
-                TorqueGroup g = new TorqueGroup();
-                g.setId((Integer)group.getId());
-                g.setName(name);
-                g.setNew(false);
-                g.save();
-            }
-            catch (Exception e)
-            {
-                throw new DataBackendException("Renaming Group '" + group + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown group '" + group + "'");
-        }
-    }
-
-    /**
-     * Determines if the <code>Group</code> exists in the security system.
-     *
-     * @param groupName a <code>Group</code> value
-     * @return true if the group name exists in the system, false otherwise
-     * @throws DataBackendException when more than one Group with
-     *         the same name exists.
-     */
-    public boolean checkExists(String groupName) throws DataBackendException
-    {
-        List groups;
-
-        try
-        {
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueGroupPeer.GROUP_NAME, groupName);
-
-            groups = TorqueGroupPeer.doSelect(criteria);
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Error retrieving group information", e);
-        }
-
-        if (groups.size() > 1)
-        {
-            throw new DataBackendException(
-                    "Multiple groups with same name '" + groupName + "'");
-        }
-
-        return (groups.size() == 1);
-    }
-
-    /**
-    * Creates a new group with specified attributes.
-    *
-    * @param group the object describing the group to be created.
-    * @return a new Group object that has id set up properly.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws EntityExistsException if the group already exists.
-    */
-    protected synchronized Group persistNewGroup(Group group)
-        throws DataBackendException
-    {
-        try
-        {
-            TorqueGroup g = new TorqueGroup();
-            g.setName(group.getName());
-            g.save();
-            
-            group.setId(g.getId());
-        }
-        catch (Exception e)
-        {
-            throw new DataBackendException("Adding Group '" + group + "' failed", e);
-        }
-        
-        return group;
-    }
-
-    /**
-     * Retrieve a Group object with specified id.
-     *
-     * @param id
-     *            the id of the Group.
-     * @return an object representing the Group with specified id.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the group does not exist.
-     */
-    public Group getGroupById(Object id)
-        throws DataBackendException, UnknownEntityException
-    {
-        Group group = getGroupInstance();
-
-        if (id != null && id instanceof Integer)
-        {
-            Connection con = null;
-
-            try
-            {
-                con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
-                
-                TorqueGroup g = TorqueGroupPeer.retrieveByPK((Integer)id, con);
-                
-                group.setId(g.getId());
-                group.setName(g.getName());
-                
-                // Add users if they exist
-                ((BasicGroup)group).setUsers(getUsersForGroup(group, con));
-
-                Transaction.commit(con);
-            }
-            catch (NoRowsException e)
-            {
-                Transaction.safeRollback(con);
-                throw new UnknownEntityException("Group with id '" + id + "' does not exist.", e);
-            }
-            catch (TorqueException e)
-            {
-                Transaction.safeRollback(con);
-                throw new DataBackendException("Error retrieving group information", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Invalid group id '" + group.getId() + "'");
-        }
-
-        return group;
-    }
-
-    /**
      * Provides the users for the given group
      *  
      * @param group the group for which the users should be retrieved  
      * @param con a database connection
      */
-    private UserSet getUsersForGroup(Group group, Connection con)
+    protected void attachObjectsForGroup(Group group, Connection con)
         throws TorqueException, DataBackendException
     {
         UserSet userSet = new UserSet();
@@ -345,6 +67,6 @@
             userSet.add(user);
         }
         
-        return userSet;
+        ((BasicGroup)group).setUsers(userSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicUserManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicUserManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicUserManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/basic/TorqueBasicUserManagerImpl.java Sun Oct  8 12:54:07 2006
@@ -1,6 +1,6 @@
 package org.apache.fulcrum.security.torque.basic;
 /*
- *  Copyright 2001-2004 The Apache Software Foundation
+ *  Copyright 2001-2006 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
  *  limitations under the License.
  */
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -23,310 +22,29 @@
 import org.apache.fulcrum.security.entity.Group;
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.basic.entity.BasicUser;
-import org.apache.fulcrum.security.spi.AbstractUserManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractUserManager;
 import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroupPeer;
 import org.apache.fulcrum.security.torque.om.TorqueGroup;
 import org.apache.fulcrum.security.torque.om.TorqueGroupPeer;
-import org.apache.fulcrum.security.torque.om.TorqueUser;
-import org.apache.fulcrum.security.torque.om.TorqueUserPeer;
 import org.apache.fulcrum.security.util.DataBackendException;
-import org.apache.fulcrum.security.util.EntityExistsException;
 import org.apache.fulcrum.security.util.GroupSet;
-import org.apache.fulcrum.security.util.UnknownEntityException;
-import org.apache.fulcrum.security.util.UserSet;
-import org.apache.torque.NoRowsException;
 import org.apache.torque.TorqueException;
-import org.apache.torque.om.SimpleKey;
 import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Transaction;
 /**
  * This implementation persists to a database via Torque.
  *
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  * @version $Id:$
  */
-public class TorqueBasicUserManagerImpl extends AbstractUserManager
+public class TorqueBasicUserManagerImpl extends TorqueAbstractUserManager
 {
     /**
-     * Check whether a specified user's account exists.
-     *
-     * The login name is used for looking up the account.
-     *
-     * @param userName The name of the user to be checked.
-     * @return true if the specified account exists
-     * @throws DataBackendException if there was an error accessing
-     *         the data backend.
-     */
-    public boolean checkExists(String userName) throws DataBackendException
-    {
-        List users;
-
-        try
-        {
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueUserPeer.LOGIN_NAME, userName.toLowerCase());
-
-            users = TorqueUserPeer.doSelect(criteria);
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Error retrieving user information", e);
-        }
-
-        if (users.size() > 1)
-        {
-            throw new DataBackendException("Multiple Users with same username '" + userName + "'");
-        }
-        
-        return (users.size() == 1);
-    }
-
-    /**
-     * Retrieve a user from persistent storage using username as the
-     * key.
-     *
-     * @param userName the name of the user.
-     * @return an User object.
-     * @exception UnknownEntityException if the user's account does not
-     *            exist in the database.
-     * @exception DataBackendException if there is a problem accessing the
-     *            storage.
-     */
-    public User getUser(String userName) throws UnknownEntityException, DataBackendException
-    {
-        User user = getUserInstance();
-        List users = Collections.EMPTY_LIST;
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
-            
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueUserPeer.LOGIN_NAME, userName.toLowerCase());
-
-            users = TorqueUserPeer.doSelect(criteria, con);
-
-            if (users.size() == 1)
-            {
-                TorqueUser u = (TorqueUser) users.get(0);
-                
-                user.setId(u.getId());
-                user.setName(u.getName());
-                user.setPassword(u.getPassword());
-                
-                // Add groups if they exist
-                ((BasicUser)user).setGroups(getGroupsForUser(user, con));
-            }
-            
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving user information", e);
-        }
-
-        if (users.size() == 0)
-        {
-            throw new UnknownEntityException("Unknown user '" + userName + "'");
-        }
-
-        if (users.size() > 1)
-        {
-            throw new DataBackendException("Multiple Users with same username '" + userName + "'");
-        }
-
-        return user;
-    }
-
-    /**
-       * Retrieves all users defined in the system.
-       *
-       * @return the names of all users defined in the system.
-       * @throws DataBackendException if there was an error accessing the data
-       *         backend.
-       */
-    public UserSet getAllUsers() throws DataBackendException
-    {
-        UserSet userSet = new UserSet();
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
-            
-            List users = TorqueUserPeer.doSelect(new Criteria(), con);
-
-            for (Iterator i = users.iterator(); i.hasNext();)
-            {
-                User user = getUserInstance();
-                TorqueUser u = (TorqueUser)i.next();
-                user.setId(u.getId());
-                user.setName(u.getName());
-                user.setPassword(u.getPassword());
-
-                // Add groups if they exist
-                ((BasicUser)user).setGroups(getGroupsForUser(user, con));
-                
-                userSet.add(user);
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving all users", e);
-        }
-        
-        return userSet;
-    }
-
-    /**
-    * Removes an user account from the system.
-    *
-    * @param user the object describing the account to be removed.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws UnknownEntityException if the user account is not present.
-    */
-    public synchronized void removeUser(User user) throws DataBackendException, UnknownEntityException
-    {
-        try
-        {
-            TorqueUserPeer.doDelete(SimpleKey.keyFor((Integer)user.getId()));
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Removing User '" + user + "' failed", e);
-        }
-    }
-
-    /**
-       * Creates new user account with specified attributes.
-       *
-       * @param user the object describing account to be created.
-       * @param password The password to use for the account.
-       *
-       * @throws DataBackendException if there was an error accessing the
-       *         data backend.
-       * @throws EntityExistsException if the user account already exists.
-       */
-    protected synchronized User persistNewUser(User user) throws DataBackendException
-    {
-        try
-        {
-            TorqueUser u = new TorqueUser();
-            
-            u.setName(user.getName());
-            u.setPassword(user.getPassword());
-            u.save();
-
-            user.setId(u.getId());
-        }
-        catch (Exception e)
-        {
-            throw new DataBackendException("Adding User '" + user + "' failed", e);
-        }
-        
-        return user;
-    }
-
-    /**
-       * Stores User attributes. The User is required to exist in the system.
-       *
-       * @param role The User to be stored.
-       * @throws DataBackendException if there was an error accessing the data
-       *         backend.
-       * @throws UnknownEntityException if the role does not exist.
-       */
-    public synchronized void saveUser(User user) throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(user))
-        {
-            try
-            {
-                TorqueUser u = new TorqueUser();
-                
-                u.setId((Integer)user.getId());
-                u.setName(user.getName());
-                u.setPassword(user.getPassword());
-                u.setNew(false);
-                u.save();
-            }
-            catch (Exception e)
-            {
-                throw new DataBackendException("Saving User '" + user + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown user '" + user + "'");
-        }
-    }
-
-    /**
-     * Retrieve a User object with specified id.
-     *
-     * @param id
-     *            the id of the User.
-     * @return an object representing the User with specified id.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the user does not exist.
-     */
-    public User getUserById(Object id)
-        throws DataBackendException, UnknownEntityException
-    {
-        User user = getUserInstance();
-
-        if (id != null && id instanceof Integer)
-        {
-            Connection con = null;
-            
-            try
-            {
-                con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
-                
-                TorqueUser u = TorqueUserPeer.retrieveByPK((Integer)id, con);
-
-                user.setId(u.getId());
-                user.setName(u.getName());
-                user.setPassword(u.getPassword());
-
-                // Add groups if they exist
-                ((BasicUser)user).setGroups(getGroupsForUser(user, con));
-                
-                Transaction.commit(con);
-            }
-            catch (NoRowsException e)
-            {
-                Transaction.safeRollback(con);
-                throw new UnknownEntityException("User with id '" + id + "' does not exist.", e);
-            }
-            catch (TorqueException e)
-            {
-                Transaction.safeRollback(con);
-                throw new DataBackendException("Error retrieving user information", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Invalid user id '" + user.getId() + "'");
-        }
-
-        return user;
-    }
-    
-    /**
      * Provides the groups for the given user
      *  
      * @param user the user for which the groups should be retrieved  
      * @param con a database connection
      */
-    private GroupSet getGroupsForUser(User user, Connection con)
+    protected void attachObjectsForUser(User user, Connection con)
         throws TorqueException, DataBackendException
     {
         GroupSet groupSet = new GroupSet();
@@ -348,6 +66,6 @@
             groupSet.add(group);
         }
         
-        return groupSet;
+        ((BasicUser)user).setGroups(groupSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicGroupManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicGroupManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicGroupManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicGroupManagerImpl.java Sun Oct  8 12:54:07 2006
@@ -15,7 +15,6 @@
  *  limitations under the License.
  */
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -25,319 +24,33 @@
 import org.apache.fulcrum.security.entity.Role;
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup;
-import org.apache.fulcrum.security.spi.AbstractGroupManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractGroupManager;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicGroupRolePeer;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicUserGroupPeer;
-import org.apache.fulcrum.security.torque.om.TorqueGroup;
-import org.apache.fulcrum.security.torque.om.TorqueGroupPeer;
 import org.apache.fulcrum.security.torque.om.TorqueRole;
 import org.apache.fulcrum.security.torque.om.TorqueRolePeer;
 import org.apache.fulcrum.security.torque.om.TorqueUser;
 import org.apache.fulcrum.security.torque.om.TorqueUserPeer;
 import org.apache.fulcrum.security.util.DataBackendException;
-import org.apache.fulcrum.security.util.EntityExistsException;
-import org.apache.fulcrum.security.util.GroupSet;
 import org.apache.fulcrum.security.util.RoleSet;
-import org.apache.fulcrum.security.util.UnknownEntityException;
 import org.apache.fulcrum.security.util.UserSet;
-import org.apache.torque.NoRowsException;
 import org.apache.torque.TorqueException;
-import org.apache.torque.om.SimpleKey;
 import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Transaction;
 /**
  * This implementation persists to a database via Torque.
  *
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  * @version $Id:$
  */
-public class TorqueDynamicGroupManagerImpl extends AbstractGroupManager
+public class TorqueDynamicGroupManagerImpl extends TorqueAbstractGroupManager
 {
     /**
-     * Retrieve a Group object with specified name.
-     *
-     * @param name the name of the Group.
-     * @return an object representing the Group with specified name.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     * @throws UnknownEntityException if the group does not exist.
-     */
-    public Group getGroupByName(String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        Group group = getGroupInstance();
-        List groups = Collections.EMPTY_LIST;
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
-            
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueGroupPeer.GROUP_NAME, name);
-
-            groups = TorqueGroupPeer.doSelect(criteria, con);
-
-            if (groups.size() == 1)
-            {
-                TorqueGroup g = (TorqueGroup) groups.get(0);
-                
-                group.setId(g.getId());
-                group.setName(g.getName());
-                
-                // Add users if they exist
-                ((DynamicGroup)group).setUsers(getUsersForGroup(group, con));
-
-                // Add roles if they exist
-                ((DynamicGroup)group).setRoles(getRolesForGroup(group, con));
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving group information", e);
-        }
-
-        if (groups.size() == 0)
-        {
-            throw new UnknownEntityException("Could not find group" + name);
-        }
-
-        if (groups.size() > 1)
-        {
-            throw new DataBackendException("Multiple Groups with same name '" + name + "'");
-        }
-
-        return group;
-    }
-
-    /**
-     * Retrieves all groups defined in the system.
-     *
-     * @return the names of all groups defined in the system.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     */
-    public GroupSet getAllGroups() throws DataBackendException
-    {
-        GroupSet groupSet = new GroupSet();
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
-
-            List groups = TorqueGroupPeer.doSelect(new Criteria(), con);
-            
-            for (Iterator i = groups.iterator(); i.hasNext();)
-            {
-                Group group = getGroupInstance();
-                TorqueGroup g = (TorqueGroup)i.next();
-                group.setId(g.getId());
-                group.setName(g.getName());
-                
-                // Add users if they exist
-                ((DynamicGroup)group).setUsers(getUsersForGroup(group, con));
-
-                // Add roles if they exist
-                ((DynamicGroup)group).setRoles(getRolesForGroup(group, con));
-
-                groupSet.add(group);
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving group information", e);
-        }
-
-        return groupSet;
-    }
-
-    /**
-        * Removes a Group from the system.
-        *
-        * @param group The object describing the group to be removed.
-        * @throws DataBackendException if there was an error accessing the data
-        *         backend.
-        * @throws UnknownEntityException if the group does not exist.
-        */
-    public synchronized void removeGroup(Group group)
-        throws DataBackendException, UnknownEntityException
-    {
-        try
-        {
-            TorqueGroupPeer.doDelete(SimpleKey.keyFor((Integer)group.getId()));
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Removing Group '" + group + "' failed", e);
-        }
-    }
-
-    /**
-        * Renames an existing Group.
-        *
-        * @param group The object describing the group to be renamed.
-        * @param name the new name for the group.
-        * @throws DataBackendException if there was an error accessing the data
-        *         backend.
-        * @throws UnknownEntityException if the group does not exist.
-        */
-    public synchronized void renameGroup(Group group, String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(group))
-        {
-            group.setName(name);
-
-            try
-            {
-                TorqueGroup g = new TorqueGroup();
-                g.setId((Integer)group.getId());
-                g.setName(name);
-                g.setNew(false);
-                g.save();
-            }
-            catch (Exception e)
-            {
-                throw new DataBackendException("Renaming Group '" + group + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown group '" + group + "'");
-        }
-    }
-
-    /**
-     * Determines if the <code>Group</code> exists in the security system.
-     *
-     * @param groupName a <code>Group</code> value
-     * @return true if the group name exists in the system, false otherwise
-     * @throws DataBackendException when more than one Group with
-     *         the same name exists.
-     */
-    public boolean checkExists(String groupName) throws DataBackendException
-    {
-        List groups;
-
-        try
-        {
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueGroupPeer.GROUP_NAME, groupName);
-
-            groups = TorqueGroupPeer.doSelect(criteria);
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Error retrieving group information", e);
-        }
-
-        if (groups.size() > 1)
-        {
-            throw new DataBackendException(
-                    "Multiple groups with same name '" + groupName + "'");
-        }
-
-        return (groups.size() == 1);
-    }
-
-    /**
-    * Creates a new group with specified attributes.
-    *
-    * @param group the object describing the group to be created.
-    * @return a new Group object that has id set up properly.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws EntityExistsException if the group already exists.
-    */
-    protected synchronized Group persistNewGroup(Group group)
-        throws DataBackendException
-    {
-        try
-        {
-            TorqueGroup g = new TorqueGroup();
-            g.setName(group.getName());
-            g.save();
-            
-            group.setId(g.getId());
-        }
-        catch (Exception e)
-        {
-            throw new DataBackendException("Adding Group '" + group + "' failed", e);
-        }
-        
-        return group;
-    }
-
-    /**
-     * Retrieve a Group object with specified id.
-     *
-     * @param id
-     *            the id of the Group.
-     * @return an object representing the Group with specified id.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the group does not exist.
-     */
-    public Group getGroupById(Object id)
-        throws DataBackendException, UnknownEntityException
-    {
-        Group group = getGroupInstance();
-
-        if (id != null && id instanceof Integer)
-        {
-            Connection con = null;
-
-            try
-            {
-                con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
-                
-                TorqueGroup g = TorqueGroupPeer.retrieveByPK((Integer)id, con);
-                
-                group.setId(g.getId());
-                group.setName(g.getName());
-                
-                // Add users if they exist
-                ((DynamicGroup)group).setUsers(getUsersForGroup(group, con));
-
-                // Add roles if they exist
-                ((DynamicGroup)group).setRoles(getRolesForGroup(group, con));
-
-                Transaction.commit(con);
-            }
-            catch (NoRowsException e)
-            {
-                Transaction.safeRollback(con);
-                throw new UnknownEntityException("Group with id '" + id + "' does not exist.", e);
-            }
-            catch (TorqueException e)
-            {
-                Transaction.safeRollback(con);
-                throw new DataBackendException("Error retrieving group information", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Invalid group id '" + group.getId() + "'");
-        }
-
-        return group;
-    }
-
-    /**
-     * Provides the users for the given group
+     * Provides the users and roles for the given group
      *  
-     * @param group the group for which the users should be retrieved  
+     * @param group the group for which the users/roles should be retrieved  
      * @param con a database connection
      */
-    private UserSet getUsersForGroup(Group group, Connection con)
+    protected void attachObjectsForGroup(Group group, Connection con)
         throws TorqueException, DataBackendException
     {
         UserSet userSet = new UserSet();
@@ -360,21 +73,11 @@
             userSet.add(user);
         }
         
-        return userSet;
-    }
+        ((DynamicGroup)group).setUsers(userSet);
 
-    /**
-     * Provides the roles for the given group
-     *  
-     * @param group the group for which the roles should be retrieved  
-     * @param con a database connection
-     */
-    private RoleSet getRolesForGroup(Group group, Connection con)
-        throws TorqueException, DataBackendException
-    {
         RoleSet roleSet = new RoleSet();
         
-        Criteria criteria = new Criteria();
+        criteria.clear();
         criteria.addJoin(TorqueDynamicGroupRolePeer.ROLE_ID, TorqueRolePeer.ROLE_ID);
         criteria.add(TorqueDynamicGroupRolePeer.GROUP_ID, (Integer)group.getId());
         
@@ -391,6 +94,6 @@
             roleSet.add(role);
         }
         
-        return roleSet;
+        ((DynamicGroup)group).setRoles(roleSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicPermissionManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicPermissionManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicPermissionManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicPermissionManagerImpl.java Sun Oct  8 12:54:07 2006
@@ -15,7 +15,6 @@
  *  limitations under the License.
  */
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -23,323 +22,29 @@
 import org.apache.fulcrum.security.entity.Permission;
 import org.apache.fulcrum.security.entity.Role;
 import org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission;
-import org.apache.fulcrum.security.spi.AbstractPermissionManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractPermissionManager;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicRolePermissionPeer;
-import org.apache.fulcrum.security.torque.om.TorquePermission;
-import org.apache.fulcrum.security.torque.om.TorquePermissionPeer;
 import org.apache.fulcrum.security.torque.om.TorqueRole;
 import org.apache.fulcrum.security.torque.om.TorqueRolePeer;
 import org.apache.fulcrum.security.util.DataBackendException;
-import org.apache.fulcrum.security.util.EntityExistsException;
-import org.apache.fulcrum.security.util.PermissionSet;
 import org.apache.fulcrum.security.util.RoleSet;
-import org.apache.fulcrum.security.util.UnknownEntityException;
-import org.apache.torque.NoRowsException;
 import org.apache.torque.TorqueException;
-import org.apache.torque.om.SimpleKey;
 import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Transaction;
 /**
  * This implementation persists to a database via Torque.
  *
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  * @version $Id:$
  */
-public class TorqueDynamicPermissionManagerImpl extends AbstractPermissionManager
+public class TorqueDynamicPermissionManagerImpl extends TorqueAbstractPermissionManager
 {
     /**
-     * Retrieves all permissions defined in the system.
-     *
-     * @return the names of all roles defined in the system.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     */
-    public PermissionSet getAllPermissions() throws DataBackendException
-    {
-        PermissionSet permissionSet = new PermissionSet();
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorquePermissionPeer.DATABASE_NAME);
-            
-            List permissions = TorquePermissionPeer.doSelect(new Criteria(), con);
-            
-            for (Iterator i = permissions.iterator(); i.hasNext();)
-            {
-                TorquePermission p = (TorquePermission)i.next();
-
-                // TODO: This throws UnknownEntityException.
-                Permission permission = getPermissionInstance();
-                permission.setId(p.getId());
-                permission.setName(p.getName());
-
-                // Add roles if they exist
-                ((DynamicPermission)permission).setRoles(getRolesForPermission(permission, con));
-
-                permissionSet.add(permission);
-            }
-
-            Transaction.commit(con);
-        }
-        catch (Exception e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving permission information", e);
-        }
-
-        return permissionSet;
-    }
-
-    /**
-     * Renames an existing Permission.
-     *
-     * @param permission
-     *            The object describing the permission to be renamed.
-     * @param name
-     *            the new name for the permission.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the permission does not exist.
-     */
-    public synchronized void renamePermission(Permission permission, String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(permission))
-        {
-            permission.setName(name);
-            
-            try
-            {
-                TorquePermission p = new TorquePermission();
-                p.setId((Integer)permission.getId());
-                p.setName(name);
-                p.setNew(false);
-                p.save();
-            }
-            catch (Exception e)
-            {
-                throw new DataBackendException("Renaming Permission '" + permission + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown permission '" + permission + "'");
-        }
-    }
-
-    /**
-     * Determines if the <code>Permission</code> exists in the security
-     * system.
-     *
-     * @param permissionName
-     *            a <code>Permission</code> value
-     * @return true if the permission name exists in the system, false otherwise
-     * @throws DataBackendException
-     *             when more than one Permission with the same name exists.
-     */
-    public boolean checkExists(String permissionName) throws DataBackendException
-    {
-        List permissions;
-        
-        try
-        {
-            Criteria criteria = new Criteria();
-            criteria.add(TorquePermissionPeer.PERMISSION_NAME, permissionName);
-
-            permissions = TorquePermissionPeer.doSelect(criteria);
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Error retrieving permission information", e);
-        }
-
-        if (permissions.size() > 1)
-        {
-            throw new DataBackendException("Multiple permissions with same name '" + permissionName + "'");
-        }
-
-        return (permissions.size() == 1);
-    }
-
-    /**
-     * Removes a Permission from the system.
-     *
-     * @param permission
-     *            The object describing the permission to be removed.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the permission does not exist.
-     */
-    public synchronized void removePermission(Permission permission)
-        throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(permission))
-        {
-            try
-            {
-                TorquePermissionPeer.doDelete(SimpleKey.keyFor((Integer)permission.getId()));
-            }
-            catch (TorqueException e)
-            {
-                throw new DataBackendException("Removing Permission '" + permission + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown permission '" + permission + "'");
-        }
-    }
-
-    /**
-     * Creates a new permission with specified attributes.
-     *
-     * @param permission
-     *            the object describing the permission to be created.
-     * @return a new Permission object that has id set up properly.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws EntityExistsException
-     *             if the permission already exists.
-     */
-    protected synchronized Permission persistNewPermission(Permission permission)
-        throws DataBackendException
-    {
-        try
-        {
-            TorquePermission p = new TorquePermission();
-            p.setName(permission.getName());
-            p.save();
-            
-            permission.setId(p.getId());
-        }
-        catch (Exception e)
-        {
-            throw new DataBackendException("Adding Permission '" + permission + "' failed", e);
-        }
-
-        return permission;
-    }
-
-    /**
-     * Retrieve a Permission object with specified id.
-     *
-     * @param id
-     *            the id of the Permission.
-     * @return an object representing the Permission with specified id.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the permission does not exist.
-     */
-    public Permission getPermissionById(Object id)
-        throws DataBackendException, UnknownEntityException
-    {
-        Permission permission = getPermissionInstance();
-
-        if (id != null && id instanceof Integer)
-        {
-            Connection con = null;
-
-            try
-            {
-                con = Transaction.begin(TorquePermissionPeer.DATABASE_NAME);
-                
-                TorquePermission p = 
-                    TorquePermissionPeer.retrieveByPK((Integer)id, con);
-                permission.setId(p.getId());
-                permission.setName(p.getName());
-
-                // Add roles if they exist
-                ((DynamicPermission)permission).setRoles(getRolesForPermission(permission, con));
-
-                Transaction.commit(con);
-            }
-            catch (NoRowsException e)
-            {
-                Transaction.safeRollback(con);
-                throw new UnknownEntityException("Permission with id '" + id + "' does not exist.", e);
-            }
-            catch (TorqueException e)
-            {
-                Transaction.safeRollback(con);
-                throw new DataBackendException("Error retrieving permission information", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Invalid permission id '" + permission.getId() + "'");
-        }
-
-        return permission;
-    }
-
-    /**
-     * Retrieve a Permission object with specified name.
-     *
-     * @param name the name of the Group.
-     * @return an object representing the Group with specified name.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     * @throws UnknownEntityException if the group does not exist.
-     */
-    public Permission getPermissionByName(String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        Permission permission = getPermissionInstance();
-        List permissions = Collections.EMPTY_LIST;
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorquePermissionPeer.DATABASE_NAME);
-            
-            Criteria criteria = new Criteria();
-            criteria.add(TorquePermissionPeer.PERMISSION_NAME, name);
-
-            permissions = TorquePermissionPeer.doSelect(criteria, con);
-
-            if (permissions.size() == 1)
-            {
-                TorquePermission p = (TorquePermission) permissions.get(0);
-                
-                permission.setId(p.getId());
-                permission.setName(p.getName());
-                
-                // Add roles if they exist
-                ((DynamicPermission)permission).setRoles(getRolesForPermission(permission, con));
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving permission information", e);
-        }
-
-        if (permissions.size() == 0)
-        {
-            throw new UnknownEntityException("Could not find permission " + name);
-        }
-
-        if (permissions.size() > 1)
-        {
-            throw new DataBackendException("Multiple Permissions with same name '" + name + "'");
-        }
-
-        return permission;
-    }
-
-    /**
      * Provides the roles for the given permission
      *  
      * @param permission the permission for which the roles should be retrieved  
      * @param con a database connection
      */
-    private RoleSet getRolesForPermission(Permission permission, Connection con)
+    protected void attachObjectsForPermission(Permission permission, Connection con)
         throws TorqueException, DataBackendException
     {
         RoleSet roleSet = new RoleSet();
@@ -361,6 +66,6 @@
             roleSet.add(role);
         }
         
-        return roleSet;
+        ((DynamicPermission)permission).setRoles(roleSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicRoleManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicRoleManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicRoleManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicRoleManagerImpl.java Sun Oct  8 12:54:07 2006
@@ -15,7 +15,6 @@
  *  limitations under the License.
  */
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -25,329 +24,35 @@
 import org.apache.fulcrum.security.entity.Permission;
 import org.apache.fulcrum.security.entity.Role;
 import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
-import org.apache.fulcrum.security.spi.AbstractRoleManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractRoleManager;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicGroupRolePeer;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicRolePermissionPeer;
 import org.apache.fulcrum.security.torque.om.TorqueGroup;
 import org.apache.fulcrum.security.torque.om.TorqueGroupPeer;
 import org.apache.fulcrum.security.torque.om.TorquePermission;
 import org.apache.fulcrum.security.torque.om.TorquePermissionPeer;
-import org.apache.fulcrum.security.torque.om.TorqueRole;
-import org.apache.fulcrum.security.torque.om.TorqueRolePeer;
 import org.apache.fulcrum.security.util.DataBackendException;
-import org.apache.fulcrum.security.util.EntityExistsException;
 import org.apache.fulcrum.security.util.GroupSet;
 import org.apache.fulcrum.security.util.PermissionSet;
-import org.apache.fulcrum.security.util.RoleSet;
 import org.apache.fulcrum.security.util.UnknownEntityException;
-import org.apache.torque.NoRowsException;
 import org.apache.torque.TorqueException;
-import org.apache.torque.om.SimpleKey;
 import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Transaction;
 /**
  * This implementation persists to a database via Torque.
  *
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  * @version $Id:$
  */
-public class TorqueDynamicRoleManagerImpl extends AbstractRoleManager
+public class TorqueDynamicRoleManagerImpl extends TorqueAbstractRoleManager
 {
     /**
-    * Renames an existing Role.
-    *
-    * @param role The object describing the role to be renamed.
-    * @param name the new name for the role.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws UnknownEntityException if the role does not exist.
-    */
-    public synchronized void renameRole(Role role, String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(role))
-        {
-            role.setName(name);
-            
-            try
-            {
-                TorqueRole r = new TorqueRole();
-                r.setId((Integer)role.getId());
-                r.setName(name);
-                r.setNew(false);
-                r.save();
-            }
-            catch (Exception e)
-            {
-                throw new DataBackendException("Renaming Role '" + role + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown Role '" + role + "'");
-        }
-    }
-
-    /**
-      * Determines if the <code>Role</code> exists in the security system.
-      *
-      * @param roleName a <code>Role</code> value
-      * @return true if the role name exists in the system, false otherwise
-      * @throws DataBackendException when more than one Role with
-      *         the same name exists.
-      */
-    public boolean checkExists(String roleName) throws DataBackendException
-    {
-        List roles;
-
-        try
-        {
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueRolePeer.ROLE_NAME, roleName);
-
-            roles = TorqueRolePeer.doSelect(criteria);
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Error retrieving role information", e);
-        }
-
-        if (roles.size() > 1)
-        {
-            throw new DataBackendException("Multiple roles with same name '" + roleName + "'");
-        }
-        
-        return (roles.size() == 1);
-    }
-
-    /**
-     * Retrieves all roles defined in the system.
-     *
-     * @return the names of all roles defined in the system.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     */
-    public RoleSet getAllRoles() throws DataBackendException
-    {
-        RoleSet roleSet = new RoleSet();
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueRolePeer.DATABASE_NAME);
-            
-            List roles = TorqueRolePeer.doSelect(new Criteria(), con);
-            
-            for (Iterator i = roles.iterator(); i.hasNext();)
-            {
-                Role role = getRoleInstance();
-                TorqueRole r = (TorqueRole)i.next();
-                role.setId(r.getId());
-                role.setName(r.getName());
-
-                // Add groups if they exist
-                ((DynamicRole)role).setGroups(getGroupsForRole(role, con));
-
-                // Add permissions if they exist
-                ((DynamicRole)role).setPermissions(getPermissionsForRole(role, con));
-
-                roleSet.add(role);
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving role information", e);
-        }
-        catch (UnknownEntityException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error creating permission instance", e);
-        }
-        
-        return roleSet;
-    }
-
-    /**
-    * Creates a new role with specified attributes.
-    *
-    * @param role the object describing the role to be created.
-    * @return a new Role object that has id set up properly.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws EntityExistsException if the role already exists.
-    */
-    protected synchronized Role persistNewRole(Role role) throws DataBackendException
-    {
-        try
-        {
-            TorqueRole r = new TorqueRole();
-            r.setName(role.getName());
-            r.save();
-            
-            role.setId(r.getId());
-        }
-        catch (Exception e)
-        {
-            throw new DataBackendException("Adding Role '" + role + "' failed", e);
-        }
-        
-        return role;
-    }
-
-    /**
-    * Removes a Role from the system.
-    *
-    * @param role The object describing the role to be removed.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws UnknownEntityException if the role does not exist.
-    */
-    public synchronized void removeRole(Role role) throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(role))
-        {
-            try
-            {
-                TorqueRolePeer.doDelete(SimpleKey.keyFor((Integer)role.getId()));
-            }
-            catch (TorqueException e)
-            {
-                throw new DataBackendException("Removing Role '" + role + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown role '" + role + "'");
-        }
-    }
-
-    /**
-     * Retrieve a Role object with specified id.
-     *
-     * @param id
-     *            the id of the Role.
-     * @return an object representing the Role with specified id.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the role does not exist.
-     */
-    public Role getRoleById(Object id)
-        throws DataBackendException, UnknownEntityException 
-    {
-        Role role = getRoleInstance();
-
-        if (id != null && id instanceof Integer)
-        {
-            Connection con = null;
-
-            try
-            {
-                con = Transaction.begin(TorqueRolePeer.DATABASE_NAME);
-                
-                TorqueRole r = 
-                    TorqueRolePeer.retrieveByPK((Integer)id, con);
-                role.setId(r.getId());
-                role.setName(r.getName());
-                
-                // Add groups if they exist
-                ((DynamicRole)role).setGroups(getGroupsForRole(role, con));
-
-                // Add permissions if they exist
-                ((DynamicRole)role).setPermissions(getPermissionsForRole(role, con));
-
-                Transaction.commit(con);
-            }
-            catch (NoRowsException e)
-            {
-                Transaction.safeRollback(con);
-                throw new UnknownEntityException("Role with id '" + id + "' does not exist.", e);
-            }
-            catch (TorqueException e)
-            {
-                Transaction.safeRollback(con);
-                throw new DataBackendException("Error retrieving role information", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Invalid role id '" + role.getId() + "'");
-        }
-
-        return role;
-    }
-
-    /**
-     * Retrieve a Role object with specified name.
-     *
-     * @param name the name of the Role.
-     * @return an object representing the Role with specified name.
-     * @throws DataBackendException if there was an error accessing the
-     *         data backend.
-     * @throws UnknownEntityException if the role does not exist.
-     */
-    public Role getRoleByName(String name)
-        throws DataBackendException, UnknownEntityException
-    {
-        Role role = getRoleInstance();
-        List roles = Collections.EMPTY_LIST;
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueRolePeer.DATABASE_NAME);
-            
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueRolePeer.ROLE_NAME, name);
-
-            roles = TorqueRolePeer.doSelect(criteria, con);
-
-            if (roles.size() == 1)
-            {
-                TorqueRole r = (TorqueRole) roles.get(0);
-                
-                role.setId(r.getId());
-                role.setName(r.getName());
-                
-                // Add groups if they exist
-                ((DynamicRole)role).setGroups(getGroupsForRole(role, con));
-
-                // Add permissions if they exist
-                ((DynamicRole)role).setPermissions(getPermissionsForRole(role, con));
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving role information", e);
-        }
-
-        if (roles.size() == 0)
-        {
-            throw new UnknownEntityException("Could not find role" + name);
-        }
-
-        if (roles.size() > 1)
-        {
-            throw new DataBackendException("Multiple Roles with same name '" + name + "'");
-        }
-
-        return role;
-    }
-
-    /**
-     * Provides the groups for the given role
+     * Provides the groups/permissions for the given role
      *  
-     * @param role the role for which the groups should be retrieved  
+     * @param role the role for which the groups/permissions should be retrieved  
      * @param con a database connection
      */
-    private GroupSet getGroupsForRole(Role role, Connection con)
-        throws TorqueException, DataBackendException
+    protected void attachObjectsForRole(Role role, Connection con)
+        throws TorqueException, DataBackendException, UnknownEntityException
     {
         GroupSet groupSet = new GroupSet();
         
@@ -368,21 +73,11 @@
             groupSet.add(group);
         }
         
-        return groupSet;
-    }
+        ((DynamicRole)role).setGroups(groupSet);
 
-    /**
-     * Provides the permissions for the given role
-     *  
-     * @param role the role for which the permissions should be retrieved  
-     * @param con a database connection
-     */
-    private PermissionSet getPermissionsForRole(Role role, Connection con)
-        throws TorqueException, DataBackendException, UnknownEntityException
-    {
         PermissionSet permissionSet = new PermissionSet();
         
-        Criteria criteria = new Criteria();
+        criteria.clear();
         criteria.addJoin(TorqueDynamicRolePermissionPeer.PERMISSION_ID, TorquePermissionPeer.PERMISSION_ID);
         criteria.add(TorqueDynamicRolePermissionPeer.ROLE_ID, (Integer)role.getId());
         
@@ -399,6 +94,6 @@
             permissionSet.add(permission);
         }
         
-        return permissionSet;
+        ((DynamicRole)role).setPermissions(permissionSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicUserManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicUserManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicUserManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/dynamic/TorqueDynamicUserManagerImpl.java Sun Oct  8 12:54:07 2006
@@ -15,7 +15,6 @@
  *  limitations under the License.
  */
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -25,7 +24,7 @@
 import org.apache.fulcrum.security.entity.Group;
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.dynamic.entity.DynamicUser;
-import org.apache.fulcrum.security.spi.AbstractUserManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractUserManager;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicUserDelegatesPeer;
 import org.apache.fulcrum.security.torque.om.TorqueDynamicUserGroupPeer;
 import org.apache.fulcrum.security.torque.om.TorqueGroup;
@@ -33,321 +32,24 @@
 import org.apache.fulcrum.security.torque.om.TorqueUser;
 import org.apache.fulcrum.security.torque.om.TorqueUserPeer;
 import org.apache.fulcrum.security.util.DataBackendException;
-import org.apache.fulcrum.security.util.EntityExistsException;
 import org.apache.fulcrum.security.util.GroupSet;
-import org.apache.fulcrum.security.util.UnknownEntityException;
-import org.apache.fulcrum.security.util.UserSet;
-import org.apache.torque.NoRowsException;
 import org.apache.torque.TorqueException;
-import org.apache.torque.om.SimpleKey;
 import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Transaction;
 /**
  * This implementation persists to a database via Torque.
  *
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  * @version $Id:$
  */
-public class TorqueDynamicUserManagerImpl extends AbstractUserManager
+public class TorqueDynamicUserManagerImpl extends TorqueAbstractUserManager
 {
     /**
-     * Check whether a specified user's account exists.
-     *
-     * The login name is used for looking up the account.
-     *
-     * @param userName The name of the user to be checked.
-     * @return true if the specified account exists
-     * @throws DataBackendException if there was an error accessing
-     *         the data backend.
-     */
-    public boolean checkExists(String userName) throws DataBackendException
-    {
-        List users;
-
-        try
-        {
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueUserPeer.LOGIN_NAME, userName.toLowerCase());
-
-            users = TorqueUserPeer.doSelect(criteria);
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Error retrieving user information", e);
-        }
-
-        if (users.size() > 1)
-        {
-            throw new DataBackendException("Multiple Users with same username '" + userName + "'");
-        }
-        
-        return (users.size() == 1);
-    }
-
-    /**
-     * Retrieve a user from persistent storage using username as the
-     * key.
-     *
-     * @param userName the name of the user.
-     * @return an User object.
-     * @exception UnknownEntityException if the user's account does not
-     *            exist in the database.
-     * @exception DataBackendException if there is a problem accessing the
-     *            storage.
-     */
-    public User getUser(String userName) throws UnknownEntityException, DataBackendException
-    {
-        User user = getUserInstance();
-        List users = Collections.EMPTY_LIST;
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
-            
-            Criteria criteria = new Criteria();
-            criteria.add(TorqueUserPeer.LOGIN_NAME, userName.toLowerCase());
-
-            users = TorqueUserPeer.doSelect(criteria, con);
-
-            if (users.size() == 1)
-            {
-                TorqueUser u = (TorqueUser) users.get(0);
-                
-                user.setId(u.getId());
-                user.setName(u.getName());
-                user.setPassword(u.getPassword());
-                
-                // Add groups if they exist
-                ((DynamicUser)user).setGroups(getGroupsForUser(user, con));
-
-                // Add delegators if they exist
-                ((DynamicUser)user).setDelegators(getDelegatorsForUser(user, con));
-
-                // Add delegatees if they exist
-                ((DynamicUser)user).setDelegatees(getDelegateesForUser(user, con));
-            }
-            
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving user information", e);
-        }
-
-        if (users.size() == 0)
-        {
-            throw new UnknownEntityException("Unknown user '" + userName + "'");
-        }
-
-        if (users.size() > 1)
-        {
-            throw new DataBackendException("Multiple Users with same username '" + userName + "'");
-        }
-
-        return user;
-    }
-
-    /**
-       * Retrieves all users defined in the system.
-       *
-       * @return the names of all users defined in the system.
-       * @throws DataBackendException if there was an error accessing the data
-       *         backend.
-       */
-    public UserSet getAllUsers() throws DataBackendException
-    {
-        UserSet userSet = new UserSet();
-        Connection con = null;
-
-        try
-        {
-            con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
-            
-            List users = TorqueUserPeer.doSelect(new Criteria(), con);
-
-            for (Iterator i = users.iterator(); i.hasNext();)
-            {
-                User user = getUserInstance();
-                TorqueUser u = (TorqueUser)i.next();
-                user.setId(u.getId());
-                user.setName(u.getName());
-                user.setPassword(u.getPassword());
-
-                // Add groups if they exist
-                ((DynamicUser)user).setGroups(getGroupsForUser(user, con));
-
-                // Add delegators if they exist
-                ((DynamicUser)user).setDelegators(getDelegatorsForUser(user, con));
-
-                // Add delegatees if they exist
-                ((DynamicUser)user).setDelegatees(getDelegateesForUser(user, con));
-                
-                userSet.add(user);
-            }
-
-            Transaction.commit(con);
-        }
-        catch (TorqueException e)
-        {
-            Transaction.safeRollback(con);
-            throw new DataBackendException("Error retrieving all users", e);
-        }
-        
-        return userSet;
-    }
-
-    /**
-    * Removes an user account from the system.
-    *
-    * @param user the object describing the account to be removed.
-    * @throws DataBackendException if there was an error accessing the data
-    *         backend.
-    * @throws UnknownEntityException if the user account is not present.
-    */
-    public synchronized void removeUser(User user) throws DataBackendException, UnknownEntityException
-    {
-        try
-        {
-            TorqueUserPeer.doDelete(SimpleKey.keyFor((Integer)user.getId()));
-        }
-        catch (TorqueException e)
-        {
-            throw new DataBackendException("Removing User '" + user + "' failed", e);
-        }
-    }
-
-    /**
-       * Creates new user account with specified attributes.
-       *
-       * @param user the object describing account to be created.
-       * @param password The password to use for the account.
-       *
-       * @throws DataBackendException if there was an error accessing the
-       *         data backend.
-       * @throws EntityExistsException if the user account already exists.
-       */
-    protected synchronized User persistNewUser(User user) throws DataBackendException
-    {
-        try
-        {
-            TorqueUser u = new TorqueUser();
-            
-            u.setName(user.getName());
-            u.setPassword(user.getPassword());
-            u.save();
-
-            user.setId(u.getId());
-        }
-        catch (Exception e)
-        {
-            throw new DataBackendException("Adding User '" + user + "' failed", e);
-        }
-        
-        return user;
-    }
-
-    /**
-       * Stores User attributes. The User is required to exist in the system.
-       *
-       * @param role The User to be stored.
-       * @throws DataBackendException if there was an error accessing the data
-       *         backend.
-       * @throws UnknownEntityException if the role does not exist.
-       */
-    public synchronized void saveUser(User user) throws DataBackendException, UnknownEntityException
-    {
-        if (checkExists(user))
-        {
-            try
-            {
-                TorqueUser u = new TorqueUser();
-                
-                u.setId((Integer)user.getId());
-                u.setName(user.getName());
-                u.setPassword(user.getPassword());
-                u.setNew(false);
-                u.save();
-            }
-            catch (Exception e)
-            {
-                throw new DataBackendException("Saving User '" + user + "' failed", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Unknown user '" + user + "'");
-        }
-    }
-
-    /**
-     * Retrieve a User object with specified id.
-     *
-     * @param id
-     *            the id of the User.
-     * @return an object representing the User with specified id.
-     * @throws DataBackendException
-     *             if there was an error accessing the data backend.
-     * @throws UnknownEntityException
-     *             if the user does not exist.
-     */
-    public User getUserById(Object id)
-        throws DataBackendException, UnknownEntityException
-    {
-        User user = getUserInstance();
-
-        if (id != null && id instanceof Integer)
-        {
-            Connection con = null;
-            
-            try
-            {
-                con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
-                
-                TorqueUser u = TorqueUserPeer.retrieveByPK((Integer)id, con);
-
-                user.setId(u.getId());
-                user.setName(u.getName());
-                user.setPassword(u.getPassword());
-
-                // Add groups if they exist
-                ((DynamicUser)user).setGroups(getGroupsForUser(user, con));
-
-                // Add delegators if they exist
-                ((DynamicUser)user).setDelegators(getDelegatorsForUser(user, con));
-
-                // Add delegatees if they exist
-                ((DynamicUser)user).setDelegatees(getDelegateesForUser(user, con));
-                
-                Transaction.commit(con);
-            }
-            catch (NoRowsException e)
-            {
-                Transaction.safeRollback(con);
-                throw new UnknownEntityException("User with id '" + id + "' does not exist.", e);
-            }
-            catch (TorqueException e)
-            {
-                Transaction.safeRollback(con);
-                throw new DataBackendException("Error retrieving user information", e);
-            }
-        }
-        else
-        {
-            throw new UnknownEntityException("Invalid user id '" + user.getId() + "'");
-        }
-
-        return user;
-    }
-    
-    /**
-     * Provides the groups for the given user
+     * Provides the attached objects for the given user
      *  
-     * @param user the user for which the groups should be retrieved  
+     * @param user the user for which the attached objects should be retrieved  
      * @param con a database connection
      */
-    private GroupSet getGroupsForUser(User user, Connection con)
+    protected void attachObjectsForUser(User user, Connection con)
         throws TorqueException, DataBackendException
     {
         GroupSet groupSet = new GroupSet();
@@ -369,21 +71,11 @@
             groupSet.add(group);
         }
         
-        return groupSet;
-    }
+        ((DynamicUser)user).setGroups(groupSet);
 
-    /**
-     * Provides the delegators for the given user
-     *  
-     * @param user the user for which the delegators should be retrieved  
-     * @param con a database connection
-     */
-    private Set getDelegatorsForUser(User user, Connection con)
-        throws TorqueException, DataBackendException
-    {
         Set delegatorsSet = new HashSet();
         
-        Criteria criteria = new Criteria();
+        criteria.clear();
         criteria.addJoin(TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID, TorqueUserPeer.USER_ID);
         criteria.add(TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID, (Integer)user.getId());
         
@@ -400,25 +92,15 @@
             delegatorsSet.add(delegator);
         }
         
-        return delegatorsSet;
-    }
+        ((DynamicUser)user).setDelegators(delegatorsSet);
 
-    /**
-     * Provides the delegatees for the given user
-     *  
-     * @param user the user for which the delegatees should be retrieved  
-     * @param con a database connection
-     */
-    private Set getDelegateesForUser(User user, Connection con)
-        throws TorqueException, DataBackendException
-    {
         Set delegateesSet = new HashSet();
         
-        Criteria criteria = new Criteria();
+        criteria.clear();
         criteria.addJoin(TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID, TorqueUserPeer.USER_ID);
         criteria.add(TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID, (Integer)user.getId());
         
-        List users = TorqueUserPeer.doSelect(criteria, con);
+        users = TorqueUserPeer.doSelect(criteria, con);
         
         for (Iterator i = users.iterator(); i.hasNext();)
         {
@@ -431,6 +113,6 @@
             delegateesSet.add(delegatee);
         }
         
-        return delegateesSet;
+        ((DynamicUser)user).setDelegatees(delegateesSet);
     }
 }

Propchange: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/om/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Oct  8 12:54:07 2006
@@ -1 +1,3 @@
+
 *.java
+map



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org