View Javadoc

1   package org.apache.turbine.om.security.peer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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             // Place any checks here to intercept criteria which require
154             // custom SQL.  For example:
155             // if ( criteria.containsKey("SomeTable.SomeColumn") )
156             // {
157             //     String whereSql = "SomeTable.SomeColumn IN (Select ...";
158             //     criteria.add("SomeTable.SomeColumn",
159             //                  whereSQL, criteria.CUSTOM);
160             // }
161 
162             // BasePeer returns a Vector of Value (Village) arrays.  The
163             // array order follows the order columns were placed in the
164             // Select clause.
165             List rows = BasePeer.doSelect(criteria);
166             List results = new ArrayList();
167 
168             // Populate the object(s).
169             for (int i = 0; i < rows.size(); i++)
170             {
171                 //Role obj = new Role();
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                 // TODO: Clean up OBJECTDATA columns. They are no longer supported
179                 /*
180                 byte[] objectData = row.getValue(3).asBytes();
181                 Map temp = (Map) ObjectUtils.deserialize(objectData);
182                 if (temp != null)
183                 {
184                     ((TurbineRole) obj).setAttributes(temp);
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         // causing the removal and updating of roles to
213         // crap out because of the generated SQL.
214         //criteria.add(OBJECTDATA, role.getAttributes());
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 }