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

📄 websessionctx.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 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. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.util;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.compiere.model.*;


/**
 *	Web Session Context Value Object
 *	
 *  @author Jorg Janke
 *  @version $Id: WebSessionCtx.java,v 1.5 2005/11/06 01:17:27 jjanke Exp $
 */
public class WebSessionCtx implements Serializable
{
	/**
	 * 	Get/create Web Session Context
	 *	@param request request
	 *	@return ctx or null
	 */
	public static WebSessionCtx get (HttpServletRequest request)
	{
		HttpSession session = request.getSession(false);
		if (session == null)
			session = request.getSession(true);
		if (session == null)
			return null;
		WebSessionCtx wsc = (WebSessionCtx)session.getAttribute(NAME);
		//	Create New
		if (wsc == null)
		{
			wsc = new WebSessionCtx (request);
			session.setAttribute(NAME, wsc);
		}
		return wsc;
	}	//	get
	
	public final static String		CTX_SERVER_CONTEXT = "context";
	public final static String		CTX_DOCUMENT_DIR = "documentDir";
	/** Header (Error) Message			*/
	public final static String		HDR_MESSAGE = "hdrMessage";
	/** Header Info Message				*/
	public final static String		HDR_INFO = "hdrInfo";

	/**	Logger							*/
	static private CLogger			log = CLogger.getCLogger (WebSessionCtx.class);
	/** Cache 60 minutes				*/
	static private CCache<Integer,Properties> s_cacheCtx = new CCache<Integer,Properties>("WebSessionCtx", 30, 60);
	
