/*
* Created on 21-Mar-2003
*/
package net.sf.jportlet.service.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jportlet.impl.PortletRequestImpl;
import net.sf.jportlet.impl.UserImpl;
import net.sf.jportlet.portlet.PortletRequest;
import net.sf.jportlet.portlet.User;
import net.sf.jportlet.service.PortletServiceAdapter;
import net.sf.jportlet.service.PortletServiceConfig;
import net.sf.jportlet.service.PortletServiceException;
import net.sf.jportlet.service.jdbc.JDBCService;
import net.sf.jportlet.util.LocaleHelper;
/**
* Implementation of {@link net.sf.jportlet.service.user.UserService} based on
* JDBC.
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class UserServiceJDBC
extends PortletServiceAdapter
implements UserService
{
//~ Static fields/initializers ---------------------------------------------
private static Log __log = LogFactory.getLog( UserServiceJDBC.class );
//~ Instance fields --------------------------------------------------------
private String _getRolesSQL;
private String _getUserSQL;
private String _getUserByEmailSQL;
private String _addUserSQL;
private String _updateUserSQL;
//~ Methods ----------------------------------------------------------------
private void close( ResultSet rs,
PreparedStatement stmt,
Connection cnn )
{
if ( rs != null )
{
try
{
rs.close( );
}
catch ( Exception e )
{
_log.warn( "Unable to close the ResultSet", e );
}
}
if ( stmt != null )
{
try
{
stmt.close( );
}
catch ( Exception e )
{
_log.warn( "Unable to close the PreparedStatemetn", e );
}
}
if ( cnn != null )
{
try
{
cnn.close( );
}
catch ( Exception e )
{
_log.warn( "Unable to close the Connection", e );
}
}
}
/**
* @see net.sf.jportlet.service.user.UserService#getRoles(java.lang.String)
*/
public Collection getRoles( String userId )
throws UserException
{
boolean debug = __log.isDebugEnabled( );
Connection cnn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Collection roles = new ArrayList( );
try
{
if ( debug )
{
__log.debug( "executing SQL: " + _getRolesSQL );
}
cnn = getConnection();
stmt = cnn.prepareStatement( _getRolesSQL );
stmt.setString( 1, userId );
rs = stmt.executeQuery( );
while ( rs.next( ) )
{
roles.add( new String( rs.getString( 1 ) ) );
}
if ( debug )
{
__log.debug( "...roles=" + roles );
}
return roles;
}
catch ( Exception e )
{
throw new UserException( e );
}
finally
{
close( rs, stmt, cnn );
}
}
/**
* @see net.sf.jportlet.service.PortletService#getServiceName()
*/
public String getServiceName( )
{
return UserService.NAME;
}
/**
* @see net.sf.jportlet.service.user.UserService#getUser(java.lang.String)
*/
public User getUser( String userId )
throws UserException
{
boolean debug = __log.isDebugEnabled( );
Connection cnn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
UserImpl usr;
try
{
if ( debug )
{
__log.debug( "executing SQL: " + _getUserSQL );
}
cnn = getConnection();
stmt = cnn.prepareStatement( _getUserSQL );
stmt.setString( 1, userId );
rs = stmt.executeQuery( );
if ( rs.next( ) )
{
usr = new UserImpl( );
usr.setId( rs.getString( 1 ) );
usr.setPassword( rs.getString( 2 ) );
usr.setEmail( rs.getString( 3 ) );
usr.setFirstname( rs.getString( 4 ) );
usr.setLastname( rs.getString( 5 ) );
usr.setLocale( LocaleHelper.getLocale( rs.getString( 6 ), null ) );
return usr;
}
else
{
throw new UserNotFoundException( "userId=" + userId );
}
}
catch ( SQLException e )
{
throw new UserException( e );
}
finally
{
close( rs, stmt, cnn );
}
}
/**
* @see net.sf.jportlet.service.PortletService#init(net.sf.jportlet.service.PortletServiceConfig)
*/
public void init( PortletServiceConfig serviceConfig )
throws PortletServiceException
{
super.init( serviceConfig );
_getUserSQL = getInitParameter( "sql.getUser" );
_getUserByEmailSQL = getInitParameter( "sql.getUserByEmail" );
_addUserSQL = getInitParameter( "sql.addUser" );
_updateUserSQL = getInitParameter( "sql.updateUser" );
_getRolesSQL = getInitParameter( "sql.getRoles" );
}
/**
* @see net.sf.jportlet.service.user.UserService#addUser(net.sf.jportlet.portlet.PortletRequest)
*/
public void addUser( PortletRequest request )
throws UserException
{
boolean debug = __log.isDebugEnabled( );
Connection cnn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String userId = request.getParameter( UserService.USER_ID ).trim( );
String password = request.getParameter( UserService.PASSWORD ).trim( );
String email = request.getParameter( UserService.EMAIL ).trim( );
String firstname = request.getParameter( UserService.FIRSTNAME ).trim( );
String lastname = request.getParameter( UserService.LASTNAME ).trim( );
String locale = request.getParameter( UserService.LOCALE ).trim( );
try
{
if ( debug )
{
__log.debug( "executing SQL: " + _addUserSQL );
}
cnn = getConnection();
/* Make sure that userId is unique */
if ( !isUserIdUnique( userId, cnn ) )
{
throw new UserIdNotUniqueException( userId );
}
/* Make sure that email is unique */
if ( !isEmailUnique( userId, email, cnn ) )
{
throw new UserEmailNotUniqueException( email );
}
/* create */
stmt = cnn.prepareStatement( _addUserSQL );
stmt.setString( 1, userId );
stmt.setString( 2, password );
stmt.setString( 3, email );
stmt.setString( 4, firstname );
stmt.setString( 5, lastname );
stmt.setString( 6, ( locale != null )
? locale.toString( )
: null );
cnn.setAutoCommit( true );
rs = stmt.executeQuery( );
}
catch ( SQLException e )
{
throw new UserException( e );
}
finally
{
close( rs, stmt, cnn );
}
}
/**
* @see net.sf.jportlet.service.user.UserService#updateUser(net.sf.jportlet.portlet.PortletRequest)
*/
public void updateUser( PortletRequest request )
throws UserException
{
boolean debug = __log.isDebugEnabled( );
Connection cnn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String userId = request.getParameter( UserService.USER_ID ).trim( );
String password = request.getParameter( UserService.PASSWORD ).trim( );
String email = request.getParameter( UserService.EMAIL ).trim( );
String firstname = request.getParameter( UserService.FIRSTNAME ).trim( );
String lastname = request.getParameter( UserService.LASTNAME ).trim( );
String locale = request.getParameter( UserService.LOCALE ).trim( );
try
{
if ( debug )
{
__log.debug( "executing SQL: " + _updateUserSQL );
}
cnn = getConnection();
/* Make sure that email is unique */
if ( !isEmailUnique( userId, email, cnn ) )
{
throw new UserEmailNotUniqueException( email );
}
/* Update */
stmt = cnn.prepareStatement( _updateUserSQL );
stmt.setString( 1, password );
stmt.setString( 2, email );
stmt.setString( 3, firstname );
stmt.setString( 4, lastname );
stmt.setString( 5, ( locale != null )
? locale.toString( )
: null );
stmt.setString( 6, userId );
cnn.setAutoCommit( true );
rs = stmt.executeQuery( );
/* Sync the request */
UserImpl user = new UserImpl( userId, password, email, firstname, lastname, LocaleHelper.getLocale( locale, null ) );
( ( PortletRequestImpl ) request ).userChanged( user );
}
catch ( SQLException e )
{
throw new UserException( e );
}
finally
{
close( rs, stmt, cnn );
}
}
/**
* @see net.sf.jportlet.service.user.UserService#getUserByEmail(java.lang.String)
*/
public User getUserByEmail( String email )
throws UserException
{
boolean debug = __log.isDebugEnabled( );
Connection cnn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
UserImpl usr;
try
{
if ( debug )
{
__log.debug( "executing SQL: " + _getUserByEmailSQL );
}
cnn = getConnection();
stmt = cnn.prepareStatement( _getUserByEmailSQL );
stmt.setString( 1, email );
rs = stmt.executeQuery( );
if ( rs.next( ) )
{
usr = new UserImpl( );
usr.setId( rs.getString( 1 ) );
usr.setPassword( rs.getString( 2 ) );
usr.setEmail( rs.getString( 3 ) );
usr.setFirstname( rs.getString( 4 ) );
usr.setLastname( rs.getString( 5 ) );
usr.setLocale( LocaleHelper.getLocale( rs.getString( 6 ), null ) );
return usr;
}
else
{
throw new UserNotFoundException( "email=" + email );
}
}
catch ( SQLException e )
{
throw new UserException( e );
}
finally
{
close( rs, stmt, cnn );
}
}
public Connection getConnection( )
throws UserException
{
try
{
JDBCService srv = ( JDBCService ) _serviceContext.getPortletService( JDBCService.NAME );
return srv.getConnection();
}
catch( Exception e )
{
throw new UserException( e );
}
}
private boolean isUserIdUnique( String userId,
Connection cnn )
throws SQLException
{
ResultSet rs = null;
PreparedStatement stmt = null;
try
{
stmt = cnn.prepareStatement( _getUserSQL );
stmt.setString( 1, userId );
rs = stmt.executeQuery( );
return !rs.next( );
}
finally
{
close( rs, stmt, null );
}
}
private boolean isEmailUnique( String userId,
String email,
Connection cnn )
throws SQLException
{
ResultSet rs = null;
PreparedStatement stmt = null;
try
{
stmt = cnn.prepareStatement( _getUserByEmailSQL );
stmt.setString( 1, email );
rs = stmt.executeQuery( );
while ( rs.next( ) )
{
if ( !rs.getString( 1 ).equals( userId ) )
{
return false;
}
}
return true;
}
finally
{
close( rs, stmt, null );
}
}
}