View Javadoc

1   package org.appfuse.webapp.services.impl;
2   
3   import org.apache.tapestry5.ValueEncoder;
4   import org.apache.tapestry5.ioc.internal.util.InternalUtils;
5   import org.appfuse.model.User;
6   import org.appfuse.service.UserManager;
7   
8   
9   public class UserEncoder implements ValueEncoder<User> {
10  
11      private UserManager userManager;
12  
13      public UserEncoder(UserManager userManager) {
14          this.userManager = userManager;
15      }
16  
17      public String toClient(User value) {
18          return value != null ? String.valueOf(value.getId()) : null;
19      }
20  
21      public User toValue(String clientValue) {
22  
23          if (clientValue == null || "null".equals(clientValue)) {
24              return null;
25          }
26  
27          Long id = null;
28          try {
29              id = Long.valueOf(clientValue);
30          }
31          catch (Exception ex) {
32              return new User();
33          }
34          User user = userManager.get(id);
35          // Reset confirmed password
36          if (user != null) {
37              user.setConfirmPassword(user.getPassword());
38          }
39  
40          return user;
41      }
42  
43  }