KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > wstore > BasketServlet


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.wstore;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18 import java.io.*;
19 import java.util.*;
20 import java.sql.*;
21 import java.math.*;
22
23 import org.apache.ecs.*;
24 import org.apache.ecs.xhtml.*;
25
26 import org.compiere.util.*;
27 import org.compiere.www.*;
28
29 /**
30  * Shopping Basket.
31  * you could add a new line via parameter
32  * ?M_Product_ID=11&Name=aaaaa&Quantity=1&Price=11.11
33  * if price list found, the price will be potentially overwritten
34  *
35  * @author Jorg Janke
36  * @version $Id: BasketServlet.java,v 1.9 2003/10/04 03:58:26 jjanke Exp $
37  */

38 public class BasketServlet extends HttpServlet
39 {
40     /** Logging */
41     private Logger log = Logger.getCLogger(getClass());
42     /** Name */
43     static public final String JavaDoc NAME = "basketServlet";
44
45     /**
46      * Initialize global variables
47      *
48      * @param config Configuration
49      * @throws ServletException
50      */

51     public void init(ServletConfig config)
52         throws ServletException
53     {
54         super.init(config);
55         if (!WEnv.initWeb(config))
56             throw new ServletException("BasketServlet.init");
57     } // init
58

59     /**
60      * Get Servlet information
61      * @return Info
62      */

63     public String JavaDoc getServletInfo()
64     {
65         return "Compiere Web Basket";
66     } // getServletInfo
67

68     /**
69      * Clean up resources
70      */

71     public void destroy()
72     {
73         log.debug("destroy");
74     } // destroy
75

76
77     /*************************************************************************/
78
79     /**
80      * Process the HTTP Get request.
81      *
82      * @param request request
83      * @param response response
84      * @throws ServletException
85      * @throws IOException
86      */

87     public void doGet(HttpServletRequest request, HttpServletResponse response)
88         throws ServletException, IOException
89     {
90         log.info("doGet from " + request.getRemoteHost() + " - " + request.getRemoteAddr()
91             + " - " + request.getRequestURL());
92         Properties ctx = JSPEnv.getCtx(request);
93         HttpSession session = request.getSession(true);
94         session.removeAttribute(JSPEnv.HDR_MESSAGE);
95
96         // Create WebBasket
97
WebBasket wb = (WebBasket)session.getAttribute(WebBasket.NAME);
98         if (wb == null)
99             wb = new WebBasket();
100         session.setAttribute(WebBasket.NAME, wb);
101
102
103         // Get Price List
104
PriceList pl = (PriceList)session.getAttribute(PriceList.NAME);
105         if (pl == null)
106         {
107             log.warn("No Price List in session - " + PriceList.NAME);
108             pl = (PriceList)request.getAttribute(PriceList.NAME);
109         }
110         if (pl == null)
111         {
112             // Create Price List
113
int AD_Client_ID = Env.getContextAsInt(ctx, "AD_Client_ID");
114             int M_PriceList_ID = Env.getContextAsInt(ctx, "M_PriceList_ID");
115             pl = PriceList.get(AD_Client_ID, M_PriceList_ID);
116         }
117         log.debug("- PL=" + pl);
118
119         // Do we delete? Delete_x
120
deleteLine(request, wb);
121         // Do we add? Add_x
122
addLine(request, pl, wb);
123
124         log.debug("- now - " + wb);
125         // Go back to basket
126
String JavaDoc url = "basket.jsp";
127         log.info ("doGet - Forward to " + url);
128         RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url);
129         dispatcher.forward (request, response);
130     } // doGet
131

132     /**
133      * Add Line
134      * @param request request
135      * @param pl price list
136      * @param wb web basket
137      */

138     private void addLine (HttpServletRequest request, PriceList pl, WebBasket wb)
139     {
140         // Get Parameter
141
int M_PriceList_ID = WUtil.getParameterAsInt (request, "M_PriceList_ID");
142         int M_PriceList_Version_ID = WUtil.getParameterAsInt (request, "M_PriceList_Version_ID");
143         wb.setM_PriceList_ID(M_PriceList_ID);
144         wb.setM_PriceList_Version_ID(M_PriceList_Version_ID);
145         //
146
int M_Product_ID = WUtil.getParameterAsInt (request, "M_Product_ID");
147         String JavaDoc Name = request.getParameter ("Name");
148         String JavaDoc sQuantity = request.getParameter ("Quantity");
149         String JavaDoc sPrice = request.getParameter ("Price");
150
151         // Search for Product ID Add_134 = Add
152
Enumeration en = request.getParameterNames ();
153         while (M_Product_ID == 0 && en.hasMoreElements ())
154         {
155             String JavaDoc parameter = (String JavaDoc)en.nextElement ();
156             if (parameter.startsWith ("Add_"))
157             {
158                 if (WUtil.exists (request, parameter)) // to be sure
159
{
160                     try
161                     {
162                         M_Product_ID = Integer.parseInt (parameter.substring (4));
163                         log.debug ("- found Parameter=" + parameter + " -> " + M_Product_ID);
164                         if (!WUtil.exists(sQuantity))
165                             sQuantity = request.getParameter ("Qty_" + M_Product_ID);
166                         if (!WUtil.exists(sPrice))
167                             sPrice = request.getParameter("Price_" + M_Product_ID);
168                         if (!WUtil.exists(Name))
169                             Name = request.getParameter("Name_" + M_Product_ID);
170                         log.debug("- found Parameters " + Name + ",Qty=" + sQuantity + ",Price=" + sPrice);
171                     }
172                     catch (NumberFormatException JavaDoc ex)
173                     {
174                         log.warn ("ParseError for " + parameter + " - " + ex.toString ());
175                     }
176                 }
177             }
178         }
179         if (M_Product_ID == 0)
180             return;
181
182         // Get Qty
183
BigDecimal Qty = null;
184         try
185         {
186             if (sQuantity != null && sQuantity.length () > 0)
187                 Qty = new BigDecimal (sQuantity);
188         }
189         catch (Exception JavaDoc ex1)
190         {
191         }
192         if (Qty == null)
193             Qty = WebBasketLine.ONE;
194
195         // Get Price
196
BigDecimal Price = null;
197         // Find info in price list
198
if (M_Product_ID != 0 && pl != null)
199         {
200             ArrayList prices = pl.getPrices ();
201             for (int i = 0; i < prices.size (); i++)
202             {
203                 PriceListProduct plp = (PriceListProduct)prices.get (i);
204                 if (plp.getId () == M_Product_ID)
205                 {
206                     Price = plp.getPrice ();
207                     Name = plp.getName ();
208                     log.debug ("- found in PL = " + Name + " - " + Price);
209                     break;
210                 }
211             }
212         }
213         try // if not found and as parameter
214
{
215             if (Price == null && sPrice != null && sPrice.length () > 0)
216                 Price = new BigDecimal (sPrice);
217         }
218         catch (Exception JavaDoc ex1)
219         {
220         }
221         if (Price == null)
222             Price = WebBasketLine.ONE;
223
224         WebBasketLine wbl = new WebBasketLine (M_Product_ID, Name, Qty, Price);
225         wb.add (wbl);
226         log.debug ("- added - " + wbl);
227     } // addLine
228

229     /**
230      * Delete Line.
231      * Delete_x
232      * @param request request
233      * @param wb web basket
234      */

235     private void deleteLine (HttpServletRequest request, WebBasket wb)
236     {
237         Enumeration en = request.getParameterNames();
238         while (en.hasMoreElements())
239         {
240             String JavaDoc parameter = (String JavaDoc)en.nextElement();
241             if (parameter.startsWith("Delete_"))
242             {
243                 try
244                 {
245                     int line = Integer.parseInt (parameter.substring (7));
246                     log.debug ("- delete parameter=" + parameter + " -> " + line);
247                     wb.delete(line);
248                 }
249                 catch (NumberFormatException JavaDoc ex)
250                 {
251                     log.warn("ParseError for " + parameter + " - " + ex.toString());
252                 }
253             }
254         }
255     } // deleteLine
256

257
258     /**
259      * Process the HTTP Post request
260      *
261      * @param request request
262      * @param response response
263      * @throws ServletException
264      * @throws IOException
265      */

266     public void doPost(HttpServletRequest request, HttpServletResponse response)
267         throws ServletException, IOException
268     {
269     // log.info("doPost from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
270
doGet(request, response);
271     }
272 } // Basket
273
Popular Tags