KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package xpetstore.web.webwork.action;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.MissingResourceException JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import cirrus.hibernate.Session;
16 import cirrus.hibernate.SessionFactory;
17
18 import webwork.action.ActionSupport;
19 import webwork.action.ApplicationAware;
20 import webwork.action.LocaleAware;
21 import webwork.action.SessionAware;
22
23 import xpetstore.domain.Customer;
24
25 import xpetstore.web.servlet.ActionServlet;
26
27
28 /**
29  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
30  */

31 public abstract class BaseAction
32     extends ActionSupport
33     implements SessionAware,
34                    ApplicationAware,
35                    LocaleAware
36 {
37     //~ Static fields/initializers ---------------------------------------------
38

39     public static final String JavaDoc BUNDLE_NAME = "Resources";
40     public static final String JavaDoc CART_KEY = "cart";
41     public static final String JavaDoc USERID_KEY = "userId";
42     public static final String JavaDoc USERNAME_KEY = "username";
43
44     //~ Instance fields --------------------------------------------------------
45

46     protected Map JavaDoc _application;
47     private Session _hibernateSession;
48     protected Locale JavaDoc _locale;
49     protected Log _log = LogFactory.getLog( getClass( ) );
50     protected String JavaDoc _redirectUri = "";
51     protected Map JavaDoc _session;
52
53     //~ Methods ----------------------------------------------------------------
54

55     protected void checkLength( String JavaDoc key,
56                                 String JavaDoc msg,
57                                 String JavaDoc value,
58                                 int length )
59     {
60         if ( ( value == null ) || ( value.length( ) < length ) )
61         {
62             addError( key, getText( msg ) );
63         }
64     }
65
66     protected void checkNotEmpty( String JavaDoc key,
67                                   String JavaDoc msg,
68                                   String JavaDoc value )
69     {
70         if ( ( value == null ) || ( value.length( ) == 0 ) )
71         {
72             addError( key, getText( msg ) );
73         }
74     }
75
76     protected void clearSession( )
77     {
78         _session.remove( USERID_KEY );
79         _session.remove( USERNAME_KEY );
80         _session.remove( CART_KEY );
81     }
82
83     /**
84      * @see webwork.action.Action#execute()
85      */

86     public String JavaDoc execute( )
87         throws Exception JavaDoc
88     {
89         _log.info( "execute()" );
90         return super.execute( );
91     }
92
93     /**
94      * Returns the shoppping cart.
95      * The cart is a <code>java.util.Map</code> containing the quantities of
96      * the items of the shopping cart indexed by their <code>itemId</code>
97      *
98      * @return Map
99      */

100     public Map JavaDoc getCart( )
101     {
102         Map JavaDoc cart = ( Map JavaDoc ) _session.get( CART_KEY );
103
104         if ( cart == null )
105         {
106             cart = new HashMap JavaDoc( );
107             _session.put( CART_KEY, cart );
108         }
109
110         return cart;
111     }
112
113     public Session getHibernateSession( )
114         throws Exception JavaDoc
115     {
116         if ( _hibernateSession == null )
117         {
118             SessionFactory factory = ( SessionFactory ) _application.get( ActionServlet.SESSION_FACTORY_KEY );
119             _hibernateSession = factory.openSession( );
120         }
121
122         if ( !_hibernateSession.isOpen( ) )
123         {
124             _hibernateSession.reconnect( );
125         }
126
127         return _hibernateSession;
128     }
129
130     /**
131      * @see webwork.action.ActionSupport#getLocale()
132      */

133     public Locale JavaDoc getLocale( )
134     {
135         return _locale;
136     }
137
138     /**
139      * @return String
140      */

141     public String JavaDoc getRedirectUri( )
142     {
143         return _redirectUri;
144     }
145
146     /**
147      * @see webwork.action.ActionSupport#getText(java.lang.String)
148      */

149     public String JavaDoc getText( String JavaDoc key )
150     {
151         try
152         {
153             return getTexts( ).getString( key );
154         }
155         catch ( MissingResourceException JavaDoc m )
156         {
157             return "???" + key + "???";
158         }
159     }
160
161     /**
162      * @see webwork.action.ActionSupport#getTexts()
163      */

164     public ResourceBundle JavaDoc getTexts( )
165     {
166         return ResourceBundle.getBundle( BUNDLE_NAME, getLocale( ) );
167     }
168
169     public String JavaDoc getUserId( )
170     {
171         return ( String JavaDoc ) _session.get( USERID_KEY );
172     }
173
174     protected void initSession( Customer customer )
175     {
176         _session.put( USERID_KEY, customer.getUserId( ) );
177         _session.put( USERNAME_KEY, customer.getFirstname( ) + " " + customer.getLastname( ) );
178
179         /* TODO set the locale */
180     }
181
182     /**
183      * @see webwork.action.ApplicationAware#setApplication(java.util.Map)
184      */

185     public void setApplication( Map JavaDoc application )
186     {
187         _application = application;
188     }
189
190     /**
191       * @see webwork.action.LocaleAware#setLocale(java.util.Locale)
192      */

193     public void setLocale( Locale JavaDoc locale )
194     {
195         _locale = locale;
196     }
197
198     /**
199      * Sets the nextUri.
200      * @param nextUri The nextUri to set
201      */

202     public void setRedirectUri( String JavaDoc nextUri )
203     {
204         _redirectUri = nextUri;
205     }
206
207     /**
208      * @see webwork.action.SessionAware#setSession(java.util.Map)
209      */

210     public void setSession( Map JavaDoc session )
211     {
212         _session = session;
213     }
214 }
215
Popular Tags