1 package org.apache.turbine.om.security.peer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.torque.Torque;
26 import org.apache.torque.TorqueException;
27 import org.apache.torque.om.BaseObject;
28 import org.apache.torque.om.NumberKey;
29 import org.apache.torque.om.Persistent;
30 import org.apache.torque.util.BasePeer;
31 import org.apache.torque.util.Criteria;
32 import org.apache.turbine.om.security.Group;
33 import org.apache.turbine.om.security.Role;
34 import org.apache.turbine.om.security.TurbineRole;
35 import org.apache.turbine.om.security.User;
36 import org.apache.turbine.util.db.map.TurbineMapBuilder;
37 import org.apache.turbine.util.security.DataBackendException;
38 import org.apache.turbine.util.security.RoleSet;
39
40 import com.workingdogs.village.Record;
41
42
43 /***
44 * This class handles all the database access for the ROLE table.
45 * This table contains all the roles that a given member can play.
46 *
47 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
48 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
49 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
50 *
51 * @deprecated Use {@link org.apache.turbine.services.security.torque.TorqueSecurityService}
52 * instead.
53 *
54 * @version $Id: RolePeer.java 571795 2007-09-01 13:09:35Z tv $
55 */
56 public class RolePeer extends BasePeer
57 {
58 /*** Serial Version UID */
59 private static final long serialVersionUID = 8236100811297919996L;
60
61 /*** The map builder for this Peer. */
62 private static final TurbineMapBuilder MAP_BUILDER;
63
64 /*** The table name for this peer. */
65 private static final String TABLE_NAME;
66
67 /*** The column name for the role id field. */
68 public static final String ROLE_ID;
69
70 /*** The column name for the name field. */
71 public static final String NAME;
72
73 /*** The column name for the ObjectData field */
74 public static final String OBJECTDATA;
75
76 static
77 {
78 try
79 {
80 MAP_BUILDER = (TurbineMapBuilder) Torque.getMapBuilder(TurbineMapBuilder.class.getName());
81 }
82 catch (TorqueException e)
83 {
84 log.error("Could not initialize Peer", e);
85 throw new RuntimeException(e);
86 }
87
88 TABLE_NAME = MAP_BUILDER.getTableRole();
89 ROLE_ID = MAP_BUILDER.getRole_RoleId();
90 NAME = MAP_BUILDER.getRole_Name();
91 OBJECTDATA = MAP_BUILDER.getRole_ObjectData();
92 }
93
94 /***
95 * Retrieves/assembles a RoleSet based on the Criteria passed in
96 *
97 * @param criteria The criteria to use.
98 * @return a RoleSet
99 * @exception Exception a generic exception.
100 */
101 public static RoleSet retrieveSet(Criteria criteria) throws Exception
102 {
103 List results = RolePeer.doSelect(criteria);
104 RoleSet rs = new RoleSet();
105 for (int i = 0; i < results.size(); i++)
106 {
107 rs.add((Role) results.get(i));
108 }
109 return rs;
110 }
111
112 /***
113 * Retrieves a set of Roles that an User was assigned in a Group
114 *
115 * @param user An user.
116 * @param group A group
117 * @return A Set of Roles of this User in the Group
118 * @exception Exception a generic exception.
119 */
120 public static RoleSet retrieveSet(User user, Group group) throws Exception
121 {
122 Criteria criteria = new Criteria();
123 criteria.add(UserGroupRolePeer.USER_ID,
124 ((Persistent) user).getPrimaryKey());
125 criteria.add(UserGroupRolePeer.GROUP_ID,
126 ((Persistent) group).getPrimaryKey());
127 criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID);
128 return retrieveSet(criteria);
129 }
130
131 /***
132 * Issues a select based on a criteria.
133 *
134 * @param criteria object containing data that is used to create
135 * the SELECT statement.
136 * @return Vector containing Role objects.
137 * @exception TorqueException a generic exception.
138 */
139 public static List doSelect(Criteria criteria) throws TorqueException
140 {
141 try
142 {
143 criteria.addSelectColumn(ROLE_ID)
144 .addSelectColumn(NAME)
145 .addSelectColumn(OBJECTDATA);
146
147 if (criteria.getOrderByColumns() == null
148 || criteria.getOrderByColumns().size() == 0)
149 {
150 criteria.addAscendingOrderByColumn(NAME);
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165 List rows = BasePeer.doSelect(criteria);
166 List results = new ArrayList();
167
168
169 for (int i = 0; i < rows.size(); i++)
170 {
171
172 Role obj = new TurbineRole();
173 Record row = (Record) rows.get(i);
174 ((TurbineRole) obj).setPrimaryKey(
175 new NumberKey(row.getValue(1).asInt()));
176 ((TurbineRole) obj).setName(row.getValue(2).asString());
177
178
179
180
181
182
183
184
185
186
187 results.add(obj);
188 }
189
190 return results;
191 }
192 catch (Exception ex)
193 {
194 throw new TorqueException (ex);
195 }
196 }
197
198 /***
199 * Builds a criteria object based upon an Role object
200 *
201 * @param role object to build the criteria
202 * @return the Criteria
203 */
204 public static Criteria buildCriteria(Role role)
205 {
206 Criteria criteria = new Criteria();
207 if (!((BaseObject) role).isNew())
208 {
209 criteria.add(ROLE_ID, ((BaseObject) role).getPrimaryKey());
210 }
211 criteria.add(NAME, role.getName());
212
213
214
215 return criteria;
216 }
217
218 /***
219 * Issues an update based on a criteria.
220 *
221 * @param criteria object containing data that is used to create
222 * the UPDATE statement.
223 * @exception TorqueException a generic exception.
224 */
225 public static void doUpdate(Criteria criteria)
226 throws TorqueException
227 {
228 Criteria selectCriteria = new Criteria(2);
229 selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID));
230 BasePeer.doUpdate(selectCriteria, criteria);
231 }
232
233 /***
234 * Checks if a Role is defined in the system. The name
235 * is used as query criteria.
236 *
237 * @param role The Role to be checked.
238 * @return <code>true</code> if given Role exists in the system.
239 * @throws DataBackendException when more than one Role with
240 * the same name exists.
241 * @throws Exception a generic exception.
242 */
243 public static boolean checkExists(Role role)
244 throws DataBackendException, Exception
245 {
246 Criteria criteria = new Criteria();
247 criteria.addSelectColumn(ROLE_ID);
248 criteria.add(NAME, role.getName());
249 List results = BasePeer.doSelect(criteria);
250 if (results.size() > 1)
251 {
252 throw new DataBackendException("Multiple roles named '"
253 + role.getName() + "' exist!");
254 }
255 return (results.size() == 1);
256 }
257
258 /***
259 * Get the name of this table.
260 *
261 * @return A String with the name of the table.
262 */
263 public static String getTableName()
264 {
265 return TABLE_NAME;
266 }
267
268 /***
269 * Returns the full name of a column.
270 *
271 * @param name name of a column
272 * @return A String with the full name of the column.
273 */
274 public static String getColumnName (String name)
275 {
276 StringBuffer sb = new StringBuffer();
277 sb.append(TABLE_NAME);
278 sb.append(".");
279 sb.append(name);
280 return sb.toString();
281 }
282 }