%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
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; |
|
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,v 1.11.2.2 2004/05/20 03:05:16 seade Exp $ |
|
47 | */ |
|
48 | 0 | public class RolePeer extends BasePeer |
49 | { |
|
50 | /** The map builder for this Peer. */ |
|
51 | 0 | private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) |
52 | 0 | getMapBuilder(TurbineMapBuilder.class.getName()); |
53 | ||
54 | /** The table name for this peer. */ |
|
55 | 0 | private static final String TABLE_NAME = MAP_BUILDER.getTableRole(); |
56 | ||
57 | /** The column name for the role id field. */ |
|
58 | 0 | public static final String ROLE_ID = MAP_BUILDER.getRole_RoleId(); |
59 | ||
60 | /** The column name for the name field. */ |
|
61 | 0 | public static final String NAME = MAP_BUILDER.getRole_Name(); |
62 | ||
63 | /** The column name for the ObjectData field */ |
|
64 | 0 | public static final String 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 |
|
74 | { |
|
75 | 0 | List results = RolePeer.doSelect(criteria); |
76 | 0 | RoleSet rs = new RoleSet(); |
77 | 0 | for (int i = 0; i < results.size(); i++) |
78 | { |
|
79 | 0 | rs.add((Role) results.get(i)); |
80 | } |
|
81 | 0 | 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 |
|
93 | { |
|
94 | 0 | Criteria criteria = new Criteria(); |
95 | 0 | criteria.add(UserGroupRolePeer.USER_ID, |
96 | ((Persistent) user).getPrimaryKey()); |
|
97 | 0 | criteria.add(UserGroupRolePeer.GROUP_ID, |
98 | ((Persistent) group).getPrimaryKey()); |
|
99 | 0 | criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID); |
100 | 0 | 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 doSelect(Criteria criteria) throws TorqueException |
|
112 | { |
|
113 | try |
|
114 | { |
|
115 | 0 | criteria.addSelectColumn(ROLE_ID) |
116 | .addSelectColumn(NAME) |
|
117 | .addSelectColumn(OBJECTDATA); |
|
118 | ||
119 | 0 | if (criteria.getOrderByColumns() == null |
120 | || criteria.getOrderByColumns().size() == 0) |
|
121 | { |
|
122 | 0 | 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 | 0 | List rows = BasePeer.doSelect(criteria); |
138 | 0 | List results = new ArrayList(); |
139 | ||
140 | // Populate the object(s). |
|
141 | 0 | for (int i = 0; i < rows.size(); i++) |
142 | { |
|
143 | //Role obj = new Role(); |
|
144 | 0 | Role obj = new TurbineRole(); |
145 | 0 | Record row = (Record) rows.get(i); |
146 | 0 | ((TurbineRole) obj).setPrimaryKey( |
147 | new NumberKey(row.getValue(1).asInt())); |
|
148 | 0 | ((TurbineRole) obj).setName(row.getValue(2).asString()); |
149 | 0 | byte[] objectData = row.getValue(3).asBytes(); |
150 | 0 | Map temp = (Map) ObjectUtils.deserialize(objectData); |
151 | 0 | if (temp != null) |
152 | { |
|
153 | 0 | ((TurbineRole) obj).setAttributes(temp); |
154 | } |
|
155 | 0 | results.add(obj); |
156 | } |
|
157 | ||
158 | 0 | return results; |
159 | } |
|
160 | 0 | catch (Exception ex) |
161 | { |
|
162 | 0 | 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 | 0 | Criteria criteria = new Criteria(); |
175 | 0 | if (!((BaseObject) role).isNew()) |
176 | { |
|
177 | 0 | criteria.add(ROLE_ID, ((BaseObject) role).getPrimaryKey()); |
178 | } |
|
179 | 0 | 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 | 0 | 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 | 0 | Criteria selectCriteria = new Criteria(2); |
197 | 0 | selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID)); |
198 | 0 | BasePeer.doUpdate(selectCriteria, criteria); |
199 | 0 | } |
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 |
|
213 | { |
|
214 | 0 | Criteria criteria = new Criteria(); |
215 | 0 | criteria.addSelectColumn(ROLE_ID); |
216 | 0 | criteria.add(NAME, role.getName()); |
217 | 0 | List results = BasePeer.doSelect(criteria); |
218 | 0 | if (results.size() > 1) |
219 | { |
|
220 | 0 | throw new DataBackendException("Multiple roles named '" |
221 | + role.getName() + "' exist!"); |
|
222 | } |
|
223 | 0 | 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 getTableName() |
|
232 | { |
|
233 | 0 | 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 getColumnName (String name) |
|
243 | { |
|
244 | 0 | StringBuffer sb = new StringBuffer(); |
245 | 0 | sb.append(TABLE_NAME); |
246 | 0 | sb.append("."); |
247 | 0 | sb.append(name); |
248 | 0 | return sb.toString(); |
249 | } |
|
250 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |