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 com.workingdogs.village.Schema; 59 import com.workingdogs.village.Value; 60 import java.sql.Connection; 61 import java.util.ArrayList; 62 import java.util.Hashtable; 63 import java.util.List; 64 import org.apache.torque.TorqueException; 65 import org.apache.torque.map.TableMap; 66 import org.apache.torque.om.NumberKey; 67 import org.apache.torque.om.Persistent; 68 import org.apache.torque.util.BasePeer; 69 import org.apache.torque.util.Criteria; 70 import org.apache.turbine.om.security.User; 71 import org.apache.turbine.services.security.TurbineSecurity; 72 import org.apache.turbine.util.ObjectUtils; 73 import org.apache.turbine.util.db.map.TurbineMapBuilder; 74 import org.apache.turbine.util.security.DataBackendException; 75 76 77 /*** 78 * This class handles all the database access for the User/User 79 * table. This table contains all the information for a given user. 80 * 81 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 82 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 83 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 84 * @version $Id: TurbineUserPeer.java,v 1.3 2002/07/11 07:34:30 mpoeschl Exp $ 85 */ 86 public class TurbineUserPeer extends BasePeer implements UserPeer 87 { 88 /*** The mapBuilder for this Peer. */ 89 private static final TurbineMapBuilder mapBuilder = 90 (TurbineMapBuilder) getMapBuilder("org.apache.turbine.util.db.map.TurbineMapBuilder"); 91 92 // column names 93 /*** The column name for the visitor id field. */ 94 private static final String USER_ID_COLUMN = mapBuilder.getUserId(); 95 96 /*** The column name for the login name field */ 97 private static final String USERNAME_COLUMN = mapBuilder.getUsername(); 98 99 /*** The key name for the first name field. */ 100 private static final String FIRST_NAME_COLUMN = mapBuilder.getFirstName(); 101 102 /*** The key name for the last name field. */ 103 private static final String LAST_NAME_COLUMN = mapBuilder.getLastName(); 104 105 /*** The column name for the modified field. */ 106 private static final String MODIFIED_COLUMN = mapBuilder.getModified(); 107 108 /*** The column name for the created field. */ 109 private static final String CREATED_COLUMN = mapBuilder.getCreated(); 110 111 /*** The column name for the last_login field. */ 112 private static final String LAST_LOGIN_COLUMN = mapBuilder.getLastLogin(); 113 114 /*** The column name for the email field. */ 115 private static final String EMAIL_COLUMN = mapBuilder.getEmail(); 116 117 /*** The column name for the confirm_value field. */ 118 private static final String CONFIRM_VALUE_COLUMN = 119 mapBuilder.getConfirmValue(); 120 121 /*** This is the value that is stored in the database for confirmed users. */ 122 public static final String CONFIRM_DATA = 123 org.apache.turbine.om.security.User.CONFIRM_DATA; 124 125 /*** The column name for the visitor id field. */ 126 private static final String OBJECT_DATA_COLUMN = mapBuilder.getObjectData(); 127 128 /*** The table name for this peer. */ 129 private static final String TABLE_NAME = mapBuilder.getTableUser(); 130 131 // Criteria Keys 132 /*** The key name for the visitor id field. */ 133 public static final String USER_ID = mapBuilder.getUser_UserId(); 134 135 /*** The key name for the username field. */ 136 public static final String USERNAME = mapBuilder.getUser_Username(); 137 138 /*** The key name for the password field. */ 139 public static final String PASSWORD = mapBuilder.getUser_Password(); 140 141 /*** The key name for the first name field. */ 142 public static final String FIRST_NAME = mapBuilder.getUser_FirstName(); 143 144 /*** The key name for the last name field. */ 145 public static final String LAST_NAME = mapBuilder.getUser_LastName(); 146 147 /*** The key name for the modified field. */ 148 public static final String MODIFIED = mapBuilder.getUser_Modified(); 149 150 /*** The key name for the created field. */ 151 public static final String CREATED = mapBuilder.getUser_Created(); 152 153 /*** The key name for the email field. */ 154 public static final String EMAIL = mapBuilder.getUser_Email(); 155 156 /*** The key name for the last_login field. */ 157 public static final String LAST_LOGIN = mapBuilder.getUser_LastLogin(); 158 159 /*** The key name for the confirm_value field. */ 160 public static final String CONFIRM_VALUE = 161 mapBuilder.getUser_ConfirmValue(); 162 163 /*** The key name for the object_data field. */ 164 public static final String OBJECT_DATA = mapBuilder.getUser_ObjectData(); 165 166 /*** The Oracle sequence name for this peer. */ 167 private static final String SEQUENCE_NAME = mapBuilder.getSequenceUser(); 168 169 /*** The schema. */ 170 private static Schema schema = initTableSchema(TABLE_NAME); 171 172 /*** The columns. */ 173 private static com.workingdogs.village.Column[] columns = 174 initTableColumns(schema); 175 176 /*** The names of the columns. */ 177 public static String[] columnNames = initColumnNames(columns); 178 179 /*** The keys for the criteria. */ 180 public static String[] criteriaKeys = 181 initCriteriaKeys(TABLE_NAME, columnNames); 182 183 184 /*** 185 * Get the name of this table. 186 * 187 * @return A String with the name of the table. 188 */ 189 public static String getTableName() 190 { 191 return TABLE_NAME; 192 } 193 194 /*** 195 * Returns the full name of a column. 196 * 197 * @return A String with the full name of the column. 198 */ 199 public static String getColumnName (String name) 200 { 201 StringBuffer sb = new StringBuffer(); 202 sb.append (TABLE_NAME); 203 sb.append ("."); 204 sb.append (name); 205 return sb.toString(); 206 } 207 208 /*** 209 * 210 * Returns the full name of a column. 211 * 212 * @return A String with the full name of the column. 213 */ 214 public String getFullColumnName (String name) 215 { 216 StringBuffer sb = new StringBuffer(); 217 sb.append (TABLE_NAME); 218 sb.append ("."); 219 sb.append (name); 220 return sb.toString(); 221 } 222 223 /*** 224 * Builds a criteria object based upon an User object 225 */ 226 public static Criteria buildCriteria(User user) 227 { 228 Hashtable permData = (Hashtable) user.getPermStorage().clone(); 229 Criteria criteria = new Criteria(); 230 if ( !((Persistent)user).isNew() ) 231 { 232 criteria.add(USER_ID, ((Persistent)user).getPrimaryKey()); 233 } 234 235 for (int i=1; i<TurbineUserPeer.columnNames.length; i++ ) 236 { 237 if ( permData.containsKey(TurbineUserPeer.columnNames[i]) ) 238 { 239 criteria.add( TurbineUserPeer.criteriaKeys[i], 240 permData.remove(TurbineUserPeer.columnNames[i]) ); 241 } 242 } 243 criteria.add( TurbineUserPeer.OBJECT_DATA, permData ); 244 return criteria; 245 } 246 247 /*** Add all the columns needed to create a new object */ 248 public static void addSelectColumns (Criteria criteria) 249 throws TorqueException 250 { 251 for( int i=0; i<columnNames.length; i++ ) 252 { 253 criteria.addSelectColumn(new StringBuffer() 254 .append(TABLE_NAME) 255 .append(".") 256 .append(columnNames[i]).toString() ); 257 } 258 } 259 260 public static void populateObject(Record row, int offset, User obj) 261 throws TorqueException 262 { 263 try 264 { 265 // Set values are where columns are expected. They are not 266 // required to be in these positions, as we set the positions 267 // immediately following. 268 int idPosition = 1; 269 int objectDataPosition = columnNames.length; 270 for (int i = 0; i < columnNames.length; i++) 271 { 272 if (columnNames[i].equals(USER_ID_COLUMN)) 273 { 274 idPosition = i + 1; 275 } 276 if (columnNames[i].equals(OBJECT_DATA_COLUMN)) 277 { 278 objectDataPosition = i + 1; 279 } 280 } 281 282 ((Persistent) obj).setPrimaryKey( 283 new NumberKey(row.getValue(idPosition).asBigDecimal()) ); 284 285 // Restore the Permanent Storage Hashtable. First the 286 // Hashtable is restored, then any explicit table columns 287 // which should be included in the Hashtable are added. 288 byte[] objectData = (byte[])row.getValue(objectDataPosition).asBytes(); 289 Hashtable tempHash = (Hashtable)ObjectUtils.deserialize(objectData); 290 if (tempHash == null) 291 { 292 tempHash = new Hashtable(10); 293 } 294 295 for( int j=0; j<columnNames.length; j++ ) 296 { 297 if ( ! ( columnNames[j].equalsIgnoreCase( USER_ID_COLUMN ) 298 || columnNames[j].equalsIgnoreCase( OBJECT_DATA_COLUMN ) )) 299 { 300 Object obj2 = null; 301 Value value = row.getValue(j+1); 302 if (value.isByte()) obj2 = new Byte(value.asByte()); 303 if (value.isBigDecimal()) obj2 = value.asBigDecimal(); 304 if (value.isBytes()) obj2 = value.asBytes(); 305 if (value.isDate()) obj2 = value.asDate(); 306 if (value.isShort()) obj2 = new Short(value.asShort()); 307 if (value.isInt()) obj2 = new Integer(value.asInt()); 308 if (value.isLong()) obj2 = new Long(value.asLong()); 309 if (value.isDouble()) obj2 = new Double(value.asDouble()); 310 if (value.isFloat()) obj2 = new Float(value.asFloat()); 311 if (value.isBoolean()) obj2 = new Boolean(value.asBoolean()); 312 if (value.isString()) obj2 = value.asString(); 313 if (value.isTime()) obj2 = value.asTime(); 314 if (value.isTimestamp()) obj2 = value.asTimestamp(); 315 if (value.isUtilDate()) obj2 = value.asUtilDate(); 316 if ( obj2 != null ) 317 tempHash.put( columnNames[j], obj2 ); 318 } 319 } 320 obj.setPermStorage( tempHash ); 321 } 322 catch (Exception ex) 323 { 324 throw new TorqueException(ex); 325 } 326 } 327 328 /*** 329 * Issues a select based on a criteria. 330 * 331 * @param criteria Object containing data that is used to create 332 * the SELECT statement. 333 * @return Vector containing TurbineUser objects. 334 * @exception Exception, a generic exception. 335 */ 336 public static List doSelect(Criteria criteria) 337 throws TorqueException 338 { 339 return doSelect(criteria, (User) null); 340 } 341 342 /*** 343 * Issues a select based on a criteria. 344 * 345 * @param criteria Object containing data that is used to create 346 * the SELECT statement. 347 * @param current User object that is to be used as part of the 348 * results - if not passed, then a new one is created. 349 * @return Vector containing TurbineUser objects. 350 * @exception Exception, a generic exception. 351 */ 352 public static List doSelect(Criteria criteria, User current) 353 throws TorqueException 354 { 355 // stuff for postgresql problem..... 356 criteria.setBlobFlag(); 357 358 // add User table columns 359 addSelectColumns(criteria); 360 361 if (criteria.getOrderByColumns() == null) 362 { 363 criteria.addAscendingOrderByColumn(LAST_NAME); 364 } 365 366 // Place any checks here to intercept criteria which require 367 // custom SQL. For example: 368 // if ( criteria.containsKey("SomeTable.SomeColumn") ) 369 // { 370 // String whereSql = "SomeTable.SomeColumn IN (Select ..."; 371 // criteria.add("SomeTable.SomeColumn", 372 // whereSQL, criteria.CUSTOM); 373 // } 374 375 // BasePeer returns a Vector of Record (Village) objects. The 376 // array order follows the order columns were placed in the 377 // Select clause. 378 List rows = BasePeer.doSelect(criteria); 379 List results = new ArrayList(); 380 381 // Populate the object(s). 382 for ( int i = 0; i < rows.size(); i++ ) 383 { 384 Record row = (Record) rows.get(i); 385 // Add User to the return Vector. 386 if (current == null) 387 { 388 results.add(row2Object(row, 1, null)); 389 } 390 else 391 { 392 populateObject(row, 1, current); 393 ((Persistent) current).setNew(false); 394 } 395 } 396 return results; 397 } 398 399 /*** 400 * Issues a select based on a criteria. 401 * 402 * @param criteria Object containing data that is used to create 403 * the SELECT statement. 404 * @param current User object that is to be used as part of the 405 * results - if not passed, then a new one is created. 406 * @return Vector containing TurbineUser objects. 407 * @exception Exception, a generic exception. 408 */ 409 public static List doSelect(Criteria criteria, Connection dbConn) 410 throws TorqueException 411 { 412 // stuff for postgresql problem..... 413 criteria.setBlobFlag(); 414 415 // add User table columns 416 addSelectColumns(criteria); 417 418 if (criteria.getOrderByColumns() == null) 419 { 420 criteria.addAscendingOrderByColumn(LAST_NAME); 421 } 422 423 // BasePeer returns a Vector of Record (Village) objects. The 424 // array order follows the order columns were placed in the 425 // Select clause. 426 List rows = BasePeer.doSelect(criteria, dbConn); 427 List results = new ArrayList(); 428 429 // Populate the object(s). 430 for (int i = 0; i < rows.size(); i++) 431 { 432 Record row = (Record) rows.get(i); 433 // Add User to the return Vector. 434 results.add(row2Object(row, 1, null)); 435 } 436 return results; 437 } 438 439 /*** 440 * Implementss torque peers' method. Does not use the Class argument 441 * as Users need to go through TurbineSecurity 442 */ 443 public static User row2Object (Record row, int offset, Class cls) 444 throws TorqueException 445 { 446 try 447 { 448 User obj = TurbineSecurity.getUserInstance(); 449 populateObject(row, offset, obj); 450 ((Persistent)obj).setNew(false); 451 ((Persistent)obj).setModified(false); 452 return obj; 453 } 454 catch (Exception ex) 455 { 456 throw new TorqueException (ex); 457 } 458 } 459 460 /*** 461 * The type of User this peer will instantiate. 462 */ 463 public static Class getOMClass() 464 throws Exception 465 { 466 return TurbineSecurity.getUserClass(); 467 } 468 469 /*** 470 * Issues an update based on a criteria. 471 * The criteria only uses USER_ID. 472 * 473 * @param criteria Object containing data that is used to create 474 * the UPDATE statement. 475 * @exception Exception, a generic exception. 476 */ 477 public static void doUpdate(Criteria criteria) 478 throws TorqueException 479 { 480 Criteria selectCriteria = new Criteria(2); 481 selectCriteria.put( USER_ID, criteria.remove(USER_ID) ); 482 BasePeer.doUpdate( selectCriteria, criteria ); 483 } 484 485 /*** 486 * Checks if a User is defined in the system. The name 487 * is used as query criteria. 488 * 489 * @param permission The User to be checked. 490 * @return <code>true</code> if given User exists in the system. 491 * @throws DataBackendException when more than one User with 492 * the same name exists. 493 * @throws Exception, a generic exception. 494 */ 495 public static boolean checkExists( User user ) 496 throws DataBackendException, Exception 497 { 498 Criteria criteria = new Criteria(); 499 criteria.addSelectColumn(USER_ID); 500 criteria.add(USERNAME, user.getUserName()); 501 List results = BasePeer.doSelect(criteria); 502 if(results.size() > 1) 503 { 504 throw new DataBackendException("Multiple users named '" + 505 user.getUserName() + "' exist!"); 506 } 507 return (results.size()==1); 508 } 509 510 /*** 511 * Returns a vector of all User objects. 512 * 513 * @return A Vector with all users in the system. 514 * @exception Exception, a generic exception. 515 */ 516 public static List selectAllUsers() 517 throws Exception 518 { 519 Criteria criteria = new Criteria(); 520 criteria.addAscendingOrderByColumn(TurbineUserPeer.LAST_NAME); 521 criteria.addAscendingOrderByColumn(TurbineUserPeer.FIRST_NAME); 522 criteria.setIgnoreCase(true); 523 return TurbineUserPeer.doSelect(criteria); 524 } 525 526 /*** 527 * Returns a vector of all confirmed User objects. 528 * 529 * @return A Vector with all confirmed users in the system. 530 * @exception Exception, a generic exception. 531 */ 532 public static List selectAllConfirmedUsers() 533 throws Exception 534 { 535 Criteria criteria = new Criteria(); 536 criteria.add ( User.CONFIRM_VALUE, User.CONFIRM_DATA ); 537 criteria.addAscendingOrderByColumn(TurbineUserPeer.LAST_NAME); 538 criteria.addAscendingOrderByColumn(TurbineUserPeer.FIRST_NAME); 539 criteria.setIgnoreCase(true); 540 return TurbineUserPeer.doSelect(criteria); 541 } 542 543 /*** 544 * Returns the TableMap related to this peer. This method is not 545 * needed for general use but a specific application could have a 546 * need. 547 */ 548 protected static TableMap getTableMap() 549 { 550 return mapBuilder.getDatabaseMap().getTable(TABLE_NAME); 551 } 552 }

This page was automatically generated by Maven