📄 basketservlet.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.wstore;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import org.apache.ecs.*;
import org.apache.ecs.xhtml.*;
import org.apache.log4j.Logger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.www.*;
/**
* Shopping Basket.
* you could add a new line via parameter
* ?M_Product_ID=11&Name=aaaaa&Quantity=1&Price=11.11
* if price list found, the price will be potentially overwritten
*
* @author Jorg Janke
* @version $Id: BasketServlet.java,v 1.7 2003/05/04 06:47:27 jjanke Exp $
*/
public class BasketServlet extends HttpServlet
{
/** Logging */
private Logger log = Logger.getLogger(getClass());
/** Name */
static public final String NAME = "basketServlet";
/**
* Initialize global variables
*
* @param config Configuration
* @throws ServletException
*/
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
if (!WEnv.initWeb(config))
throw new ServletException("BasketServlet.init");
} // init
/**
* Get Servlet information
* @return Info
*/
public String getServletInfo()
{
return "Compiere Web Basket";
} // getServletInfo
/**
* Clean up resources
*/
public void destroy()
{
log.debug("destroy");
} // destroy
/*************************************************************************/
/**
* Process the HTTP Get request.
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
log.info("doGet from " + request.getRemoteHost() + " - " + request.getRemoteAddr()
+ " - " + request.getRequestURL());
Properties ctx = JSPEnv.getCtx(request);
HttpSession session = request.getSession(true);
// Create WebBasket
WebBasket wb = (WebBasket)session.getAttribute(WebBasket.NAME);
if (wb == null)
wb = new WebBasket();
session.setAttribute(WebBasket.NAME, wb);
// Get Price List
PriceList pl = (PriceList)session.getAttribute(PriceList.NAME);
if (pl == null)
{
log.warn("No Price List in session - " + PriceList.NAME);
pl = (PriceList)request.getAttribute(PriceList.NAME);
}
if (pl == null)
{
// Create Price List
int AD_Client_ID = Env.getContextAsInt(ctx, "AD_Client_ID");
int M_PriceList_ID = Env.getContextAsInt(ctx, "M_PriceList_ID");
pl = PriceList.get(AD_Client_ID, M_PriceList_ID);
}
log.debug("- PL=" + pl);
// Do we delete? Delete_x
deleteLine(request, wb);
// Do we add? Add_x
addLine(request, pl, wb);
log.debug("- now - " + wb);
// Go back to basket
String url = "basket.jsp";
log.info ("doGet - Forward to " + url);
RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url);
dispatcher.forward (request, response);
} // doGet
/**
* Add Line
* @param request request
* @param pl price list
* @param wb web basket
*/
private void addLine (HttpServletRequest request, PriceList pl, WebBasket wb)
{
// Get Parameter
int M_PriceList_ID = WUtil.getParameterAsInt (request, "M_PriceList_ID");
int M_PriceList_Version_ID = WUtil.getParameterAsInt (request, "M_PriceList_Version_ID");
wb.setM_PriceList_ID(M_PriceList_ID);
wb.setM_PriceList_Version_ID(M_PriceList_Version_ID);
//
int M_Product_ID = WUtil.getParameterAsInt (request, "M_Product_ID");
String Name = request.getParameter ("Name");
String sQuantity = request.getParameter ("Quantity");
String sPrice = request.getParameter ("Price");
// Search for Product ID Add_134 = Add
Enumeration en = request.getParameterNames ();
while (M_Product_ID == 0 && en.hasMoreElements ())
{
String parameter = (String)en.nextElement ();
if (parameter.startsWith ("Add_"))
{
if (WUtil.exists (request, parameter)) // to be sure
{
try
{
M_Product_ID = Integer.parseInt (parameter.substring (4));
log.debug ("- found Parameter=" + parameter + " -> " + M_Product_ID);
if (!WUtil.exists(sQuantity))
sQuantity = request.getParameter ("Qty_" + M_Product_ID);
if (!WUtil.exists(sPrice))
sPrice = request.getParameter("Price_" + M_Product_ID);
if (!WUtil.exists(Name))
Name = request.getParameter("Name_" + M_Product_ID);
log.debug("- found Parameters " + Name + ",Qty=" + sQuantity + ",Price=" + sPrice);
}
catch (NumberFormatException ex)
{
log.warn ("ParseError for " + parameter + " - " + ex.toString ());
}
}
}
}
if (M_Product_ID == 0)
return;
// Get Qty
BigDecimal Qty = null;
try
{
if (sQuantity != null && sQuantity.length () > 0)
Qty = new BigDecimal (sQuantity);
}
catch (Exception ex1)
{
}
if (Qty == null)
Qty = WebBasketLine.ONE;
// Get Price
BigDecimal Price = null;
// Find info in price list
if (M_Product_ID != 0 && pl != null)
{
ArrayList prices = pl.getPrices ();
for (int i = 0; i < prices.size (); i++)
{
PriceListProduct plp = (PriceListProduct)prices.get (i);
if (plp.getId () == M_Product_ID)
{
Price = plp.getPrice ();
Name = plp.getName ();
log.debug ("- found in PL = " + Name + " - " + Price);
break;
}
}
}
try // if not found and as parameter
{
if (Price == null && sPrice != null && sPrice.length () > 0)
Price = new BigDecimal (sPrice);
}
catch (Exception ex1)
{
}
if (Price == null)
Price = WebBasketLine.ONE;
WebBasketLine wbl = new WebBasketLine (M_Product_ID, Name, Qty, Price);
wb.add (wbl);
log.debug ("- added - " + wbl);
} // addLine
/**
* Delete Line.
* Delete_x
* @param request request
* @param wb web basket
*/
private void deleteLine (HttpServletRequest request, WebBasket wb)
{
Enumeration en = request.getParameterNames();
while (en.hasMoreElements())
{
String parameter = (String)en.nextElement();
if (parameter.startsWith("Delete_"))
{
try
{
int line = Integer.parseInt (parameter.substring (7));
log.debug ("- delete parameter=" + parameter + " -> " + line);
wb.delete(line);
}
catch (NumberFormatException ex)
{
log.warn("ParseError for " + parameter + " - " + ex.toString());
}
}
}
} // deleteLine
/**
* Process the HTTP Post request
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// log.info("doPost from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
doGet(request, response);
}
} // Basket
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -