com.samskivert.servlet.user
Class UserRepository

java.lang.Object
  extended by com.samskivert.jdbc.Repository
      extended by com.samskivert.jdbc.SimpleRepository
          extended by com.samskivert.jdbc.JORARepository
              extended by com.samskivert.servlet.user.UserRepository

public class UserRepository
extends JORARepository

Interfaces with the RDBMS in which the user information is stored. The user repository encapsulates the creating, loading and management of users and sessions.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.samskivert.jdbc.SimpleRepository
SimpleRepository.PreCondition
 
Nested classes/interfaces inherited from class com.samskivert.jdbc.Repository
Repository.Operation<V>
 
Field Summary
protected  Table<User> _utable
          A wrapper that provides access to the userstable.
static String USER_REPOSITORY_IDENT
          The database identifier used to obtain a connection from our connection provider.
 
Fields inherited from class com.samskivert.jdbc.SimpleRepository
_dbident, _precond
 
Fields inherited from class com.samskivert.jdbc.Repository
_provider
 
Constructor Summary
UserRepository(ConnectionProvider provider)
          Creates the repository and opens the user database.
 
Method Summary
protected  void createTables()
          During construction, this function will be called to give the repository implementation the opportunity to create its table objects.
 int createUser(Username username, Password password, String realname, String email, int siteId)
          Requests that a new user be created in the repository.
 void deleteUser(User user)
          'Delete' the users account such that they can no longer access it, however we do not delete the record from the db.
protected  String genIdString(int[] userIds)
          Take the passed in int array and create the a string suitable for using in a SQL set query (I.e., "select foo, from bar where userId in (genIdString(userIds))"; )
protected  int insertUser(User user)
          Inserts the supplied user record into the user database, assigning it a userid in the process, which is returned.
 String[] loadAllRealNames()
          Returns an array with the real names of every user in the system.
protected  String[] loadNames(int[] userIds, String column)
           
 String[] loadRealNames(int[] userIds)
          Loads the real names of the users identified by the supplied user ids and returns them in an array.
 User loadUser(int userId)
          Looks up a user by userid.
 User loadUser(String username)
          Looks up a user by username.
 User loadUserBySession(String sessionKey)
          Looks up a user by a session identifier.
 String[] loadUserNames(int[] userIds)
          Loads the usernames of the users identified by the supplied user ids and returns them in an array.
 HashIntMap<User> loadUsersFromId(int[] userIds)
          Looks up users by userid
protected  User loadUserWhere(String where)
          Loads up a user record that matches the specified where clause.
 ArrayList<User> lookupUsersByEmail(String email)
          Lookup a user by email address, something that is not efficient and should really only be done by site admins attempting to look up a user record.
 ArrayList<User> lookupUsersWhere(String where)
          Looks up a list of users that match an arbitrary query.
protected  void populateUser(User user, Username name, Password pass, String realname, String email, int siteId)
          Configures the supplied user record with the provided information in preparation for inserting the record into the database for the first time.
 void pruneSessions()
          Prunes any expired sessions from the sessions table.
 boolean refreshSession(String sessionKey, int expireDays)
          Validates that the supplied session key is still valid and if so, refreshes it for the specified number of days.
 String registerSession(User user, int expireDays)
          Creates a new session for the specified user and returns the randomly generated session identifier for that session.
 boolean updateUser(User user)
          Updates a user that was previously fetched from the repository.
 
Methods inherited from class com.samskivert.jdbc.JORARepository
delete, insert, load, load, loadAll, loadAll, loadAllByExample, loadAllByExample, loadByExample, loadByExample, store, update, update, updateField, updateFields
 
Methods inherited from class com.samskivert.jdbc.SimpleRepository
checkedUpdate, configureDatabaseIdent, execute, execute, executeUpdate, gotConnection, maintenance, migrateSchema, setExecutePreCondition, toString, update, warnedUpdate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USER_REPOSITORY_IDENT

public static final String USER_REPOSITORY_IDENT
The database identifier used to obtain a connection from our connection provider. The value is userdb which you'll probably need to know to provide the proper configuration to your connection provider.

See Also:
Constant Field Values

_utable

protected Table<User> _utable
A wrapper that provides access to the userstable.

Constructor Detail

UserRepository

public UserRepository(ConnectionProvider provider)
               throws PersistenceException
Creates the repository and opens the user database. The database identifier used to fetch our database connection is documented by USER_REPOSITORY_IDENT.

Parameters:
provider - the database connection provider.
Throws:
PersistenceException
Method Detail

createUser

public int createUser(Username username,
                      Password password,
                      String realname,
                      String email,
                      int siteId)
               throws UserExistsException,
                      PersistenceException
Requests that a new user be created in the repository.

