1 23 package org.enhydra.barracuda.discRack.pres.screens; 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.plankton.data.*; 42 import org.enhydra.barracuda.core.util.dom.*; 43 import org.enhydra.barracuda.core.util.http.*; 44 import org.apache.log4j.*; 45 46 import org.enhydra.barracuda.discRack.biz.*; 47 import org.enhydra.barracuda.discRack.biz.person.*; 48 import org.enhydra.barracuda.discRack.pres.events.*; 49 import org.enhydra.barracuda.discRack.pres.services.*; 50 import org.enhydra.barracuda.discRack.pres.xmlc.*; 51 52 53 89 public class RegistrationScreen extends DefaultEventGateway { 90 91 protected static Logger logger = Logger.getLogger(RegistrationScreen.class.getName()); 94 95 private static final String REGISTRATION_FORM = RegistrationScreen.class.getName()+".RegistrationForm"; private static final String REGISTRATION_ERR = RegistrationScreen.class.getName()+".RegistrationErr"; 99 private ListenerFactory getRegisterFactory = new EventForwardingFactory(new RenderRegister()); 101 private ListenerFactory renderRegisterFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderRegisterHandler();} public String getListenerID() {return getID(RenderRegisterHandler.class);}}; 102 private ListenerFactory doRegisterFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new DoRegisterHandler();} public String getListenerID() {return getID(DoRegisterHandler.class);}}; 103 104 106 107 111 public RegistrationScreen() { 112 specifyLocalEventInterests(getRegisterFactory, GetRegister.class); 114 specifyLocalEventInterests(renderRegisterFactory, RenderRegister.class); 115 specifyLocalEventInterests(doRegisterFactory, DoRegister.class); 116 } 117 118 119 126 class DoRegisterHandler extends DefaultBaseEventListener { 127 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 128 HttpServletRequest req = context.getRequest(); 130 131 if (logger.isDebugEnabled()) logger.debug("Creating registration form"); 133 RegistrationForm rf = new RegistrationForm(); 134 ValidationException ve = null; 135 try { 136 if (logger.isDebugEnabled()) logger.debug("Mapping/Validating registration form"); 138 rf.map(req).validate(true); 139 140 if (logger.isDebugEnabled()) logger.debug("Creating user account"); 142 HttpSession session = SessionServices.getSession(req); 143 try { 144 Person person = new Person(); 145 person.setLogin(rf.getStringVal(RegistrationForm.USER)); 146 person.setPassword(rf.getStringVal(RegistrationForm.PASSWORD)); 147 person.setFirstname(rf.getStringVal(RegistrationForm.FIRST_NAME)); 148 person.setLastname(rf.getStringVal(RegistrationForm.LAST_NAME)); 149 person.save(); 150 if (logger.isDebugEnabled()) logger.debug("Logging user in"); 151 LoginServices.logIn(session, person); 152 } catch (DiscRackBusinessException e) { 153 if (logger.isDebugEnabled()) logger.debug("Biz err:"+e+" Make sure user is Logged out"); 156 LoginServices.logOut(session); 157 throw new ValidationException(this, "Error creating account: "+e.getMessage()+" Please try again. If the problem persists contact your system administrator.", e); 158 } 159 } catch (ValidationException e) { 160 if (logger.isDebugEnabled()) logger.debug("Validation Exception: "+e); 161 if (logger.isDebugEnabled()) CollectionsUtil.printStackTrace(e.getExceptionList(), 0, logger, null); 162 ve = e; 163 } 164 165 if (logger.isDebugEnabled()) logger.debug("Saving form, errors"); 167 context.putState(REGISTRATION_FORM, rf); 168 context.putState(REGISTRATION_ERR, ve); 169 170 if (ve==null) { 174 if (logger.isDebugEnabled()) logger.debug("Redirecting to GetLogin"); 175 throw new ClientSideRedirectException(new GetLogin()); 176 } else { 177 if (logger.isDebugEnabled()) logger.debug("InterruptDispatch to GetRegister"); 178 throw new InterruptDispatchException(new GetRegister()); 179 } 180 } 181 } 182 183 184 190 class RenderRegisterHandler extends DefaultBaseEventListener { 191 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 192 RegisterHTML page = (RegisterHTML) DefaultDOMLoader.getGlobalInstance().getDOM(RegisterHTML.class); 196 197 227 228 if (logger.isDebugEnabled()) logger.debug("Creating template component"); 230 Node node = page.getDocument().getElementById("RegistrationForm"); 231 BTemplate templateComp = new BTemplate(new RegistrationModel(context)); 232 templateComp.setView(new DefaultTemplateView(node)); 233 try {templateComp.render(new DefaultViewContext(context));} 234 catch (RenderException re) {logger.warn("Render err:"+re);} 235 236 if (logger.isDebugEnabled()) logger.debug("Rendering document"); 239 new DefaultDOMWriter().write(page, context.getResponse()); 240 } 241 } 242 243 249 class RegistrationModel extends AbstractTemplateModel { 250 251 RegistrationForm fm = null; 252 ValidationException ve = null; 253 254 public RegistrationModel(EventContext ec) { 256 fm = (RegistrationForm) ec.getState(REGISTRATION_FORM); 257 ve = (ValidationException) ec.getState(REGISTRATION_ERR); 258 } 259 260 public String getName() {return "Registration";} 262 263 public Object getItem(String key) { 265 ViewContext vc = getViewContext(); 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("LoginButton")) { 277 BAction baComp = new BAction(new GetLogin()); 278 baComp.setDisableBackButton(true); 279 return baComp; 280 } else if (key.equals("RegisterButton")) { 281 BAction baComp = new BAction(new DoRegister()); 282 baComp.setDisableBackButton(true); 283 return baComp; 284 } else if (key.equals("Errors")) { 285 if (ve==null) return null; 286 List errlist = ve.getExceptionList(); 287 StringBuffer sb = new StringBuffer (errlist.size()>1 ? "There were several errors:" : ""); 288 Iterator it = errlist.iterator(); 289 while (it.hasNext()) { 290 sb.append(" "+((ValidationException) it.next()).getMessage()); 291 } 292 return sb.toString(); 293 } else { 294 return super.getItem(key); 295 } 296 } 297 } 298 299 305 class RegistrationForm extends DefaultFormMap { 306 static final String FIRST_NAME = "firstname"; 308 static final String LAST_NAME = "lastname"; 309 static final String USER = "login"; 310 static final String PASSWORD = "password"; 311 static final String REPASSWORD = "repassword"; 312 313 public RegistrationForm() { 314 if (logger.isDebugEnabled()) logger.debug("Defining Registration form elements"); 316 this.defineElement(new DefaultFormElement(FIRST_NAME, FormType.STRING, null, new NotNullValidator("You must enter a First name."))); 317 this.defineElement(new DefaultFormElement(LAST_NAME, FormType.STRING, null, new NotNullValidator("You must enter a Last name."))); 318 this.defineElement(new DefaultFormElement(USER, FormType.STRING, null, new NotNullValidator("You must enter a Login."))); 319 this.defineElement(new DefaultFormElement(PASSWORD, FormType.STRING, null, new NotNullValidator("You must enter a Password."))); 320 this.defineElement(new DefaultFormElement(REPASSWORD, FormType.STRING, null, new NotNullValidator("You must Confirm your password."))); 321 322 if (logger.isDebugEnabled()) logger.debug("Defining Registration form validator"); 324 this.defineValidator(new RegistrationValidator()); 325 } 326 } 327 328 331 class RegistrationValidator extends DefaultFormValidator { 332 public void validateForm(FormMap imap, boolean deferExceptions) throws ValidationException { 333 if (logger.isDebugEnabled()) logger.debug("Validating registration form"); 334 335 if (logger.isDebugEnabled()) logger.debug("Make sure passwords match"); 337 RegistrationForm map = (RegistrationForm) imap; 338 DeferredValidationException defValException = new DeferredValidationException(); 339 String password = map.getStringVal(RegistrationForm.PASSWORD); 340 String repassword = map.getStringVal(RegistrationForm.REPASSWORD); 341 if ((password != null && repassword != null) && !password.equals(repassword)) defValException.addSubException(new DeferredValidationException(map.getElement(RegistrationForm.PASSWORD), "Passwords do not match. Please re-enter.")); 342 343 if (logger.isDebugEnabled()) logger.debug("Make sure the login isn't taken"); 345 String user = map.getStringVal(RegistrationForm.USER); 346 if (user != null) { 347 try { 348 if (PersonFactory.findPerson(user)!=null) defValException.addSubException(new DeferredValidationException(map.getElement(RegistrationForm.USER), "This login already taken. Please select another.")); 349 } catch (DiscRackBusinessException e) { 350 defValException.addSubException(new DeferredValidationException(map.getElement(RegistrationForm.USER), "Error checking login against database. Please try again. If the problem persists, contact your system administrator.", e)); 351 } 352 } 353 if (defValException.hasSubExceptions()) { 354 throw defValException; 355 } 356 } 357 } 358 } 359 360 | Popular Tags |