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 java.util.ArrayList;
020: import java.util.Enumeration;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Vector;
024: import org.apache.torque.TorqueException;
025: import org.apache.torque.om.BaseObject;
026: import org.apache.torque.om.NumberKey;
027: import org.apache.torque.util.BasePeer;
028: import org.apache.torque.util.Criteria;
029: import org.apache.turbine.om.security.Permission;
030: import org.apache.turbine.om.security.Role;
031: import org.apache.turbine.om.security.SecurityObject;
032: import org.apache.turbine.om.security.TurbineRole;
033: import org.apache.turbine.services.security.TurbineSecurity;
034: import org.apache.turbine.util.ObjectUtils;
035: import org.apache.turbine.util.db.map.TurbineMapBuilder;
036: import org.apache.turbine.util.security.DataBackendException;
037: import org.apache.turbine.util.security.PermissionSet;
038: import com.workingdogs.village.Record;
039:
040: /**
041: * This class handles all the database access for the PERMISSION
042: * table. This table contains all the permissions that are used in
043: * the system.
044: *
045: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
046: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
047: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
048: * @version $Id: PermissionPeer.java 264148 2005-08-29 14:21:04Z henning $
049: */
050: public class PermissionPeer extends BasePeer {
051: /** Serial Version UID */
052: private static final long serialVersionUID = 2762005892291909743L;
053:
054: /** The map builder for this Peer. */
055: private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) getMapBuilder(TurbineMapBuilder.class
056: .getName());
057:
058: /** The table name for this peer. */
059: private static final String TABLE_NAME = MAP_BUILDER
060: .getTablePermission();
061:
062: /** The column name for the permission id field. */
063: public static final String PERMISSION_ID = MAP_BUILDER
064: .getPermission_PermissionId();
065:
066: /** The column name for the name field. */
067: public static final String NAME = MAP_BUILDER.getPermission_Name();
068:
069: /** The column name for the ObjectData field */
070: public static final String OBJECTDATA = MAP_BUILDER
071: .getPermission_ObjectData();
072:
073: /**
074: * Retrieves/assembles a PermissionSet
075: *
076: * @param criteria The criteria to use.
077: * @return A PermissionSet.
078: * @exception Exception a generic exception.
079: */
080: public static PermissionSet retrieveSet(Criteria criteria)
081: throws Exception {
082: List results = PermissionPeer.doSelect(criteria);
083: PermissionSet ps = new PermissionSet();
084: for (int i = 0; i < results.size(); i++) {
085: ps.add((Permission) results.get(i));
086: }
087: return ps;
088: }
089:
090: /**
091: * Retrieves a set of Permissions associated with a particular Role.
092: *
093: * @param role The role to query permissions of.
094: * @return A set of permissions associated with the Role.
095: * @exception Exception a generic exception.
096: */
097: public static PermissionSet retrieveSet(Role role) throws Exception {
098: Criteria criteria = new Criteria();
099: criteria.add(RolePermissionPeer.ROLE_ID, ((TurbineRole) role)
100: .getPrimaryKey());
101: criteria.addJoin(RolePermissionPeer.PERMISSION_ID,
102: PermissionPeer.PERMISSION_ID);
103: return retrieveSet(criteria);
104: }
105:
106: /**
107: * Issues a select based on a criteria.
108: *
109: * @param criteria Object containing data that is used to create
110: * the SELECT statement.
111: * @return Vector containing Permission objects.
112: * @exception TorqueException a generic exception.
113: */
114: public static List doSelect(Criteria criteria)
115: throws TorqueException {
116: try {
117: criteria.addSelectColumn(PERMISSION_ID).addSelectColumn(
118: NAME).addSelectColumn(OBJECTDATA);
119:
120: if (criteria.getOrderByColumns() == null
121: || criteria.getOrderByColumns().size() == 0) {
122: criteria.addAscendingOrderByColumn(NAME);
123: }
124:
125: // Place any checks here to intercept criteria which require
126: // custom SQL. For example:
127: // if ( criteria.containsKey("SomeTable.SomeColumn") )
128: // {
129: // String whereSql = "SomeTable.SomeColumn IN (Select ...";
130: // criteria.add("SomeTable.SomeColumn",
131: // whereSQL, criteria.CUSTOM);
132: // }
133:
134: // BasePeer returns a Vector of Value (Village) arrays. The
135: // array order follows the order columns were placed in the
136: // Select clause.
137: List rows = BasePeer.doSelect(criteria);
138: List results = new ArrayList();
139:
140: // Populate the object(s).
141: for (int i = 0; i < rows.size(); i++) {
142: Permission obj = TurbineSecurity
143: .getPermissionInstance(null);
144: Record row = (Record) rows.get(i);
145: ((SecurityObject) obj).setPrimaryKey(new NumberKey(row
146: .getValue(1).asInt()));
147: ((SecurityObject) obj).setName(row.getValue(2)
148: .asString());
149: byte[] objectData = row.getValue(3).asBytes();
150: Map temp = (Map) ObjectUtils.deserialize(objectData);
151: if (temp != null) {
152: ((SecurityObject) obj).setAttributes(temp);
153: }
154: results.add(obj);
155: }
156:
157: return results;
158: } catch (Exception ex) {
159: throw new TorqueException(ex);
160: }
161: }
162:
163: /**
164: * Builds a criteria object based upon an Permission object
165: *
166: * @param permission object to build the criteria
167: * @return the Criteria
168: */
169: public static Criteria buildCriteria(Permission permission) {
170: Criteria criteria = new Criteria();
171: if (!((BaseObject) permission).isNew()) {
172: criteria.add(PERMISSION_ID, ((BaseObject) permission)
173: .getPrimaryKey());
174: }
175: criteria.add(NAME, ((SecurityObject) permission).getName());
176:
177: /*
178: * This is causing the the removal and updating of
179: * a permission to crap out. This addition to the
180: * criteria produces something like:
181: *
182: * where OBJECTDATA = {}
183: *
184: * Is the NAME even necessary. Wouldn't
185: * criteria.add(PERMISSION_ID, N) be enough to
186: * generate a where clause that would remove the
187: * permission?
188: *
189: * criteria.add(OBJECTDATA, permission.getAttributes());
190: */
191: return criteria;
192: }
193:
194: /**
195: * Issues an update based on a criteria.
196: *
197: * @param criteria Object containing data that is used to create
198: * the UPDATE statement.
199: * @exception TorqueException a generic exception.
200: */
201: public static void doUpdate(Criteria criteria)
202: throws TorqueException {
203: Criteria selectCriteria = new Criteria(2);
204: selectCriteria.put(PERMISSION_ID, criteria
205: .remove(PERMISSION_ID));
206: BasePeer.doUpdate(selectCriteria, criteria);
207: }
208:
209: /**
210: * Checks if a Permission is defined in the system. The name
211: * is used as query criteria.
212: *
213: * @param permission The Permission to be checked.
214: * @return <code>true</code> if given Permission exists in the system.
215: * @throws DataBackendException when more than one Permission with
216: * the same name exists.
217: * @throws Exception a generic exception.
218: */
219: public static boolean checkExists(Permission permission)
220: throws DataBackendException, Exception {
221: Criteria criteria = new Criteria();
222: criteria.addSelectColumn(PERMISSION_ID);
223: criteria.add(NAME, ((SecurityObject) permission).getName());
224: List results = BasePeer.doSelect(criteria);
225: if (results.size() > 1) {
226: throw new DataBackendException(
227: "Multiple permissions named '"
228: + ((SecurityObject) permission).getName()
229: + "' exist!");
230: }
231: return (results.size() == 1);
232: }
233:
234: /**
235: * Get the name of this table.
236: *
237: * @return A String with the name of the table.
238: */
239: public static String getTableName() {
240: return TABLE_NAME;
241: }
242:
243: /**
244: * Returns the full name of a column.
245: *
246: * @param name name of a column
247: * @return A String with the full name of the column.
248: */
249: public static String getColumnName(String name) {
250: StringBuffer sb = new StringBuffer();
251: sb.append(TABLE_NAME);
252: sb.append(".");
253: sb.append(name);
254: return sb.toString();
255: }
256:
257: /**
258: * Pass in two Vector's of Permission Objects. It will return a
259: * new Vector with the difference of the two Vectors: C = (A - B).
260: *
261: * @param some Vector B in C = (A - B).
262: * @param all Vector A in C = (A - B).
263: * @return Vector C in C = (A - B).
264: */
265: public static final Vector getDifference(Vector some, Vector all) {
266: Vector clone = (Vector) all.clone();
267: for (Enumeration e = some.elements(); e.hasMoreElements();) {
268: Permission tmp = (Permission) e.nextElement();
269: for (Enumeration f = clone.elements(); f.hasMoreElements();) {
270: Permission tmp2 = (Permission) f.nextElement();
271: if (((BaseObject) tmp).getPrimaryKey() == ((BaseObject) tmp2)
272: .getPrimaryKey()) {
273: clone.removeElement(tmp2);
274: break;
275: }
276: }
277: }
278: return clone;
279: }
280: }
|