KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > web > struts > action > BaseAction


1 package xpetstore.web.struts.action;
2
3 import java.util.ResourceBundle JavaDoc;
4
5 import javax.naming.InitialContext JavaDoc;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.http.HttpSession JavaDoc;
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 /**
25  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
26  */

27 public abstract class BaseAction
28     extends Action
29 {
30     //~ Static fields/initializers ---------------------------------------------
31

32     public static final String JavaDoc BUNDLE_NAME = "Resources";
33     public static final String JavaDoc CART_KEY = "cart";
34     public static final String JavaDoc ERROR = "error";
35     public static final String JavaDoc MESSAGE_KEY = "message";
36     public static final String JavaDoc SUCCESS = "success";
37     public static final String JavaDoc USERID_KEY = "userId";
38     public static final String JavaDoc USERNAME_KEY = "username";
39
40     //~ Instance fields --------------------------------------------------------
41

42     protected Log _log = LogFactory.getLog( getClass( ) );
43
44     //~ Methods ----------------------------------------------------------------
45

46     protected void clearSession( HttpServletRequest JavaDoc request )
47         throws Exception JavaDoc
48     {
49         HttpSession JavaDoc 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 JavaDoc request,
66                                                 HttpServletResponse JavaDoc response )
67         throws Exception JavaDoc;
68
69     /**
70      *
71      * @see org.apache.struts.action.Action#execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)
72      */

73     public ActionForward execute( ActionMapping mapping,
74                                   ActionForm form,
75                                   HttpServletRequest JavaDoc request,
76                                   HttpServletResponse JavaDoc response )
77         throws Exception JavaDoc
78     {
79         _log.info( "execute()" );
80
81         return doExecute( mapping, form, request, response );
82     }
83
84     public Cart getCart( boolean create,
85                               HttpServletRequest JavaDoc request )
86         throws Exception JavaDoc
87     {
88         HttpSession JavaDoc session = request.getSession( );
89         Cart cart = ( Cart ) session.getAttribute( CART_KEY );
90         if ( ( cart == null ) && create )
91         {
92             cart = (Cart)new InitialContext JavaDoc().lookup("ejb/Cart");
93             session.setAttribute( CART_KEY, cart );
94         }
95
96         return cart;
97     }
98
99     public Cart getCart( HttpServletRequest JavaDoc request )
100         throws Exception JavaDoc
101     {
102         return getCart( true, request );
103     }
104
105     public Petstore getPetstore( )
106         throws Exception JavaDoc
107     {
108         return (Petstore)new InitialContext JavaDoc().lookup("ejb/Petstore");
109     }
110
111     public String JavaDoc getString( String JavaDoc key )
112     {
113         try
114         {
115             return ResourceBundle.getBundle( BUNDLE_NAME ).getString( key );
116         }
117         catch ( Exception JavaDoc e )
118         {
119             return "???" + key + "???";
120         }
121     }
122
123     protected void initSession( Customer customer,
124                                 HttpServletRequest JavaDoc request )
125     {
126         HttpSession JavaDoc session = request.getSession( );
127         session.setAttribute( USERID_KEY, customer.getUserId( ) );
128         session.setAttribute( USERNAME_KEY, customer.getFirstname( ) + " " + customer.getLastname( ) );
129     }
130 }
131
Popular Tags