1 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 38 public class BasketServlet extends HttpServlet 39 { 40 41 private Logger log = Logger.getCLogger(getClass()); 42 43 static public final String NAME = "basketServlet"; 44 45 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 } 59 63 public String getServletInfo() 64 { 65 return "Compiere Web Basket"; 66 } 68 71 public void destroy() 72 { 73 log.debug("destroy"); 74 } 76 77 78 79 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 WebBasket wb = (WebBasket)session.getAttribute(WebBasket.NAME); 98 if (wb == null) 99 wb = new WebBasket(); 100 session.setAttribute(WebBasket.NAME, wb); 101 102 103 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 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 deleteLine(request, wb); 121 addLine(request, pl, wb); 123 124 log.debug("- now - " + wb); 125 String url = "basket.jsp"; 127 log.info ("doGet - Forward to " + url); 128 RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url); 129 dispatcher.forward (request, response); 130 } 132 138 private void addLine (HttpServletRequest request, PriceList pl, WebBasket wb) 139 { 140 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 int M_Product_ID = WUtil.getParameterAsInt (request, "M_Product_ID"); 147 String Name = request.getParameter ("Name"); 148 String sQuantity = request.getParameter ("Quantity"); 149 String sPrice = request.getParameter ("Price"); 150 151 Enumeration en = request.getParameterNames (); 153 while (M_Product_ID == 0 && en.hasMoreElements ()) 154 { 155 String parameter = (String )en.nextElement (); 156 if (parameter.startsWith ("Add_")) 157 { 158 if (WUtil.exists (request, parameter)) { 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 ex) 173 { 174 log.warn ("ParseError for " + parameter + " - " + ex.toString ()); 175 } 176 } 177 } 178 } 179 if (M_Product_ID == 0) 180 return; 181 182 BigDecimal Qty = null; 184 try 185 { 186 if (sQuantity != null && sQuantity.length () > 0) 187 Qty = new BigDecimal (sQuantity); 188 } 189 catch (Exception ex1) 190 { 191 } 192 if (Qty == null) 193 Qty = WebBasketLine.ONE; 194 195 BigDecimal Price = null; 197 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 { 215 if (Price == null && sPrice != null && sPrice.length () > 0) 216 Price = new BigDecimal (sPrice); 217 } 218 catch (Exception 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 } 229 235 private void deleteLine (HttpServletRequest request, WebBasket wb) 236 { 237 Enumeration en = request.getParameterNames(); 238 while (en.hasMoreElements()) 239 { 240 String parameter = (String )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 ex) 250 { 251 log.warn("ParseError for " + parameter + " - " + ex.toString()); 252 } 253 } 254 } 255 } 257 258 266 public void doPost(HttpServletRequest request, HttpServletResponse response) 267 throws ServletException, IOException 268 { 269 doGet(request, response); 271 } 272 } | Popular Tags |