View Javadoc
1 package org.apache.turbine.om.security.peer; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import com.workingdogs.village.Record; 58 import java.util.ArrayList; 59 import java.util.List; 60 import java.util.Map; 61 import org.apache.torque.TorqueException; 62 import org.apache.torque.om.BaseObject; 63 import org.apache.torque.util.BasePeer; 64 import org.apache.torque.util.Criteria; 65 import org.apache.turbine.om.security.Group; 66 import org.apache.turbine.om.security.SecurityObject; 67 import org.apache.turbine.om.security.TurbineGroup; 68 import org.apache.turbine.services.security.TurbineSecurity; 69 import org.apache.turbine.util.ObjectUtils; 70 import org.apache.turbine.util.db.map.TurbineMapBuilder; 71 import org.apache.turbine.util.security.DataBackendException; 72 import org.apache.turbine.util.security.GroupSet; 73 74 /*** 75 * This class handles all the database access for the Group table. 76 * This table contains all the Groups that a given member can play. 77 * 78 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 79 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 80 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 81 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 82 * @version $Id: GroupPeer.java,v 1.3 2002/07/11 07:34:30 mpoeschl Exp $ 83 */ 84 public class GroupPeer extends BasePeer 85 { 86 private static final TurbineMapBuilder mapBuilder = 87 (TurbineMapBuilder) getMapBuilder("org.apache.turbine.util.db.map.TurbineMapBuilder"); 88 89 /*** The table name for this peer. */ 90 private static final String TABLE_NAME = mapBuilder.getTableGroup(); 91 92 /*** The column name for the Group id field. */ 93 public static final String GROUP_ID = mapBuilder.getGroup_GroupId(); 94 95 /*** The column name for the name field. */ 96 public static final String NAME = mapBuilder.getGroup_Name(); 97 98 /*** The column name for the ObjectData field */ 99 public static final String OBJECTDATA = mapBuilder.getGroup_ObjectData(); 100 101 /*** The Oracle sequence name for this peer. */ 102 private static final String SEQUENCE_NAME = mapBuilder.getSequenceGroup(); 103 104 /*** 105 * Retrieves/assembles a GroupSet of all of the Groups. 106 * 107 * @param criteria The criteria to use. 108 * @return A GroupSet. 109 * @exception Exception, a generic exception. 110 */ 111 public static GroupSet retrieveSet() 112 throws Exception 113 { 114 return retrieveSet(new Criteria()); 115 } 116 117 /*** 118 * Retrieves/assembles a GroupSet based on the Criteria passed in 119 */ 120 public static GroupSet retrieveSet(Criteria criteria) throws Exception 121 { 122 List results = GroupPeer.doSelect(criteria); 123 GroupSet rs = new GroupSet(); 124 for (int i = 0; i < results.size(); i++) 125 { 126 rs.add((Group) results.get(i)); 127 } 128 return rs; 129 } 130 131 /*** 132 * Issues a select based on a criteria. 133 * 134 * @param Criteria object containing data that is used to create 135 * the SELECT statement. 136 * @return Vector containing Group objects. 137 * @exception Exception, a generic exception. 138 */ 139 public static List doSelect(Criteria criteria) 140 throws TorqueException 141 { 142 try 143 { 144 criteria.addSelectColumn(GROUP_ID) 145 .addSelectColumn(NAME) 146 .addSelectColumn(OBJECTDATA); 147 148 if (criteria.getOrderByColumns() == null || 149 criteria.getOrderByColumns().size() == 0) 150 { 151 criteria.addAscendingOrderByColumn(NAME); 152 } 153 154 // Place any checks here to intercept criteria which require 155 // custom SQL. For example: 156 // if ( criteria.containsKey("SomeTable.SomeColumn") ) 157 // { 158 // String whereSql = "SomeTable.SomeColumn IN (Select ..."; 159 // criteria.add("SomeTable.SomeColumn", 160 // whereSQL, criteria.CUSTOM); 161 // } 162 163 // BasePeer returns a Vector of Value (Village) arrays. The 164 // array order follows the order columns were placed in the 165 // Select clause. 166 List rows = BasePeer.doSelect(criteria); 167 List results = new ArrayList(); 168 169 // Populate the object(s). 170 for ( int i = 0; i < rows.size(); i++ ) 171 { 172 Group obj = TurbineSecurity.getNewGroup(null); 173 Record row = (Record) rows.get(i); 174 ((SecurityObject) obj).setPrimaryKey(row.getValue(1).asInt()); 175 ((SecurityObject) obj).setName(row.getValue(2).asString()); 176 byte[] objectData = (byte[]) row.getValue(3).asBytes(); 177 Map temp = (Map) ObjectUtils.deserialize(objectData); 178 if (temp != null) 179 { 180 ((SecurityObject) obj).setAttributes(temp); 181 } 182 results.add(obj); 183 } 184 185 return results; 186 } 187 catch (Exception ex) 188 { 189 throw new TorqueException(ex); 190 } 191 } 192 193 /*** 194 * Issues an update based on a criteria. 195 * 196 * @param Criteria object containing data that is used to create 197 * the UPDATE statement. 198 * @exception Exception, a generic exception. 199 */ 200 public static void doUpdate(Criteria criteria) 201 throws TorqueException 202 { 203 Criteria selectCriteria = new Criteria(2); 204 selectCriteria.put( GROUP_ID, criteria.remove(GROUP_ID) ); 205 BasePeer.doUpdate( selectCriteria, criteria ); 206 } 207 208 /*** 209 * Checks if a Group is defined in the system. The name 210 * is used as query criteria. 211 * 212 * @param permission The Group to be checked. 213 * @return <code>true</code> if given Group exists in the system. 214 * @throws DataBackendException when more than one Group with 215 * the same name exists. 216 * @throws Exception, a generic exception. 217 */ 218 public static boolean checkExists( Group group ) 219 throws DataBackendException, Exception 220 { 221 Criteria criteria = new Criteria(); 222 criteria.addSelectColumn(GROUP_ID); 223 criteria.add(NAME, ((SecurityObject)group).getName()); 224 List results = BasePeer.doSelect(criteria); 225 if (results.size() > 1) 226 { 227 throw new DataBackendException("Multiple groups named '" + 228 ((TurbineGroup)group).getName() + "' exist!"); 229 } 230 return (results.size()==1); 231 } 232 233 /*** 234 * Get the name of this table. 235 * 236 * @return A String with the name of the table. 237 */ 238 public static String getTableName() 239 { 240 return TABLE_NAME; 241 } 242 243 /*** 244 * Returns the full name of a column. 245 * 246 * @return A String with the full name of the column. 247 */ 248 public static String getColumnName (String name) 249 { 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 * Builds a criteria object based upon an Group object 259 */ 260 public static Criteria buildCriteria( Group group ) 261 { 262 Criteria criteria = new Criteria(); 263 criteria.add(NAME, ((SecurityObject)group).getName()); 264 if ( !((BaseObject)group).isNew() ) 265 { 266 criteria.add(GROUP_ID, ((BaseObject)group).getPrimaryKey()); 267 } 268 // Causing the removal and updating of a group to 269 // crap out. 270 //criteria.add(OBJECTDATA, group.getAttributes()); 271 return criteria; 272 } 273 }

This page was automatically generated by Maven