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.om.Persistent;
030: import org.apache.torque.util.BasePeer;
031: import org.apache.torque.util.Criteria;
032: import org.apache.turbine.om.security.Group;
033: import org.apache.turbine.om.security.Role;
034: import org.apache.turbine.om.security.TurbineRole;
035: import org.apache.turbine.om.security.User;
036: import org.apache.turbine.util.db.map.TurbineMapBuilder;
037: import org.apache.turbine.util.security.DataBackendException;
038: import org.apache.turbine.util.security.RoleSet;
039:
040: import com.workingdogs.village.Record;
041:
042: /**
043: * This class handles all the database access for the ROLE table.
044: * This table contains all the roles that a given member can play.
045: *
046: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
047: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
048: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
049: *
050: * @deprecated Use {@link org.apache.turbine.services.security.torque.TorqueSecurityService}
051: * instead.
052: *
053: * @version $Id: RolePeer.java 571795 2007-09-01 13:09:35Z tv $
054: */
055: public class RolePeer extends BasePeer {
056: /** Serial Version UID */
057: private static final long serialVersionUID = 8236100811297919996L;
058:
059: /** The map builder for this Peer. */
060: private static final TurbineMapBuilder MAP_BUILDER;
061:
062: /** The table name for this peer. */
063: private static final String TABLE_NAME;
064:
065: /** The column name for the role id field. */
066: public static final String ROLE_ID;
067:
068: /** The column name for the name field. */
069: public static final String NAME;
070:
071: /** The column name for the ObjectData field */
072: public static final String OBJECTDATA;
073:
074: static {
075: try {
076: MAP_BUILDER = (TurbineMapBuilder) Torque
077: .getMapBuilder(TurbineMapBuilder.class.getName());
078: } catch (TorqueException e) {
079: log.error("Could not initialize Peer", e);
080: throw new RuntimeException(e);
081: }
082:
083: TABLE_NAME = MAP_BUILDER.getTableRole();
084: ROLE_ID = MAP_BUILDER.getRole_RoleId();
085: NAME = MAP_BUILDER.getRole_Name();
086: OBJECTDATA = MAP_BUILDER.getRole_ObjectData();
087: }
088:
089: /**
090: * Retrieves/assembles a RoleSet based on the Criteria passed in
091: *
092: * @param criteria The criteria to use.
093: * @return a RoleSet
094: * @exception Exception a generic exception.
095: */
096: public static RoleSet retrieveSet(Criteria criteria)
097: throws Exception {
098: List results = RolePeer.doSelect(criteria);
099: RoleSet rs = new RoleSet();
100: for (int i = 0; i < results.size(); i++) {
101: rs.add((Role) results.get(i));
102: }
103: return rs;
104: }
105:
106: /**
107: * Retrieves a set of Roles that an User was assigned in a Group
108: *
109: * @param user An user.
110: * @param group A group
111: * @return A Set of Roles of this User in the Group
112: * @exception Exception a generic exception.
113: */
114: public static RoleSet retrieveSet(User user, Group group)
115: throws Exception {
116: Criteria criteria = new Criteria();
117: criteria.add(UserGroupRolePeer.USER_ID, ((Persistent) user)
118: .getPrimaryKey());
119: criteria.add(UserGroupRolePeer.GROUP_ID, ((Persistent) group)
120: .getPrimaryKey());
121: criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID);
122: return retrieveSet(criteria);
123: }
124:
125: /**
126: * Issues a select based on a criteria.
127: *
128: * @param criteria object containing data that is used to create
129: * the SELECT statement.
130: * @return Vector containing Role objects.
131: * @exception TorqueException a generic exception.
132: */
133: public static List doSelect(Criteria criteria)
134: throws TorqueException {
135: try {
136: criteria.addSelectColumn(ROLE_ID).addSelectColumn(NAME)
137: .addSelectColumn(OBJECTDATA);
138:
139: if (criteria.getOrderByColumns() == null
140: || criteria.getOrderByColumns().size() == 0) {
141: criteria.addAscendingOrderByColumn(NAME);
142: }
143:
144: // Place any checks here to intercept criteria which require
145: // custom SQL. For example:
146: // if ( criteria.containsKey("SomeTable.SomeColumn") )
147: // {
148: // String whereSql = "SomeTable.SomeColumn IN (Select ...";
149: // criteria.add("SomeTable.SomeColumn",
150: // whereSQL, criteria.CUSTOM);
151: // }
152:
153: // BasePeer returns a Vector of Value (Village) arrays. The
154: // array order follows the order columns were placed in the
155: // Select clause.
156: List rows = BasePeer.doSelect(criteria);
157: List results = new ArrayList();
158:
159: // Populate the object(s).
160: for (int i = 0; i < rows.size(); i++) {
161: //Role obj = new Role();
162: Role obj = new TurbineRole();
163: Record row = (Record) rows.get(i);
164: ((TurbineRole) obj).setPrimaryKey(new NumberKey(row
165: .getValue(1).asInt()));
166: ((TurbineRole) obj).setName(row.getValue(2).asString());
167:
168: // TODO: Clean up OBJECTDATA columns. They are no longer supported
169: /*
170: byte[] objectData = row.getValue(3).asBytes();
171: Map temp = (Map) ObjectUtils.deserialize(objectData);
172: if (temp != null)
173: {
174: ((TurbineRole) obj).setAttributes(temp);
175: }
176: */
177: results.add(obj);
178: }
179:
180: return results;
181: } catch (Exception ex) {
182: throw new TorqueException(ex);
183: }
184: }
185:
186: /**
187: * Builds a criteria object based upon an Role object
188: *
189: * @param role object to build the criteria
190: * @return the Criteria
191: */
192: public static Criteria buildCriteria(Role role) {
193: Criteria criteria = new Criteria();
194: if (!((BaseObject) role).isNew()) {
195: criteria.add(ROLE_ID, ((BaseObject) role).getPrimaryKey());
196: }
197: criteria.add(NAME, role.getName());
198: // causing the removal and updating of roles to
199: // crap out because of the generated SQL.
200: //criteria.add(OBJECTDATA, role.getAttributes());
201: return criteria;
202: }
203:
204: /**
205: * Issues an update based on a criteria.
206: *
207: * @param criteria object containing data that is used to create
208: * the UPDATE statement.
209: * @exception TorqueException a generic exception.
210: */
211: public static void doUpdate(Criteria criteria)
212: throws TorqueException {
213: Criteria selectCriteria = new Criteria(2);
214: selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID));
215: BasePeer.doUpdate(selectCriteria, criteria);
216: }
217:
218: /**
219: * Checks if a Role is defined in the system. The name
220: * is used as query criteria.
221: *
222: * @param role The Role to be checked.
223: * @return <code>true</code> if given Role exists in the system.
224: * @throws DataBackendException when more than one Role with
225: * the same name exists.
226: * @throws Exception a generic exception.
227: */
228: public static boolean checkExists(Role role)
229: throws DataBackendException, Exception {
230: Criteria criteria = new Criteria();
231: criteria.addSelectColumn(ROLE_ID);
232: criteria.add(NAME, role.getName());
233: List results = BasePeer.doSelect(criteria);
234: if (results.size() > 1) {
235: throw new DataBackendException("Multiple roles named '"
236: + role.getName() + "' exist!");
237: }
238: return (results.size() == 1);
239: }
240:
241: /**
242: * Get the name of this table.
243: *
244: * @return A String with the name of the table.
245: */
246: public static String getTableName() {
247: return TABLE_NAME;
248: }
249:
250: /**
251: * Returns the full name of a column.
252: *
253: * @param name name of a column
254: * @return A String with the full name of the column.
255: */
256: public static String getColumnName(String name) {
257: StringBuffer sb = new StringBuffer();
258: sb.append(TABLE_NAME);
259: sb.append(".");
260: sb.append(name);
261: return sb.toString();
262: }
263: }
|