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: import org.springframework.web.servlet.view.RedirectView;
040:
041: /**
042: * @author glapouch
043: *
044: */
045: /**
046: * @author glapouch
047: *
048: */
049: public class CreateUserController extends SimpleFormController {
050:
051: /** This form's olstore instance that is provided via Spring. */
052: private OlstoreFacade olstore;
053:
054: /**
055: * Sets up this form controller by providing the parent SimpleFormController
056: * with details of what form backing object to use and which view to display. *
057: */
058: public CreateUserController() {
059: setSessionForm(true);
060: setValidateOnBinding(false);
061: setCommandClass(UserForm.class);
062: setFormView("createUser");
063: setSuccessView("createUser");
064: }
065:
066: /**
067: * Spring injection setter method for the olstore instance.
068: * @param olstore the olstore bean instance for this form.
069: */
070: public void setOlstore(OlstoreFacade olstore) {
071: this .olstore = olstore;
072: }
073:
074: /* (non-Javadoc)
075: * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
076: */
077: protected Object formBackingObject(HttpServletRequest request)
078: throws Exception {
079: return new UserForm(new UserValue(), new AddressValue());
080: }
081:
082: /* (non-Javadoc)
083: * @see org.springframework.web.servlet.mvc.BaseCommandController#onBindAndValidate(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
084: */
085: protected void onBindAndValidate(HttpServletRequest request,
086: Object command, BindException errors) throws Exception {
087: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user.fname",
088: "FNAME_REQUIRED", "First name is required.");
089: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user.lname",
090: "LNAME_REQUIRED", "Last name is required.");
091: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
092: "user.emailAdd", "EAMAIL_REQUIRED",
093: "Email address is required.");
094: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
095: "user.phoneNum", "EAMAIL_REQUIRED",
096: "Email address is required.");
097: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
098: "user.passwd", "PASS1_REQUIRED",
099: "Password is required.");
100: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
101: "passwordConfirm", "PASS2_REQUIRED",
102: "Confirmation password is required.");
103: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
104: "user.username", "USERNAME_REQUIRED",
105: "Username is required.");
106:
107: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
108: "address.country", "COUNTRY_REQUIRED",
109: "Country name is required.");
110: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
111: "address.city", "CITY_REQUIRED",
112: "Name of the city is required.");
113: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
114: "address.postalCode", "CODE_REQUIRED",
115: "Postal code is required.");
116: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
117: "address.street1", "STREET_REQUIRED",
118: "Street name and number are required.");
119: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
120: "address.province", "PROVINCE_REQUIRED",
121: "Province is required.");
122:
123: UserForm form = (UserForm) command;
124: if (!form.getPasswordConfirm().equals(
125: form.getUser().getPasswd())) {
126: errors.rejectValue("user.passwd", "PASS_NOTMATCH",
127: "The two passwords you entered did not match.");
128: }
129:
130: try {
131: if (this .olstore.getUser(form.getUser().getUsername()) != null) {
132: errors.rejectValue("user.username", "USERNAME_TAKEN",
133: "The username you chose is already taken.");
134: }
135: } catch (Exception e) {
136: // if an exception was thrown because no such user was found then there is no problem
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
142: */
143: protected Map referenceData(HttpServletRequest request)
144: throws Exception {
145: Map model = ControllerHelper.initModel(request, olstore);
146: model.put("page_title", "User Registration - Olstore");
147: return model;
148: }
149:
150: /* (non-Javadoc)
151: * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
152: */
153: protected ModelAndView onSubmit(HttpServletRequest request,
154: HttpServletResponse response, Object command,
155: BindException errors) throws Exception {
156: // supply the errors to the view inside the model map
157: Map model = errors.getModel();
158: // add the reference data once again so it will continue to be displayed
159: model.putAll(referenceData(request));
160:
161: UserForm user = (UserForm) command;
162: try {
163: this .olstore.createUser(user.getUser(), user.getAddress());
164: } catch (Exception ex) {
165: model
166: .put("error",
167: "There was an error trying to create your account.");
168: return showForm(request, response, errors, model);
169: }
170:
171: return new ModelAndView(new RedirectView("/store/index.do",
172: true));
173: }
174: }
|