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.math.BigDecimal;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Vector;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import olstore.domain.logic.OlstoreFacade;
036: import olstore.dto.ItemValue;
037: import olstore.dto.PictureValue;
038: import olstore.dto.PropertyValue;
039:
040: import org.springframework.validation.BindException;
041: import org.springframework.validation.ValidationUtils;
042: import org.springframework.web.servlet.ModelAndView;
043: import org.springframework.web.servlet.mvc.SimpleFormController;
044:
045: /**
046: * Controls the creating of a new item.
047: */
048: public class ItemController extends SimpleFormController {
049:
050: // This form's olstore instance injected by spring.
051: private OlstoreFacade olstore;
052:
053: /**
054: * Creates a new item controller with values set for the form's view as well as
055: * the command class to use as the form's backing object.
056: *
057: */
058: public ItemController() {
059: setSessionForm(true);
060: setValidateOnBinding(false);
061: setCommandClass(ItemValue.class);
062: setFormView("createItem");
063: setSuccessView("createItem");
064: }
065:
066: /**
067: * Spring injection method to set this form's olstore instance.
068: * @param olstore this form's olstore instance object.
069: */
070: public void setOlstore(OlstoreFacade olstore) {
071: this .olstore = olstore;
072: }
073:
074: /**
075: * Returns this form's backing object.
076: *
077: * @param request the HttpServletRequest
078: * @return this form's backing object.
079: */
080: protected Object formBackingObject(HttpServletRequest request)
081: throws Exception {
082: String itemId = request.getParameter("itemId");
083: if (itemId != null) {
084: return olstore.getItemById(itemId);
085: } else {
086: ItemValue value = new ItemValue();
087: value.setTypeId(olstore.getDefaultType());
088: value.setProperties(olstore.getPropertiesForType(olstore
089: .getDefaultType()));
090: value.setPrice(new BigDecimal(0));
091: return value;
092: }
093: }
094:
095: /**
096: * The validation method used to validate the fields of the command object.
097: *
098: * @param request the HttpServletRequest
099: * @param command this form's backing object.
100: * @param errors the errors that occurred while validating this form.
101: */
102: protected void onBindAndValidate(HttpServletRequest request,
103: Object command, BindException errors) throws Exception {
104:
105: if (!isFormChangeRequest(request)) {
106: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
107: "NAME_REQUIRED", "Item name is required.");
108: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
109: "shortDesc", "SHRT_REQUIRED",
110: "Short description is required.");
111: ValidationUtils.rejectIfEmptyOrWhitespace(errors,
112: "longDesc", "LONG_REQUIRED",
113: "Long description is required.");
114: ItemValue item = (ItemValue) command;
115: Iterator i = item.getProperties().iterator();
116: while (i.hasNext()) {
117: PropertyValue prop = (PropertyValue) i.next();
118: if (prop.getName() == null
119: || prop.getName().length() == 0) {
120: errors.rejectValue("properties", "PROP_REQUIRED",
121: "Property name is required.");
122: }
123: }
124: }
125: }
126:
127: /**
128: * Returns a basic map to use in this form's view to store basic information.
129: * @param request the HttpServletRequest.
130: * @return the model for this form's view.
131: */
132: protected Map referenceData(HttpServletRequest request)
133: throws Exception {
134: Map model = ControllerHelper.initModel(request, olstore);
135: model.put("page_title", "Create/Edit an Item - Olstore");
136: model.put("types", this .olstore.getTypes());
137: return model;
138: }
139:
140: /**
141: * Handles when this form is submitted and returns a new model and view
142: * to represent the form.
143: *
144: * @param request the HttpServletRequest.
145: * @param response the HttpServletResponse.
146: * @param errors the errors that occured during validation.
147: * @return a new model and view to represent this form.
148: */
149: protected ModelAndView onSubmit(HttpServletRequest request,
150: HttpServletResponse response, Object command,
151: BindException errors) throws Exception {
152:
153: ItemValue item = (ItemValue) command;
154:
155: // Checks to see if there are any items or images already in this item
156: // if there aren't any, set an empty one to prevent problems on the view.
157: try {
158: if (item.getProperties() == null) {
159: item.setProperties(new ArrayList());
160: }
161: if (item.getPictures() == null) {
162: item.setPictures(new ArrayList());
163: }
164:
165: this .olstore.saveItem(item);
166: } catch (Exception ex) {
167: return showForm(request, response, errors);
168: }
169:
170: // supply the errors to the view inside the model map
171: Map model = errors.getModel();
172: // add the reference data once again so it will continue to be displayed
173: model.putAll(referenceData(request));
174: return new ModelAndView("createItem", model);
175: }
176:
177: /**
178: * If the incoming request is to change the form, this method is invoked.
179: *
180: * @param request the HttpServletRequest.
181: * @param response the HttpServletResponse.
182: * @param command this form's backing object.
183: */
184: protected void onFormChange(HttpServletRequest request,
185: HttpServletResponse response, Object command)
186: throws Exception {
187: ItemValue item = (ItemValue) command;
188: if (request.getParameter("add-picture") != null) {
189: try {
190: item.getPictures().add(new PictureValue());
191: } catch (NullPointerException e) {
192: Vector v = new Vector();
193: v.add(new PictureValue());
194: item.setPictures(v);
195: }
196: } else {
197: item.setProperties(olstore.getPropertiesForType(item
198: .getTypeId()));
199: }
200: }
201:
202: /**
203: * Checks to see if the request is to change the form or a regular submit request.
204: *
205: * @param request the HttpServletRequest.
206: * @return true if the request is to change the form, false otherwise.
207: */
208: protected boolean isFormChangeRequest(HttpServletRequest request) {
209: return request.getParameter("save-item") == null;
210: }
211:
212: }
|