KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > web > webwork > action > order > CreateOrderAction


1 /*
2  * Created on Feb 26, 2003
3  */

4 package xpetstore.web.webwork.action.order;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
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 /**
22  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
23  *
24  * @webwork.action
25  * name="createOrder"
26  * success="confirmation.vm"
27  */

28 public class CreateOrderAction
29     extends BaseAction
30 {
31     //~ Methods ----------------------------------------------------------------
32

33     /**
34      * @see webwork.action.ActionSupport#doExecute()
35      */

36     protected String JavaDoc doExecute( )
37         throws Exception JavaDoc
38     {
39         Session s = getHibernateSession( );
40         Transaction tx = null;
41
42         try
43         {
44             tx = s.beginTransaction( );
45
46             /* Customer */
47             Customer cst = ( Customer ) s.load( Customer.class, getUserId( ) );
48
49             /* Order + Items*/
50             Order order = new Order( cst );
51             Map JavaDoc cart = getCart( );
52             Iterator JavaDoc it = cart.keySet( ).iterator( );
53
54             while ( it.hasNext( ) )
55             {
56                 String JavaDoc itemId = ( String JavaDoc ) it.next( );
57                 Item item = ( Item ) s.load( Item.class, itemId );
58                 Integer JavaDoc quantity = ( Integer JavaDoc ) cart.get( itemId );
59                 order.add( item, quantity.intValue( ) );
60             }
61
62             /* Save */
63             s.save( order );
64             tx.commit( );
65
66             /* Empty the cart */
67             getCart( ).clear( );
68
69             /* send the email */
70             String JavaDoc subject = "[xpetstore] Order Confimation";
71             String JavaDoc 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 JavaDoc e )
78             {
79                 _log.error( "Unexpected error while sending email", e );
80             }
81
82             return SUCCESS;
83         }
84         catch ( Exception JavaDoc 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