	/**************************************************************************
	 * 	Web Session Context
	 * 	@param request request
	 */
	private WebSessionCtx (HttpServletRequest request)
	{
		log.info (request.getContextPath() + " (" + request.getRemoteAddr() 
			+ " - " + request.getLocale() + ") #" + counter);
		ctx = new Properties();
		setLanguage(request);
		
		HttpSession session = request.getSession(false);
		
		//	Add Servlet Init Parameters (webStore/src/web/WEB-INF/web.xml)
		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);
			log.config (key + "=" + value); 
		}

		setWStore (request.getContextPath());
		ctx = getDefaults ();
		
		//	ServerContext	- dev2/wstore
		ctx.put(CTX_SERVER_CONTEXT, request.getServerName() + request.getContextPath());
		//	Make Context directly availabe to jsp's
		session.setAttribute("ctx", ctx);
		//
		log.fine("#" + ctx.size());
	}	//	WebSessionCtx

	/**	Sessition Attribute Name			*/
	public static final String	NAME = "WebSessionCtx";
	
	/**	Static counter				*/
	public static int 		s_counter = 0;
	/** Instance Counter			*/
	public int				counter = ++s_counter;
	
	
	/** Session Context 			*/
	public Properties		ctx = null;
	/**	Session Language			*/
	public Language			language = null;
	
	/** Localized Date format       */
	public SimpleDateFormat dateFormat = null;
	/** Localized Timestamp format  */
	public SimpleDateFormat dateTimeFormat = null;

	/** Localized Amount format    */
	public DecimalFormat 	amountFormat = null;
	/** Localized Integer format    */
	public DecimalFormat 	integerFormat = null;
	/** Localized Number format     */
	public DecimalFormat 	numberFormat = null;
	/** Localized Quantity format   */
	public DecimalFormat 	quantityFormat = null;

	/** Login Info					*/
	public String			loginInfo = "";
	/** Web Store					*/
	public MStore			wstore = null;
	
	/**
	 * 	Set Web Store
	 *	@param contextPath web server context path
	 */
	private void setWStore (String contextPath)
	{
		//	get from context
		int W_Store_ID = Env.getContextAsInt(ctx, "W_Store_ID");
		if (W_Store_ID != 0)
		{
			wstore = MStore.get(ctx, W_Store_ID);
			if (wstore.getW_Store_ID() != 0)
			{
				log.info("From web.xml - " + wstore);
				return;
			}
		}
		if ("/compiere".equals(contextPath))	//	HTML UI
			return;
		//
		wstore = MStore.get(ctx, contextPath);
		if (wstore == null)
			throw new IllegalStateException("No Web Store found - " + contextPath);
	}	//	setWStore
	
	
	/**
	 * 	Get Web Store Defaults
	 * 	@return context
	 */
	private Properties getDefaults ()
	{
		//	No Web Store
		if (wstore == null)
			return new Properties();
		//
		Integer key = new Integer (wstore.getW_Store_ID());
		Properties newCtx = (Properties)s_cacheCtx.get(key);
		
		/**	Create New Context		*/
		if (newCtx == null)
		{
			log.info(wstore.getWebContext());
			newCtx = new Properties();
			//	copy explicitly
			Enumeration e = ctx.keys();
			while (e.hasMoreElements())
			{
				String pKey = (String)e.nextElement();
				newCtx.setProperty(pKey, ctx.getProperty(pKey));
			}
			
			Env.setContext(newCtx, "#AD_Client_ID", wstore.getAD_Client_ID());
			Env.setContext(newCtx, "#AD_Org_ID", wstore.getAD_Org_ID());
			//
			Env.setContext(newCtx, "#SalesRep_ID", wstore.getSalesRep_ID());
			Env.setContext(newCtx, "#M_PriceList_ID", wstore.getM_PriceList_ID());
			Env.setContext(newCtx, "#M_Warehouse_ID", wstore.getM_Warehouse_ID());
			//
			String s = wstore.getWebParam1();
			Env.setContext(newCtx, "webParam1", s == null ? "" : s);
			s = wstore.getWebParam2();
			Env.setContext(newCtx, "webParam2", s == null ? "" : s);
			s = wstore.getWebParam3();
			Env.setContext(newCtx, "webParam3", s == null ? "" : s);
			s = wstore.getWebParam4();
			Env.setContext(newCtx, "webParam4", s == null ? "" : s);
			s = wstore.getWebParam5();
			Env.setContext(newCtx, "webParam5", s == null ? "" : s);
			s = wstore.getWebParam6();
			Env.setContext(newCtx, "webParam6", s == null ? "" : s);
			//
			s = wstore.getWebInfo();
			if (s != null && s.length() > 0)
				Env.setContext(newCtx, HDR_INFO, s);
			
			//	Payment Term
			Env.setContext(newCtx, "#M_PriceList_ID", wstore.getM_PriceList_ID());

			//	Default User - SalesRep
			if (Env.getContextAsInt(newCtx, "#AD_User_ID") == 0)
				Env.setContext(newCtx, "#AD_User_ID", wstore.getSalesRep_ID());
			
			//	Default Role for access
			if (Env.getContextAsInt(newCtx, "#AD_Role_ID") == 0)
			{
				int AD_Role_ID = 0;		//	HARDCODED - System
				Env.setContext(newCtx, "#AD_Role_ID", AD_Role_ID);
			}

			//	Client
			MClient client = MClient.get (newCtx, wstore.getAD_Client_ID());
			//	Name,Description, SMTPHost,RequestEMail,RequestUser, RequestUserPw
			Env.setContext(newCtx, "name", client.getName());
			Env.setContext(newCtx, "description", client.getDescription());
			
			//	AD_Language
			if (newCtx.getProperty("#AD_Language") == null && client.getAD_Language() != null)
				Env.setContext(newCtx, "#AD_Language", client.getAD_Language());
			//	DocumentDir
			String docDir = client.getDocumentDir();
			Env.setContext(newCtx, CTX_DOCUMENT_DIR, docDir == null ? "" : docDir);

			//	Default Language
			if (newCtx.getProperty("#AD_Language") == null)
				Env.setContext(newCtx, "#AD_Language", "en_US");

			//	Save Context - Key is AD_Client_ID
			s_cacheCtx.put(key, newCtx);
		}
		//	return new Properties (pp);	seems not to work with JSP
		Enumeration e = newCtx.keys();
		while (e.hasMoreElements())
		{
			String pKey = (String)e.nextElement();
			ctx.setProperty(pKey, newCtx.getProperty(pKey));
		}
		return ctx;
	}	//	getDefaults

	
	/**************************************************************************
	 *  Set Language from request or session in.
	 *  - Properties
	 *  - Cookie
	 *  - Session
	 *  @param request request
	 */
	private void setLanguage (HttpServletRequest request)
	{
		//  Get Cookie
		Properties cProp = WebUtil.getCookieProprties(request);
		
		//  Get/set Parameter:      Language
		String AD_Language = WebUtil.getParameter (request, Env.LANGUAGE);
		if (AD_Language == null)
		{
			//  Check Cookie
			AD_Language = cProp.getProperty(Env.LANGUAGE);
			if (AD_Language == null)
			{
				//  Check Request Locale
				Locale locale = request.getLocale();
				AD_Language = Language.getAD_Language (locale);
			}
		}
		if (AD_Language != null)
		{
			Language lang = Language.getLanguage(AD_Language);
			Env.verifyLanguage (ctx, lang);
			Env.setContext(ctx, Env.LANGUAGE, lang.getAD_Language());
			Msg.getMsg(ctx, "0");
			cProp.setProperty(Env.LANGUAGE, lang.getAD_Language());
			setLanguage(lang);
		}
		else if (language == null)	//	set base language
			setLanguage (Language.getBaseLanguage());
	}	//	setLanguage

	/**
	 * 	Set Language and init formats
	 *	@param lang language
	 */
	private void setLanguage (Language lang)
	{
		language = lang;
		//
		dateFormat = DisplayType.getDateFormat(DisplayType.Date, language);
		dateTimeFormat = DisplayType.getDateFormat(DisplayType.DateTime, language);
		//
		amountFormat = DisplayType.getNumberFormat(DisplayType.Amount, language);
		integerFormat = DisplayType.getNumberFormat(DisplayType.Integer, language);
		numberFormat = DisplayType.getNumberFormat(DisplayType.Number, language);
		quantityFormat = DisplayType.getNumberFormat(DisplayType.Quantity, language);
	}	//	setLanguage
	
	/**
	 * 	Finalize
	 *	@throws java.lang.Throwable
	 */
	protected void finalize ()
		throws Throwable
	{
		super.finalize ();
	}	//	finalize
	
	/**
	 * 	String representation
	 *	@return Session + count
	 */
	public String toString ()
	{
		return "WSessionCtx#" + counter;
	}	//	toString
	
}	//	WSessionCtx

⌨️ 快捷键说明

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