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  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)/* Torque. */getMapBuilder(TurbineMapBuilder.class.getName());
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             // Place any checks here to intercept criteria which require
155             // custom SQL.  For example:
156             // if ( criteria.containsKey("SomeTable.SomeColumn") )
157             // {
158             //     String whereSql = "SomeTable.SomeColumn IN (Select ...";
159             //     criteria.add("SomeTable.SomeColumn",
160             //                  whereSQL, criteria.CUSTOM);
161             // }
162 
163             // BasePeer returns a Vector of Value (Village) arrays.  The
164             // array order follows the order columns were placed in the
165             // Select clause.
166             List rows = BasePeer.doSelect(criteria);
167             List results = new ArrayList();
168 
169             // Populate the object(s).
170             for (int i = 0; i < rows.size(); i++)
171             {
172                 //Role obj = new Role();
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         // causing the removal and updating of roles to
210         // crap out because of the generated SQL.
211         //criteria.add(OBJECTDATA, role.getAttributes());
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 }