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

📄 mlookup.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.model;

import java.sql.*;
import java.util.*;
import java.text.*;
import java.io.Serializable;

import org.compiere.util.*;

/**
 *	An intelligent MutableComboBoxModel, which determines what can be cached.
 *  <pre>
 *      Validated   - SQL is final / not dynamic
 *      AllLoaded   - All Records are loaded
 *
 *		Get Info about Lookup
 *		-	SQL
 *		-	KeyColumn
 *		-	Zoom Target
 *  </pre>
 * 	@author 	Jorg Janke
 * 	@version 	$Id: MLookup.java,v 1.6 2003/05/04 06:46:24 jjanke Exp $
 */
public final class MLookup extends Lookup implements Serializable
{
	/**
	 *  MLookup Constructor
	 *  @param info info
	 *  @param TabNo tab no
	 */
	public MLookup (MLookupInfo info, int TabNo)
	{
		super();
		m_info = info;
		Log.trace(Log.l6_Database, "MLookup", m_info.KeyColumn);

		//  Don't load Parents
		if (m_info.IsParent)
		{
			m_hasInactive = true;		//	creates focus listener for dynamic loading
			return;						//	required when parent needs to be selected (e.g. price from product)
		}

		//  load into local lookup, if already cached
		if (MLookupCache.loadFromCache (m_info, m_lookup))
			return;

		//  Don't load Search
		if (m_info.DisplayType == DisplayType.Search)
			return;
		//
		m_loader = new Loader();
	//	if (TabNo != 0)
	//		m_loader.setPriority(Thread.MIN_PRIORITY);
		m_loader.start();
	//	m_loader.run();
	}	//	MLookup

	/** Inactive Marker Start       */
	public static final String  INACTIVE_S = "~";
	/** Inactive Marker End         */
	public static final String  INACTIVE_E = "~";

	/** The Lookup Info Value Object        */
	private MLookupInfo         m_info = null;

	/** Storage of data  Key-NamePair	*/
	private volatile LinkedHashMap	m_lookup = new LinkedHashMap();
	/** The Data Loader                 */
	private Loader				m_loader;
	/** Number of rows to load          */
	private int 				m_maxRows = 1000000;	//	1 Mio
	//

	/** All Data loaded                 */
	private boolean             m_allLoaded = false;
	/** Inactive records exists         */
	private boolean 		    m_hasInactive = false;

	/*  Refreshing - disable cashing    */
	private boolean             m_refreshing = false;

	/** Where Clause                    */
	private String 				m_whereClause = "";

	/**
	 *  Dispose
	 */
	protected void dispose()
	{
		Log.trace(Log.l6_Database, "MLookup.dispose " + m_info.KeyColumn);
		if (m_loader != null)
		{
			while (m_loader.isAlive())
				m_loader.interrupt();
		}
		m_loader = null;
		//
		if (m_lookup != null)
			m_lookup.clear();
		m_lookup = null;
		//
		m_info = null;
		//
		super.dispose();
	}   //  dispose

	/**
	 *  Wait until async Load Complete
	 */
	public void loadComplete()
	{
		if (m_loader != null && m_loader.isAlive())
		{
			try
			{
				m_loader.join();
				m_loader = null;
			}
			catch (InterruptedException ie)
			{
				Log.error("MLookup.loadComplete - join interrupted", ie);
			}
		}
	}   //  loadComplete

