001: package org.apache.turbine.om.security.peer;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.torque.Torque;
026: import org.apache.torque.TorqueException;
027: import org.apache.torque.om.BaseObject;
028: import org.apache.torque.om.NumberKey;
029: import org.apache.torque.util.BasePeer;
030: import org.apache.torque.util.Criteria;
031: import org.apache.turbine.om.security.Group;
032: import org.apache.turbine.om.security.TurbineGroup;
033: import org.apache.turbine.services.security.TurbineSecurity;
034: import org.apache.turbine.util.db.map.TurbineMapBuilder;
035: import org.apache.turbine.util.security.DataBackendException;
036: import org.apache.turbine.util.security.GroupSet;
037:
038: import com.workingdogs.village.Record;
039:
040: /**
041: * This class handles all the database access for the Group table.
042: * This table contains all the Groups that a given member can play.
043: *
044: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
045: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
046: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
047: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
048: *
049: * @deprecated Use {@link org.apache.turbine.services.security.torque.TorqueSecurityService}
050: * instead.
051: *
052: * @version $Id: GroupPeer.java 571795 2007-09-01 13:09:35Z tv $
053: */
054: public class GroupPeer extends BasePeer {
055: /** Serial Version UID */
056: private static final long serialVersionUID = 2902002237040953323L;
057:
058: /** The map builder for this Peer. */
059: private static final TurbineMapBuilder MAP_BUILDER;
060:
061: /** The table name for this peer. */
062: private static final String TABLE_NAME;
063:
064: /** The column name for the Group id field. */
065: public static final String GROUP_ID;
066:
067: /** The column name for the name field. */
068: public static final String NAME;
069:
070: /** The column name for the ObjectData field */
071: public static final String OBJECTDATA;
072:
073: static {
074: try {
075: MAP_BUILDER = (TurbineMapBuilder) Torque
076: .getMapBuilder(TurbineMapBuilder.class.getName());
077: } catch (TorqueException e) {
078: log.error("Could not initialize Peer", e);
079: throw new RuntimeException(e);
080: }
081:
082: TABLE_NAME = MAP_BUILDER.getTableGroup();
083: GROUP_ID = MAP_BUILDER.getGroup_GroupId();
084: NAME = MAP_BUILDER.getGroup_Name();
085: OBJECTDATA = MAP_BUILDER.getGroup_ObjectData();
086: }
087:
088: /**
089: * Retrieves/assembles a GroupSet of all of the Groups.
090: *
091: * @return A GroupSet.
092: * @exception Exception a generic exception.
093: */
094: public static GroupSet retrieveSet() throws Exception {
095: return retrieveSet(new Criteria());
096: }
097:
098: /**
099: * Retrieves/assembles a GroupSet based on the Criteria passed in
100: *
101: * @param criteria The criteria to use.
102: * @throws Exception a generic exception.
103: * @return a GroupSet
104: */
105: public static GroupSet retrieveSet(Criteria criteria)
106: throws Exception {
107: List results = GroupPeer.doSelect(criteria);
108: GroupSet rs = new GroupSet();
109: for (int i = 0; i < results.size(); i++) {
110: rs.add((Group) results.get(i));
111: }
112: return rs;
113: }
114:
115: /**
116: * Issues a select based on a criteria.
117: *
118: * @param criteria object containing data that is used to create
119: * the SELECT statement.
120: * @return Vector containing Group objects.
121: * @exception TorqueException a generic exception.
122: */
123: public static List doSelect(Criteria criteria)
124: throws TorqueException {
125: try {
126: criteria.addSelectColumn(GROUP_ID).addSelectColumn(NAME)
127: .addSelectColumn(OBJECTDATA);
128:
129: if (criteria.getOrderByColumns() == null
130: || criteria.getOrderByColumns().size() == 0) {
131: criteria.addAscendingOrderByColumn(NAME);
132: }
133:
134: // Place any checks here to intercept criteria which require
135: // custom SQL. For example:
136: // if ( criteria.containsKey("SomeTable.SomeColumn") )
137: // {
138: // String whereSql = "SomeTable.SomeColumn IN (Select ...";
139: // criteria.add("SomeTable.SomeColumn",
140: // whereSQL, criteria.CUSTOM);
141: // }
142:
143: // BasePeer returns a Vector of Value (Village) arrays. The
144: // array order follows the order columns were placed in the
145: // Select clause.
146: List rows = BasePeer.doSelect(criteria);
147: List results = new ArrayList();
148:
149: // Populate the object(s).
150: for (int i = 0; i < rows.size(); i++) {
151: Group obj = TurbineSecurity.getGroupInstance(null);
152: Record row = (Record) rows.get(i);
153: ((TurbineGroup) obj).setPrimaryKey(new NumberKey(row
154: .getValue(1).asInt()));
155: ((TurbineGroup) obj)
156: .setName(row.getValue(2).asString());
157:
158: // TODO: Clean up OBJECTDATA columns. They are no longer supported
159: /*
160: byte[] objectData = row.getValue(3).asBytes();
161: Map temp = (Map) ObjectUtils.deserialize(objectData);
162: if (temp != null)
163: {
164: ((TurbineGroup) obj).setAttributes(temp);
165: }
166: */
167:
168: results.add(obj);
169: }
170:
171: return results;
172: } catch (Exception ex) {
173: throw new TorqueException(ex);
174: }
175: }
176:
177: /**
178: * Issues an update based on a criteria.
179: *
180: * @param criteria object containing data that is used to create
181: * the UPDATE statement.
182: * @exception TorqueException a generic exception.
183: */
184: public static void doUpdate(Criteria criteria)
185: throws TorqueException {
186: Criteria selectCriteria = new Criteria(2);
187: selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID));
188: BasePeer.doUpdate(selectCriteria, criteria);
189: }
190:
191: /**
192: * Checks if a Group is defined in the system. The name
193: * is used as query criteria.
194: *
195: * @param group The Group to be checked.
196: * @return <code>true</code> if given Group exists in the system.
197: * @throws DataBackendException when more than one Group with
198: * the same name exists.
199: * @throws Exception a generic exception.
200: */
201: public static boolean checkExists(Group group)
202: throws DataBackendException, Exception {
203: Criteria criteria = new Criteria();
204: criteria.addSelectColumn(GROUP_ID);
205: criteria.add(NAME, ((TurbineGroup) group).getName());
206: List results = BasePeer.doSelect(criteria);
207: if (results.size() > 1) {
208: throw new DataBackendException("Multiple groups named '"
209: + ((TurbineGroup) group).getName() + "' exist!");
210: }
211: return (results.size() == 1);
212: }
213:
214: /**
215: * Get the name of this table.
216: *
217: * @return A String with the name of the table.
218: */
219: public static String getTableName() {
220: return TABLE_NAME;
221: }
222:
223: /**
224: * Returns the full name of a column.
225: *
226: * @param name name of the column
227: * @return A String with the full name of the column.
228: */
229: public static String getColumnName(String name) {
230: StringBuffer sb = new StringBuffer();
231: sb.append(TABLE_NAME);
232: sb.append(".");
233: sb.append(name);
234: return sb.toString();
235: }
236:
237: /**
238: * Builds a criteria object based upon an Group object
239: *
240: * @param group object to build the Criteria
241: * @return the Criteria
242: */
243: public static Criteria buildCriteria(Group group) {
244: Criteria criteria = new Criteria();
245: criteria.add(NAME, ((TurbineGroup) group).getName());
246: if (!((BaseObject) group).isNew()) {
247: criteria
248: .add(GROUP_ID, ((BaseObject) group).getPrimaryKey());
249: }
250: // Causing the removal and updating of a group to
251: // crap out.
252: //criteria.add(OBJECTDATA, group.getAttributes());
253: return criteria;
254: }
255: }
|