KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > security > impl > db > entity > TurbineRolePeer


1 package org.apache.fulcrum.security.impl.db.entity;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.List JavaDoc;
58
59 import org.apache.fulcrum.security.TurbineSecurity;
60 import org.apache.fulcrum.security.entity.Group;
61 import org.apache.fulcrum.security.entity.Role;
62 import org.apache.fulcrum.security.entity.User;
63 import org.apache.fulcrum.security.impl.db.DBSecurityService;
64 import org.apache.fulcrum.security.util.DataBackendException;
65 import org.apache.fulcrum.security.util.RoleSet;
66 import org.apache.torque.om.Persistent;
67 import org.apache.torque.util.BasePeer;
68 import org.apache.torque.util.Criteria;
69
70 /**
71  * This class handles all the database access for the ROLE table.
72  * This table contains all the roles that a given member can play.
73  *
74  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
75  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
76  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
77  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
78  * @version $Id: TurbineRolePeer.java,v 1.1 2004/11/12 10:25:43 epugh Exp $
79  */

80 public class TurbineRolePeer
81     extends org.apache.fulcrum.security.impl.db.entity.BaseTurbineRolePeer
82 {
83     /** The column name for the name field. */
84     public static final String JavaDoc NAME = ROLE_NAME;
85
86     /**
87      * Checks if a Role is defined in the system. The name
88      * is used as query criteria.
89      *
90      * @param permission The Role to be checked.
91      * @return <code>true</code> if given Role exists in the system.
92      * @throws DataBackendException when more than one Role with
93      * the same name exists.
94      * @throws Exception, a generic exception.
95      */

96     public static boolean checkExists(Role role)
97         throws DataBackendException, Exception JavaDoc
98     {
99         Criteria criteria = new Criteria();
100         criteria.addSelectColumn(ROLE_ID);
101         criteria.add(NAME, role.getName());
102         List JavaDoc results = BasePeer.doSelect(criteria);
103         if (results.size() > 1)
104         {
105             throw new DataBackendException("Multiple roles named '" +
106                 role.getName() + "' exist!");
107         }
108         return (results.size() == 1);
109     }
110
111     /**
112      * Returns the full name of a column.
113      *
114      * @return A String with the full name of the column.
115      */

116     public static String JavaDoc getColumnName(String JavaDoc name)
117     {
118         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
119         sb.append(TurbineRolePeer.TABLE_NAME);
120         sb.append(".");
121         sb.append(name);
122         return sb.toString();
123     }
124
125     /**
126      * Get the name of this table.
127      *
128      * @return A String with the name of the table.
129      */

130     public static String JavaDoc getTableName()
131     {
132         return TABLE_NAME;
133     }
134
135     /**
136      * Retrieves/assembles a RoleSet based on the Criteria passed in
137      */

138     public static RoleSet retrieveSet(Criteria criteria) throws Exception JavaDoc
139     {
140         List JavaDoc results = doSelect(criteria);
141         RoleSet rs = new RoleSet();
142         for (int i=0; i<results.size(); i++)
143         {
144             rs.add( (Role)results.get(i) );
145         }
146         return rs;
147     }
148
149     /**
150      * Retrieves a set of Roles that an User was assigned in a Group
151      *
152      * @param user An user.
153      * @param group A group
154      * @return A Set of Roles of this User in the Group
155      * @exception Exception, a generic exception.
156      */

157     public static RoleSet retrieveSet(User user, Group group)
158         throws Exception JavaDoc
159     {
160         Criteria criteria = new Criteria();
161
162         /*
163          * Peer specific methods should absolutely NOT be part
164          * of any of the generic interfaces in the security system.
165          * this is not good.
166          *
167          * UserPeer up = TurbineSecurity.getUserPeerInstance();
168          */

169         UserPeer up = ((DBSecurityService)TurbineSecurity.getService())
170             .getUserPeerInstance();
171
172         criteria.add(TurbineUserPeer.USERNAME, user.getUserName());
173         criteria.add(TurbineUserGroupRolePeer.GROUP_ID,
174                      ((Persistent)group).getPrimaryKey());
175
176         criteria.addJoin(TurbineUserPeer.USER_ID, TurbineUserGroupRolePeer.USER_ID);
177         criteria.addJoin(TurbineUserGroupRolePeer.ROLE_ID, TurbineRolePeer.ROLE_ID);
178         return retrieveSet(criteria);
179     }
180
181 }
182
Popular Tags