	/**
	 *	Get value (name) for key.
	 *  If not found return null;
	 *  @param key key
	 *  @return value
	 */
	public NamePair get (Object key)
	{
		if (key == null)
			return null;

		//	try cache
		NamePair retValue = (NamePair)m_lookup.get(key);
		if (retValue != null)
			return retValue;

		//	Not found and waiting for loader
		if (m_loader != null && m_loader.isAlive())
		{
			Log.trace (7, "MLookup.get - waiting for Loader", m_info.KeyColumn==null ? "ID="+m_info.AD_Column_ID : m_info.KeyColumn);
			loadComplete();
			//	is most current
			retValue = (NamePair)m_lookup.get(key);
			if (retValue != null)
				return retValue;
		}

		//  Always check for parents - not if we SQL was validated and completely loaded
		if (!m_info.IsParent && m_info.IsValidated && m_allLoaded)
		{
			Log.trace (7, "MLookup.get <NULL>", m_info.KeyColumn + "=" + key // + "(" + key.getClass()
				+ "; Size=" + m_lookup.size());
		//	Log.trace(10, m_lookup.keySet().toString(), "ContainsKey = " + m_lookup.containsKey(key));
			//  also for new values and inactive ones
			return getDirect(key, false);    //  do not save in cache as validated
		}

		Log.trace (7, "MLookup.get " + m_info.KeyColumn + "=" + key
			+ "; Size=" + m_lookup.size() + "; Validated=" + m_info.IsValidated
			+ "; All Loaded=" + m_allLoaded + "; HasInactive=" + m_hasInactive);
		//	never loaded
		if (!m_allLoaded && m_lookup.size() == 0 && m_info.DisplayType != DisplayType.Search)
		{
			m_loader = new Loader();
			m_loader.run();		//	sync!
			retValue = (NamePair)m_lookup.get(key);
			if (retValue != null)
				return retValue;
		}
		//	Try to get it directly
	//	Log.trace(Log.l6_Database, "- getDirect");
		return getDirect(key, true);         //  save in cache
	}	//	get

	/**
	 *	Get Display value (name).
	 *  If not found return key embedded in inactive signs.
	 *  @param key key
	 *  @return value
	 */
	public String getDisplay (Object key)
	{
		if (key == null)
			return "";
		//
		Object display = get (key);
		if (display == null)
			return "<" + key.toString() + ">";
		return display.toString();
	}	//	getDisplay

	/**
	 *  The Lookup contains the key
	 *  @param key key
	 *  @return true if key is known
	 */
	public boolean containsKey (Object key)
	{
		return m_lookup.containsKey(key);
	}   //  containsKey

	/**
	 * @return  a string representation of the object.
	 */
	public String toString()
	{
		return "MLookup[" + m_info.KeyColumn + ",AD_Column_ID=" + m_info.AD_Column_ID + ",Size=" + m_lookup.size() + "]";
	}	//	toString

	/**
	 * Indicates whether some other object is "equal to" this one.
	 * @param   obj   the reference object with which to compare.
	 * @return  <code>true</code> if this object is the same as the obj
	 *          argument; <code>false</code> otherwise.
	 */
	public boolean equals(Object obj)
	{
		if (obj instanceof MLookup)
		{
			MLookup ll = (MLookup)obj;
			if (ll.m_info.AD_Column_ID == this.m_info.AD_Column_ID)
				return true;
		}
		return false;
	}	//	equals

	/**
	 *	Return Size
	 *  @return size
	 */
	public int size()
	{
		return m_lookup.size();
	}	//	size

	/**
	 *	Is it all loaded
	 *  @return true, if all loaded
	 */
	public boolean isAllLoaded()
	{
		return m_allLoaded;
	}	//	isAllLoaded

	/**
	 *	Is the List fully Validated
	 *  @return true, if validated
	 */
	public boolean isValidated()
	{
		return m_info.IsValidated;
	}	//	isValidated

	/**
	 *  Get Validation SQL
	 *  @return Valudation SQL
	 */
	public String getValidation()
	{
		return m_info.Validation;
	}   //  getValidation

	/**
	 *  Has inactive elements in list
	 *  @return true, if list contains inactive values
	 */
	public boolean hasInactive()
	{
		return m_hasInactive;
	}   //  hasInactive

	/**
	 *	Return info as ArrayList containing Value/KeyNamePair
	 *  @param onlyValidated only validated
	 *  @return List
	 */
	public ArrayList getData (boolean onlyValidated)
	{
		if (m_loader != null && m_loader.isAlive())
		{
			Log.trace(Log.l6_Database, "MLookup.getData - waiting for Loader", m_info.KeyColumn==null ? "ID="+m_info.AD_Column_ID : m_info.KeyColumn);
			loadComplete();

⌨️ 快捷键说明

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