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,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 TABLE_NAME = MAP_BUILDER.getTableGroup();
55
56 /*** The column name for the Group id field. */
57 public static final String GROUP_ID = MAP_BUILDER.getGroup_GroupId();
58
59 /*** The column name for the name field. */
60 public static final String NAME = MAP_BUILDER.getGroup_Name();
61
62 /*** The column name for the ObjectData field */
63 public static final String 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
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
84 {
85 List 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 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
117
118
119
120
121
122
123
124
125
126
127
128 List rows = BasePeer.doSelect(criteria);
129 List results = new ArrayList();
130
131
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 temp = (Map) 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 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
183 {
184 Criteria criteria = new Criteria();
185 criteria.addSelectColumn(GROUP_ID);
186 criteria.add(NAME, ((SecurityObject) group).getName());
187 List 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 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 getColumnName(String name)
213 {
214 StringBuffer sb = new StringBuffer();
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
236
237
238 return criteria;
239 }
240 }