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.Iterator;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import olstore.domain.logic.OlstoreFacade;
033: import olstore.dto.OrderEntry;
034:
035: import org.springframework.validation.BindException;
036: import org.springframework.web.servlet.ModelAndView;
037: import org.springframework.web.servlet.mvc.SimpleFormController;
038:
039: /**
040: * A controller to handle the update order action.
041: */
042: public class UpdateOrderController extends SimpleFormController {
043:
044: // The olstore bean instance provided via Spring.
045: private OlstoreFacade olstore;
046:
047: /**
048: * Create a new form controller and set the form views as well as the
049: * backing object class.
050: */
051: public UpdateOrderController() {
052: setSessionForm(true);
053: setValidateOnBinding(false);
054: setCommandClass(OrderFormHelper.class);
055: setFormView("updateOrder");
056: setSuccessView("updateOrder");
057: }
058:
059: /**
060: * Spring injection setter method.
061: * @param olstore the bean instance that is injected via Spring.
062: */
063: public void setOlstore(OlstoreFacade olstore) {
064: this .olstore = olstore;
065: }
066:
067: /**
068: * Return a new helper object that will be used as this form's backing object.
069: * @param request the HttpServletRequest.
070: * @return a backing object for this form.
071: */
072: protected Object formBackingObject(HttpServletRequest request)
073: throws Exception {
074: return new OrderFormHelper(olstore.findOrdersForUpdate());
075: }
076:
077: /**
078: * This form's validation method to handle the backing object's fields.
079: * @param request the HttpServletRequest.
080: * @param command the form's backing object.
081: * @param errors the reported from the validation.
082: */
083: protected void onBindAndValidate(HttpServletRequest request,
084: Object command, BindException errors) throws Exception {
085: // ValidationUtils.rejectIfEmptyOrWhitespace(errors, "fname", "FNAME_REQUIRED", "First name is required.");
086: // ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lname", "LNAME_REQUIRED", "Last name is required.");
087: // TODO
088: }
089:
090: /**
091: * The map used in this form's view.
092: * @param request the HttpServletRequest.
093: * @return a map representing the data for this form's view.
094: */
095: protected Map referenceData(HttpServletRequest request)
096: throws Exception {
097: Map model = ControllerHelper.initModel(request, olstore);
098: model.put("page_title", "Edit Order Status - Olstore");
099: return model;
100: }
101:
102: /**
103: * Handles the submission of this form.
104: *
105: * @param request the HttpServletRequest.
106: * @param response the HttpServletResponse.
107: * @param command this forms backing object.
108: * @param errors this form's errors as reported via validation.
109: * @return a new model and view to represent this form.
110: */
111: protected ModelAndView onSubmit(HttpServletRequest request,
112: HttpServletResponse response, Object command,
113: BindException errors) throws Exception {
114: // supply the errors to the view inside the model map
115: Map model = errors.getModel();
116: // add the reference data once again so it will continue to be displayed
117: model.putAll(referenceData(request));
118:
119: OrderFormHelper orders = (OrderFormHelper) command;
120: try {
121: Iterator orderIt = orders.getOrdersList().iterator();
122: while (orderIt.hasNext()) {
123: OrderEntry order = (OrderEntry) orderIt.next();
124: olstore.setStatus(order.getOrderId(), order
125: .getOrderStatus());
126: }
127: } catch (Exception ex) {
128: model
129: .put("error",
130: "There was an error trying to update order status.");
131: return showForm(request, response, errors, model);
132: }
133:
134: model.put("message",
135: "Your changes have been successfully recorded.");
136: model.put("command", formBackingObject(request));
137: return new ModelAndView("updateOrder", model);
138: }
139: }
|