KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > om > security > peer > RolePeer


1 package org.apache.turbine.om.security.peer;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import org.apache.torque.TorqueException;
23 import org.apache.torque.om.BaseObject;
24 import org.apache.torque.om.NumberKey;
25 import org.apache.torque.om.Persistent;
26 import org.apache.torque.util.BasePeer;
27 import org.apache.torque.util.Criteria;
28 import org.apache.turbine.om.security.Group;
29 import org.apache.turbine.om.security.Role;
30 import org.apache.turbine.om.security.TurbineRole;
31 import org.apache.turbine.om.security.User;
32 import org.apache.turbine.util.ObjectUtils;
33 import org.apache.turbine.util.db.map.TurbineMapBuilder;
34 import org.apache.turbine.util.security.DataBackendException;
35 import org.apache.turbine.util.security.RoleSet;
36 import com.workingdogs.village.Record;
37
38
39 /**
40  * This class handles all the database access for the ROLE table.
41  * This table contains all the roles that a given member can play.
42  *
43  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
44  * @author <a HREF="mailto:john.mcnally@clearink.com">John D. McNally</a>
45  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
46  * @version $Id: RolePeer.java,v 1.11.2.2 2004/05/20 03:05:16 seade Exp $
47  */

48 public class RolePeer extends BasePeer
49 {
50     /** The map builder for this Peer. */
51     private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder)
52             getMapBuilder(TurbineMapBuilder.class.getName());
53
54     /** The table name for this peer. */
55     private static final String JavaDoc TABLE_NAME = MAP_BUILDER.getTableRole();
56
57     /** The column name for the role id field. */
58     public static final String JavaDoc ROLE_ID = MAP_BUILDER.getRole_RoleId();
59
60     /** The column name for the name field. */
61     public static final String JavaDoc NAME = MAP_BUILDER.getRole_Name();
62
63     /** The column name for the ObjectData field */
64     public static final String JavaDoc OBJECTDATA = MAP_BUILDER.getRole_ObjectData();
65
66     /**
67      * Retrieves/assembles a RoleSet based on the Criteria passed in
68      *
69      * @param criteria The criteria to use.
70      * @return a RoleSet
71      * @exception Exception a generic exception.
72      */

73     public static RoleSet retrieveSet(Criteria criteria) throws Exception JavaDoc
74     {
75         List JavaDoc results = RolePeer.doSelect(criteria);
76         RoleSet rs = new RoleSet();
77         for (int i = 0; i < results.size(); i++)
78         {
79             rs.add((Role) results.get(i));
80         }
81         return rs;
82     }
83
84     /**
85      * Retrieves a set of Roles that an User was assigned in a Group
86      *
87      * @param user An user.
88      * @param group A group
89      * @return A Set of Roles of this User in the Group
90      * @exception Exception a generic exception.
91      */

92     public static RoleSet retrieveSet(User user, Group group) throws Exception JavaDoc
93     {
94         Criteria criteria = new Criteria();
95         criteria.add(UserGroupRolePeer.USER_ID,
96                 ((Persistent) user).getPrimaryKey());
97         criteria.add(UserGroupRolePeer.GROUP_ID,
98                 ((Persistent) group).getPrimaryKey());
99         criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID);
100         return retrieveSet(criteria);
101     }
102
103     /**
104      * Issues a select based on a criteria.
105      *
106      * @param criteria object containing data that is used to create
107      * the SELECT statement.
108      * @return Vector containing Role objects.
109      * @exception TorqueException a generic exception.
110      */

