Session s = getHibernateSession( );
Transaction tx = null;
try
{
tx = s.beginTransaction( );
/* Customer */
Customer cst = ( Customer ) s.load( Customer.class, getUserId( ) );
/* Order + Items*/
Order order = new Order( cst );
Map cart = getCart( );
Iterator it = cart.keySet( ).iterator( );
while ( it.hasNext( ) )
{
String itemId = ( String ) it.next( );
Item item = ( Item ) s.load( Item.class, itemId );
Integer quantity = ( Integer ) cart.get( itemId );
order.add( item, quantity.intValue( ) );
}
/* Save */
s.save( order );
tx.commit( );
/* Empty the cart */
getCart( ).clear( );
/* send the email */
String subject = "[xpetstore] Order Confimation";
String body = "Your order has been submitted.\nThe order number is: " + order.getOrderId( );
try
{
MailUtil.send( cst.getEmail( ), subject, body );
}
catch ( Exception e )
{
_log.error( "Unexpected error while sending email", e );
}
return SUCCESS;
}
catch ( Exception e )
{
_log.error( "Unexpected error", e );
if ( tx != null )
{
tx.rollback( );
}
throw e;
}
finally
{
s.close( );
}