View Javadoc

1   package org.gnomekr.potron.web.view;
2   
3   import static org.gnomekr.potron.util.NavigationCases.NAV_HOME;
4   import static org.gnomekr.potron.util.NavigationCases.NAV_PROFILE_UPDATE;
5   import static org.gnomekr.potron.util.NavigationCases.NAV_USER_REGISTER;
6   
7   import javax.faces.application.FacesMessage;
8   
9   import net.sf.acegisecurity.providers.encoding.PasswordEncoder;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.apache.myfaces.util.MessageUtils;
13  import org.gnomekr.potron.data.User;
14  import org.gnomekr.potron.service.IUserManager;
15  import org.gnomekr.potron.service.UserExistsException;
16  
17  /***
18   * UserView.java
19   * @author Xavier Cho
20   * @version $Revision 1.1 $ $Date: 2005/07/21 21:36:45 $
21   * @jsf.bean
22   *              name = "user"
23   *              scope = "request"
24   * @jsf.navigation
25   *              from = "*"
26   *              to = "/register.jsf"
27   *              result = "user.register"
28   * @jsf.navigation
29   *              from = "*"
30   *              to = "/profile.jsf"
31   *              result = "user.update"
32   */
33  public class UserView extends AbstractFacesView {
34  
35      private IUserManager manager;
36      private PasswordEncoder passwordEncoder;
37  
38      private String userName;
39      private String realName;
40      private String password;
41      private String passwordConfirm;
42      private String email;
43      private String homepage;
44      private String comment;
45  
46      /***
47       * @return
48       * @jsf.managed-property
49       *          value = "#{userManager}"
50       */
51      public IUserManager getManager() {
52          return manager;
53      }
54  
55      /***
56       * @return Returns the passwordEncoder.
57       * @jsf.managed-property
58       *          value = "#{passwordEncoder}"
59       */
60      public PasswordEncoder getPasswordEncoder() {
61          return passwordEncoder;
62      }
63  
64      public String prepareUserUpdate() {
65          User user = manager.getUser(getCurrentUserName());
66  
67          if (user != null) {
68              this.userName = user.getUserName();
69              this.realName = user.getRealName();
70              this.email = user.getEmail();
71              this.homepage = user.getHomepage();
72              this.comment = user.getComment();
73          }
74  
75          return NAV_PROFILE_UPDATE;
76      }
77  
78      public String createUser() {
79          User user = new User();
80          user.setUserName(userName);
81          user.setRealName(realName);
82          user.setPassword(passwordEncoder.encodePassword(password, null));
83          user.setEmail(email);
84          user.setComment(comment);
85          user.setHomepage(homepage);
86  
87          try {
88              manager.createUser(user);
89          } catch (UserExistsException e) {
90              MessageUtils.addMessage(
91                      FacesMessage.SEVERITY_WARN,
92                      "error.user.exists",
93                      null);
94  
95              return NAV_USER_REGISTER;
96          }
97  
98          MessageUtils.addMessage(
99                  FacesMessage.SEVERITY_INFO,
100                 "msg.user.register.successful",
101                 null);
102 
103         return NAV_HOME;
104     }
105 
106     public String updateUser() {
107         User user = manager.getUser(getCurrentUserName());
108         user.setRealName(realName);
109 
110         if (StringUtils.isNotBlank(password)) {
111             user.setPassword(passwordEncoder.encodePassword(password, null));
112         }
113 
114         user.setEmail(email);
115         user.setComment(comment);
116         user.setHomepage(homepage);
117 
118         manager.updateUser(user);
119 
120         MessageUtils.addMessage(
121                 FacesMessage.SEVERITY_INFO,
122                 "msg.user.update.successful",
123                 null);
124 
125         return NAV_HOME;
126     }
127 
128     public String getEmail() {
129         return email;
130     }
131 
132     public String getHomepage() {
133         return homepage;
134     }
135 
136     public String getComment() {
137         return comment;
138     }
139 
140     public String getPassword() {
141         return password;
142     }
143 
144     /***
145      * @param password
146      */
147     public void setPassword(String password) {
148         this.password = password;
149     }
150 
151     public String getPasswordConfirm() {
152         return passwordConfirm;
153     }
154 
155     public String getRealName() {
156         return realName;
157     }
158 
159     public String getUserName() {
160         return userName;
161     }
162 
163     /***
164      * @param userName
165      */
166     public void setUserName(String userName) {
167         this.userName = userName;
168     }
169 
170     /***
171      * @param realName The realName to set.
172      */
173     public void setRealName(String realName) {
174         this.realName = realName;
175     }
176 
177     /***
178      * @param passwordConfirm
179      */
180     public void setPasswordConfirm(String passwordConfirm) {
181         this.passwordConfirm = passwordConfirm;
182     }
183 
184     /***
185      * @param email
186      */
187     public void setEmail(String email) {
188         this.email = email;
189     }
190 
191     /***
192      * @param homepage
193      */
194     public void setHomepage(String homepage) {
195         this.homepage = homepage;
196     }
197 
198     /***
199      * @param comment
200      */
201     public void setComment(String comment) {
202         this.comment = comment;
203     }
204 
205     /***
206      * @param manager
207      */
208     public void setManager(IUserManager manager) {
209         this.manager = manager;
210     }
211 
212     /***
213      * @param passwordEncoder The passwordEncoder to set.
214      */
215     public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
216         this.passwordEncoder = passwordEncoder;
217     }
218 }