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.ArrayList;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import olstore.domain.logic.OlstoreFacade;
034: import olstore.dto.PropertyValue;
035: import olstore.dto.TypeValue;
036:
037: import org.springframework.validation.BindException;
038: import org.springframework.validation.ValidationUtils;
039: import org.springframework.web.servlet.ModelAndView;
040: import org.springframework.web.servlet.mvc.SimpleFormController;
041:
042: /**
043: * A simple form controller to handle creating a new type.
044: */
045: public class CreateTypeController extends SimpleFormController {
046:
047: // Olstore instance injected by Spring.
048: private OlstoreFacade olstore;
049:
050: /**
051: * Creates a new controller and sets the class to use as a backing object
052: * as well as views, and validation.
053: *
054: */
055: public CreateTypeController() {
056: setSessionForm(true);
057: setValidateOnBinding(false);
058: setCommandClass(TypeValue.class);
059: setFormView("createType");
060: setSuccessView("createType");
061: }
062:
063: /**
064: * Spring method for dependency injection.
065: * @param olstore supplied via spring.
066: */
067: public void setOlstore(OlstoreFacade olstore) {
068: this .olstore = olstore;
069: }
070:
071: /**
072: * Returns the object that will be used as the form's backing object.
073: *
074: * @param request the HttpServletRequest object.
075: * @return the object that this form will use as its backing object.
076: */
077: protected Object formBackingObject(HttpServletRequest request)
078: throws Exception {
079: TypeValue type = new TypeValue();
080: ArrayList props = new ArrayList();
081: props.add(new PropertyValue());
082: type.setProperties(props);
083: return type;
084: }
085:
086: /**
087: * Sets what validation is to be done on the command object's fields.
088: *
089: * @param request the HttpServletRequest
090: * @param command the command object used as the backing object.
091: * @param errors where the errors are reported if validation fails.
092: */
093: protected void onBindAndValidate(HttpServletRequest request,
094: Object command, BindException errors) throws Exception {
095:
096: System.out.println("need to validate");
097: if (!isFormChangeRequest(request)) {
098: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
099: "NAME_REQUIRED", "Type name is required.");
100: TypeValue type = (TypeValue) command;
101: Iterator i = type.getProperties().iterator();
102: while (i.hasNext()) {
103: PropertyValue prop = (PropertyValue) i.next();
104: if (prop.getName() == null
105: || prop.getName().length() == 0) {
106: errors.rejectValue("properties", "PROP_REQUIRED",
107: "Property name is required.");
108: }
109: }
110: }
111: }
112:
113: /**
114: * The initial model to be used in this form.
115: *
116: * @param request the HttpServletRequest object.
117: * @return a map that represents the model to be used in the view.
118: */
119: protected Map referenceData(HttpServletRequest request)
120: throws Exception {
121: Map model = ControllerHelper.initModel(request, olstore);
122: model.put("page_title", "Create New Type - Olstore");
123: model.put("types", this .olstore.getTypes());
124: return model;
125: }
126:
127: /**
128: * Handles the submission of the simple form object, and returns a new
129: * model and view to represent the changes.
130: *
131: * @param request the HttpServletRequest
132: * @param response the HttpServletResponse
133: * @param command the backing object of this form.
134: * @param errors the errors to report for this form's validation.
135: * @return a new model and view to represent the form.
136: */
137: protected ModelAndView onSubmit(HttpServletRequest request,
138: HttpServletResponse response, Object command,
139: BindException errors) throws Exception {
140: // supply the errors to the view inside the model map
141: Map model = errors.getModel();
142: // add the reference data once again so it will continue to be displayed
143: model.putAll(referenceData(request));
144:
145: TypeValue type = (TypeValue) command;
146: try {
147: this .olstore.saveType(type);
148: } catch (Exception ex) {
149: model.put("error",
150: "There was an error trying to save your new type.");
151: return showForm(request, response, errors, model);
152: }
153: // refresh the types displayed on the sidebar to include the newly added one
154: model.put("types", this .olstore.getTypes());
155:
156: model.put("message", "New type successfully saved.");
157: return new ModelAndView("createType", model);
158: }
159:
160: /**
161: * Called when the form is changed so that if a new property is needed
162: * it can be properly added to the backing command object.
163: *
164: * @param request the HttpServletRequest.
165: * @param response the HttpServletResponse
166: * @param command the backing objecdt of this form.
167: */
168: protected void onFormChange(HttpServletRequest request,
169: HttpServletResponse response, Object command)
170: throws Exception {
171: TypeValue type = (TypeValue) command;
172: // add another property to the object
173: type.getProperties().add(new PropertyValue());
174: }
175:
176: /**
177: * Checks to see if the request is to change the form, or an actual submit.
178: *
179: * @param request the HttpServletRequest
180: * @return true if the request is to change the form, false otherwise.
181: */
182: protected boolean isFormChangeRequest(HttpServletRequest request) {
183: return request.getParameter("add-property") != null;
184: }
185:
186: }
|