001 package com.bea.medrec.beans;
002
003 import com.bea.medrec.utils.MedRecWebAppUtils;
004 import com.bea.medrec.value.User;
005 import javax.servlet.http.HttpServletRequest;
006 import org.apache.struts.action.ActionErrors;
007 import org.apache.struts.action.ActionMapping;
008 import org.apache.struts.validator.Resources;
009
010 /**
011 * <p>User bean to hold user login and username/password registration.
012 * This form has the following fields,
013 * with default values in square brackets:
014 * <ul>
015 * <li><b>password</b> - Entered password value
016 * <li><b>username</b> - Entered username value
017 * </ul>
018 * </p>
019 *
020 * @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
021 */
022 public final class UserBean extends BaseBean {
023 // Instance Variables
024 private String username = "";
025 private String password = "";
026
027 // Constructors
028 public UserBean() {
029 }
030
031 // Getters
032 public String getUsername() {
033 return this.username;
034 }
035
036 public String getPassword() {
037 return this.password;
038 }
039
040 // Setters
041 public void setUsername(String username) {
042 this.username = MedRecWebAppUtils.cleanParam(username);
043 }
044
045 public void setPassword(String password) {
046 this.password = MedRecWebAppUtils.cleanParam(password);
047 }
048
049 // Public Methods
050 public void reset() {
051 this.password = "";
052 this.username = "";
053 }
054
055 /**
056 * <p>Validate registration.</p>
057 *
058 * @param mapping
059 * @param request
060 *
061 * @return ActionErrors
062 */
063 public ActionErrors validate(ActionMapping mapping,
064 HttpServletRequest request) {
065 ActionErrors errors = new ActionErrors();
066 // only validate if the user has clicked "Login"
067 String loginSubmit = Resources.getMessage(request, "button.Login");
068 if (loginSubmit.equals(request.getParameter("action"))) {
069 errors = super.validate(mapping, request);
070 }
071 return errors;
072 }
073
074 /**
075 * <p>Converts user presentation bean to user value object.</p>
076 *
077 * @return User
078 */
079 public User toUser() {
080 return new User(getUsername(), getPassword(), null);
081 }
082
083 public String toString() {
084 StringBuffer str = new StringBuffer();
085 str.append("UserBean [");
086 str.append("Username: " + username);
087 str.append(" | Password: " + printPassword());
088 str.append("]");
089
090 return str.toString();
091 }
092
093 private String printPassword() {
094 StringBuffer pwd = new StringBuffer();
095 for (int i = 0; i < password.length(); i++) {
096 pwd.append("*");
097 }
098 return pwd.toString();
099 }
100 }
|