| 1 4 package xpetstore.web.webwork.action.order; 5 6 import java.util.Iterator ; 7 import java.util.Map ; 8 9 import cirrus.hibernate.Session; 10 import cirrus.hibernate.Transaction; 11 12 import xpetstore.domain.Customer; 13 import xpetstore.domain.Item; 14 import xpetstore.domain.Order; 15 16 import xpetstore.util.MailUtil; 17 18 import xpetstore.web.webwork.action.BaseAction; 19 20 21 28 public class CreateOrderAction 29 extends BaseAction 30 { 31 33 36 protected String doExecute( ) 37 throws Exception  38 { 39 Session s = getHibernateSession( ); 40 Transaction tx = null; 41 42 try 43 { 44 tx = s.beginTransaction( ); 45 46 47 Customer cst = ( Customer ) s.load( Customer.class, getUserId( ) ); 48 49 50 Order order = new Order( cst ); 51 Map cart = getCart( ); 52 Iterator it = cart.keySet( ).iterator( ); 53 54 while ( it.hasNext( ) ) 55 { 56 String itemId = ( String ) it.next( ); 57 Item item = ( Item ) s.load( Item.class, itemId ); 58 Integer quantity = ( Integer ) cart.get( itemId ); 59 order.add( item, quantity.intValue( ) ); 60 } 61 62 63 s.save( order ); 64 tx.commit( ); 65 66 67 getCart( ).clear( ); 68 69 70 String subject = "[xpetstore] Order Confimation"; 71 String body = "Your order has been submitted.\nThe order number is: " + order.getOrderId( ); 72 73 try 74 { 75 MailUtil.send( cst.getEmail( ), subject, body ); 76 } 77 catch ( Exception e ) 78 { 79 _log.error( "Unexpected error while sending email", e ); 80 } 81 82 return SUCCESS; 83 } 84 catch ( Exception e ) 85 { 86 _log.error( "Unexpected error", e ); 87 88 if ( tx != null ) 89 { 90 tx.rollback( ); 91 } 92 93 throw e; 94 } 95 finally 96 { 97 s.close( ); 98 } 99 } 100 } 101 | Popular Tags |