View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.order.webapp.controller.action;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import net.sf.oness.order.model.bo.Order;
26  import net.sf.oness.order.model.bo.OrderItem;
27  import net.sf.oness.order.model.to.OrderWithParty;
28  import net.sf.oness.order.webapp.controller.actionform.ItemActionForm;
29  import net.sf.oness.order.webapp.controller.actionform.MasterActionForm;
30  
31  import org.apache.commons.beanutils.BeanUtils;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  import org.apache.struts.action.ActionMessage;
36  import org.apache.struts.action.ActionMessages;
37  
38  /***
39   * Order actions
40   * 
41   * @author Carlos Sanchez
42   * @version $Revision: 1.9 $
43   */
44  public class OrderItemAction extends AbstractAction {
45  
46      /***
47       * Add a product to current order.
48       * 
49       * @param mapping
50       * @param actionForm
51       * @param request
52       * @param response
53       * @return ActionForward success
54       * @throws Exception
55       */
56      public ActionForward create(ActionMapping mapping, ActionForm actionForm,
57              HttpServletRequest request, HttpServletResponse response)
58              throws Exception {
59  
60          /* get current order from session */
61          HttpSession session = request.getSession(true);
62          Long orderId = (Long) session
63                  .getAttribute(CURRENT_ORDER_ID_SESSION_ATTRIBUTE);
64  
65          if (orderId == null) {
66              ActionMessages errors = getErrors(request);
67              if (errors == null)
68                  errors = new ActionMessages();
69              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
70                      "errors.noOrder"));
71              saveErrors(request, errors);
72              return mapping.findForward("noOrder");
73          }
74  
75          MasterActionForm form = (MasterActionForm) actionForm;
76          ItemActionForm[] items = form.getItems();
77  
78          Collection orderItems = new ArrayList(items.length);
79          for (int i = 0; i < items.length; i++) {
80  
81              boolean price = true;
82              if ((items[i].getPrice() == null)
83                      || (items[i].getPrice().equals(""))) {
84                  items[i].setPrice("0");
85                  price = false;
86              }
87  
88              OrderItem orderItem = new OrderItem();
89              BeanUtils.copyProperties(orderItem, items[i]);
90              if (!price)
91                  orderItem.setPrice(null);
92  
93              orderItem.setDeliveryDocketItems(new ArrayList(0));
94              orderItem.setId(null);
95  
96              if (orderItem.getQuantity().intValue() != 0)
97                  orderItems.add(orderItem);
98          }
99  
100         OrderWithParty orderWithParty = getFacade().createOrderItems(
101                 orderItems, orderId);
102         request.setAttribute("order", orderWithParty.getOrder());
103         request.setAttribute("party", orderWithParty.getParty());
104         request.setAttribute("items", orderWithParty.getItems());
105         request.setAttribute("itemsSize", Integer.toString(orderWithParty
106                 .getItems().size()));
107 
108         Order order = orderWithParty.getOrder();
109         Collection c = order.getItems();
110         order.setItems(null);
111         BeanUtils.copyProperties(actionForm, order);
112         order.setItems(c);
113 
114         /* Return ActionForward. */
115         return mapping.findForward("success");
116     }
117 
118 }