1 package org.apache.turbine.om.security.peer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import com.workingdogs.village.Record;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
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 264148 2005-08-29 14:21:04Z henning $
46 */
47 public class GroupPeer extends BasePeer
48 {
49 /*** Serial Version UID */
50 private static final long serialVersionUID = 2902002237040953323L;
51
52 /*** The map builder for this Peer. */
53 private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder)
54 getMapBuilder(TurbineMapBuilder.class.getName());
55
56 /*** The table name for this peer. */
57 private static final String TABLE_NAME = MAP_BUILDER.getTableGroup();
58
59 /*** The column name for the Group id field. */
60 public static final String GROUP_ID = MAP_BUILDER.getGroup_GroupId();
61
62 /*** The column name for the name field. */
63 public static final String NAME = MAP_BUILDER.getGroup_Name();
64
65 /*** The column name for the ObjectData field */
66 public static final String OBJECTDATA = MAP_BUILDER.getGroup_ObjectData();
67
68 /***
69 * Retrieves/assembles a GroupSet of all of the Groups.
70 *
71 * @return A GroupSet.
72 * @exception Exception a generic exception.
73 */
74 public static GroupSet retrieveSet() throws Exception
75 {
76 return retrieveSet(new Criteria());
77 }
78
79 /***
80 * Retrieves/assembles a GroupSet based on the Criteria passed in
81 *
82 * @param criteria The criteria to use.
83 * @throws Exception a generic exception.
84 * @return a GroupSet
85 */
86 public static GroupSet retrieveSet(Criteria criteria) throws Exception
87 {
88 List results = GroupPeer.doSelect(criteria);
89 GroupSet rs = new GroupSet();
90 for (int i = 0; i < results.size(); i++)
91 {
92 rs.add((Group) results.get(i));
93 }
94 return rs;
95 }
96
97 /***
98 * Issues a select based on a criteria.
99 *
100 * @param criteria object containing data that is used to create
101 * the SELECT statement.
102 * @return Vector containing Group objects.
103 * @exception TorqueException a generic exception.
104 */
105 public static List doSelect(Criteria criteria) throws TorqueException
106 {
107 try
108 {
109 criteria.addSelectColumn(GROUP_ID)
110 .addSelectColumn(NAME)
111 .addSelectColumn(OBJECTDATA);
112
113 if (criteria.getOrderByColumns() == null
114 || criteria.getOrderByColumns().size() == 0)
115 {
116 criteria.addAscendingOrderByColumn(NAME);
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131 List rows = BasePeer.doSelect(criteria);
132 List results = new ArrayList();
133
134
135 for (int i = 0; i < rows.size(); i++)
136 {
137 Group obj = TurbineSecurity.getGroupInstance(null);
138 Record row = (Record) rows.get(i);
139 ((SecurityObject) obj).setPrimaryKey(
140 new NumberKey(row.getValue(1).asInt()));
141 ((SecurityObject) obj).setName(row.getValue(2).asString());
142 byte[] objectData = row.getValue(3).asBytes();
143 Map temp = (Map) ObjectUtils.deserialize(objectData);
144 if (temp != null)
145 {
146 ((SecurityObject) obj).setAttributes(temp);
147 }
148 results.add(obj);
149 }
150
151 return results;
152 }
153 catch (Exception ex)
154 {
155 throw new TorqueException(ex);
156 }
157 }
158
159 /***
160 * Issues an update based on a criteria.
161 *
162 * @param criteria object containing data that is used to create
163 * the UPDATE statement.
164 * @exception TorqueException a generic exception.
165 */
166 public static void doUpdate(Criteria criteria)
167 throws TorqueException
168 {
169 Criteria selectCriteria = new Criteria(2);
170 selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID));
171 BasePeer.doUpdate(selectCriteria, criteria);
172 }
173
174 /***
175 * Checks if a Group is defined in the system. The name
176 * is used as query criteria.
177 *
178 * @param group The Group to be checked.
179 * @return <code>true</code> if given Group exists in the system.
180 * @throws DataBackendException when more than one Group with
181 * the same name exists.
182 * @throws Exception a generic exception.
183 */
184 public static boolean checkExists(Group group)
185 throws DataBackendException, Exception
186 {
187 Criteria criteria = new Criteria();
188 criteria.addSelectColumn(GROUP_ID);
189 criteria.add(NAME, ((SecurityObject) group).getName());
190 List results = BasePeer.doSelect(criteria);
191 if (results.size() > 1)
192 {
193 throw new DataBackendException("Multiple groups named '"
194 + ((TurbineGroup) group).getName() + "' exist!");
195 }
196 return (results.size() == 1);
197 }
198
199 /***
200 * Get the name of this table.
201 *
202 * @return A String with the name of the table.
203 */
204 public static String getTableName()
205 {
206 return TABLE_NAME;
207 }
208
209 /***
210 * Returns the full name of a column.
211 *
212 * @param name name of the column
213 * @return A String with the full name of the column.
214 */
215 public static String getColumnName(String name)
216 {
217 StringBuffer sb = new StringBuffer();
218 sb.append(TABLE_NAME);
219 sb.append(".");
220 sb.append(name);
221 return sb.toString();
222 }
223
224 /***
225 * Builds a criteria object based upon an Group object
226 *
227 * @param group object to build the Criteria
228 * @return the Criteria
229 */
230 public static Criteria buildCriteria(Group group)
231 {
232 Criteria criteria = new Criteria();
233 criteria.add(NAME, ((SecurityObject) group).getName());
234 if (!((BaseObject) group).isNew())
235 {
236 criteria.add(GROUP_ID, ((BaseObject) group).getPrimaryKey());
237 }
238
239
240
241 return criteria;
242 }
243 }