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 [2/2] - in /jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque: basic/ dynamic/ om/ turbine/

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineGroupManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineGroupManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineGroupManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineGroupManagerImpl.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;
@@ -28,9 +27,7 @@
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.turbine.entity.TurbineGroup;
 import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
-import org.apache.fulcrum.security.spi.AbstractGroupManager;
-import org.apache.fulcrum.security.torque.om.TorqueGroup;
-import org.apache.fulcrum.security.torque.om.TorqueGroupPeer;
+import org.apache.fulcrum.security.torque.TorqueAbstractGroupManager;
 import org.apache.fulcrum.security.torque.om.TorqueRole;
 import org.apache.fulcrum.security.torque.om.TorqueRolePeer;
 import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRole;
@@ -38,298 +35,23 @@
 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.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 TorqueTurbineGroupManagerImpl extends AbstractGroupManager
+public class TorqueTurbineGroupManagerImpl 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 user/group/role-relations if they exist
-                ((TurbineGroup)group).setUserGroupRoleSet(getUgrForGroup(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 user/group/role-relations if they exist
-                ((TurbineGroup)group).setUserGroupRoleSet(getUgrForGroup(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 user/group/role-relations if they exist
-                ((TurbineGroup)group).setUserGroupRoleSet(getUgrForGroup(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 user/group/role-relations for the given group
      *  
      * @param group the group for which the relations should be retrieved  
      * @param con a database connection
      */
-    private Set getUgrForGroup(Group group, Connection con)
+    protected void attachObjectsForGroup(Group group, Connection con)
         throws TorqueException, DataBackendException
     {
         Set ugrSet = new HashSet();
@@ -364,6 +86,6 @@
             ugrSet.add(ugr);
         }
         
-        return ugrSet;
+        ((TurbineGroup)group).setUserGroupRoleSet(ugrSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbinePermissionManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbinePermissionManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbinePermissionManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbinePermissionManagerImpl.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.turbine.entity.TurbinePermission;
-import org.apache.fulcrum.security.spi.AbstractPermissionManager;
-import org.apache.fulcrum.security.torque.om.TorquePermission;
-import org.apache.fulcrum.security.torque.om.TorquePermissionPeer;
+import org.apache.fulcrum.security.torque.TorqueAbstractPermissionManager;
 import org.apache.fulcrum.security.torque.om.TorqueRole;
 import org.apache.fulcrum.security.torque.om.TorqueRolePeer;
 import org.apache.fulcrum.security.torque.om.TorqueTurbineRolePermissionPeer;
 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 TorqueTurbinePermissionManagerImpl extends AbstractPermissionManager
+public class TorqueTurbinePermissionManagerImpl 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
-                ((TurbinePermission)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
-                ((TurbinePermission)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
-                ((TurbinePermission)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;
+        ((TurbinePermission)permission).setRoles(roleSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineRoleManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineRoleManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineRoleManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineRoleManagerImpl.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;
@@ -30,331 +29,37 @@
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.turbine.entity.TurbineRole;
 import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
-import org.apache.fulcrum.security.spi.AbstractRoleManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractRoleManager;
 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.torque.om.TorqueTurbineRolePermissionPeer;
 import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRole;
 import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRolePeer;
 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.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 TorqueTurbineRoleManagerImpl extends AbstractRoleManager
+public class TorqueTurbineRoleManagerImpl 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 user/group/role-relations if they exist
-                ((TurbineRole)role).setUserGroupRoleSet(getUgrForRole(role, con));
-
-                // Add permissions if they exist
-                ((TurbineRole)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 user/group/role-relations if they exist
-                ((TurbineRole)role).setUserGroupRoleSet(getUgrForRole(role, con));
-
-                // Add permissions if they exist
-                ((TurbineRole)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 user/group/role-relations if they exist
-                ((TurbineRole)role).setUserGroupRoleSet(getUgrForRole(role, con));
-
-                // Add permissions if they exist
-                ((TurbineRole)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 user/group/role-relations for the given role
      *  
      * @param role the role for which the relations should be retrieved  
      * @param con a database connection
      */
-    private Set getUgrForRole(Role role, Connection con)
-        throws TorqueException, DataBackendException
+    protected void attachObjectsForRole(Role role, Connection con)
+        throws TorqueException, DataBackendException, UnknownEntityException
     {
         Set ugrSet = new HashSet();
         
@@ -388,21 +93,11 @@
             ugrSet.add(ugr);
         }
         
-        return ugrSet;
-    }
+        ((TurbineRole)role).setUserGroupRoleSet(ugrSet);
 
-    /**
-     * 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(TorqueTurbineRolePermissionPeer.PERMISSION_ID, TorquePermissionPeer.PERMISSION_ID);
         criteria.add(TorqueTurbineRolePermissionPeer.ROLE_ID, (Integer)role.getId());
         
@@ -419,6 +114,6 @@
             permissionSet.add(permission);
         }
         
-        return permissionSet;
+        ((TurbineRole)role).setPermissions(permissionSet);
     }
 }

Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineUserManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineUserManagerImpl.java?view=diff&rev=454198&r1=454197&r2=454198
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineUserManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/turbine/TorqueTurbineUserManagerImpl.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;
@@ -28,312 +27,31 @@
 import org.apache.fulcrum.security.entity.User;
 import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
 import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
-import org.apache.fulcrum.security.spi.AbstractUserManager;
+import org.apache.fulcrum.security.torque.TorqueAbstractUserManager;
 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.TorqueTurbineUserGroupRole;
 import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRolePeer;
-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.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 TorqueTurbineUserManagerImpl extends AbstractUserManager
+public class TorqueTurbineUserManagerImpl 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 user/group/role-relations if they exist
-                ((TurbineUser)user).setUserGroupRoleSet(getUgrForUser(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 user/group/role-relations if they exist
-                ((TurbineUser)user).setUserGroupRoleSet(getUgrForUser(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 user/group/role-relations if they exist
-                ((TurbineUser)user).setUserGroupRoleSet(getUgrForUser(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 user/group/role-relations for the given user
      *  
      * @param user the user for which the relations should be retrieved  
      * @param con a database connection
      */
-    private Set getUgrForUser(User user, Connection con)
+    protected void attachObjectsForUser(User user, Connection con)
         throws TorqueException, DataBackendException
     {
         Set ugrSet = new HashSet();
@@ -367,6 +85,6 @@
             ugrSet.add(ugr);
         }
         
-        return ugrSet;
+        ((TurbineUser)user).setUserGroupRoleSet(ugrSet);
     }
 }



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