📄 mlocator.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.model;
import java.sql.*;
import java.util.*;
import java.math.*;
import java.io.Serializable;
import org.compiere.util.*;
/**
* Warehouse Loactor Lookup Model.
*
* @author Jorg Janke
* @version $Id: MLocator.java,v 1.7 2003/02/23 19:42:21 jjanke Exp $
*/
public final class MLocator extends Lookup implements Serializable
{
/**
* Constructor
* @param ctx context
* @param WindowNo window no
*/
public MLocator(Properties ctx, int WindowNo)
{
m_ctx = ctx;
m_WindowNo = WindowNo;
//
m_loader = new Loader();
m_loader.start();
} // MLocator
private Properties m_ctx;
private int m_WindowNo;
protected int C_Locator_ID;
private Loader m_loader;
//
private volatile HashMap m_lookup = new HashMap();
private int m_maxRows = 100; // how many rows to read
private String m_SQL;
/**
* Dispose
*/
public void dispose()
{
Log.trace(Log.l5_DData, "MLocator.dispose " + C_Locator_ID);
if (m_loader != null)
{
while (m_loader.isAlive())
m_loader.interrupt();
}
m_loader = null;
if (m_lookup != null)
m_lookup.clear();
m_lookup = null;
//
super.dispose();
} // dispose
/**
* Wait until async Load Complete
*/
public void loadComplete()
{
if (m_loader != null)
{
try
{
m_loader.join();
}
catch (InterruptedException ie)
{
Log.error("MLocator.loadComplete - join interrupted", ie);
}
}
} // loadComplete
/**
* Get value
* @param key key
* @return value value
*/
public NamePair get (Object key)
{
if (key == null)
return null;
// try cache
Locator loc = (Locator) m_lookup.get(key);
if (loc != null)
return new KeyNamePair (loc.M_Locator_ID, loc.toString());
// Not found and waiting for loader
if (m_loader.isAlive())
{
Log.trace(Log.l5_DData, "MLocator.get - waiting for Loader");
loadComplete();
// is most current
loc = (Locator) m_lookup.get(key);
}
if (loc != null)
return new KeyNamePair (loc.M_Locator_ID, loc.toString());
// Try to get it directly
return getDirect(key, true);
} // get
/**
* Get Display value
* @param value value
* @return String to display
*/
public String getDisplay (Object value)
{
if (value == null)
return "";
//
Object display = get (value);
if (display == null)
return "<" + value.toString() + ">";
return display.toString();
} // getDisplay
/**
* The Lookup contains the key
* @param key key
* @return true, if lookup contains key
*/
public boolean containsKey (Object key)
{
return m_lookup.containsKey(key);
} // containsKey
/**
* Get Data Direct from Table
* @param keyValue key value
* @param saveInCache save in cache
* @return Object directly loaded
*/
public NamePair getDirect (Object keyValue, boolean saveInCache)
{
// Log.trace(Log.l6_Database, "MLocator.getDirect " + keyValue.getClass() + "=" + keyValue);
int keyInt = 0;
try
{
keyInt = Integer.parseInt(keyValue.toString());
}
catch (Exception e)
{}
if (keyInt == 0)
{
Log.error("MLocator.getDirect - invalid key=" + keyValue);
return null;
}
//
NamePair retValue = null;
try
{
PreparedStatement pstmt = DB.prepareStatement(m_SQL);
pstmt.setInt(1, keyInt);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
int key = rs.getInt(1);
Locator loc = new Locator(key, rs.getString(2), rs.getString(3),rs.getString(4),rs.getString(5),
rs.getInt(6), rs.getString(7));
if (saveInCache)
m_lookup.put(new Integer(key), loc);
retValue = new KeyNamePair(key, loc.toString());
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("MLocator.getDirect - SQL=" + m_SQL + "; Key=" + keyValue, e);
retValue = null;
}
return retValue;
} // getDirect
/**
* @return a string representation of the object.
*/
public String toString()
{
return "MLocator[Size=" + m_lookup.size() + "]";
} // toString
/**************************************************************************
* Loader
*/
class Loader extends Thread implements Serializable
{
public Loader()
{
super("MLocator");
} // Loader
/**
* Load Lookup
*/
public void run()
{
// Log.trace(Log.l3_Util, "MLocator Loader.run " + m_AD_Column_ID);
// Set Info
String SQL = "SELECT M_Locator_ID,M_Locator.Value,M_Locator.X,M_Locator.Y,M_Locator.Z," // 1..5
+ "wh.M_Warehouse_ID,wh.Name " // 6..7
+ "FROM M_Locator, M_Warehouse wh "
+ "WHERE M_Locator.M_Warehouse_ID=wh.M_Warehouse_ID";
m_SQL = SQL + " AND M_Locator_ID=?"; // Direct Access
SQL = Access.addROAccessSQL(m_ctx, SQL + " AND wh.IsActive='Y' AND M_Locator.IsActive='Y'", "M_Locator", true);
// Check Static Cache
HashMap temp = MLocator.cacheGet(m_WindowNo, SQL);
if (temp != null && temp.size() > 0)
{
m_lookup = temp;
temp = null;
Log.trace(Log.l3_Util, "MLocator Loader.run - use previously cached #" + m_lookup.size());
return;
}
temp = null;
MLocator.cacheAdd(m_WindowNo, SQL, m_lookup);
//
if (isInterrupted())
{
Log.error("MLocator Loader.run interrupted");
return;
}
// Reset
m_lookup.clear();
int rows = 0;
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
// Get first 100 rows
while (rs.next() && rows++ < m_maxRows)
{
int key = rs.getInt(1);
Locator loc = new Locator(key, rs.getString(2), rs.getString(3),rs.getString(4),rs.getString(5),
rs.getInt(6), rs.getString(7));
m_lookup.put(new Integer(key), loc);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("MLocator Loader\nSQL=" + SQL, e);
}
Log.trace(Log.l3_Util, "MLocator Loader.run - complete #" + m_lookup.size());
} // run
} // Loader
/**
* Return info as ArrayList containing Locator, waits for the loader to finish
* @return Collection of lookup values
*/
public Collection getData ()
{
if (m_loader.isAlive())
{
Log.trace(Log.l5_DData, "MLocator.getData - waiting for Loader");
try
{
m_loader.join();
}
catch (InterruptedException ie)
{
Log.error ("MLocator.getData - join interrupted - " + ie.getMessage());
}
}
return m_lookup.values();
} // getData
/**
* Return data as sorted ArrayList
* @param mandatory mandatory
* @param onlyValidated only validated
* @param onlyActive only active
* @return ArrayList of lookup values
*/
public ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive)
{
// create list
ArrayList list = new ArrayList (getData());
// Sort Data
Locator l = new Locator (0,"","","","",0,"");
if (!mandatory)
list.add (l);
Collections.sort (list, l);
return list;
} // getArray
/**
* Refresh Values
* @return new size of lookup
*/
public int refresh()
{
Log.trace(Log.l4_Data, "MLocator.refresh - start");
m_loader = new Loader();
m_loader.start();
try
{
m_loader.join();
}
catch (InterruptedException ie)
{
}
Log.trace(Log.l4_Data, "MLocator.refresh - #" + m_lookup.size());
return m_lookup.size();
} // refresh
/**
* Get underlying fully qualified Table.Column Name
* @return Table.ColumnName
*/
public String getColumnName()
{
return "M_Locator.M_Locator_ID";
} // getColumnName
/**************************************************************************
* Static Cache
*/
private static HashMap s_cache = new HashMap();
/**
* Reset Cache
*
public static void cacheReset(int WindowNo)
{
s_cache.clear();
} // cacheReset
/**
* Add to cache
* @param WindowNo window no
* @param SQL sql
* @param lookup lookup
*/
public static void cacheAdd (int WindowNo, String SQL, HashMap lookup)
{
s_cache.put(SQL, lookup);
} // cacheAdd
/**
* Get cache
* @param WindowNo window no
* @param SQL sql
* @return HashMap with Lookup values
*/
public static HashMap cacheGet (int WindowNo, String SQL)
{
return (HashMap)s_cache.get(SQL);
} // cacheGet
} // MLocator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -