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.inventory.webapp.controller.action;
17  
18  import java.math.BigDecimal;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import net.sf.oness.common.webapp.controller.action.AutoDispatchAction;
28  import net.sf.oness.inventory.model.facade.InventoryFacadeDelegate;
29  import net.sf.oness.inventory.model.product.bo.Fare;
30  import net.sf.oness.inventory.model.product.bo.Model;
31  import net.sf.oness.inventory.model.product.bo.Price;
32  import net.sf.oness.inventory.model.product.bo.Product;
33  import net.sf.oness.inventory.webapp.controller.actionform.PriceActionForm;
34  import net.sf.oness.inventory.webapp.controller.actionform.PricesActionForm;
35  import net.sf.oness.inventory.webapp.controller.actionform.ProductPricesActionForm;
36  import net.sf.oness.inventory.webapp.controller.listener.StartupListener;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /***
43   * Model actions
44   * 
45   * @author Carlos Sanchez
46   * @version $Revision: 1.9 $
47   */
48  public class PriceAction extends AutoDispatchAction {
49  
50      /***
51       * Update prices.
52       * 
53       * @param mapping
54       * @param actionForm
55       * @param request
56       * @param response
57       * @return ActionForward success
58       * @throws Exception
59       */
60      public ActionForward update(ActionMapping mapping, ActionForm actionForm,
61              HttpServletRequest request, HttpServletResponse response)
62              throws Exception {
63  
64          PricesActionForm form = (PricesActionForm) actionForm;
65  
66          ProductPricesActionForm[] productPricesActionForms = form
67                  .getProductPrices();
68  
69          Map productPrices = new HashMap();
70  
71          /* populate the products */
72          for (int i = 0; i < productPricesActionForms.length; i++) {
73              PriceActionForm[] priceActionForms = productPricesActionForms[i]
74                      .getPrices();
75              Map prices = new HashMap();
76  
77              /* populate the prices */
78              for (int j = 0; j < priceActionForms.length; j++) {
79                  PriceActionForm priceActionForm = priceActionForms[j];
80                  if (priceActionForm.getAmount().length() > 0) {
81                      prices.put(new Long(priceActionForm.getFareId()),
82                              new BigDecimal(priceActionForm.getAmount()));
83                  } else {
84                      prices.put(new Long(priceActionForm.getFareId()), null);
85                  }
86              }
87  
88              productPrices.put(new Long(productPricesActionForms[i].getId()),
89                      prices);
90          }
91  
92          request.setAttribute("model", getFacade().updatePrices(productPrices));
93          return mapping.findForward("success");
94      }
95  
96      /***
97       * Edit a model.
98       * 
99       * @param mapping
100      * @param actionForm
101      * @param request
102      * @param response
103      * @return ActionForward success
104      * @throws Exception
105      */
106     public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
107             HttpServletRequest request, HttpServletResponse response)
108             throws Exception {
109 
110         PricesActionForm form = (PricesActionForm) actionForm;
111 
112         Model model = getFacade().editPrices(
113                 new Long((String) form.getModelId()));
114         Collection products = model.getProducts();
115 
116         Collection fares = (Collection) getServlet().getServletContext()
117                 .getAttribute(StartupListener.FARES_ATTRIBUTE);
118 
119         form.setModelId(model.getId().toString());
120         ProductPricesActionForm[] productPricesActionForms = new ProductPricesActionForm[products
121                 .size()];
122         form.setProductPrices(productPricesActionForms);
123         form.setProductPricesSize(form.getProductPrices().length);
124         form.setFaresSize(fares.size());
125 
126         Iterator iter = products.iterator();
127         for (int i = 0; i < productPricesActionForms.length; i++) {
128             Product product = (Product) iter.next();
129 
130             /* populate the product form */
131             productPricesActionForms[i] = new ProductPricesActionForm();
132             ProductPricesActionForm productPricesActionForm = productPricesActionForms[i];
133             productPricesActionForm.setId(product.getId().toString());
134             productPricesActionForm.setStockKeepingUnit(product
135                     .getStockKeepingUnit());
136             PriceActionForm[] priceActionForms = new PriceActionForm[fares
137                     .size()];
138             productPricesActionForm.setPrices(priceActionForms);
139 
140             /* get all prices to make a map (fare, amount) */
141             Map prices = new HashMap();
142             Iterator iterator = product.getPrices().iterator();
143             while (iterator.hasNext()) {
144                 Price price = (Price) iterator.next();
145                 prices.put(price.getFare().getId(), price.getAmount());
146             }
147 
148             /* populate the prices */
149             Iterator faresIter = fares.iterator();
150             for (int j = 0; j < priceActionForms.length; j++) {
151                 priceActionForms[j] = new PriceActionForm();
152                 PriceActionForm priceActionForm = priceActionForms[j];
153                 Fare fare = (Fare) faresIter.next();
154                 BigDecimal amount = (BigDecimal) prices.get(fare.getId());
155                 priceActionForm.setAmount(amount == null ? null : amount
156                         .toString());
157                 priceActionForm.setFareId(fare.getId().toString());
158             }
159         }
160 
161         return mapping.findForward("success");
162     }
163 
164     private InventoryFacadeDelegate getFacade() {
165         return (InventoryFacadeDelegate) getBean("inventoryFacadeDelegate");
166     }
167 
168 }