View Javadoc

1   package org.appfuse.webapp.components;
2   
3   import org.apache.tapestry5.*;
4   import org.apache.tapestry5.annotations.*;
5   import org.apache.tapestry5.corelib.components.Form;
6   import org.apache.tapestry5.internal.services.StringValueEncoder;
7   import org.apache.tapestry5.ioc.Messages;
8   import org.apache.tapestry5.ioc.annotations.Inject;
9   import org.apache.tapestry5.services.javascript.JavaScriptSupport;
10  import org.appfuse.model.Role;
11  import org.appfuse.model.User;
12  import org.appfuse.service.RoleManager;
13  import org.appfuse.webapp.AppFuseEventConstants;
14  import org.appfuse.webapp.services.CountryService;
15  import org.appfuse.webapp.services.impl.RoleEncoder;
16  import org.slf4j.Logger;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * Generic form for User manipulation
25   *
26   * @author Serge Eby
27   * @version $Id: UserForm.java 5 2008-08-30 09:59:21Z serge.eby $
28   */
29  @SupportsInformalParameters
30  public class UserForm implements ClientElement, FormValidationControl {
31  
32      @Inject
33      private Logger logger;
34  
35      @Parameter(required = true, autoconnect = true)
36      @Property
37      private User user;
38  
39      @Parameter("true")
40      @Property
41      private boolean signup;
42  
43      @Parameter(value = "message:button.save", defaultPrefix = BindingConstants.LITERAL)
44      @Property
45      private String submitLabel;
46  
47      @Parameter(allowNull = true)
48      private String from;
49  
50      @Parameter(value = "true")
51      @Property
52      private boolean cookieLogin;
53  
54      @Property
55      @Parameter
56      private String heading;
57  
58      @Parameter(allowNull = true)
59      @Property
60      private List<Role> selectedRoles;
61  
62      @Property(write = false)
63      private String infoMessage;
64  
65      @Inject
66      private ComponentResources resources;
67  
68      @Inject
69      private Messages messages;
70  
71      @Inject
72      private JavaScriptSupport jsSupport;
73  
74      @Inject
75      HttpServletRequest request;
76  
77      @Inject
78      private RoleManager roleManager;
79  
80      @Inject
81      private CountryService countryService;
82  
83  
84      @SuppressWarnings("unchecked")
85      @Environmental
86      private TrackableComponentEventCallback eventCallback;
87  
88      @Component(parameters = "validatationId=componentResources.id",
89              publishParameters = "clientValidation,autofocus,zone")
90      private Form form;
91  
92      public Form getForm() {
93          return form;
94      }
95  
96      boolean onValidateFromForm() {
97          resources.triggerEvent(AppFuseEventConstants.VALIDATE_PASSWORD, null, null);
98          return true;
99      }
100 
101     public String getClientId() {
102         return form.getClientId();
103     }
104 
105     public void clearErrors() {
106         form.clearErrors();
107     }
108 
109     public boolean getHasErrors() {
110         return form.getHasErrors();
111     }
112 
113     public boolean isValid() {
114         return form.isValid();
115     }
116 
117     public void recordError(Field field, String errorMessage) {
118         form.recordError(field, errorMessage);
119     }
120 
121     public void recordError(String errorMessage) {
122         form.recordError(errorMessage);
123     }
124 
125     @Cached
126     public Map<String, String> getCountryModel() {
127         return countryService.getAvailableCountries();
128     }
129 
130     public ValueEncoder<String> getCountryEncoder() {
131         return new StringValueEncoder();
132     }
133 
134     @Cached
135     public List<Role> getRoleModel() {
136         return roleManager.getAll();
137     }
138 
139     public ValueEncoder getRoleEncoder() {
140         return new RoleEncoder(roleManager);
141     }
142 
143     public List<String> getUserRoles() {
144         List<String> userRoles = new ArrayList<String>();
145         if (user != null) {
146             for (Role r : user.getRoles()) {
147                 userRoles.add(r.getName());
148             }
149         }
150         return userRoles;
151     }
152 
153     public boolean isUserVersionNull() {
154         return (user == null || user.getVersion() == null);
155     }
156 
157     public boolean isFromList() {
158         return "list".equals(from);
159     }
160 
161     public String getFrom() {
162         return from;
163     }
164 
165     public void setFrom(String from) {
166         this.from = from;
167     }
168 
169     public boolean isUserPersisted() {
170         return user != null && user.getId() != null;
171     }
172 
173     public void setInfoMessage(String infoMessage) {
174         this.infoMessage = infoMessage;
175     }
176 
177     public String getConfirmDeletion() {
178         return messages.format("delete.confirm", "user: " + user.getUsername());
179     }
180 
181 
182     public boolean isSelf() {
183         return isUserPersisted() && user.getUsername().equals(request.getRemoteUser());
184     }
185 
186 }