%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.om.security.peer.RolePeer |
|
|
1 | package org.apache.turbine.om.security.peer; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 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; |
|
20 | import java.util.List; |
|
21 | import java.util.Map; |
|
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 264148 2005-08-29 14:21:04Z henning $ |
|
47 | */ |
|
48 | 0 | public class RolePeer extends BasePeer |
49 | { |
|
50 | /** Serial Version UID */ |
|
51 | private static final long serialVersionUID = 8236100811297919996L; |
|
52 | ||
53 | /** The map builder for this Peer. */ |
|
54 | 0 | private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) |
55 | 0 | getMapBuilder(TurbineMapBuilder.class.getName()); |
56 | ||
57 | /** The table name for this peer. */ |
|
58 | 0 | private static final String TABLE_NAME = MAP_BUILDER.getTableRole(); |
59 | ||
60 | /** The column name for the role id field. */ |
|
61 | 0 | public static final String ROLE_ID = MAP_BUILDER.getRole_RoleId(); |
62 | ||
63 | /** The column name for the name field. */ |
|
64 | 0 | public static final String NAME = MAP_BUILDER.getRole_Name(); |
65 | ||
66 | /** The column name for the ObjectData field */ |
|
67 | 0 | public static final String OBJECTDATA = MAP_BUILDER.getRole_ObjectData(); |
68 | ||
69 | /** |
|
70 | * Retrieves/assembles a RoleSet based on the Criteria passed in |
|
71 | * |
|
72 | * @param criteria The criteria to use. |
|
73 | * @return a RoleSet |
|
74 | * @exception Exception a generic exception. |
|
75 | */ |
|
76 | public static RoleSet retrieveSet(Criteria criteria) throws Exception |
|
77 | { |
|
78 | 0 | List results = RolePeer.doSelect(criteria); |
79 | 0 | RoleSet rs = new RoleSet(); |
80 | 0 | for (int i = 0; i < results.size(); i++) |
81 | { |
|
82 | 0 | rs.add((Role) results.get(i)); |
83 | } |
|
84 | 0 | return rs; |
85 | } |
|
86 | ||
87 | /** |
|
88 | * Retrieves a set of Roles that an User was assigned in a Group |
|
89 | * |
|
90 | * @param user An user. |
|
91 | * @param group A group |
|
92 | * @return A Set of Roles of this User in the Group |
|
93 | * @exception Exception a generic exception. |
|
94 | */ |
|
95 | public static RoleSet retrieveSet(User user, Group group) throws Exception |
|
96 | { |
|
97 | 0 | Criteria criteria = new Criteria(); |
98 | 0 | criteria.add(UserGroupRolePeer.USER_ID, |
99 | ((Persistent) user).getPrimaryKey()); |
|
100 | 0 | criteria.add(UserGroupRolePeer.GROUP_ID, |
101 | ((Persistent) group).getPrimaryKey()); |
|
102 | 0 | criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID); |
103 | 0 | return retrieveSet(criteria); |
104 | } |
|
105 | ||
106 | /** |
|
107 | * Issues a select based on a criteria. |
|
108 | * |
|
109 | * @param criteria object containing data that is used to create |
|
110 | * the SELECT statement. |
|
111 | * @return Vector containing Role objects. |
|
112 | * @exception TorqueException a generic exception. |
|
113 | */ |
|
114 | public static List doSelect(Criteria criteria) throws TorqueException |
|
115 | { |
|
116 | try |
|
117 | { |
|
118 | 0 | criteria.addSelectColumn(ROLE_ID) |
119 | .addSelectColumn(NAME) |
|
120 | .addSelectColumn(OBJECTDATA); |
|
121 | ||
122 | 0 | if (criteria.getOrderByColumns() == null |
123 | || criteria.getOrderByColumns().size() == 0) |
|
124 | { |
|
125 | 0 | criteria.addAscendingOrderByColumn(NAME); |
126 | } |
|
127 | ||
128 | // Place any checks here to intercept criteria which require |
|
129 | // custom SQL. For example: |
|
130 | // if ( criteria.containsKey("SomeTable.SomeColumn") ) |
|
131 | // { |
|
132 | // String whereSql = "SomeTable.SomeColumn IN (Select ..."; |
|
133 | // criteria.add("SomeTable.SomeColumn", |
|
134 | // whereSQL, criteria.CUSTOM); |
|
135 | // } |
|
136 | ||
137 | // BasePeer returns a Vector of Value (Village) arrays. The |
|
138 | // array order follows the order columns were placed in the |
|
139 | // Select clause. |
|
140 | 0 | List rows = BasePeer.doSelect(criteria); |
141 | 0 | List results = new ArrayList(); |
142 | ||
143 | // Populate the object(s). |
|
144 | 0 | for (int i = 0; i < rows.size(); i++) |
145 | { |
|
146 | //Role obj = new Role(); |
|
147 | 0 | Role obj = new TurbineRole(); |
148 | 0 | Record row = (Record) rows.get(i); |
149 | 0 | ((TurbineRole) obj).setPrimaryKey( |
150 | new NumberKey(row.getValue(1).asInt())); |
|
151 | 0 | ((TurbineRole) obj).setName(row.getValue(2).asString()); |
152 | 0 | byte[] objectData = row.getValue(3).asBytes(); |
153 | 0 | Map temp = (Map) ObjectUtils.deserialize(objectData); |
154 | 0 | if (temp != null) |
155 | { |
|
156 | 0 | ((TurbineRole) obj).setAttributes(temp); |
157 | } |
|
158 | 0 | results.add(obj); |
159 | } |
|
160 | ||
161 | 0 | return results; |
162 | } |
|
163 | 0 | catch (Exception ex) |
164 | { |
|
165 | 0 | throw new TorqueException (ex); |
166 | } |
|
167 | } |
|
168 | ||
169 | /** |
|
170 | * Builds a criteria object based upon an Role object |
|
171 | * |
|
172 | * @param role object to build the criteria |
|
173 | * @return the Criteria |
|
174 | */ |
|
175 | public static Criteria buildCriteria(Role role) |
|
176 | { |
|
177 | 0 | Criteria criteria = new Criteria(); |
178 | 0 | if (!((BaseObject) role).isNew()) |
179 | { |
|
180 | 0 | criteria.add(ROLE_ID, ((BaseObject) role).getPrimaryKey()); |
181 | } |
|
182 | 0 | criteria.add(NAME, role.getName()); |
183 | // causing the removal and updating of roles to |
|
184 | // crap out because of the generated SQL. |
|
185 | //criteria.add(OBJECTDATA, role.getAttributes()); |
|
186 | 0 | return criteria; |
187 | } |
|
188 | ||
189 | /** |
|
190 | * Issues an update based on a criteria. |
|
191 | * |
|
192 | * @param criteria object containing data that is used to create |
|
193 | * the UPDATE statement. |
|
194 | * @exception TorqueException a generic exception. |
|
195 | */ |
|
196 | public static void doUpdate(Criteria criteria) |
|
197 | throws TorqueException |
|
198 | { |
|
199 | 0 | Criteria selectCriteria = new Criteria(2); |
200 | 0 | selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID)); |
201 | 0 | BasePeer.doUpdate(selectCriteria, criteria); |
202 | 0 | } |
203 | ||
204 | /** |
|
205 | * Checks if a Role is defined in the system. The name |
|
206 | * is used as query criteria. |
|
207 | * |
|
208 | * @param role The Role to be checked. |
|
209 | * @return <code>true</code> if given Role exists in the system. |
|
210 | * @throws DataBackendException when more than one Role with |
|
211 | * the same name exists. |
|
212 | * @throws Exception a generic exception. |
|
213 | */ |
|
214 | public static boolean checkExists(Role role) |
|
215 | throws DataBackendException, Exception |
|
216 | { |
|
217 | 0 | Criteria criteria = new Criteria(); |
218 | 0 | criteria.addSelectColumn(ROLE_ID); |
219 | 0 | criteria.add(NAME, role.getName()); |
220 | 0 | List results = BasePeer.doSelect(criteria); |
221 | 0 | if (results.size() > 1) |
222 | { |
|
223 | 0 | throw new DataBackendException("Multiple roles named '" |
224 | + role.getName() + "' exist!"); |
|
225 | } |
|
226 | 0 | return (results.size() == 1); |
227 | } |
|
228 | ||
229 | /** |
|
230 | * Get the name of this table. |
|
231 | * |
|
232 | * @return A String with the name of the table. |
|
233 | */ |
|
234 | public static String getTableName() |
|
235 | { |
|
236 | 0 | return TABLE_NAME; |
237 | } |
|
238 | ||
239 | /** |
|
240 | * Returns the full name of a column. |
|
241 | * |
|
242 | * @param name name of a column |
|
243 | * @return A String with the full name of the column. |
|
244 | */ |
|
245 | public static String getColumnName (String name) |
|
246 | { |
|
247 | 0 | StringBuffer sb = new StringBuffer(); |
248 | 0 | sb.append(TABLE_NAME); |
249 | 0 | sb.append("."); |
250 | 0 | sb.append(name); |
251 | 0 | return sb.toString(); |
252 | } |
|
253 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |