1 23 package barracudaDiscRack.presentation.personMgmt; 24 25 import java.io.*; 26 import java.util.*; 27 import java.net.*; 28 import javax.servlet.*; 29 import javax.servlet.http.*; 30 31 import org.w3c.dom.*; 32 import org.w3c.dom.html.*; 33 34 import org.enhydra.xml.xmlc.*; 35 36 import org.enhydra.barracuda.core.comp.*; 37 import org.enhydra.barracuda.core.event.*; 38 import org.enhydra.barracuda.core.event.helper.*; 39 import org.enhydra.barracuda.core.forms.*; 40 import org.enhydra.barracuda.core.forms.validators.*; 41 import org.enhydra.barracuda.core.util.dom.*; 42 import org.enhydra.barracuda.plankton.data.CollectionsUtil; 43 44 import org.apache.log4j.*; 45 46 import barracudaDiscRack.presentation.*; 48 import barracudaDiscRack.business.*; 49 import barracudaDiscRack.business.person.*; 50 import barracudaDiscRack.presentation.events.*; 51 import barracudaDiscRack.presentation.personMgmt.events.*; 52 53 54 90 public class RegistrationEventGateway extends BaseDiscRackEventGateway { 91 92 protected static Logger logger = Logger.getLogger(RegistrationEventGateway.class.getName()); 94 95 private static final String REGISTRATION_FORM = RegistrationEventGateway.class.getName()+".RegistrationForm"; private static final String REGISTRATION_ERR = RegistrationEventGateway.class.getName()+".RegistrationErr"; 99 private ListenerFactory getRegisterFactory = new EventForwardingFactory(new RenderRegister()); 101 private ListenerFactory renderRegisterFactory = new DefaultListenerFactory() { 102 public BaseEventListener getInstance() { 103 return new RenderRegisterHandler(); 104 } public String getListenerID() { 105 return getID(RenderRegisterHandler.class); 106 } 107 }; 108 private ListenerFactory doRegisterFactory = new DefaultListenerFactory() { 109 public BaseEventListener getInstance() { 110 return new DoRegisterHandler(); 111 } public String getListenerID() { 112 return getID(DoRegisterHandler.class); 113 } 114 }; 115 116 118 119 123 public RegistrationEventGateway() { 124 specifyLocalEventInterests(getRegisterFactory, GetRegister.class); 126 specifyLocalEventInterests(renderRegisterFactory, RenderRegister.class); 127 specifyLocalEventInterests(doRegisterFactory, DoRegister.class); 128 } 129 130 131 138 class DoRegisterHandler extends DefaultBaseEventListener { 139 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 140 initUsingContext( context ); 142 143 HttpServletRequest req = context.getRequest(); 145 146 if ( logger.isDebugEnabled() ) logger.debug("Creating registration form"); 148 RegistrationForm rf = new RegistrationForm(); 149 ValidationException ve = null; 150 try { 151 if ( logger.isDebugEnabled() ) logger.debug("Mapping/Validating registration form"); 153 rf.map(req).validate(true); 154 155 if ( logger.isDebugEnabled() ) logger.debug("Creating user account"); 157 HttpSession session = getComms().session.getHttpSession(); 158 try { 159 Person person = new Person(); 160 person.setLogin(rf.getStringVal(RegistrationForm.USER)); 161 person.setPassword(rf.getStringVal(RegistrationForm.PASSWORD)); 162 person.setFirstname(rf.getStringVal(RegistrationForm.FIRST_NAME)); 163 person.setLastname(rf.getStringVal(RegistrationForm.LAST_NAME)); 164 person.save(); 165 if ( logger.isDebugEnabled() ) logger.debug("Logging user in"); 166 setUser( person ); 167 } catch ( DiscRackBusinessException e ) { 168 if ( logger.isDebugEnabled() ) logger.debug("Biz err:"+e+" Make sure user is Logged out"); 171 removeUserFromSession(); 172 throw new ValidationException(this, "Error creating account: "+e.getMessage()+" Please try again. If the problem persists contact your system administrator.", e); 173 } catch ( DiscRackPresentationException e ) { 174 if ( logger.isDebugEnabled() ) logger.debug("Biz err:"+e+" Make sure user is Logged out"); 175 removeUserFromSession(); 176 throw new ValidationException(this, "Error creating account: "+e.getMessage()+" Please try again. If the problem persists contact your system administrator.", e); 177 } 178 } catch ( ValidationException e ) { 179 if ( logger.isDebugEnabled() ) logger.debug("Validation Exception: "+e); 180 181 if ( logger.isDebugEnabled() ) 182 CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 183 184 ve = e; 185 } 186 187 if ( logger.isDebugEnabled() ) logger.debug("Saving form, errors"); 189 context.putState(REGISTRATION_FORM, rf); 190 context.putState(REGISTRATION_ERR, ve); 191 192 if ( ve==null ) { 196 if ( logger.isDebugEnabled() ) logger.debug("Redirecting to GetLogin"); 197 throw new ClientSideRedirectException(new GetLogin()); 198 } else { 199 if ( logger.isDebugEnabled() ) logger.debug("InterruptDispatch to GetRegister"); 200 throw new InterruptDispatchException(new GetRegister()); 201 } 202 } 203 } 204 205 206 212 class RenderRegisterHandler extends DefaultBaseEventListener { 213 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 214 initUsingContext( context ); 216 217 RegisterHTML page = (RegisterHTML) getComms().xmlcFactory.create(RegisterHTML.class); 219 220 if ( logger.isDebugEnabled() ) logger.debug("Creating template component"); 222 Node node = page.getDocument().getElementById("RegistrationForm"); 223 224 TemplateView tv = new DefaultTemplateView(node); 225 TemplateModel tm = new RegistrationModel(context); 226 BTemplate templateComp = new BTemplate(tm); 227 templateComp.setView(tv); 228 229 try { 230 templateComp.render(new DefaultViewContext(context)); 231 } catch ( RenderException re ) { 232 logger.warn("Render err:"+re); 233 } 234 235 if ( logger.isDebugEnabled() ) logger.debug("Rendering document"); 238 new DefaultDOMWriter().write(page, context.getResponse()); 239 } 240 } 241 242 248 class RegistrationModel extends AbstractTemplateModel { 249 250 RegistrationForm fm = null; 251 ValidationException ve = null; 252 253 public RegistrationModel(EventContext ec) { 255 fm = (RegistrationForm) ec.getState(REGISTRATION_FORM); 256 ve = (ValidationException) ec.getState(REGISTRATION_ERR); 257 } 258 259 public String getName() { 261 return "Registration"; 262 } 263 264 public Object getItem(String key) { 266 if ( key.equals("FirstName") ) { 267 return(fm!=null ? fm.getStringVal(RegistrationForm.FIRST_NAME, "") : ""); 268 } else if ( key.equals("LastName") ) { 269 return(fm!=null ? fm.getStringVal(RegistrationForm.LAST_NAME, "") : ""); 270 } else if ( key.equals("Username") ) { 271 return(fm!=null ? fm.getStringVal(RegistrationForm.USER, "") : ""); 272 } else if ( key.equals("Password") ) { 273 return(fm!=null ? fm.getStringVal(RegistrationForm.PASSWORD, "") : ""); 274 } else if ( key.equals("Repassword") ) { 275 return(fm!=null ? fm.getStringVal(RegistrationForm.REPASSWORD, "") : ""); 276 } else if ( key.equals("Errors") ) { 277 if ( ve==null ) return ""; 278 List errlist = ve.getExceptionList(); 279 StringBuffer sb = new StringBuffer (errlist.size()>1 ? "There were several errors:" : ""); 280 Iterator it = errlist.iterator(); 281 while ( it.hasNext() ) { 282 sb.append(" "+((ValidationException) it.next()).getMessage()); 283 } 284 return sb.toString(); 285 } else 286 return super.getItem(key); 287 } 288 289 } 290 291 297 class RegistrationForm extends DefaultFormMap { 298 static final String FIRST_NAME = "firstname"; 300 static final String LAST_NAME = "lastname"; 301 static final String USER = "login"; 302 static final String PASSWORD = "password"; 303 static final String REPASSWORD = "repassword"; 304 305 public RegistrationForm() { 306 if ( logger.isDebugEnabled() ) logger.debug("Defining Registration form elements"); 308 this.defineElement(new DefaultFormElement(FIRST_NAME, FormType.STRING, null, new NotNullValidator("You must enter a First name."))); 309 this.defineElement(new DefaultFormElement(LAST_NAME, FormType.STRING, null, new NotNullValidator("You must enter a Last name."))); 310 this.defineElement(new DefaultFormElement(USER, FormType.STRING, null, new NotNullValidator("You must enter a Login."))); 311 this.defineElement(new DefaultFormElement(PASSWORD, FormType.STRING, null, new NotNullValidator("You must enter a Password."))); 312 this.defineElement(new DefaultFormElement(REPASSWORD, FormType.STRING, null, new NotNullValidator("You must Confirm your password."))); 313 314 if ( logger.isDebugEnabled() ) logger.debug("Defining Registration form validator"); 316 this.defineValidator(new RegistrationValidator()); 317 } 318 } 319 320 323 class RegistrationValidator extends DefaultFormValidator { 324 public void validateForm(FormMap imap, boolean deferExceptions) throws ValidationException { 325 if ( logger.isDebugEnabled() ) logger.debug("Validating registration form"); 326 327 if ( logger.isDebugEnabled() ) logger.debug("Make sure passwords match"); 329 RegistrationForm map = (RegistrationForm) imap; 330 String password = map.getStringVal(RegistrationForm.PASSWORD); 331 String repassword = map.getStringVal(RegistrationForm.REPASSWORD); 332 if ( !password.equals(repassword) ) throw new ValidationException(map.getElement(RegistrationForm.PASSWORD), "Passwords do not match. Please re-enter."); 333 334 if ( logger.isDebugEnabled() ) logger.debug("Make sure the login isn't taken"); 336 String user = map.getStringVal(RegistrationForm.USER); 337 try { 338 if ( PersonFactory.findPerson(user)!=null ) throw new ValidationException(map.getElement(RegistrationForm.USER), "This login already taken. Please select another."); 339 } catch ( DiscRackBusinessException e ) { 340 throw new ValidationException(map.getElement(RegistrationForm.USER), "Error checking login against database. Please try again. If the problem persists, contact your system administrator.", e); 341 } 342 } 343 } 344 } 345 346 | Popular Tags |