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.annotations.Inject;
8   import org.apache.tapestry5.services.javascript.JavaScriptSupport;
9   import org.appfuse.model.Role;
10  import org.appfuse.model.User;
11  import org.appfuse.service.RoleManager;
12  import org.appfuse.webapp.AppFuseEventConstants;
13  import org.appfuse.webapp.services.CountryService;
14  import org.appfuse.webapp.services.impl.RoleEncoder;
15  import org.slf4j.Logger;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.Map;
20  
21  /**
22   * Generic form for User manipulation
23   *
24   * @author Serge Eby
25   * @version $Id: UserForm.java 5 2008-08-30 09:59:21Z serge.eby $
26   */
27  @SupportsInformalParameters
28  public class UserForm implements ClientElement, FormValidationControl {
29  
30      @Inject
31      private Logger logger;
32  
33      @Parameter(required = true, autoconnect = true)
34      @Property
35      private User user;
36  
37      @Parameter("true")
38      @Property
39      private boolean signup;
40  
41      @Parameter(value = "message:button.save", defaultPrefix = BindingConstants.LITERAL)
42      @Property
43      private String submitLabel;
44  
45      @Parameter(allowNull = true)
46      private String from;
47  
48      @Parameter(value = "true")
49      @Property
50      private boolean cookieLogin;
51  
52      @Parameter(value = "false")
53      private boolean clientValidation;
54  
55      @Property
56      @Parameter
57      private String heading;
58  
59      @Parameter(allowNull = true)
60      @Property
61      private List<Role> selectedRoles;
62  
63      @Property(write = false)
64      private String infoMessage;
65  
66  
67      @Inject
68      private ComponentResources resources;
69  
70      @Inject
71      private JavaScriptSupport jsSupport;
72  
73      @Inject
74      private RoleManager roleManager;
75  
76      @Inject
77      private CountryService countryService;
78  
79  
80      @SuppressWarnings("unchecked")
81      @Environmental
82      private TrackableComponentEventCallback eventCallback;
83  
84      @Component(parameters = "validatationId=componentResources.id",
85              publishParameters = "clientValidation,autofocus,zone")
86      private Form form;
87  
88      public Form getForm() {
89          return form;
90      }
91  
92  
93  
94      boolean onValidateFromForm() {
95          resources.triggerEvent(AppFuseEventConstants.VALIDATE_PASSWORD, null, null);
96          return true;
97      }
98  
99      public String getClientId() {
100         return form.getClientId();
101     }
102 
103 
104     public void clearErrors() {
105         form.clearErrors();
106     }
107 
108     public boolean getHasErrors() {
109         return form.getHasErrors();
110     }
111 
112     public boolean isValid() {
113         return form.isValid();
114     }
115 
116     public void recordError(Field field, String errorMessage) {
117         form.recordError(field, errorMessage);
118     }
119 
120     public void recordError(String errorMessage) {
121         form.recordError(errorMessage);
122     }
123 
124     @Cached
125     public Map<String, String> getCountryModel() {
126         return countryService.getAvailableCountries();
127     }
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 //
178 //    void afterRender() {
179 //        jsSupport.addScript("$F.focusFirstElement('%s'); highlightFormElements();", getClientId());
180 //
181 //    }
182 }