⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jspenv.java

📁 Java写的ERP系统
💻 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 Smart Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.wstore;

import java.util.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger;

import org.compiere.*;
import org.compiere.util.*;

/**
 *  JSP Environment Utilities
 *
 *  @author Jorg Janke
 *  @version $Id: JSPEnv.java,v 1.10 2003/05/04 06:47:27 jjanke Exp $
 */
public class JSPEnv
{
	/**	Logger							*/
	static private Logger			s_log = Logger.getLogger (JSPEnv.class);

	public final static String		CONTEXT_NAME = "ctx";
	public final static String		CTX_SERVER_CONTEXT = "context";
	public final static String		CTX_DOCUMENT_DIR = "documentDir";

	/**
	 * 	Get Context from Session
	 *	@param session session
	 * 	@return properties
	 */
	public static Properties getCtx (HttpServletRequest request)
	{
		//	Session
		HttpSession session = request.getSession(true);
		Properties ctx = (Properties)session.getAttribute(CONTEXT_NAME);
		if (ctx != null)
			return ctx;

		//
		ctx = new Properties();
		//	Create New
		ServletContext sc = session.getServletContext();
		Enumeration en = sc.getInitParameterNames();
		while (en.hasMoreElements())
		{
			String key = (String)en.nextElement();
			String value = sc.getInitParameter(key);
			ctx.setProperty(key, value);
		}
		//	Default Client
		int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID");
		if (AD_Client_ID == 0)
		{
			AD_Client_ID = DB.getSQLValue("SELECT AD_Client_ID FROM AD_Client WHERE AD_Client_ID > 11 AND IsActive='Y'");
			if (AD_Client_ID < 0)
				AD_Client_ID = 11;	//	GardenWorld
			Env.setContext (ctx, "#AD_Client_ID", AD_Client_ID);
		}

		ctx = getDefaults(ctx, AD_Client_ID);
		//	ServerContext	- dev2/wstore
		ctx.put(CTX_SERVER_CONTEXT, request.getServerName() + request.getContextPath());

		//	save it
		s_log.debug("getCtx #" + ctx.size());
		session.setAttribute(CONTEXT_NAME, ctx);
		return ctx;
	}	//	getCtx

	private static HashMap		s_cache = new HashMap();

