1 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 42 43 public class OrderStatusAction extends DemoBaseAction { 44 45 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 |