Parameters:
username - the username of the new user to create.
password - the password for the new user.
realname - the user's real name.
email - the user's email address.
siteId - the unique identifier of the site through which this account is being created. The resulting user will be tracked as originating from this site for accounting purposes (SiteIdentifier.DEFAULT_SITE_ID can be used by systems that don't desire to perform site tracking.
Returns:
The userid of the newly created user.
Throws:
UserExistsException
PersistenceException

loadUser

public User loadUser(String username)
              throws PersistenceException
Looks up a user by username.

Returns:
the user with the specified user id or null if no user with that id exists.
Throws:
PersistenceException

loadUser

public User loadUser(int userId)
              throws PersistenceException
Looks up a user by userid.

Returns:
the user with the specified user id or null if no user with that id exists.
Throws:
PersistenceException

loadUserBySession

public User loadUserBySession(String sessionKey)
                       throws PersistenceException
Looks up a user by a session identifier.

Returns:
the user associated with the specified session or null of no session exists with the supplied identifier.
Throws:
PersistenceException

loadUsersFromId

public HashIntMap<User> loadUsersFromId(int[] userIds)
                                 throws PersistenceException
Looks up users by userid

Returns:
the users whom have a user id in the userIds array.
Throws:
PersistenceException

lookupUsersByEmail

public ArrayList<User> lookupUsersByEmail(String email)
                                   throws PersistenceException
Lookup a user by email address, something that is not efficient and should really only be done by site admins attempting to look up a user record.

Returns:
the user with the specified user id or null if no user with that id exists.
Throws:
PersistenceException

lookupUsersWhere

public ArrayList<User> lookupUsersWhere(String where)
                                 throws PersistenceException
Looks up a list of users that match an arbitrary query. Care should be taken in constructing these queries as the user table is likely to be large and a query that does not make use of indices could be very slow.

Returns:
the users matching the specified query or an empty list if there are no matches.
Throws:
PersistenceException

updateUser

public boolean updateUser(User user)
                   throws PersistenceException
Updates a user that was previously fetched from the repository. Only fields that have been modified since it was loaded will be written to the database and those fields will subsequently be marked clean once again.

Returns:
true if the record was updated, false if the update was skipped because no fields in the user record were modified.
Throws:
PersistenceException

deleteUser

public void deleteUser(User user)
                throws PersistenceException
'Delete' the users account such that they can no longer access it, however we do not delete the record from the db. The name is changed such that the original name has XX=FOO if the name were FOO originally. If we have to lop off any of the name to get our prefix to fit we use a minus sign instead of a equals side. The password field is set to be the empty string so that no one can log in (since nothing hashes to the empty string. We also make sure their email address no longer works, so in case we don't ignore 'deleted' users when we do the sql to get emailaddresses for the mass mailings we still won't spam delete folk. We leave the emailaddress intact exect for the @ sign which gets turned to a #, so that we can see what their email was incase it was an accidently deletion and we have to verify through email.

Throws:
PersistenceException

registerSession

public String registerSession(User user,
                              int expireDays)
                       throws PersistenceException
Creates a new session for the specified user and returns the randomly generated session identifier for that session. If a session entry already exists for the specified user it will be reused.

Parameters:
expireDays - the number of days in which the session token should expire.
Throws:
PersistenceException

refreshSession

public boolean refreshSession(String sessionKey,
                              int expireDays)
                       throws PersistenceException
Validates that the supplied session key is still valid and if so, refreshes it for the specified number of days.

Returns:
true if the session was located and refreshed, false if it no longer exists.
Throws:
PersistenceException

pruneSessions

public void pruneSessions()
                   throws PersistenceException
Prunes any expired sessions from the sessions table.

Throws:
PersistenceException

loadUserNames

public String[] loadUserNames(int[] userIds)
                       throws PersistenceException
Loads the usernames of the users identified by the supplied user ids and returns them in an array. If any users do not exist, their slot in the array will contain a null.

Throws:
PersistenceException

loadRealNames

public String[] loadRealNames(int[] userIds)
                       throws PersistenceException
Loads the real names of the users identified by the supplied user ids and returns them in an array. If any users do not exist, their slot in the array will contain a null.

Throws:
PersistenceException

loadAllRealNames

public String[] loadAllRealNames()
                          throws PersistenceException
Returns an array with the real names of every user in the system.

Throws:
PersistenceException

populateUser

protected void populateUser(User user,
                            Username name,
                            Password pass,
                            String realname,
                            String email,
                            int siteId)
Configures the supplied user record with the provided information in preparation for inserting the record into the database for the first time.


insertUser

protected int insertUser(User user)
                  throws UserExistsException,
                         PersistenceException
Inserts the supplied user record into the user database, assigning it a userid in the process, which is returned.

Throws:
UserExistsException
PersistenceException

loadUserWhere

protected User loadUserWhere(String where)
                      throws PersistenceException
Loads up a user record that matches the specified where clause. Returns null if no record matches.

Throws:
PersistenceException

loadNames

protected String[] loadNames(int[] userIds,
                             String column)
                      throws PersistenceException
Throws:
PersistenceException

genIdString

protected String genIdString(int[] userIds)
Take the passed in int array and create the a string suitable for using in a SQL set query (I.e., "select foo, from bar where userId in (genIdString(userIds))"; )


createTables

protected void createTables()
Description copied from class: JORARepository
During construction, this function will be called to give the repository implementation the opportunity to create its table objects.

Specified by:
createTables in class JORARepository


Copyright © 2000-2008 Michael Bayne. All Rights Reserved.