001: /**
002: * Copyright (c) 2004 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: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.action;
027:
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import javax.servlet.http.*;
032: import olstore.entity.OrderLocalHome;
033: import olstore.entity.OrderLocal;
034: import olstore.dto.OrderEntry;
035: import olstore.form.UpdateOrderStatusForm;
036: import olstore.framework.EJBHomeFactory;
037:
038: import org.apache.struts.action.*;
039:
040: //import org.apache.commons.beanutils.BeanUtils;
041:
042: public class OrderStatusAction extends DemoBaseAction {
043:
044: /**
045: * Acts as a relay for all item related tasks with a default action
046: *
047: */
048: public ActionForward execute(ActionMapping mapping,
049: ActionForm form, HttpServletRequest request,
050: HttpServletResponse response) throws Exception {
051:
052: String submitType = ((UpdateOrderStatusForm) form)
053: .getSubmitType();
054: if (submitType == null || submitType.equals("")) {
055: loadOrders(mapping, form, request, response);
056: } else if (submitType.equals("update")) {
057: try {
058: updateOrders(mapping, form, request, response);
059: loadOrders(mapping, form, request, response);
060: ActionMessages messages = new ActionMessages();
061: ActionMessage msg = new ActionMessage(
062: "order.update.success");
063: messages.add("success", msg);
064: saveMessages(request, messages);
065: } catch (Exception e) {
066: ActionErrors errors = new ActionErrors();
067: errors.add("error", new ActionError(
068: "errors.order.update", e.getMessage()));
069: saveErrors(request, errors);
070: return (new ActionForward(mapping.getInput()));
071: }
072: }
073:
074: return mapping.findForward("/admin/updateStatus");
075:
076: }
077:
078: public void resetForm(ActionMapping mapping, ActionForm form,
079: HttpServletRequest request, HttpServletResponse response)
080: throws Exception {
081: ((UpdateOrderStatusForm) form).reset();
082: }
083:
084: public void loadOrders(ActionMapping mapping, ActionForm form,
085: HttpServletRequest request, HttpServletResponse response)
086: throws Exception {
087: ArrayList orderEntries = new ArrayList();
088: EJBHomeFactory factory = EJBHomeFactory.getInstance();
089: OrderLocalHome orderLocalHome = (OrderLocalHome) factory
090: .getLocalHome(EJBHomeFactory.ORDER);
091: Collection orderLocals = orderLocalHome
092: .findByStatus("Delivered");
093: if (!orderLocals.isEmpty()) {
094: Iterator orderIterator = orderLocals.iterator();
095: while (orderIterator.hasNext()) {
096: OrderLocal orderLocal = (OrderLocal) orderIterator
097: .next();
098: OrderEntry orderEntry = new OrderEntry(orderLocal);
099: orderEntries.add(orderEntry);
100: ((UpdateOrderStatusForm) form).setOrders(orderEntries);
101: }
102: } else {
103: ActionMessages messages = new ActionMessages();
104: ActionMessage msg = new ActionMessage("order.load.empty");
105: messages.add("success", msg);
106: saveMessages(request, messages);
107: resetForm(mapping, form, request, response);
108: }
109:
110: }
111:
112: public void updateOrders(ActionMapping mapping, ActionForm form,
113: HttpServletRequest request, HttpServletResponse response)
114: throws Exception {
115: Iterator ordersIterator = ((UpdateOrderStatusForm) form)
116: .getOrders().iterator();
117: EJBHomeFactory factory = EJBHomeFactory.getInstance();
118: OrderLocalHome orderLocalHome = (OrderLocalHome) factory
119: .getLocalHome(EJBHomeFactory.ORDER);
120: while (ordersIterator.hasNext()) {
121: OrderEntry orderEntry = (OrderEntry) ordersIterator.next();
122: OrderLocal orderLocal = orderLocalHome
123: .findByPrimaryKey(orderEntry.getOrderId());
124: orderLocal.setStatus(orderEntry.getOrderStatus());
125: }
126: }
127:
128: }
|