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

📄 wmenu.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * 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.www;

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

import org.apache.ecs.xhtml.*;

import org.compiere.util.*;

/**
 *  Web Menu
 *
 *  @author Jorg Janke
 *  @version  $Id: WMenu.java,v 1.2 2003/03/31 05:27:26 jjanke Exp $
 */
public class WMenu extends HttpServlet
{
	/**
	 *  Initialize global variables
	 *  @param config config
	 *  @throws ServletException
	 */
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		if (!WEnv.initWeb(config))
			throw new ServletException("WMenu.init");
	}   //  init

	/**
	 * 	Get Servlet information
	 *  @return servlet info
	 */
	public String getServletInfo()
	{
		return "Compiere Web Menu";
	}	//	getServletInfo

	/**
	 * Clean up resources
	 */
	public void destroy()
	{
		Log.trace(Log.l1_User, "WMenu.destroy");
		super.destroy();
	}   //  destroy

	/** */
	private PreparedStatement	m_pstmt;


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

	/**
	 *  Process the HTTP Get request.
	 *  - Exit (Logout)
	 *  - AD_Window_ID Forward to Window
	 *
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		Log.trace(Log.l1_User, "WMenu.doGet - Process Menu Request");
		//  Get Parameter: Exit
		if (request.getParameter("Exit") != null)
		{
			WUtil.createLoginPage (request, response, this, null, "Exit");
			return;
		}

		//  Get Session attributes
		HttpSession sess = request.getSession();
		Properties ctx = (Properties)sess.getAttribute(WEnv.SA_CONTEXT);
		if (ctx == null)
		{
			WUtil.createTimeoutPage(request, response, this, null, null);
			return;
		}

		//  Window
		int AD_Window_ID = WUtil.getParameterAsInt(request, "AD_Window_ID");

		//  Forward to WWindow
		if (AD_Window_ID != 0)
		{
			Log.trace(Log.l4_Data, "AD_Window_ID=" + AD_Window_ID);
			//
			String url = WEnv.getBaseDirectory("WWindow?AD_Window_ID=" + AD_Window_ID);
			Log.trace(Log.l4_Data, "Forward to - " + url);
			//
			RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
			rd.forward(request, response);
			return;
		}

		//  Request not serviceable
		WUtil.createErrorPage(request, response, this, null, "NotImplemented");
	}   //  doGet


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

	/**
	 *  Process the HTTP Post request - Verify Input & Create Menu
	 *
	 * @param request request
	 * @param response response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		Log.trace(Log.l1_User, "WMenu.doPost - Create Menu");
		//  Get Session attributes
		HttpSession sess = request.getSession();
		Properties ctx = (Properties)sess.getAttribute(WEnv.SA_CONTEXT);
		Properties cProp = WUtil.getCookieProprties(request);
		if (ctx == null)
		{
			WUtil.createTimeoutPage(request, response, this, null, null);
			return;
		}

		//  Get Parameters: Role, Client, Org, Warehouse, Date
		String role = request.getParameter(WLogin.P_ROLE);
		String client = request.getParameter(WLogin.P_CLIENT);
		String org = request.getParameter(WLogin.P_ORG);
		String wh = request.getParameter(WLogin.P_WAREHOUSE);
		if (wh == null)
			wh = "";

		//  Get context
		if (role == null || client == null || org == null)
		{
			WUtil.createTimeoutPage(request, response, this, ctx, Msg.getMsg(ctx, "ParameterMissing"));
			return;
		}

		//  Get Info from Context - User, Role, Client
		int AD_User_ID = Env.getContextAsInt(ctx, "#AD_User_ID");
		int AD_Role_ID = Env.getContextAsInt(ctx, "#AD_Role_ID");
		int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID");
		//  Not available in context yet - Org, Warehouse
		int AD_Org_ID = -1;
		int M_Warehouse_ID = -1;

		//  Get latest info from context
		try
		{
			int req_role = Integer.parseInt(role);
			if (req_role != AD_Role_ID)
			{
				Log.trace(Log.l5_DData, "AD_Role_ID - changed from " + AD_Role_ID);
				AD_Role_ID = req_role;
				Env.setContext(ctx, "#AD_Role_ID", AD_Role_ID);
			}
			Log.trace(Log.l5_DData, "AD_Role_ID = " + AD_Role_ID);
			//
			int req_client = Integer.parseInt(client);
			if (req_client != AD_Client_ID)
			{
				Log.trace(Log.l5_DData, "AD_Client_ID - changed from " + AD_Client_ID);
				AD_Client_ID = req_client;
				Env.setContext(ctx, "#AD_Client_ID", AD_Client_ID);
			}
			Log.trace(Log.l5_DData, "AD_Client_ID = " + AD_Client_ID);
			//
			AD_Org_ID = Integer.parseInt(org);
			Log.trace(Log.l5_DData, "AD_Org_ID = " + AD_Org_ID);
			//
			if (wh.length() > 0)
			{
				M_Warehouse_ID = Integer.parseInt(wh);
				Log.trace(Log.l5_DData, "M_Warehouse_ID = " + M_Warehouse_ID);
			}
		}
		catch (Exception e)
		{
			Log.error("WMenu.doPost - Parameter", e);
			WUtil.createTimeoutPage(request, response, this, ctx, Msg.getMsg(ctx, "ParameterMissing"));
			return;
		}

		//  Check Login info and set environment
		String loginInfo = checkLogin (ctx, AD_User_ID, AD_Role_ID, AD_Client_ID, AD_Org_ID, M_Warehouse_ID);
		if (loginInfo == null)
		{
			WUtil.createErrorPage(request, response, this, ctx, Msg.getMsg(ctx, "RoleInconsistent"));
			return;
		}
		sess.setAttribute(WEnv.SA_LOGININFO, loginInfo);

		//  Set cookie for future defaults
		cProp.setProperty(WLogin.P_ROLE, String.valueOf(AD_Role_ID));
		cProp.setProperty(WLogin.P_CLIENT, String.valueOf(AD_Client_ID));
		cProp.setProperty(WLogin.P_ORG, String.valueOf(AD_Org_ID));
		if (M_Warehouse_ID == -1)
			cProp.setProperty(WLogin.P_WAREHOUSE, "");
		else
			cProp.setProperty(WLogin.P_WAREHOUSE, String.valueOf(M_Warehouse_ID));

		//  Set Date
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		//  Try to parse Date
		String dateString = request.getParameter(WLogin.P_DATE);
		try
		{
			if (dateString != null && dateString.length() > 0)
			{
				Language language = (Language)sess.getAttribute(WEnv.SA_LANGUAGE);
				DateFormat df = DisplayType.getDateFormat(DisplayType.Date, language);
				java.util.Date date = df.parse(dateString);
				ts = new Timestamp(date.getTime());
			}
		}
		catch (Exception e)
		{
			Log.trace(Log.l1_User, "WMenu.doPost - Cannot parse date: " + dateString + " - " + e.getMessage());
		}
		Env.setContext(ctx, "#Date", ts.toString());    //  JDBC format


	//	Log.printProperties(System.getProperties(), "System");
	//	Log.printProperties(cProp, "Cookie");
	//	Log.printProperties(ctx, "Servlet Context");
	//	Log.printProperties(Env.getCtx(), "Apps Env Context");


		/**********************************************************************
		 *  Create Menu output
		 */
		String windowTitle = Msg.getMsg(ctx, "Menu");
		String statusMessage = Msg.getMsg(ctx, "SelectMenuItem");

		//	Document
		WDoc doc = WDoc.create (windowTitle);
		head header = doc.getHead();
		//  Target
		header.addElement(new base().setTarget(WEnv.TARGET_WINDOW));
		//  Specific Menu Script/Stylesheet
		header.addElement(new script("", WEnv.getBaseDirectory("menu.js"), "text/javascript", "JavaScript1.2"));
		header.addElement(new link().setRel("stylesheet").setHref(WEnv.getBaseDirectory("menu.css")));

		//	Body
		body body = doc.getBody();
		//  Header
		body.addElement(new cite(loginInfo));
		String title = windowTitle + " - " + loginInfo;
		body.addElement(new script("top.document.title='" + title + "';"));
		body.addElement(new script("defaultStatus = '" + statusMessage + "';"));

		//  Clear Window Frame
		body.addElement(WUtil.getClearFrame(WEnv.TARGET_WINDOW));

		//  Load Menu Structure     ----------------------
		MNode root = loadTree(AD_Client_ID, AD_Role_ID, AD_User_ID, Env.getAD_Language(ctx));
		//
		StringBuffer buf = new StringBuffer();
		buf.append("<ul>");
		for (int i = 0; i < root.children.size(); i++)
		{
			MNode node = (MNode)root.children.get(i);
			node.print(node, buf);
		}
		buf.append("</ul>");
		body.addElement(buf.toString());

		//  Exit Info
		body.addElement(new hr());
		String url = request.getRequestURI() + "?Exit=true";
		body.addElement(new a(url, Msg.getMsg(ctx, "Exit")));

		//  Can we store Cookie ?
		if (!cProp.getProperty(WLogin.P_STORE, "N").equals("Y"))
			cProp.clear();

		WUtil.createResponse (request, response, this, cProp, doc, false);
	}   //  doPost

	/**
	 *  Check Login information and set context.
	 *  @returns    true if login info are OK
	 *  @param ctx context
	 *  @param AD_User_ID user
	 *  @param AD_Role_ID role
	 *  @param AD_Client_ID client
	 *  @param AD_Org_ID org
	 *  @param M_Warehouse_ID warehouse
	 */
	private String checkLogin (Properties ctx, int AD_User_ID, int AD_Role_ID, int AD_Client_ID, int AD_Org_ID, int M_Warehouse_ID)
	{
		//  Get Login Info
		String loginInfo = null;
		//  Verify existance of User/Client/Org/Role and User's acces to Client & Org
		String sql = "SELECT u.Name || '@' || c.Name || '.' || o.Name || ' [' || INITCAP(USER) || ']' AS Text "
			+ "FROM AD_User u, AD_Client c, AD_Org o, AD_User_Roles ur "
			+ "WHERE u.AD_User_ID=?"    //  #1
			+ " AND c.AD_Client_ID=?"   //  #2
			+ " AND o.AD_Org_ID=?"      //  #3
			+ " AND ur.AD_Role_ID=?"    //  #4
			+ " AND ur.AD_User_ID=u.AD_User_ID"
			+ " AND (o.AD_Client_ID = 0 OR o.AD_Client_ID=c.AD_Client_ID)"
			+ " AND c.AD_Client_ID IN (SELECT AD_Client_ID FROM AD_Role_OrgAccess ca WHERE ca.AD_Role_ID=ur.AD_Role_ID)"
			+ " AND o.AD_Org_ID IN (SELECT AD_Org_ID FROM AD_Role_OrgAccess ca WHERE ca.AD_Role_ID=ur.AD_Role_ID)";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, AD_User_ID);
			pstmt.setInt(2, AD_Client_ID);
			pstmt.setInt(3, AD_Org_ID);
			pstmt.setInt(4, AD_Role_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
				loginInfo = rs.getString(1);
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			Log.error ("WMenu.checkLogin", e);
		}

		//  not verified
		if (loginInfo == null)
			return null;

		//  Set Preferences
		KeyNamePair org = new KeyNamePair(AD_Org_ID, String.valueOf(AD_Org_ID));
		KeyNamePair wh = null;
		if (M_Warehouse_ID > 0)
			wh = new KeyNamePair(M_Warehouse_ID, String.valueOf(M_Warehouse_ID));
		//
		Timestamp date = null;
		String printer = null;
		DB.loadPreferences(ctx, org, wh, date, printer);

⌨️ 快捷键说明

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