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