1 4 package xpetstore.web.webwork.action.customer; 5 6 import java.util.Collection ; 7 8 import javax.servlet.http.HttpServletResponse ; 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 27 public abstract class BaseSaveCustomerAction 28 extends BaseAction 29 implements ServletResponseAware 30 { 31 33 protected Customer _customer = new Customer( ); 34 private HttpServletResponse _response; 35 36 38 41 protected String doExecute( ) 42 throws Exception 43 { 44 Transaction tx = null; 45 Session s = getHibernateSession( ); 46 String customerId = _customer.getUserId( ); 47 48 try 49 { 50 tx = s.beginTransaction( ); 51 52 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 67 String oql = "FROM cst IN CLASS " + Customer.class + " WHERE cst.email=?"; 68 Collection 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 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 108 protected void doValidation( ) 109 { 110 System.out.println( getClass( ).getName( ) + ".doValidation()" ); 111 112 113 Account account = _customer.getAccount( ); 114 checkLength( "customerId", "userId_length", account.getUserId( ), 4 ); 115 checkLength( "password", "password_length", account.getPassword( ), 4 ); 116 117 118 checkNotEmpty( "email", "email_required", _customer.getEmail( ) ); 119 120 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 132 public Customer getCustomer( ) 133 { 134 return _customer; 135 } 136 137 public abstract void save( Customer customer, 138 Session session ) 139 throws Exception ; 140 141 145 public void setCustomer( Customer customer ) 146 { 147 _customer = customer; 148 } 149 150 153 public void setServletResponse( HttpServletResponse response ) 154 { 155 _response = response; 156 } 157 } 158 | Popular Tags |