001: /**
002: * Copyright (c) 2006 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Greg Lapouchnian
022: * Patrick Smith
023: *
024: */package olstore.controller;
025:
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import olstore.domain.logic.OlstoreFacade;
032: import olstore.dto.AddressValue;
033: import olstore.dto.UserValue;
034:
035: import org.springframework.validation.BindException;
036: import org.springframework.validation.ValidationUtils;
037: import org.springframework.web.servlet.ModelAndView;
038: import org.springframework.web.servlet.mvc.SimpleFormController;
039:
040: /**
041: * Controller class used for editing a user's profile.
042: */
043: public class EditUserController extends SimpleFormController {
044:
045: // olstore instance injected by Spring.
046: private OlstoreFacade olstore;
047:
048: /**
049: * Creates a new form object with default values set, such as
050: * what command class to use as the backing object, and which views to use.
051: */
052: public EditUserController() {
053: setSessionForm(true);
054: setValidateOnBinding(false);
055: setCommandClass(UserForm.class);
056: setFormView("editUser");
057: setSuccessView("editUser");
058: }
059:
060: /**
061: * Spring injection method for setting the olstore bean instance.
062: * @param olstore the instance of olstore injected via spring.
063: */
064: public void setOlstore(OlstoreFacade olstore) {
065: this .olstore = olstore;
066: }
067:
068: /**
069: * Returns the backing object that this form will use.
070: *
071: * @param request the HttpServletRequest
072: * @return this form's backing object.
073: */
074: protected Object formBackingObject(HttpServletRequest request)
075: throws Exception {
076: String user = request.getRemoteUser();
077:
078: // Supply the form backing object UserForm with the User and Address DTOs
079: UserValue userValue = olstore.getUser(user);
080: AddressValue addressValue = new AddressValue();
081: addressValue.setStreet1(userValue.getAddress().getStreet1());
082: addressValue.setStreet2(userValue.getAddress().getStreet2());
083: addressValue.setCity(userValue.getAddress().getCity());
084: addressValue.setProvince(userValue.getAddress().getProvince());
085: addressValue.setPostalCode(userValue.getAddress()
086: .getPostalCode());
087: addressValue.setCountry(userValue.getAddress().getCountry());
088:
089: return new UserForm(userValue, addressValue);
090: }
091:
092: /**
093: * Validation method to handle the command's fields.
094: *
095: * @param request the HttpServletRequest.
096: * @param command the backing object for this form.
097: * @param errors the errors to report when validation fails.
098: */
099: protected void onBindAndValidate(HttpServletRequest request,
100: Object command, BindException errors) throws Exception {
101: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user.fname",
102: "FNAME_REQUIRED", "First name is required.");
103: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user.lname",
104: "LNAME_REQUIRED", "Last name is required.");
105: // TODO
106: }
107:
108: /**
109: * Returns the map model to use for this form's view.
110: *
111: * @param request the HttpServletRequest.
112: * @return a model to be used on this form's view.
113: */
114: protected Map referenceData(HttpServletRequest request)
115: throws Exception {
116: Map model = ControllerHelper.initModel(request, olstore);
117: model.put("page_title", "Edit Your Information - Olstore");
118: model.put("allUsers", this .olstore.getAllUsers());
119: return model;
120: }
121:
122: /**
123: * Handles the submission of this form.
124: *
125: * @param request the HttpServletRequest.
126: * @param response the HttpServletResponse.
127: * @param command this forms backing object.
128: * @param errors this form's errors as reported via validation.
129: * @return a new model and view to represent this form.
130: */
131: protected ModelAndView onSubmit(HttpServletRequest request,
132: HttpServletResponse response, Object command,
133: BindException errors) throws Exception {
134: // supply the errors to the view inside the model map
135: Map model = errors.getModel();
136: // add the reference data once again so it will continue to be displayed
137: model.putAll(referenceData(request));
138:
139: UserForm userForm = (UserForm) command;
140: try {
141: if (userForm.getUser().getFriendsListSelected() == null) {
142: userForm.getUser().setFriendsListSelected(
143: new String[] {});
144: }
145:
146: this .olstore.saveUser(userForm.getUser(), userForm
147: .getAddress(), userForm.getUser().getUsername());
148: } catch (Exception ex) {
149: model
150: .put("error",
151: "There was an error trying to save your account information.");
152: return showForm(request, response, errors, model);
153: }
154:
155: model.put("message",
156: "Your information has been successfully updated.");
157: model.put("command", formBackingObject(request));
158: return new ModelAndView("editUser", model);
159: }
160: }
|