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

📄 wlocation.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  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 org.apache.ecs.*;

import org.apache.ecs.*;
import org.apache.ecs.xhtml.*;

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

/**
 *  WLocation Servlet.
 *  <p>
 *  The servlet is invoked by a parent window via
 *  <code>
 *  WLocation?FormName=formName%ColumnName=columnName
 *  </code>
 *  and assumes that in the opening window/form there are two fields
 *  <code>
 *  opener.document.formName.columnName - The (hidden) field for the ID
 *  opener.document.formName.columnName_D - The display field for the value
 *  </code>
 *  When selecting an entry, the window is closed and the value of the two fields set.
 *
 *  @author Jorg Janke
 *  @version  $Id: WLocation.java,v 1.1.1.1 2002/10/12 01:06:54 jjanke Exp $
 */
public class WLocation extends HttpServlet
{
	/**
	 * Initialize global variables
	 *
	 * @param config
	 * @throws ServletException
	 */
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		if (!WEnv.initWeb(config))
			throw new ServletException("WLocation.init");
	}   //  init

	/** Parameter   */
	private static final String P_TARGET = "TARGET";
	private static final String P_C_LOCATION_ID = "C_LOCATION_ID";
	private static final String P_ADDRESS1 = "ADDRESS1";
	private static final String P_ADDRESS2 = "ADDRESS2";
	private static final String P_CITY = "CITY";
	private static final String P_POSTAL = "POSTAL";
	private static final String P_C_COUNTRY_ID = "C_COUNTRY_ID";
	private static final String P_C_REGION_ID = "C_REGION_ID";

	/**
	 * Process the HTTP Get request - initial Start
	 * Needs to have parameters FormName and ColumnName
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Log.trace(Log.l3_Util, "WLocation.doGet");
		HttpSession sess = request.getSession();
		WWindowStatus ws = null;
		if (sess != null)
			ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW);
		if (ws == null)
		{
			WUtil.createTimeoutPage(request, response, this, null, null);
			return;
		}
		//  Get Mandatory Parameters
		String formName = request.getParameter("FormName");
		String columnName = request.getParameter("ColumnName");
		MField mField = ws.curTab.getField(columnName);

		Log.trace(Log.l4_Data, "WLocation.doGet", "FormName=" + formName + ", ColumnName=" + columnName);
		if (mField == null || formName == null || columnName == null || formName.equals("") || columnName.equals(""))
		{
			WUtil.createTimeoutPage(request, response, this, ws.ctx, Msg.getMsg(ws.ctx, "ParameterMissing"));
			return;
		}
		Object value = ws.curTab.getValue(columnName);
		String target = "opener.document." + formName + "." + columnName;

		//  Create Document
		WDoc doc = WDoc.create (mField.getHeader());
		body body = doc.getBody();
		body.setOnLoad("self.focus();");
		//
		MLocation mLocation = new MLocation(ws.ctx, ws.mWindow.getWindowNo());
		if (value instanceof Integer)
			mLocation.load(((Integer)value).intValue());
		body.addElement(createForm(request.getRequestURI(), ws, mLocation, target));
		//
		WUtil.createResponse (request, response, this, null, doc, false);
	}   //  doGet


	/**
	 *  Process the HTTP Post request (update Address)
	 *
	 *  @param request
	 *  @param response
	 *  @throws ServletException
	 *  @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Log.trace(Log.l3_Util, "WLocation.doPost");
		HttpSession sess = request.getSession();
		WWindowStatus ws = null;
		if (sess != null)
			ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW);
		if (ws == null)
		{
			WUtil.createTimeoutPage(request, response, this, null, null);
			return;
		}
		String target = request.getParameter(P_TARGET);
		int C_Location_ID = WUtil.getParameterAsInt(request, P_C_LOCATION_ID);

		//  Create Location
		MLocation mLocation = new MLocation(ws.ctx, ws.mWindow.getWindowNo());
		if (C_Location_ID != 0)
			mLocation.load(C_Location_ID);
		Log.trace(Log.l4_Data, "WLocation.doPost updating C_Location_ID=" + C_Location_ID, target);

		//  Update Location
		mLocation.Address1 = request.getParameter(P_ADDRESS1);
		mLocation.Address2 = request.getParameter(P_ADDRESS2);
		mLocation.City = request.getParameter(P_CITY);
		mLocation.Postal = request.getParameter(P_POSTAL);
		mLocation.C_Region_ID = WUtil.getParameterAsInt(request, P_C_REGION_ID);
		mLocation.C_Country_ID = WUtil.getParameterAsInt(request, P_C_COUNTRY_ID);

		//  Document
		WDoc doc = WDoc.create ("WLocation");
		body b = doc.getBody();

		//  Save Location
		C_Location_ID = mLocation.save();
		if (C_Location_ID == 0)
			b.addElement(new p("ERROR - Location=0"));
		b.addElement(new p().addElement(mLocation.toString()));
		//  Update Target
		script script = new script(new StringBuffer(target)
			.append(".value='").append(C_Location_ID).append("';")
			.append(target).append("_D.value='").append(mLocation.toString())
			.append("';window.close();").toString());
		b.addElement(script);
		Log.trace(Log.l6_Database, "WLocation.doPost", script.toString());
		//
		WUtil.createResponse(request, response, this, null, doc, false);
	}   //  doPost

	/**
	 *  Create Form
	 *
	 * @param action
	 * @param ws
	 * @param mLocation
	 * @param target
	 * @return Form with Address Info
	 */
	private form createForm (String action, WWindowStatus ws, MLocation mLocation, String target)
	{
		Log.trace(Log.l4_Data, "WLocation.createForm", "Location=" + mLocation.toStringX());

		form form = null;
		form = new form (action, form.post, form.ENC_DEFAULT);
		form.addElement(new input(input.hidden, P_TARGET, target));
		form.addElement(new input(input.hidden, P_C_LOCATION_ID, mLocation.C_Location_ID));

		table table = new table();
		//  --  Line 1
		tr line = new tr();
		line.addElement(new td(Msg.getMsg(ws.ctx, "Address")+ " 1").setAlign(AlignType.right));
		input input = null;
		input = new input (input.text, P_ADDRESS1, mLocation.Address1);
		input.setMaxlength(50).setSize(50);
		line.addElement(new td(input).setAlign(AlignType.left).setColSpan(5));
		table.addElement(line);
		//  --  Line 2
		line = new tr();
		line.addElement(new td(Msg.getMsg(ws.ctx, "Address")+ " 2").setAlign(AlignType.right));
		input = new input (input.text, P_ADDRESS2, mLocation.Address2);
		input.setMaxlength(50).setSize(50);
		line.addElement(new td(input).setAlign(AlignType.left).setColSpan(5));
		table.addElement(line);
		//  --  Line 3
		line = new tr();
		line.addElement(new td(Msg.getMsg(ws.ctx, "City")).setAlign(AlignType.right));      //  1
		input = new input (input.text, P_CITY, mLocation.City);
		input.setMaxlength(30).setSize(30);
		line.addElement(new td(input).setAlign(AlignType.left));                            //  2
		//
		if (mLocation.mCountry.HasRegion)
		{
			line.addElement(new td(Msg.getMsg(ws.ctx, "Region")).setAlign(AlignType.right));    //  3
			line.addElement(new td(getRegion(mLocation, ws)).setAlign(AlignType.left));             //  4
		}
		//
		line.addElement(new td(Msg.getMsg(ws.ctx, "Postal")).setAlign(AlignType.right));    //  5
		input = new input (input.text, P_POSTAL, mLocation.Postal);
		input.setMaxlength(10).setSize(6);
		line.addElement(new td(input).setAlign(AlignType.left));                            //  6
		//
	//	input = new input (input.text, "PostalAdd", mLocation.PostalAdd );
	//	line.addElement(new td(input).setAlign(AlignType.left));
		table.addElement(line);
		//  --  Line 4
		line = new tr();
		line.addElement(new td(Msg.getMsg(ws.ctx, "Country")).setAlign(AlignType.right));
		line.addElement(new td(this.getCountry(mLocation, ws)).setAlign(AlignType.left).setColSpan(5));
		table.addElement(line);
		//
		form.addElement(table);

		//  Reset - <input type="reset" name="Reset" value="Reset">
		input reset = new input(input.reset, "Reset", "Reset");     //  translate
		//  Cancel
		button cancel = new button();
		cancel.addElement("Cancel");                                //  translate
		cancel.setOnClick("window.close();");
		//  Submit
		input submit = new input(input.submit, "Submit", "Submit"); //  translate
		//
		form.addElement(new p(AlignType.left)
			.addElement(reset)
			.addElement("&nbsp")
			.addElement(cancel)
			.addElement("&nbsp")
			.addElement(submit));
		//
		return form;
	}   //  createForm

	/**
	 *  Get Country Pick-List
	 *
	 *  @param mLocation
	 *  @param ws
	 *  @return Select Field with Countries
	 */
	private select getCountry (MLocation mLocation, WWindowStatus ws)
	{
		MCountry[] countries =  mLocation.getCountries ();
		int comp = mLocation.C_Country_ID;
		if (comp == 0)
			comp = Env.getContextAsInt(ws.ctx, "C_Country_ID");
		option[] options = new option[countries.length];
		for (int i = 0; i < countries.length; i++)
		{
			options[i] = new option (String.valueOf(countries[i].C_Country_ID));
			options[i].addElement(countries[i].Name);
			if (comp == countries[i].C_Country_ID)
				options[i].setSelected(true);
		}

		select select = new select (P_C_COUNTRY_ID, options);
		return select;
	}   //  getRegion

	/**
	 *  Get Region Pick-List
	 *
	 *  @param mLocation
	 *  @param ws
	 *  @return Select field with Region
	 */
	private select getRegion (MLocation mLocation, WWindowStatus ws)
	{
		MRegion[] regions =  mLocation.getRegions (mLocation.C_Country_ID);
		int comp = mLocation.C_Region_ID;
		if (comp == 0)
			comp = Env.getContextAsInt(ws.ctx, "C_Region_ID");
		option[] options = new option[regions.length];
		for (int i = 0; i < regions.length; i++)
		{
			options[i] = new option (String.valueOf(regions[i].C_Region_ID));
			options[i].addElement(regions[i].Name);
			if (comp == regions[i].C_Region_ID)
				options[i].setSelected(true);
		}

		select select = new select (P_C_REGION_ID, options);
		return select;
	}   //  getRegion

}   //  WLocation

⌨️ 快捷键说明

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