	/**
	 * 	Get Defaults
	 * 	@param ctx context
	 * 	@param AD_Client_ID client
	 * 	@return context
	 */
	private static Properties getDefaults (Properties ctx, int AD_Client_ID)
	{
		Integer key = new Integer (AD_Client_ID);
		Properties pp = (Properties)s_cache.get(key);
		if (pp != null)
		{
		//	return new Properties (pp);	seems not to work with JSP
			Enumeration e = pp.keys();
			while (e.hasMoreElements())
			{
				String pKey = (String)e.nextElement();
				ctx.setProperty(pKey, pp.getProperty(pKey));
			}
			return ctx;
		}

		s_log.debug("getDefaults");

		//	Default Trx! Org
		if (Env.getContextAsInt(ctx, "#AD_Org_ID") == 0)
		{
			int AD_Org_ID = DB.getSQLValue("SELECT AD_Org_ID FROM AD_Org WHERE AD_Client_ID=? AND IsActive='Y' AND IsSummary='N' ORDER BY 1", AD_Client_ID);
			Env.setContext(ctx, "#AD_Org_ID", AD_Org_ID);
		}
		//	Default User
		if (Env.getContextAsInt(ctx, "#AD_User_ID") == 0)
			ctx.setProperty("#AD_User_ID", "0");	//	System

		//	Warehouse
		if (Env.getContextAsInt(ctx, "#M_Warehouse_ID") == 0)
		{
			int M_Warehouse_ID = DB.getSQLValue("SELECT M_Warehouse_ID FROM M_Warehouse WHERE AD_Client_ID=? AND IsActive='Y' ORDER BY 1", AD_Client_ID);
			Env.setContext(ctx, "#M_Warehouse_ID", M_Warehouse_ID);
		}
		//	Sales Rep
		if (Env.getContextAsInt(ctx, "#SalesRep_ID") == 0)
		{
			int SalesRep_ID = 0;
			Env.setContext(ctx, "#SalesRep_ID", SalesRep_ID);
		}
		//	Payment Term
		if (Env.getContextAsInt(ctx, "#C_PaymentTerm_ID") == 0)
		{
			int C_PaymentTerm_ID = DB.getSQLValue("SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y' ORDER BY NetDays", AD_Client_ID);
			Env.setContext(ctx, "#C_PaymentTerm_ID", C_PaymentTerm_ID);
		}

		/****************************************/

		String sql = "SELECT c.Name,c.Description, c.SMTPHost,c.RequestEMail,c.RequestUser,c.RequestUserPw,"	//	1..6
			+ " c.AD_Language, c.WebDir, c.WebParam1,c.WebParam2,c.WebParam3,c.WebParam4, c.WebOrderEMail,"		//	7..13
			+ " ci.M_PriceList_ID, c.DocumentDir "
			+ "FROM AD_Client c"
			+ " INNER JOIN AD_ClientInfo ci ON (c.AD_Client_ID=ci.AD_Client_ID) "
			+ "WHERE c.AD_Client_ID=?";
		PreparedStatement pstmt = null;
		try
		{
			pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, AD_Client_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				//	Name,Description, SMTPHost,RequestEMail,RequestUser, RequestUserPw
				ctx.setProperty("name", rs.getString(1));
				ctx.setProperty("description", rs.getString(2));
				ctx.setProperty("SMTPHost", rs.getString(3));
				ctx.setProperty("RequestEMail", rs.getString(4));
				ctx.setProperty("RequestUser", rs.getString(5));
				ctx.setProperty("RequestUserPw", rs.getString(6));
				//	AD_Language, WebDir, WebParam1,WebParam2,WebParam3,WebParam4, WebOrderEMail
				if (ctx.getProperty("#AD_Language") == null)
					ctx.setProperty("#AD_Language", rs.getString(7));
				ctx.setProperty("webDir", rs.getString(8));
				String s = rs.getString(9);
				ctx.setProperty("webParam1", s == null ? "" : s);
				s = rs.getString(10);
				ctx.setProperty("webParam2", s == null ? "" : s);
				s = rs.getString(11);
				ctx.setProperty("webParam3", s == null ? "" : s);
				s = rs.getString(12);
				ctx.setProperty("webParam4", s == null ? "" : s);
				s = rs.getString(13);
				ctx.setProperty("webOrderEMail", s == null ? "" : s);
				//	M_PriceList_ID, DocumentDir
				Env.setContext(ctx, "#M_PriceList_ID", rs.getInt(14));
				s = rs.getString(15);
				Env.setContext(ctx, CTX_DOCUMENT_DIR, s == null ? "" : s);
			}
			rs.close();
			pstmt.close();
			pstmt = null;
		}
		catch (Exception e)
		{
			s_log.error("setDefaults", e);
		}
		finally
		{
			try
			{
				if (pstmt != null)
					pstmt.close ();
			}
			catch (Exception e)
			{}
			pstmt = null;
		}

		//	Default Language
		if (ctx.getProperty("#AD_Language") == null)
			ctx.setProperty("#AD_Language", "en_US");

		s_cache.put(key, ctx);
		return ctx;
	}	//	getDefaults

	/*************************************************************************/

	private final static String		COOKIE_NAME = "CompiereWebUser";

	/**
	 * 	Get Web User from Cookie
	 * 	@param request request with cookie
	 * 	@return web user or null
	 */
	public static String getCookieWebUser (HttpServletRequest request)
	{
		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return null;
		for (int i = 0; i < cookies.length; i++)
		{
			if (COOKIE_NAME.equals(cookies[i].getName()))
				return cookies[i].getValue();
		}
		return null;
	}

	/**
	 * 	Add Cookie with web user
	 * 	@param request request (for context path)
	 * 	@param response response to add cookie
	 * 	@param webUser email address
	 */
	public static void addCookieWebUser (HttpServletRequest request, HttpServletResponse response, String webUser)
	{
		Cookie cookie = new Cookie(COOKIE_NAME, webUser);
		cookie.setComment("Compiere Web User");
		cookie.setPath(request.getContextPath());
		cookie.setMaxAge(2592000);      //  30 days in seconds   60*60*24*30
		response.addCookie(cookie);
	}	//	setCookieWebUser

	/**
	 * 	Remove Cookie with web user by setting user to _
	 * 	@param request request (for context path)
	 * 	@param response response to add cookie
	 */
	public static void deleteCookieWebUser (HttpServletRequest request, HttpServletResponse response)
	{
		Cookie cookie = new Cookie(COOKIE_NAME, " ");
		cookie.setComment("Compiere Web User");
		cookie.setPath(request.getContextPath());
		cookie.setMaxAge(1);      //  second
		response.addCookie(cookie);
	}	//	deleteCookieWebUser

}	//	JSPEnv

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -