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