111     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
112     {
113         try
114         {
115             criteria.addSelectColumn(ROLE_ID)
116                     .addSelectColumn(NAME)
117                     .addSelectColumn(OBJECTDATA);
118
119             if (criteria.getOrderByColumns() == null
120                     || criteria.getOrderByColumns().size() == 0)
121             {
122                 criteria.addAscendingOrderByColumn(NAME);
123             }
124
125             // Place any checks here to intercept criteria which require
126
// custom SQL. For example:
127
// if ( criteria.containsKey("SomeTable.SomeColumn") )
128
// {
129
// String whereSql = "SomeTable.SomeColumn IN (Select ...";
130
// criteria.add("SomeTable.SomeColumn",
131
// whereSQL, criteria.CUSTOM);
132
// }
133

134             // BasePeer returns a Vector of Value (Village) arrays. The
135
// array order follows the order columns were placed in the
136
// Select clause.
137
List JavaDoc rows = BasePeer.doSelect(criteria);
138             List JavaDoc results = new ArrayList JavaDoc();
139
140             // Populate the object(s).
141
for (int i = 0; i < rows.size(); i++)
142             {
143                 //Role obj = new Role();
144
Role obj = new TurbineRole();
145                 Record row = (Record) rows.get(i);
146                 ((TurbineRole) obj).setPrimaryKey(
147                         new NumberKey(row.getValue(1).asInt()));
148                 ((TurbineRole) obj).setName(row.getValue(2).asString());
149                 byte[] objectData = row.getValue(3).asBytes();
150                 Map JavaDoc temp = (Map JavaDoc) ObjectUtils.deserialize(objectData);
151                 if (temp != null)
152                 {
153                     ((TurbineRole) obj).setAttributes(temp);
154                 }
155                 results.add(obj);
156             }
157
158             return results;
159         }
160         catch (Exception JavaDoc ex)
161         {
162             throw new TorqueException (ex);
163         }
164     }
165
166     /**
167      * Builds a criteria object based upon an Role object
168      *
169      * @param role object to build the criteria
170      * @return the Criteria
171      */

172     public static Criteria buildCriteria(Role role)
173     {
174         Criteria criteria = new Criteria();
175         if (!((BaseObject) role).isNew())
176         {
177             criteria.add(ROLE_ID, ((BaseObject) role).getPrimaryKey());
178         }
179         criteria.add(NAME, role.getName());
180         // causing the removal and updating of roles to
181
// crap out because of the generated SQL.
182
//criteria.add(OBJECTDATA, role.getAttributes());
183
return criteria;
184     }
185
186     /**
187      * Issues an update based on a criteria.
188      *
189      * @param criteria object containing data that is used to create
190      * the UPDATE statement.
191      * @exception TorqueException a generic exception.
192      */

193     public static void doUpdate(Criteria criteria)
194         throws TorqueException
195     {
196         Criteria selectCriteria = new Criteria(2);
197         selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID));
198         BasePeer.doUpdate(selectCriteria, criteria);
199     }
200
201     /**
202      * Checks if a Role is defined in the system. The name
203      * is used as query criteria.
204      *
205      * @param role The Role to be checked.
206      * @return <code>true</code> if given Role exists in the system.
207      * @throws DataBackendException when more than one Role with
208      * the same name exists.
209      * @throws Exception a generic exception.
210      */

211     public static boolean checkExists(Role role)
212         throws DataBackendException, Exception JavaDoc
213     {
214         Criteria criteria = new Criteria();
215         criteria.addSelectColumn(ROLE_ID);
216         criteria.add(NAME, role.getName());
217         List JavaDoc results = BasePeer.doSelect(criteria);
218         if (results.size() > 1)
219         {
220             throw new DataBackendException("Multiple roles named '"
221                     + role.getName() + "' exist!");
222         }
223         return (results.size() == 1);
224     }
225
226     /**
227      * Get the name of this table.
228      *
229      * @return A String with the name of the table.
230      */

231     public static String JavaDoc getTableName()
232     {
233         return TABLE_NAME;
234     }
235
236     /**
237      * Returns the full name of a column.
238      *
239      * @param name name of a column
240      * @return A String with the full name of the column.
241      */

242     public static String JavaDoc getColumnName (String JavaDoc name)
243     {
244         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
245         sb.append(TABLE_NAME);
246         sb.append(".");
247         sb.append(name);
248         return sb.toString();
249     }
250 }
251
Popular Tags