001: package org.apache.turbine.om.security.peer;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import com.workingdogs.village.Record;
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Map;
023: import org.apache.torque.TorqueException;
024: import org.apache.torque.om.BaseObject;
025: import org.apache.torque.om.NumberKey;
026: import org.apache.torque.util.BasePeer;
027: import org.apache.torque.util.Criteria;
028: import org.apache.turbine.om.security.Group;
029: import org.apache.turbine.om.security.SecurityObject;
030: import org.apache.turbine.om.security.TurbineGroup;
031: import org.apache.turbine.services.security.TurbineSecurity;
032: import org.apache.turbine.util.ObjectUtils;
033: import org.apache.turbine.util.db.map.TurbineMapBuilder;
034: import org.apache.turbine.util.security.DataBackendException;
035: import org.apache.turbine.util.security.GroupSet;
036:
037: /**
038: * This class handles all the database access for the Group table.
039: * This table contains all the Groups that a given member can play.
040: *
041: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
042: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
043: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
044: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
045: * @version $Id: GroupPeer.java 264148 2005-08-29 14:21:04Z henning $
046: */
047: public class GroupPeer extends BasePeer {
048: /** Serial Version UID */
049: private static final long serialVersionUID = 2902002237040953323L;
050:
051: /** The map builder for this Peer. */
052: private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) getMapBuilder(TurbineMapBuilder.class
053: .getName());
054:
055: /** The table name for this peer. */
056: private static final String TABLE_NAME = MAP_BUILDER
057: .getTableGroup();
058:
059: /** The column name for the Group id field. */
060: public static final String GROUP_ID = MAP_BUILDER
061: .getGroup_GroupId();
062:
063: /** The column name for the name field. */
064: public static final String NAME = MAP_BUILDER.getGroup_Name();
065:
066: /** The column name for the ObjectData field */
067: public static final String OBJECTDATA = MAP_BUILDER
068: .getGroup_ObjectData();
069:
070: /**
071: * Retrieves/assembles a GroupSet of all of the Groups.
072: *
073: * @return A GroupSet.
074: * @exception Exception a generic exception.
075: */
076: public static GroupSet retrieveSet() throws Exception {
077: return retrieveSet(new Criteria());
078: }
079:
080: /**
081: * Retrieves/assembles a GroupSet based on the Criteria passed in
082: *
083: * @param criteria The criteria to use.
084: * @throws Exception a generic exception.
085: * @return a GroupSet
086: */
087: public static GroupSet retrieveSet(Criteria criteria)
088: throws Exception {
089: List results = GroupPeer.doSelect(criteria);
090: GroupSet rs = new GroupSet();
091: for (int i = 0; i < results.size(); i++) {
092: rs.add((Group) results.get(i));
093: }
094: return rs;
095: }
096:
097: /**
098: * Issues a select based on a criteria.
099: *
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)
106: throws TorqueException {
107: try {
108: criteria.addSelectColumn(GROUP_ID).addSelectColumn(NAME)
109: .addSelectColumn(OBJECTDATA);
110:
111: if (criteria.getOrderByColumns() == null
112: || criteria.getOrderByColumns().size() == 0) {
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 rows = BasePeer.doSelect(criteria);
129: List results = new ArrayList();
130:
131: // Populate the object(s).
132: for (int i = 0; i < rows.size(); i++) {
133: Group obj = TurbineSecurity.getGroupInstance(null);
134: Record row = (Record) rows.get(i);
135: ((SecurityObject) obj).setPrimaryKey(new NumberKey(row
136: .getValue(1).asInt()));
137: ((SecurityObject) obj).setName(row.getValue(2)
138: .asString());
139: byte[] objectData = row.getValue(3).asBytes();
140: Map temp = (Map) ObjectUtils.deserialize(objectData);
141: if (temp != null) {
142: ((SecurityObject) obj).setAttributes(temp);
143: }
144: results.add(obj);
145: }
146:
147: return results;
148: } catch (Exception ex) {
149: throw new TorqueException(ex);
150: }
151: }
152:
153: /**
154: * Issues an update based on a criteria.
155: *
156: * @param criteria object containing data that is used to create
157: * the UPDATE statement.
158: * @exception TorqueException a generic exception.
159: */
160: public static void doUpdate(Criteria criteria)
161: throws TorqueException {
162: Criteria selectCriteria = new Criteria(2);
163: selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID));
164: BasePeer.doUpdate(selectCriteria, criteria);
165: }
166:
167: /**
168: * Checks if a Group is defined in the system. The name
169: * is used as query criteria.
170: *
171: * @param group The Group to be checked.
172: * @return <code>true</code> if given Group exists in the system.
173: * @throws DataBackendException when more than one Group with
174: * the same name exists.
175: * @throws Exception a generic exception.
176: */
177: public static boolean checkExists(Group group)
178: throws DataBackendException, Exception {
179: Criteria criteria = new Criteria();
180: criteria.addSelectColumn(GROUP_ID);
181: criteria.add(NAME, ((SecurityObject) group).getName());
182: List results = BasePeer.doSelect(criteria);
183: if (results.size() > 1) {
184: throw new DataBackendException("Multiple groups named '"
185: + ((TurbineGroup) group).getName() + "' exist!");
186: }
187: return (results.size() == 1);
188: }
189:
190: /**
191: * Get the name of this table.
192: *
193: * @return A String with the name of the table.
194: */
195: public static String getTableName() {
196: return TABLE_NAME;
197: }
198:
199: /**
200: * Returns the full name of a column.
201: *
202: * @param name name of the column
203: * @return A String with the full name of the column.
204: */
205: public static String getColumnName(String name) {
206: StringBuffer sb = new StringBuffer();
207: sb.append(TABLE_NAME);
208: sb.append(".");
209: sb.append(name);
210: return sb.toString();
211: }
212:
213: /**
214: * Builds a criteria object based upon an Group object
215: *
216: * @param group object to build the Criteria
217: * @return the Criteria
218: */
219: public static Criteria buildCriteria(Group group) {
220: Criteria criteria = new Criteria();
221: criteria.add(NAME, ((SecurityObject) group).getName());
222: if (!((BaseObject) group).isNew()) {
223: criteria
224: .add(GROUP_ID, ((BaseObject) group).getPrimaryKey());
225: }
226: // Causing the removal and updating of a group to
227: // crap out.
228: //criteria.add(OBJECTDATA, group.getAttributes());
229: return criteria;
230: }
231: }
|