1 package xpetstore.web.struts.action; 2 3 import java.util.ResourceBundle ; 4 5 import javax.naming.InitialContext ; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 9 import javax.servlet.http.HttpSession ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.apache.struts.action.Action; 14 import org.apache.struts.action.ActionForm; 15 import org.apache.struts.action.ActionForward; 16 import org.apache.struts.action.ActionMapping; 17 18 import xpetstore.domain.customer.ejb.Customer; 19 20 import xpetstore.services.cart.ejb.Cart; 21 import xpetstore.services.petstore.ejb.Petstore; 22 23 24 27 public abstract class BaseAction 28 extends Action 29 { 30 32 public static final String BUNDLE_NAME = "Resources"; 33 public static final String CART_KEY = "cart"; 34 public static final String ERROR = "error"; 35 public static final String MESSAGE_KEY = "message"; 36 public static final String SUCCESS = "success"; 37 public static final String USERID_KEY = "userId"; 38 public static final String USERNAME_KEY = "username"; 39 40 42 protected Log _log = LogFactory.getLog( getClass( ) ); 43 44 46 protected void clearSession( HttpServletRequest request ) 47 throws Exception 48 { 49 HttpSession session = request.getSession( ); 50 51 session.removeAttribute( USERID_KEY ); 52 session.removeAttribute( USERNAME_KEY ); 53 54 Cart cart = getCart( false, request ); 55 56 if ( cart != null ) 57 { 58 session.removeAttribute( CART_KEY ); 59 cart.remove( ); 60 } 61 } 62 63 protected abstract ActionForward doExecute( ActionMapping mapping, 64 ActionForm form, 65 HttpServletRequest request, 66 HttpServletResponse response ) 67 throws Exception ; 68 69 73 public ActionForward execute( ActionMapping mapping, 74 ActionForm form, 75 HttpServletRequest request, 76 HttpServletResponse response ) 77 throws Exception 78 { 79 _log.info( "execute()" ); 80 81 return doExecute( mapping, form, request, response ); 82 } 83 84 public Cart getCart( boolean create, 85 HttpServletRequest request ) 86 throws Exception 87 { 88 HttpSession session = request.getSession( ); 89 Cart cart = ( Cart ) session.getAttribute( CART_KEY ); 90 if ( ( cart == null ) && create ) 91 { 92 cart = (Cart)new InitialContext ().lookup("ejb/Cart"); 93 session.setAttribute( CART_KEY, cart ); 94 } 95 96 return cart; 97 } 98 99 public Cart getCart( HttpServletRequest request ) 100 throws Exception 101 { 102 return getCart( true, request ); 103 } 104 105 public Petstore getPetstore( ) 106 throws Exception 107 { 108 return (Petstore)new InitialContext ().lookup("ejb/Petstore"); 109 } 110 111 public String getString( String key ) 112 { 113 try 114 { 115 return ResourceBundle.getBundle( BUNDLE_NAME ).getString( key ); 116 } 117 catch ( Exception e ) 118 { 119 return "???" + key + "???"; 120 } 121 } 122 123 protected void initSession( Customer customer, 124 HttpServletRequest request ) 125 { 126 HttpSession session = request.getSession( ); 127 session.setAttribute( USERID_KEY, customer.getUserId( ) ); 128 session.setAttribute( USERNAME_KEY, customer.getFirstname( ) + " " + customer.getLastname( ) ); 129 } 130 } 131 | Popular Tags |