1 4 package xpetstore.web.webwork.action; 5 6 import java.util.HashMap ; 7 import java.util.Locale ; 8 import java.util.Map ; 9 import java.util.MissingResourceException ; 10 import java.util.ResourceBundle ; 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 31 public abstract class BaseAction 32 extends ActionSupport 33 implements SessionAware, 34 ApplicationAware, 35 LocaleAware 36 { 37 39 public static final String BUNDLE_NAME = "Resources"; 40 public static final String CART_KEY = "cart"; 41 public static final String USERID_KEY = "userId"; 42 public static final String USERNAME_KEY = "username"; 43 44 46 protected Map _application; 47 private Session _hibernateSession; 48 protected Locale _locale; 49 protected Log _log = LogFactory.getLog( getClass( ) ); 50 protected String _redirectUri = ""; 51 protected Map _session; 52 53 55 protected void checkLength( String key, 56 String msg, 57 String 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 key, 67 String msg, 68 String 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 86 public String execute( ) 87 throws Exception 88 { 89 _log.info( "execute()" ); 90 return super.execute( ); 91 } 92 93 100 public Map getCart( ) 101 { 102 Map cart = ( Map ) _session.get( CART_KEY ); 103 104 if ( cart == null ) 105 { 106 cart = new HashMap ( ); 107 _session.put( CART_KEY, cart ); 108 } 109 110 return cart; 111 } 112 113 public Session getHibernateSession( ) 114 throws Exception 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 133 public Locale getLocale( ) 134 { 135 return _locale; 136 } 137 138 141 public String getRedirectUri( ) 142 { 143 return _redirectUri; 144 } 145 146 149 public String getText( String key ) 150 { 151 try 152 { 153 return getTexts( ).getString( key ); 154 } 155 catch ( MissingResourceException m ) 156 { 157 return "???" + key + "???"; 158 } 159 } 160 161 164 public ResourceBundle getTexts( ) 165 { 166 return ResourceBundle.getBundle( BUNDLE_NAME, getLocale( ) ); 167 } 168 169 public String getUserId( ) 170 { 171 return ( String ) _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 180 } 181 182 185 public void setApplication( Map application ) 186 { 187 _application = application; 188 } 189 190 193 public void setLocale( Locale locale ) 194 { 195 _locale = locale; 196 } 197 198 202 public void setRedirectUri( String nextUri ) 203 { 204 _redirectUri = nextUri; 205 } 206 207 210 public void setSession( Map session ) 211 { 212 _session = session; 213 } 214 } 215 | Popular Tags |