KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > om > security > peer > GroupPeer


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 com.workingdogs.village.Record;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import org.apache.torque.TorqueException;
24 import org.apache.torque.om.BaseObject;
25 import org.apache.torque.om.NumberKey;
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.SecurityObject;
30 import org.apache.turbine.om.security.TurbineGroup;
31 import org.apache.turbine.services.security.TurbineSecurity;
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.GroupSet;
36
37 /**
38  * This class handles all the database access for the Group table.
39  * This table contains all the Groups that a given member can play.
40  *
41  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
42  * @author <a HREF="mailto:john.mcnally@clearink.com">John D. McNally</a>
43  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
44  * @author <a HREF="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
45  * @version $Id: GroupPeer.java,v 1.11.2.2 2004/05/20 03:05:16 seade Exp $
46  */

47 public class GroupPeer extends BasePeer
48 {
49     /** The map builder for this Peer. */
50     private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder)
51             getMapBuilder(TurbineMapBuilder.class.getName());
52
53     /** The table name for this peer. */
54     private static final String JavaDoc TABLE_NAME = MAP_BUILDER.getTableGroup();
55
56     /** The column name for the Group id field. */
57     public static final String JavaDoc GROUP_ID = MAP_BUILDER.getGroup_GroupId();
58
59     /** The column name for the name field. */
60     public static final String JavaDoc NAME = MAP_BUILDER.getGroup_Name();
61
62     /** The column name for the ObjectData field */
63     public static final String JavaDoc OBJECTDATA = MAP_BUILDER.getGroup_ObjectData();
64
65     /**
66      * Retrieves/assembles a GroupSet of all of the Groups.
67      *
68      * @return A GroupSet.
69      * @exception Exception a generic exception.
70      */

71     public static GroupSet retrieveSet() throws Exception JavaDoc
72     {
73         return retrieveSet(new Criteria());
74     }
75
76     /**
77      * Retrieves/assembles a GroupSet based on the Criteria passed in
78      *
79      * @param criteria The criteria to use.
80      * @throws Exception a generic exception.
81      * @return a GroupSet
82      */

83     public static GroupSet retrieveSet(Criteria criteria) throws Exception JavaDoc
84     {
85         List JavaDoc results = GroupPeer.doSelect(criteria);
86         GroupSet rs = new GroupSet();
87         for (int i = 0; i < results.size(); i++)
88         {
89             rs.add((Group) results.get(i));
90         }
91         return rs;
92     }
93
94     /**
95      * Issues a select based on a criteria.
96      *
97      * @param criteria object containing data that is used to create
98      * the SELECT statement.
99      * @return Vector containing Group objects.
100      * @exception TorqueException a generic exception.
101      */

102     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
103     {
104         try
105         {
106             criteria.addSelectColumn(GROUP_ID)
107                     .addSelectColumn(NAME)
108                     .addSelectColumn(OBJECTDATA);
109
110             if (criteria.getOrderByColumns() == null
111                     || criteria.getOrderByColumns().size() == 0)
112             {
113                 criteria.addAscendingOrderByColumn(NAME);
114             }
115
116             // Place any checks here to intercept criteria which require
117
// custom SQL. For example:
118
// if ( criteria.containsKey("SomeTable.SomeColumn") )
119
// {
120
// String whereSql = "SomeTable.SomeColumn IN (Select ...";
121
// criteria.add("SomeTable.SomeColumn",
122
// whereSQL, criteria.CUSTOM);
123
// }
124

125             // BasePeer returns a Vector of Value (Village) arrays. The
126
// array order follows the order columns were placed in the
127
// Select clause.
128
List JavaDoc rows = BasePeer.doSelect(criteria);
129             List JavaDoc results = new ArrayList JavaDoc();
130
131             // Populate the object(s).
132
for (int i = 0; i < rows.size(); i++)
133             {
134                 Group obj = TurbineSecurity.getGroupInstance(null);
135                 Record row = (Record) rows.get(i);
136                 ((SecurityObject) obj).setPrimaryKey(
137                         new NumberKey(row.getValue(1).asInt()));
138                 ((SecurityObject) obj).setName(row.getValue(2).asString());
139                 byte[] objectData = row.getValue(3).asBytes();
140                 Map JavaDoc temp = (Map JavaDoc) ObjectUtils.deserialize(objectData);
141                 if (temp != null)
142                 {
143                     ((SecurityObject) obj).setAttributes(temp);
144                 }
145                 results.add(obj);
146             }
147
148             return results;
149         }
150         catch (Exception JavaDoc ex)
151         {
152             throw new TorqueException(ex);
153         }
154     }
155
156     /**
157      * Issues an update based on a criteria.
158      *
159      * @param criteria object containing data that is used to create
160      * the UPDATE statement.
161      * @exception TorqueException a generic exception.
162      */

163     public static void doUpdate(Criteria criteria)
164         throws TorqueException
165     {
166         Criteria selectCriteria = new Criteria(2);
167         selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID));
168         BasePeer.doUpdate(selectCriteria, criteria);
169     }
170
171     /**
172      * Checks if a Group is defined in the system. The name
173      * is used as query criteria.
174      *
175      * @param group The Group to be checked.
176      * @return <code>true</code> if given Group exists in the system.
177      * @throws DataBackendException when more than one Group with
178      * the same name exists.
179      * @throws Exception a generic exception.
180      */

181     public static boolean checkExists(Group group)
182         throws DataBackendException, Exception JavaDoc
183     {
184         Criteria criteria = new Criteria();
185         criteria.addSelectColumn(GROUP_ID);
186         criteria.add(NAME, ((SecurityObject) group).getName());
187         List JavaDoc results = BasePeer.doSelect(criteria);
188         if (results.size() > 1)
189         {
190             throw new DataBackendException("Multiple groups named '"
191                     + ((TurbineGroup) group).getName() + "' exist!");
192         }
193         return (results.size() == 1);
194     }
195
196     /**
197      * Get the name of this table.
198      *
199      * @return A String with the name of the table.
200      */

201     public static String JavaDoc getTableName()
202     {
203         return TABLE_NAME;
204     }
205
206     /**
207      * Returns the full name of a column.
208      *
209      * @param name name of the column
210      * @return A String with the full name of the column.
211      */

212     public static String JavaDoc getColumnName(String JavaDoc name)
213     {
214         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
215         sb.append(TABLE_NAME);
216         sb.append(".");
217         sb.append(name);
218         return sb.toString();
219     }
220
221     /**
222      * Builds a criteria object based upon an Group object
223      *
224      * @param group object to build the Criteria
225      * @return the Criteria
226      */

227     public static Criteria buildCriteria(Group group)
228     {
229         Criteria criteria = new Criteria();
230         criteria.add(NAME, ((SecurityObject) group).getName());
231         if (!((BaseObject) group).isNew())
232         {
233             criteria.add(GROUP_ID, ((BaseObject) group).getPrimaryKey());
234         }
235         // Causing the removal and updating of a group to
236
// crap out.
237
//criteria.add(OBJECTDATA, group.getAttributes());
238
return criteria;
239     }
240 }
241
Popular Tags