KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > OrderStatusAction


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 package olstore.action;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Iterator;
32 import javax.servlet.http.*;
33 import olstore.entity.OrderLocalHome;
34 import olstore.entity.OrderLocal;
35 import olstore.dto.OrderEntry;
36 import olstore.form.UpdateOrderStatusForm;
37 import olstore.framework.EJBHomeFactory;
38
39 import org.apache.struts.action.*;
40 //import org.apache.commons.beanutils.BeanUtils;
41

42
43 public class OrderStatusAction extends DemoBaseAction {
44     
45     /**
46      * Acts as a relay for all item related tasks with a default action
47      *
48      */

49     public ActionForward execute ( ActionMapping mapping,
50             ActionForm form,
51             HttpServletRequest request,
52             HttpServletResponse response
53     ) throws Exception {
54         
55         
56         String submitType = ((UpdateOrderStatusForm)form).getSubmitType();
57         if ( submitType==null || submitType.equals("")) {
58             loadOrders ( mapping, form, request, response );
59         }else if(submitType.equals("update")){
60             try{
61                 updateOrders(mapping, form, request, response);
62                 loadOrders ( mapping, form, request, response );
63                 ActionMessages messages = new ActionMessages();
64                 ActionMessage msg = new ActionMessage("order.update.success");
65                 messages.add("success", msg);
66                 saveMessages(request, messages);
67             }catch(Exception e){
68                 ActionErrors errors = new ActionErrors();
69                 errors.add("error", new ActionError("errors.order.update", e.getMessage() ));
70                 saveErrors(request, errors);
71                 return (new ActionForward(mapping.getInput()));
72             }
73         }
74         
75         return mapping.findForward ( "/admin/updateStatus" );
76         
77     }
78     
79     public void resetForm ( ActionMapping mapping,
80             ActionForm form,
81             HttpServletRequest request,
82             HttpServletResponse response
83     ) throws Exception {
84         ((UpdateOrderStatusForm) form).reset();
85     }
86     
87     public void loadOrders ( ActionMapping mapping,
88             ActionForm form,
89             HttpServletRequest request,
90             HttpServletResponse response
91     ) throws Exception {
92         ArrayList orderEntries = new ArrayList();
93         EJBHomeFactory factory = EJBHomeFactory.getInstance();
94         OrderLocalHome orderLocalHome = (OrderLocalHome) factory.getLocalHome(EJBHomeFactory.ORDER);
95         Collection orderLocals = orderLocalHome.findByStatus("Delivered");
96         if(!orderLocals.isEmpty()){
97             Iterator orderIterator = orderLocals.iterator();
98             while(orderIterator.hasNext()){
99                 OrderLocal orderLocal = (OrderLocal) orderIterator.next();
100                 OrderEntry orderEntry = new
101                 OrderEntry(orderLocal);
102                 orderEntries.add(orderEntry);
103                 ((UpdateOrderStatusForm) form).setOrders(orderEntries);
104             }
105         }else{
106             ActionMessages messages = new ActionMessages();
107             ActionMessage msg = new
108             ActionMessage("order.load.empty");
109             messages.add("success", msg);
110             saveMessages(request, messages);
111             resetForm(mapping, form, request, response);
112         }
113         
114     }
115     
116     public void updateOrders ( ActionMapping mapping,
117             ActionForm form,
118             HttpServletRequest request,
119             HttpServletResponse response
120     ) throws Exception {
121         Iterator ordersIterator = ((UpdateOrderStatusForm) form).getOrders().iterator();
122         EJBHomeFactory factory = EJBHomeFactory.getInstance();
123         OrderLocalHome orderLocalHome = (OrderLocalHome) factory.getLocalHome(EJBHomeFactory.ORDER);
124         while(ordersIterator.hasNext()){
125             OrderEntry orderEntry = (OrderEntry) ordersIterator.next();
126             OrderLocal orderLocal = orderLocalHome.findByPrimaryKey(orderEntry.getOrderId());
127             orderLocal.setStatus(orderEntry.getOrderStatus());
128         }
129     }
130     
131     
132 }
133
Popular Tags