FileDocCategorySizeDatePackage
CreateOrderAction.javaAPI DocJBoss 4.2.12476Fri Jul 13 20:55:42 BST 2007xpetstore.web.webwork.action.order

CreateOrderAction

public class CreateOrderAction extends xpetstore.web.webwork.action.BaseAction
author
Herve Tchepannou
webwork.action
name="createOrder" success="confirmation.vm"

Fields Summary
Constructors Summary
Methods Summary
protected java.lang.StringdoExecute()

see
webwork.action.ActionSupport#doExecute()

        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(  );
        }