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

📄 mlookup.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}

		//	Never Loaded
		if (!m_allLoaded && m_lookup.size() == 0)
			refresh();

		//	already validation included
		if (m_info.IsValidated)
			return new ArrayList(m_lookup.values());

		if (!m_info.IsValidated && onlyValidated)
		{
			refresh();
			Log.trace(Log.l6_Database, "MLookup.getData Validated - #" + m_lookup.size());
		}

		return new ArrayList(m_lookup.values());
	}	//	getData

	/**
	 *	Return data as Array containing Value/KeyNamePair
	 *  @param mandatory if not mandatory, an additional empty value is inserted
	 *  @param onlyValidated only validated
	 *  @param onlyActive only active
	 *  @return list
	 */
	public ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive)
	{
		//	create list
		ArrayList list = getData(onlyValidated);

		//  Remove inactive choices
		if (onlyActive && m_hasInactive)
		{
			//  list from the back
			for (int i = list.size(); i > 0; i--)
			{
				Object o = list.get(i-1);
				if (o != null)
				{
					String s = o.toString();
					if (s.startsWith(INACTIVE_S) && s.endsWith(INACTIVE_E))
						list.remove(i-1);
				}
			}
		}

		//	Add Optional (empty) selection
		if (!mandatory)
		{
			NamePair p = null;
			if (m_info.KeyColumn != null && m_info.KeyColumn.endsWith("_ID"))
				p = new KeyNamePair (-1, "");
			else
				p = new ValueNamePair ("", "");
			list.add(0, p);
		}

		return list;
	}	//	getData

	/**	Save getDirect last return value	*/
	private NamePair		m_directValue = null;
	/**	Save last unsuccessful				*/
	private Object			m_directNullKey = null;

	/**
	 *	Get Data Direct from Table
	 *  @param key key
	 *  @param saveInCache save in cache
	 *  @return value
	 */
	public NamePair getDirect (Object key, boolean saveInCache)
	{
		Log.trace (7, "MLookup.getDirect - " + m_info.KeyColumn + "=" + key);
		//	Nothing to query
		if (key == null || m_info.QueryDirect == null || m_info.QueryDirect.length() == 0)
			return null;
		if (key.equals(m_directNullKey))
			return null;
		//
		boolean isNumber = m_info.KeyColumn.endsWith("_ID");
		try
		{
			//	SELECT Key, Value, Name FROM ...
			PreparedStatement pstmt = DB.prepareStatement(m_info.QueryDirect);
			if (isNumber)
				pstmt.setInt(1, Integer.parseInt(key.toString()));
			else
				pstmt.setString(1, key.toString());
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				String name = rs.getString(3);
				if (isNumber)
				{
					int keyValue = rs.getInt(1);
					KeyNamePair p = new KeyNamePair(keyValue, name);
					if (saveInCache || p.equals(m_directValue))		//	save if asked for twice
						m_lookup.put(new Integer(keyValue), p);
					m_directValue = p;
				}
				else
				{
					String value = rs.getString(2);
					ValueNamePair p = new ValueNamePair(value, name);
					if (saveInCache || p.equals(m_directValue))		//	save if asked for twice
						m_lookup.put(value, p);
					m_directValue = p;
				}
				if (rs.next())
					Log.error("MLookup.getDirect - not unique (first returned) for "
						+ m_info.KeyColumn + "=" + key + " SQL=" + m_info.QueryDirect);
			}
			else
			{
				m_directNullKey = key;
				m_directValue = null;
			}

			rs.close();
			pstmt.close();
			Log.trace (9, "MLookup.getDirect - " + m_directValue,	m_info);
		}
		catch (SQLException e)
		{
			Log.error("MLookup.getDirect - SQL=" + m_info.QueryDirect + "; Key=" + key, e);
			m_directValue = null;
		}
		return m_directValue;
	}	//	getDirect

	/**
	 *	Get Zoom
	 *  @return Zoom Window
	 */
	public int getZoom()
	{
		return m_info.ZoomWindow;
	}	//	getZoom

	/**
	 *	Get Zoom Query String
	 *  @return Zoom SQL Where Clause
	 */
	public MQuery getZoomQuery()
	{
		return m_info.ZoomQuery;
	}	//	getZoom

	/**
	 *	Get underlying fully qualified Table.Column Name
	 *  @return Key Column
	 */
	public String getColumnName()
	{
		return m_info.KeyColumn;
	}	//	g2etColumnName

	/**
	 *	Refresh & return number of items read
	 *  @return no of items read
	 */
	public int refresh()
	{
		if (m_info.IsParent)
			return 0;
		Log.trace(Log.l6_Database, "MLookup.refresh - start");
		m_refreshing = true;
		m_loader = new Loader();
		m_loader.start();
		loadComplete();
		Log.trace(Log.l6_Database, "MLookup.refresh - #" + m_lookup.size());
		m_refreshing = false;
		return m_lookup.size();
	}	//	refresh

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

	/**
	 *	MLookup Loader
	 */
	class Loader extends Thread implements Serializable
	{
		public Loader()
		{
			super("MLoader-" + m_info.KeyColumn);
		}	//	Loader

		private long m_startTime = System.currentTimeMillis();

		/**
		 *	Load Lookup
		 */
		public void run()
		{
			long startTime = System.currentTimeMillis();
			MLookupCache.loadStart (m_info);
			String sql = m_info.Query;
			//	not validated
			if (!m_info.IsValidated)
			{
				String validation = Env.parseContext(m_info.ctx, m_info.WindowNo, m_info.Validation, false);
				if (validation.length() == 0 && m_info.Validation.length() > 0)
				{
					Log.trace(Log.l6_Database, "MLookup.Loader.run - " + m_info.KeyColumn
						+ " - NOT Validated: " + m_info.Validation);
					return;
				}
				else
				{
					Log.trace(Log.l6_Database, "MLookup.Loader.run - " + m_info.KeyColumn
						+ " - Validated: " + validation);
					int posOrder = sql.lastIndexOf(" ORDER BY ");
					if (posOrder != -1)
						sql = sql.substring(0, posOrder) + " AND " + validation
							+ sql.substring(posOrder);
					else
						sql += " AND " + validation;
				}
			}
			//
			if (Log.getTraceLevel() > Log.l6_Database)
				Env.setContext(m_info.ctx, Env.WINDOW_MLOOKUP, m_info.AD_Column_ID, m_info.KeyColumn, sql);
			//
			if (isInterrupted())
			{
				Log.error("MLookup.Loader.run interrupted");
				return;
			}

			Log.trace(10, "MLookup.Loader.run - " + m_info.KeyColumn, sql);
			Log.trace(10, "MLookup.Loader.run - " + m_info.KeyColumn, "Direct= " + m_info.QueryDirect);
			//	Reset
			m_lookup.clear();
			boolean isNumber = m_info.KeyColumn.endsWith("_ID");
			m_hasInactive = false;
			int rows = 0;
			try
			{
				//	SELECT Key, Value, Name, IsActive FROM ...
				PreparedStatement pstmt = DB.prepareStatement(sql);
				ResultSet rs = pstmt.executeQuery();

				//	Get first ... rows
				m_allLoaded = true;
				while (rs.next())
				{
					if (rows++ > m_maxRows)
					{
						m_allLoaded = false;
						break;
					}
					//  check for interrupted every 50 rows
					if (rows % 50 == 0 && isInterrupted())
						break;

					//  load data
					String name = rs.getString(3);
					boolean isActive = rs.getString(4).equals("Y");
					if (!isActive)
					{
						name = INACTIVE_S + name + INACTIVE_E;
						m_hasInactive = true;
					}
					if (isNumber)
					{
						int key = rs.getInt(1);
						KeyNamePair p = new KeyNamePair(key, name);
						m_lookup.put(new Integer(key), p);
					}
					else
					{
						String value = rs.getString(2);
						ValueNamePair p = new ValueNamePair(value, name);
						m_lookup.put(value, p);
					}
				//	Log.trace(Log.l6_Database, m_info.KeyColumn + " " + name);
				}
				rs.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				Log.error("MLookup Loader - SQL=" + sql, e);
			}
			int size = m_lookup.size();
			Log.trace(Log.l6_Database, "MLookup.Loader.run",
			//	"ID=" + m_info.AD_Column_ID + " " +
				m_info.KeyColumn + " - complete #" + size
				+ " - ms=" + String.valueOf(System.currentTimeMillis()-m_startTime)
				+ " (" + String.valueOf(System.currentTimeMillis()-startTime) + ")");
			MLookupCache.loadEnd (m_info, m_lookup);
			if (size > 1000 && Log.getTraceLevel() > Log.l6_Database)
				Log.trace(Log.l1_User, "MLookup.Loader.run - " + m_info.KeyColumn + " Too many records=" + size);
		}	//	run
	}	//	Loader

}	//	MLookup

⌨️ 快捷键说明

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