001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.domain.User;
020: import info.jtrac.util.ValidationUtils;
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.Map;
024: import org.apache.wicket.behavior.HeaderContributor;
025: import org.apache.wicket.markup.html.IHeaderContributor;
026: import org.apache.wicket.markup.html.IHeaderResponse;
027: import org.apache.wicket.markup.html.WebMarkupContainer;
028: import org.apache.wicket.markup.html.WebPage;
029: import org.apache.wicket.markup.html.form.Button;
030: import org.apache.wicket.markup.html.form.CheckBox;
031: import org.apache.wicket.markup.html.form.DropDownChoice;
032: import org.apache.wicket.markup.html.form.Form;
033: import org.apache.wicket.markup.html.form.FormComponent;
034: import org.apache.wicket.markup.html.form.IChoiceRenderer;
035: import org.apache.wicket.markup.html.form.PasswordTextField;
036: import org.apache.wicket.markup.html.form.TextField;
037: import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
038: import org.apache.wicket.markup.html.link.Link;
039: import org.apache.wicket.markup.html.panel.FeedbackPanel;
040: import org.apache.wicket.model.BoundCompoundPropertyModel;
041: import org.apache.wicket.validation.IValidatable;
042: import org.apache.wicket.validation.validator.AbstractValidator;
043:
044: /**
045: * user edit form
046: */
047: public class UserFormPage extends BasePage {
048:
049: private WebPage previous;
050:
051: public void setPrevious(WebPage previous) {
052: this .previous = previous;
053: }
054:
055: public UserFormPage() {
056: User user = new User();
057: user.setLocale(getJtrac().getDefaultLocale());
058: add(new UserForm("form", user));
059: }
060:
061: public UserFormPage(User user) {
062: add(new UserForm("form", user));
063: }
064:
065: /**
066: * wicket form
067: */
068: private class UserForm extends Form {
069:
070: private User user;
071: private String password;
072: private String passwordConfirm;
073: private boolean sendNotifications;
074:
075: private JtracFeedbackMessageFilter filter;
076:
077: public User getUser() {
078: return user;
079: }
080:
081: public void setUser(User user) {
082: this .user = user;
083: }
084:
085: public String getPassword() {
086: return password;
087: }
088:
089: public void setPassword(String password) {
090: this .password = password;
091: }
092:
093: public String getPasswordConfirm() {
094: return passwordConfirm;
095: }
096:
097: public void setPasswordConfirm(String passwordConfirm) {
098: this .passwordConfirm = passwordConfirm;
099: }
100:
101: public boolean isSendNotifications() {
102: return sendNotifications;
103: }
104:
105: public void setSendNotifications(boolean sendNotifications) {
106: this .sendNotifications = sendNotifications;
107: }
108:
109: public UserForm(String id, final User user) {
110:
111: super (id);
112: this .user = user;
113: if (user.getId() == 0) {
114: sendNotifications = true;
115: }
116: FeedbackPanel feedback = new FeedbackPanel("feedback");
117: filter = new JtracFeedbackMessageFilter();
118: feedback.setFilter(filter);
119: add(feedback);
120:
121: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
122: this );
123: setModel(model);
124:
125: // delete button only if edit ======================================
126: Button delete = new Button("delete") {
127: @Override
128: public void onSubmit() {
129: int count = getJtrac()
130: .loadCountOfHistoryInvolvingUser(user);
131: if (count > 0) {
132: UserForm.this
133: .error(localize("user_delete.notPossible"));
134: return;
135: }
136: String heading = localize("user_delete.confirm");
137: String line1 = localize("user_delete.line1");
138: String warning = localize("user_delete.line2");
139: ConfirmPage confirm = new ConfirmPage(
140: UserFormPage.this , heading, warning,
141: new String[] { line1 }) {
142: public void onConfirm() {
143: getJtrac().removeUser(user);
144: // logged in user may have been allocated to space with this user assigned
145: UserFormPage.this .refreshPrincipal();
146: setResponsePage(new UserListPage());
147: }
148: };
149: setResponsePage(confirm);
150: }
151: };
152: delete.setDefaultFormProcessing(false);
153: // only way you can delete someone else is if you are an admin
154: // and of course, don't allow deleting self or the slightly special default admin user
155: if (!getPrincipal().isAdminForAllSpaces()
156: || user.getId() == getPrincipal().getId()
157: || user.getId() <= 1) {
158: delete.setVisible(false);
159: }
160: add(delete);
161:
162: // login name ======================================================
163: final TextField loginName = new TextField("user.loginName");
164: loginName.setRequired(true);
165: loginName.add(new ErrorHighlighter());
166: loginName.setOutputMarkupId(true);
167: add(new HeaderContributor(new IHeaderContributor() {
168: public void renderHead(IHeaderResponse response) {
169: response
170: .renderOnLoadJavascript("document.getElementById('"
171: + loginName.getMarkupId()
172: + "').focus()");
173: }
174: }));
175: // validation: does user already exist with same loginName?
176: loginName.add(new AbstractValidator() {
177: protected void onValidate(IValidatable v) {
178: String s = (String) v.getValue();
179: User temp = getJtrac().loadUser(s);
180: if (temp != null && temp.getId() != user.getId()) {
181: error(v);
182: }
183: }
184:
185: @Override
186: protected String resourceKey() {
187: return "user_form.loginId.error.exists";
188: }
189: });
190: // validation no strange characters
191: loginName.add(new AbstractValidator() {
192: protected void onValidate(IValidatable v) {
193: String s = (String) v.getValue();
194: if (!ValidationUtils.isValidLoginName(s)) {
195: error(v);
196: }
197: }
198:
199: @Override
200: protected String resourceKey() {
201: return "user_form.loginId.error.invalid";
202: }
203: });
204: add(loginName);
205: // locked ==========================================================
206: WebMarkupContainer locked = new WebMarkupContainer("locked");
207: // don't show the locked checkbox when creating new user
208: // only way you can edit someone else is if you are an admin
209: // and of course, don't allow locking self
210: if (user.getId() != 0
211: && user.getId() != getPrincipal().getId()) {
212: locked.add(new CheckBox("user.locked"));
213: } else {
214: locked.setVisible(false);
215: }
216: add(locked);
217: // name ============================================================
218: add(new TextField("user.name").setRequired(true).add(
219: new ErrorHighlighter()));
220: // email ===========================================================
221: add(new TextField("user.email").setRequired(true).add(
222: new ErrorHighlighter()));
223: // locale ==========================================================
224: final Map<String, String> locales = getJtrac().getLocales();
225: List<String> localeKeys = new ArrayList<String>(locales
226: .keySet());
227: DropDownChoice localeChoice = new DropDownChoice(
228: "user.locale", localeKeys, new IChoiceRenderer() {
229: public Object getDisplayValue(Object o) {
230: return locales.get(o);
231: }
232:
233: public String getIdValue(Object o, int i) {
234: return o.toString();
235: }
236: });
237: add(localeChoice);
238: // hide e-mail message if edit =====================================
239: WebMarkupContainer hide = new WebMarkupContainer("hide");
240: if (user.getId() > 0) {
241: hide.setVisible(false);
242: }
243: add(hide);
244: // password ========================================================
245: final PasswordTextField passwordField = new PasswordTextField(
246: "password");
247: add(passwordField);
248: passwordField.setRequired(false);
249: // confirm password ================================================
250: final PasswordTextField confirmPasswordField = new PasswordTextField(
251: "passwordConfirm");
252: confirmPasswordField.setRequired(false);
253: confirmPasswordField.add(new ErrorHighlighter());
254: add(confirmPasswordField);
255: // validation, do the passwords match
256: add(new AbstractFormValidator() {
257: public FormComponent[] getDependentFormComponents() {
258: return new FormComponent[] { passwordField,
259: confirmPasswordField };
260: }
261:
262: public void validate(Form form) {
263: String a = (String) passwordField
264: .getConvertedInput();
265: String b = (String) confirmPasswordField
266: .getConvertedInput();
267: if ((a != null && !a.equals(b))
268: || (b != null && !b.equals(a))) {
269: confirmPasswordField
270: .error(localize("user_form.passwordConfirm.error"));
271: }
272: }
273: });
274: // send notifications ==============================================
275: WebMarkupContainer hideSendNotifications = new WebMarkupContainer(
276: "hideSendNotifications");
277: add(hideSendNotifications);
278: if (getPrincipal().getId() != user.getId()) {
279: hideSendNotifications.add(new CheckBox(
280: "sendNotifications"));
281: } else {
282: hideSendNotifications.setVisible(false);
283: }
284: // cancel link
285: add(new Link("cancel") {
286: public void onClick() {
287: if (previous == null) {
288: setResponsePage(new OptionsPage());
289: } else {
290: if (previous instanceof UserListPage) {
291: ((UserListPage) previous)
292: .setSelectedUserId(user.getId());
293: }
294: setResponsePage(previous);
295: }
296: }
297: });
298: }
299:
300: @Override
301: protected void validate() {
302: filter.reset();
303: super .validate();
304: }
305:
306: @Override
307: protected void onSubmit() {
308: if (password != null) {
309: getJtrac().storeUser(user, password, sendNotifications);
310: } else if (user.getId() == 0) {
311: // new user, generate password and send mail
312: getJtrac().storeUser(user, null, true);
313: } else {
314: getJtrac().storeUser(user);
315: }
316: refreshPrincipal(user);
317: if (previous == null) {
318: UserListPage page = new UserListPage();
319: page.setSelectedUserId(user.getId());
320: setResponsePage(page);
321: } else {
322: if (previous instanceof UserListPage) {
323: ((UserListPage) previous).setSelectedUserId(user
324: .getId());
325: }
326: if (previous instanceof SpaceAllocatePage) {
327: SpaceAllocatePage sap = (SpaceAllocatePage) previous;
328: // TODO refactor this better, but for now this is so that the newly created user
329: // appears in the drop down for allocation and is preselected
330: previous = new SpaceAllocatePage(sap.getSpaceId(),
331: sap.getPrevious(), user.getId());
332: }
333: setResponsePage(previous);
334: }
335: }
336: }
337:
338: }
|