KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > web > webwork > action > customer > BaseSaveCustomerAction


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

4 package xpetstore.web.webwork.action.customer;
5
6 import java.util.Collection JavaDoc;
7
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 import cirrus.hibernate.Hibernate;
11 import cirrus.hibernate.ObjectNotFoundException;
12 import cirrus.hibernate.Session;
13 import cirrus.hibernate.Transaction;
14
15 import webwork.action.ServletResponseAware;
16
17 import xpetstore.domain.Account;
18 import xpetstore.domain.CreditCard;
19 import xpetstore.domain.Customer;
20
21 import xpetstore.web.webwork.action.BaseAction;
22
23
24 /**
25  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
26  */

27 public abstract class BaseSaveCustomerAction
28     extends BaseAction
29     implements ServletResponseAware
30 {
31     //~ Instance fields --------------------------------------------------------
32

33     protected Customer _customer = new Customer( );
34     private HttpServletResponse JavaDoc _response;
35
36     //~ Methods ----------------------------------------------------------------
37

38     /**
39      * @see webwork.action.ActionSupport#doExecute()
40      */

41     protected String JavaDoc doExecute( )
42         throws Exception JavaDoc
43     {
44         Transaction tx = null;
45         Session s = getHibernateSession( );
46         String JavaDoc customerId = _customer.getUserId( );
47
48         try
49         {
50             tx = s.beginTransaction( );
51
52             /* Make sure that the user-id is unique */
53             try
54             {
55                 Customer c = ( Customer ) s.load( Customer.class, customerId );
56
57                 if ( !c.getUserId( ).equalsIgnoreCase( _customer.getUserId( ) ) )
58                 {
59                     addError( "customer", getText( "duplicate_account" ) );
60
61                     return ERROR;
62                 }
63             }
64             catch ( ObjectNotFoundException o ) {}
65
66             /* Make sure that the email is unique */
67             String JavaDoc oql = "FROM cst IN CLASS " + Customer.class + " WHERE cst.email=?";
68             Collection JavaDoc col = s.find( oql, _customer.getEmail( ), Hibernate.STRING );
69
70             if ( col.size( ) > 0 )
71             {
72                 Customer c = ( Customer ) col.iterator( ).next( );
73
74                 if ( !c.getUserId( ).equalsIgnoreCase( customerId ) )
75                 {
76                     addError( "customer", getText( "duplicate_email" ) );
77
78                     return ERROR;
79                 }
80             }
81
82             save( _customer, s );
83             tx.commit( );
84
85             return SUCCESS;
86         }
87         catch ( Exception JavaDoc e )
88         {
89             _log.error( "Unexpected error", e );
90
91             if ( tx != null )
92             {
93                 tx.rollback( );
94             }
95
96             throw e;
97         }
98         finally
99         {
100             s.close( );
101         }
102     }
103
104     /**
105      *
106      * @see webwork.action.ActionSupport#doValidation()
107      */

108     protected void doValidation( )
109     {
110         System.out.println( getClass( ).getName( ) + ".doValidation()" );
111
112         /* Account */
113         Account account = _customer.getAccount( );
114         checkLength( "customerId", "userId_length", account.getUserId( ), 4 );
115         checkLength( "password", "password_length", account.getPassword( ), 4 );
116
117         /* Email */
118         checkNotEmpty( "email", "email_required", _customer.getEmail( ) );
119
120         /* Credit card */
121         CreditCard cc = _customer.getCreditCard( );
122         checkNotEmpty( "ccEmail", "ccType_required", cc.getType( ) );
123         checkNotEmpty( "ccNumber", "ccNumber_required", cc.getNumber( ) );
124         checkNotEmpty( "ccExpiryDate", "ccExpiryDate_required", cc.getExpiryDate( ) );
125
126         super.doValidation( );
127     }
128
129     /**
130      * @return Customer
131      */

132     public Customer getCustomer( )
133     {
134         return _customer;
135     }
136
137     public abstract void save( Customer customer,
138                                Session session )
139         throws Exception JavaDoc;
140
141     /**
142      * Sets the customer.
143      * @param customer The customer to set
144      */

145     public void setCustomer( Customer customer )
146     {
147         _customer = customer;
148     }
149
150     /**
151      * @see webwork.action.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
152      */

153     public void setServletResponse( HttpServletResponse JavaDoc response )
154     {
155         _response = response;
156     }
157 }
158
Popular Tags