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.util.BasePeer;
30  import org.apache.torque.util.Criteria;
31  import org.apache.turbine.om.security.Group;
32  import org.apache.turbine.om.security.SecurityObject;
33  import org.apache.turbine.om.security.TurbineGroup;
34  import org.apache.turbine.services.security.TurbineSecurity;
35  import org.apache.turbine.util.ObjectUtils;
36  import org.apache.turbine.util.db.map.TurbineMapBuilder;
37  import org.apache.turbine.util.security.DataBackendException;
38  import org.apache.turbine.util.security.GroupSet;
39  
40  import com.workingdogs.village.Record;
41  
42  /***
43   * This class handles all the database access for the Group table.
44   * This table contains all the Groups that a given member can play.
45   *
46   * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
47   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
48   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
49   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
50   *
51   * @deprecated Use {@link org.apache.turbine.services.security.torque.TorqueSecurityService}
52   * instead.
53   *
54   * @version $Id: GroupPeer.java 534527 2007-05-02 16:10:59Z tv $
55   */
56  public class GroupPeer extends BasePeer
57  {
58      /*** Serial Version UID */
59      private static final long serialVersionUID = 2902002237040953323L;
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 Group id field. */
68      public static final String GROUP_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.getTableGroup();
89          GROUP_ID = MAP_BUILDER.getGroup_GroupId();
90          NAME = MAP_BUILDER.getGroup_Name();
91          OBJECTDATA = MAP_BUILDER.getGroup_ObjectData();
92      }
93  
94      /***
95       * Retrieves/assembles a GroupSet of all of the Groups.
96       *
97       * @return A GroupSet.
98       * @exception Exception a generic exception.
99       */
100     public static GroupSet retrieveSet() throws Exception
101     {
102         return retrieveSet(new Criteria());
103     }
104 
105     /***
106      * Retrieves/assembles a GroupSet based on the Criteria passed in
107      *
108      * @param criteria The criteria to use.
109      * @throws Exception a generic exception.
110      * @return a GroupSet
111      */
112     public static GroupSet retrieveSet(Criteria criteria) throws Exception
113     {
114         List results = GroupPeer.doSelect(criteria);
115         GroupSet rs = new GroupSet();
116         for (int i = 0; i < results.size(); i++)
117         {
118             rs.add((Group) results.get(i));
119         }
120         return rs;
121     }
122 
123     /***
124      * Issues a select based on a criteria.
125      *
126      * @param criteria object containing data that is used to create
127      *        the SELECT statement.
128      * @return Vector containing Group objects.
129      * @exception TorqueException a generic exception.
130      */
131     public static List doSelect(Criteria criteria) throws TorqueException
132     {
133         try
134         {
135             criteria.addSelectColumn(GROUP_ID)
136                     .addSelectColumn(NAME)
137                     .addSelectColumn(OBJECTDATA);
138 
139             if (criteria.getOrderByColumns() == null
140                     || criteria.getOrderByColumns().size() == 0)
141             {
142                 criteria.addAscendingOrderByColumn(NAME);
143             }
144 
145             // Place any checks here to intercept criteria which require
146             // custom SQL.  For example:
147             // if ( criteria.containsKey("SomeTable.SomeColumn") )
148             // {
149             //     String whereSql = "SomeTable.SomeColumn IN (Select ...";
150             //     criteria.add("SomeTable.SomeColumn",
151             //                  whereSQL, criteria.CUSTOM);
152             // }
153 
154             // BasePeer returns a Vector of Value (Village) arrays.  The
155             // array order follows the order columns were placed in the
156             // Select clause.
157             List rows = BasePeer.doSelect(criteria);
158             List results = new ArrayList();
159 
160             // Populate the object(s).
161             for (int i = 0; i < rows.size(); i++)
162             {
163                 Group obj = TurbineSecurity.getGroupInstance(null);
164                 Record row = (Record) rows.get(i);
165                 ((SecurityObject) obj).setPrimaryKey(
166                         new NumberKey(row.getValue(1).asInt()));
167                 ((SecurityObject) obj).setName(row.getValue(2).asString());
168                 byte[] objectData = row.getValue(3).asBytes();
169                 Map temp = (Map) ObjectUtils.deserialize(objectData);
170                 if (temp != null)
171                 {
172                     ((SecurityObject) obj).setAttributes(temp);
173                 }
174                 results.add(obj);
175             }
176 
177             return results;
178         }
179         catch (Exception ex)
180         {
181             throw new TorqueException(ex);
182         }
183     }
184 
185     /***
186      * Issues an update based on a criteria.
187      *
188      * @param criteria object containing data that is used to create
189      *        the UPDATE statement.
190      * @exception TorqueException a generic exception.
191      */
192     public static void doUpdate(Criteria criteria)
193         throws TorqueException
194     {
195         Criteria selectCriteria = new Criteria(2);
196         selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID));
197         BasePeer.doUpdate(selectCriteria, criteria);
198     }
199 
200     /***
201      * Checks if a Group is defined in the system. The name
202      * is used as query criteria.
203      *
204      * @param group The Group to be checked.
205      * @return <code>true</code> if given Group exists in the system.
206      * @throws DataBackendException when more than one Group with
207      *         the same name exists.
208      * @throws Exception a generic exception.
209      */
210     public static boolean checkExists(Group group)
211         throws DataBackendException, Exception
212     {
213         Criteria criteria = new Criteria();
214         criteria.addSelectColumn(GROUP_ID);
215         criteria.add(NAME, ((SecurityObject) group).getName());
216         List results = BasePeer.doSelect(criteria);
217         if (results.size() > 1)
218         {
219             throw new DataBackendException("Multiple groups named '"
220                     + ((TurbineGroup) group).getName() + "' exist!");
221         }
222         return (results.size() == 1);
223     }
224 
225     /***
226      * Get the name of this table.
227      *
228      * @return A String with the name of the table.
229      */
230     public static String getTableName()
231     {
232         return TABLE_NAME;
233     }
234 
235     /***
236      * Returns the full name of a column.
237      *
238      * @param name name of the column
239      * @return A String with the full name of the column.
240      */
241     public static String getColumnName(String name)
242     {
243         StringBuffer sb = new StringBuffer();
244         sb.append(TABLE_NAME);
245         sb.append(".");
246         sb.append(name);
247         return sb.toString();
248     }
249 
250     /***
251      * Builds a criteria object based upon an Group object
252      *
253      * @param group object to build the Criteria
254      * @return the Criteria
255      */
256     public static Criteria buildCriteria(Group group)
257     {
258         Criteria criteria = new Criteria();
259         criteria.add(NAME, ((SecurityObject) group).getName());
260         if (!((BaseObject) group).isNew())
261         {
262             criteria.add(GROUP_ID, ((BaseObject) group).getPrimaryKey());
263         }
264         // Causing the removal and updating of a group to
265         // crap out.
266         //criteria.add(OBJECTDATA, group.getAttributes());
267         return criteria;
268     }
269 }