NC65根据sql读取缓存数据,相关类
//获取已经读出的参照数据
String sql = "select eps_code,eps_name,pk_eps,pk_org,pk_parent from pm_eps where ((pk_group = '0001E110000000001BVA') ) order by eps_code";
Vector v = getCacheValue(sql);//出自:nc.ui.bd.ref.AbstractRefTreeModel.getClassData()// 出自:nc.ui.bd.ref.RefModelHandlerForClient.getCacheValue(String sql)
public Vector getCacheValue(String sql) {SQLCache cache = SQLCache.getSQLCache();if (cache != null) {return cache.getValue(sql);}return null;}
package nc.ui.bd.ref;import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;import nc.bs.logging.Logger;
import nc.ui.dbcache.DBCacheEnv;
import nc.ui.dbcache.DBCacheFacade;
import nc.ui.dbcache.util.SqlTableParser;
import nc.vo.bd.ref.RefColumnDispConvertVO;
import nc.vo.bd.ref.RefQueryResultVO;
import nc.vo.bd.ref.RefQueryVO;
import nc.vo.bd.ref.RefcolumnVO;
import nc.vo.bd.ref.ReftableVO;
import nc.vo.logging.Debug;
import nc.vo.org.GroupVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.sm.UserVO;public class RefModelHandlerForClient implements IRefModelHandler {private AbstractRefModel targetModel;/*** AbstractRefModel 构造子注解。*/public RefModelHandlerForClient(AbstractRefModel targetModel) {super();this.targetModel = targetModel;}public RefModelHandlerForClient(){super();}/*** 删除参照缓存数据*/public void clearCacheData() {RefCache.clearRefDataCache(targetModel.getRefDataCacheKey());}public void clearDataPowerCache() {SQLCache cache = SQLCache.getSQLCache();if (cache != null) {cache.remove(targetModel.getDataPowerSqlKey(targetModel.getTableName(), targetModel.getDataPowerColumn()));}}/*** 获取缓存或数据库表中的参照数据--二维Vector。* * 最后修改人:sxj*
* 最后修改日期:2006-7-12*
* * @param* @return Vector* @exception BusinessException* @since NC5.0*/public final Vector getRefData() {String sqlKey = targetModel.getRefCacheSqlKey();Vector v = null;boolean isDataFromCache = false;boolean isNeedPutToCache = false;if (targetModel.isCacheEnabled()) {/** 从缓存读数据 */v = RefCache.getFromCache(targetModel.getRefDataCacheKey(), sqlKey);/** 从数据库中读 */if (v == null) {v = targetModel.getData();if (v != null) {isNeedPutToCache = true;}} else {isDataFromCache = true;}} else {v = targetModel.getData();isDataFromCache = false;}if (v != null) {v = targetModel.getConvertedData1(isDataFromCache, v, true);// 放入缓存,因为转换数据时 v 会发生变化,所以要转换完数据格式后再加入缓存。if (targetModel.isCacheEnabled() && isNeedPutToCache) {RefCache.putToCache(targetModel.getRefDataCacheKey(), sqlKey, v);}}//按传入Pks过滤Vector filteredVecData = RefPubUtil.getFilterPKsVector(v, targetModel);return filteredVecData;}public Vector queryRefDataFromTemplateTable(String sql) {Vector v = null;// try {//// if (OrgnizeTypeVO.ZHUZHANG_TYPE// .equals(targetModel.getOrgTypeCode())) {// v = RefBO_Client.queryMainByTempTable(targetModel// .getDataSource(), sql, targetModel.getRefNodeName(),// targetModel.getPk_GlOrgBook(), targetModel.getPara());// } else {// v = RefBO_Client.queryMainByTempTable(targetModel// .getDataSource(), sql, targetModel.getRefNodeName(),// getPk_corp(), targetModel.getPara());// }//// } catch (Exception e) {// Debug.debug(e.getMessage(), e);// }return v;}/*** 得到公司主键--默认参照使用。 创建日期:(2001-8-17 11:17:03)* * @return java.lang.String*/public String getPk_corp() {if (targetModel.m_strPk_corp != null) {return targetModel.m_strPk_corp;}
// String pk_corp = RefContext.getInstance().getPk_corp();String pk_corp = getPk_group();// 登陆界面pk_corp会为null,给个默认值,用以保存栏目信息return pk_corp == null ? IRefConst.GROUPCORP : pk_corp;}// 通知前台缓存public void fireDBCache() {List tables = null;try {//tables = SqlTableParser.getTables(targetModel.getRefSql());tables = getTables();if (tables != null) {for (String table : tables) {Logger.debug("参照开始刷新表:" + table);DBCacheFacade.refreshTable(table);Logger.debug("参照刷新表结束:" + table);}}} catch (Exception e) {Logger.debug("树、表参照刷新数据库表" + tables + "失败");}}private List getTables() {List tables = new ArrayList();if (targetModel.getRefSql() != null&& SqlTableParser.getTables(targetModel.getRefSql()) != null)tables.addAll(SqlTableParser.getTables(targetModel.getRefSql()));if (targetModel instanceof AbstractRefTreeModel) {List classTables = getClassTables();if (classTables != null && tables != null) {for (int i = 0; i < classTables.size(); i++) {if (!tables.contains(classTables.get(i))) {tables.add(classTables.get(i));}}}}return tables;}private List getClassTables() {String classSql = ((AbstractRefTreeModel) targetModel).getClassRefSql();if (classSql == null)return null;return SqlTableParser.getTables(classSql);}/*** 此处插入方法说明。 创建日期:(01-4-3 20:08:40)* * @return java.util.Hashtable* @param tableName* java.lang.String*/public Map getFieldCNName() {return BasDocFieldsRes.getInstance().getMap();}/*** 添加列条件* * @param iSelectFieldCount* @param strSql* @param strFieldCode* @param hiddenFields*/public void addQueryColumn(int iSelectFieldCount, StringBuffer strSql,String[] strFieldCode, String[] hiddenFields) {for (int i = 0; i < iSelectFieldCount; i++) {strSql.append(strFieldCode[i]);if (i < iSelectFieldCount - 1)strSql.append(",");}// 加入隐藏字段if (hiddenFields != null && hiddenFields.length > 0) {for (int k = 0; k < hiddenFields.length; k++) {if (hiddenFields[k] != null&& hiddenFields[k].trim().length() > 0) {strSql.append(",");strSql.append(hiddenFields[k]);}}}}/*** 过滤表名* * @param column* @return*/public String filterColumn(String column) {return column.substring(column.indexOf(".") + 1, column.length());}/*** @param sqlBuffer*/public String addBlurWherePart() {// 处理模糊---但是不加入WherePartStringBuffer sqlBuffer = new StringBuffer();String[] toLowCasefieldNames = new String[targetModel.getBlurFields().length];for (int i = 0; i < toLowCasefieldNames.length; i++) {toLowCasefieldNames[i] = RefPubUtil.toLowerDBFunctionWrapper(targetModel, targetModel.getBlurFields()[i]);}if (RefPubUtil.isIncludeBlurChar(targetModel.getBlurValue())) {String value = targetModel.getBlurValue().replace('*', '%').replace('?', '_');String blurSql = RefPubUtil.getCompositeSql(toLowCasefieldNames,RefPubUtil.toLowerCaseStr(targetModel, value), " like ",null, null, " or ");sqlBuffer.append(" and (");sqlBuffer.append(blurSql);sqlBuffer.append(")");} else { // 处理助记码检索为多条if (targetModel.isMnecodeInput()) {String blurSql = RefPubUtil.getCompositeSql(toLowCasefieldNames, RefPubUtil.toLowerCaseStr(targetModel, targetModel.getBlurValue()), "=",null, null, " or ");sqlBuffer.append(" and ( ").append(blurSql).append(" )");}}String blurSql = sqlBuffer.toString();return blurSql;}/** */public ReftableVO getRefTableVO(String pk_corp) {ReftableVO vo = null;String key = getReftableVOCacheKey(pk_corp);// 从缓存读Object obj = SQLCache.getSQLCache().getColumn(key);if (obj == null) {String refNodeName = targetModel.getRefNodeName();if (pk_corp == null) {pk_corp = getPk_corp();}try {obj = ReftableBO_Client.findByNode(refNodeName, pk_corp);} catch (Exception e) {Logger.debug(e);obj = SQLCache.NULLFLAG;}}if (obj instanceof ReftableVO) {vo = (ReftableVO) obj;}setReftableVO2Cache(vo, pk_corp);return vo;}/** 栏目信息数据放到缓存.*/public void setReftableVO2Cache(ReftableVO vo, String pk_org) {String key = getReftableVOCacheKey(pk_org);// 放到缓存中。if (vo != null) {SQLCache.getSQLCache().putColumns(key, vo);} else {SQLCache.getSQLCache().putColumns(key, SQLCache.NULLFLAG);}}public boolean isReftableVOCached(String pk_org) {// 从缓存读Object obj = SQLCache.getSQLCache().getColumn(getReftableVOCacheKey(pk_org));return obj != null;}public String getReftableVOCacheKey(String pk_org) {String refNodeName = targetModel.getRefNodeName();// if (OrgnizeTypeVO.ZHUZHANG_TYPE.equals(getOrgTypeCode())){// pk_org = getPk_GlOrgBook();// }else{// pk_org = getPk_corp();// }// 目前还是按当前登录公司来保存栏目信息,以后有需求可以加入对主体账簿的支持String key = refNodeName + pk_org;return key;}/*** * 最后修改人:sxj*
* 最后修改日期:2006-5-18*
* * @param* @return String* @exception BusinessException* @since NC5.0*/public String getFieldCodeWithTableName(String fieldCode) {if (fieldCode.indexOf(".") < 0) {fieldCode = RefPubUtil.getFirstTableName(targetModel.getTableName())+ "." + fieldCode;}return fieldCode;}public String getResID(String fieldCode) {String resid = null;fieldCode = getFieldCodeWithTableName(fieldCode);if (getFieldCNName() != null) {resid = (String) getFieldCNName().get(fieldCode);}return resid;}/***
* 最后修改人:sxj*
* 最后修改日期:2006-5-17*
* * @param* @return void* @exception BusinessException* @since NC5.0*/private RefValueVO getRefValueVO(Object originValue, Object newValue) {RefValueVO valueVO = new RefValueVO();valueVO.setOriginValue(originValue);valueVO.setNewValue(newValue);return valueVO;}/*** @return 返回 m_pk_user。*/public String getPk_user() {try {Class c = Class.forName("nc.desktop.ui.WorkbenchEnvironment");Method m = c.getMethod("getInstance", null);Object o = m.invoke(null, null);Method m1 = c.getMethod("getLoginUser", null);UserVO userVO = (UserVO) m1.invoke(o, null);return userVO.getPrimaryKey();} catch (Exception e) {// TODO Auto-generated catch blockLogger.debug(e);}return null;}public boolean canUseDB() {return DBCacheEnv.isCacheEnabled();}public Vector getCacheValue(String sql) {SQLCache cache = SQLCache.getSQLCache();if (cache != null) {return cache.getValue(sql);}return null;}public Vector queryMain(String dsName, String sql) {try {return RefBO_Client.queryMain(dsName, sql,targetModel.isQueryFromServer());} catch (BusinessException e) {Logger.error(e);}return null;}public void removeCache(String sql) {SQLCache cache = SQLCache.getSQLCache();if (cache != null) {cache.remove(sql);}}public void setCacheValue(String sql, Vector value) {SQLCache cache = SQLCache.getSQLCache();if (cache != null) {cache.putValue(sql, value);}}public String getCodeRuleFromPara(String orgTypeCode, String pk_GlOrgBook,String codingRule, UFBoolean boolean1) {return RefPubUtil.getCodeRuleFromPara(orgTypeCode, pk_GlOrgBook,codingRule, boolean1);}public String getCodeRuleFromPara(String codingRule) {return RefPubUtil.getCodeRuleFromPara(codingRule, getPk_corp());}public String getNumberCodingRule(String codingRule) {return RefPubUtil.getNumberCodingRule(codingRule);}public String[] getDefFields(String[] fieldCode) {return RefPubUtil.getDefFields(fieldCode);}public String getWherePartByFieldsAndValues(String[] fields, String[] values) {String wherePart = "";StringBuffer sb = new StringBuffer();if (fields != null) {int length = fields.length;for (int i = 0; i < length; i++) {if (values.length == 1) {// =sb.append(fields[i]).append("='").append(values[0]).append("' ");} else {// insb.append(fields[i]).append(" in ").append(RefPubUtil.getInSubSql(values));}if (i == length - 1) {continue;}sb.append(" or ");}} else {return null;}wherePart = sb.toString();return wherePart;}public boolean isNonSqlMatch(String matchsql) {String refNodeName = targetModel.getRefNodeName();return matchsql == null || refNodeName != null&& RefPubUtil.isNonSqlMatchRef(refNodeName);}public Map getRefColumnVOsMap() {RefcolumnVO[] columnVOs = RefPubUtil.getColumnSequences(this.targetModel);Map map = new HashMap();for (int i = 0; i < columnVOs.length; i++) {map.put(columnVOs[i].getFieldname(), columnVOs[i]);}return map;}public String getRefNodeName_mLang(String refNodeName) {return RefPubUtil.getRefNodeName_mLang(refNodeName);}public String getRefMultiLangStr(String resid, String fieldCode) {return RefPubUtil.getRefMultiLangStr(resid, fieldCode);}public void putToCache(String refDataCacheKey, String sql, Vector data) {RefCache.putToCache(refDataCacheKey, sql, data);}public RefQueryResultVO queryRefdataByQueryVO(RefQueryVO queryVO) {RefQueryResultVO resultVO = null;try {resultVO = RefBO_Client.queryRefdataByQueryVO(queryVO,targetModel.isQueryFromServer());} catch (Exception e) {Logger.info(e);}return resultVO;}public RefQueryResultVO queryBlurRefdataByQueryVO(RefQueryVO queryVO) {RefQueryResultVO resultVO = null;try {resultVO = RefBO_Client.queryBlurRefdataByQueryVO(queryVO);} catch (Exception e) {Logger.info(e);}return resultVO;}public Vector matchPK(String dataSource, String matchsql) {try {return RefPubUtil.getFilterPKsVector(RefBO_Client.matchPK(dataSource, matchsql,targetModel.isQueryFromServer()), targetModel);} catch (BusinessException e) {Debug.error(e.getMessage(), e);}return null;}public boolean equals(String m_fun_code, String m_fun_code2) {return RefPubUtil.equals(m_fun_code, m_fun_code2);}public Vector getFromCache(String refDataCacheKey, String sql) {return RefCache.getFromCache(refDataCacheKey, sql);}public String getDataPowerSubSql(String tableName, String dataPowerField,AbstractRefModel model, String resourceID) {return RefDataPowerHelper.getDataPowerSubSql(tableName,dataPowerField, model, resourceID);}public Vector setColDispValue(Vector vecData,RefColumnDispConvertVO convertVO, String className) {IRefColDispConverter converter = (IRefColDispConverter) getClassInstance(className);return converter.getDispConvertResult(convertVO);}private Object getClassInstance(String className) {Object clsInstance = null;try {Class cls = Class.forName(className);clsInstance = cls.newInstance();} catch (Exception e) {Logger.debug(e);}return clsInstance;}public String getPk_group() {try {Class c = Class.forName("nc.desktop.ui.WorkbenchEnvironment");Method m = c.getMethod("getInstance", null);Object o = m.invoke(null, null);Method m1 = c.getMethod("getGroupVO", null);GroupVO groupVO = (GroupVO) m1.invoke(o, null);if (groupVO != null)return groupVO.getPrimaryKey();} catch (Exception e) {// TODO Auto-generated catch blockLogger.debug(e);}return null;}public LRUMap getLRUMap() {// TODO Auto-generated method stubreturn SQLCache.getSQLCache().getLRUMap();}
}
package nc.ui.bd.ref;import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;
import nc.bs.logging.Logger;/*** 此处插入类型说明。 创建日期:(01-6-18 17:32:58)* * @author:童志杰 修改:张扬 1.新增clear方法 2.新增remove(String)方法*/
public class SQLCache {private java.util.Hashtable ht;private java.util.HashMap recentRecordht;public static final String REFSQLCACHE_STRING = "ref223234";LinkedList mrulist = new LinkedList();public static final int DEFAULT_SIZE = 8003;int cacheSize = DEFAULT_SIZE;public static final String NULLFLAG = "null";private static SQLCache cache = new SQLCache();private LRUMap lruMap = new LRUMap(20);/*** SQLCache 构造子注解。*/private SQLCache() {super();}/*** 此处插入方法说明。 创建日期:(2001-7-25 17:02:55)*/public void clear() {//getRecentRecordht().clear();getHt().clear();getLRUMap().clear();if (mrulist != null)mrulist.clear();elsemrulist = new LinkedList();}/*** 此处插入方法说明。 创建日期:(01-6-18 17:41:48)*/public String get(String sql) {if (sql == null || sql.trim().length() == 0)return null;if (getSQLCache().getHt().containsKey(sql.trim())) {if (mrulist == null) {mrulist = new LinkedList();} else {try {mrulist.remove(sql.trim());} catch (Exception e) {Logger.debug(e);}}mrulist.addFirst(sql.trim());return (String) getSQLCache().getHt().get(sql.trim());} elsereturn null;}public Object getObjectValue(String sql) {if (sql == null || sql.trim().length() == 0)return null;if (getSQLCache().getHt().containsKey(sql.trim())) {if (mrulist == null) {mrulist = new LinkedList();} else {mrulist.remove(sql.trim());}mrulist.addFirst(sql.trim());return getSQLCache().getHt().get(sql.trim());} elsereturn null;}/*** 此处插入方法说明。 创建日期:(01-6-18 17:41:48)*/public Object getColumn(String nodeCorpName) {if (nodeCorpName == null || nodeCorpName.trim().length() == 0)return null;if (getSQLCache().getHt().containsKey(nodeCorpName.trim())) {if (mrulist == null) {mrulist = new LinkedList();} else {mrulist.remove(nodeCorpName.trim());}mrulist.addFirst(nodeCorpName.trim());Object obj = getSQLCache().getHt().get(nodeCorpName.trim());return obj;// nc.vo.bd.ref.ReftableVO VO=null// return (nc.vo.bd.ref.ReftableVO)// getSQLCache().getHt().get(nodeCorpName.trim());} elsereturn null;}/*** 此处插入方法说明。 创建日期:(01-6-18 17:41:31)* * @return java.util.Hashtable*/private java.util.Hashtable getHt() {if (ht == null)ht = new Hashtable();return ht;}/*** 从环境变量获得SQLCache。 创建日期:(01-6-18 17:38:55)*/public static SQLCache getSQLCache() {// DesktopEnvironment ce = DesktopEnvironment.getInstance();// if (ce.getClientCache(REFSQLCACHE_STRING) == null) {// SQLCache cache = new SQLCache();// ce.putClientCache(REFSQLCACHE_STRING, cache);// }// return (SQLCache) ce.getClientCache(REFSQLCACHE_STRING);return cache;}/*** 此处插入方法说明。 创建日期:(01-6-18 17:41:48)*/public Vector getValue(String sql) {if (sql == null || sql.trim().length() == 0)return null;if (getSQLCache().getHt().containsKey(sql.trim())) {if (mrulist == null) {mrulist = new LinkedList();} else {mrulist.remove(sql.trim());}mrulist.addFirst(sql.trim());return (Vector) getSQLCache().getHt().get(sql.trim());} elsereturn null;}/*** 把用户保存的参照栏目的信息缓存,区分公司*/public void putColumns(String nodeCorpName, Object obj) {if (nodeCorpName == null || obj == null)return;// synchronized (getSQLCache().getHt()) {if (nodeCorpName != null && obj != null) {if (mrulist == null) {mrulist = new LinkedList();} else if (mrulist.size() >= cacheSize) {getSQLCache().getHt().remove(mrulist.getLast());mrulist.removeLast();}getSQLCache().getHt().put(nodeCorpName.trim(), obj);mrulist.addFirst(nodeCorpName.trim());}// }return;}/*** 把数据权限的Sql 语句缓存,解决参照取得数据时,每次都要都要调用数据权限接口。*/public void putValue(String sql, Object dataPowerSql) {if (sql == null || sql.trim().length() == 0 || dataPowerSql == null)return;// synchronized (getSQLCache().getHt()) {if (sql != null && dataPowerSql != null) {if (mrulist == null) {mrulist = new LinkedList();} else if (mrulist.size() >= cacheSize) {getSQLCache().getHt().remove(mrulist.getLast());mrulist.removeLast();}getSQLCache().getHt().put(sql.trim(), dataPowerSql);mrulist.addFirst(sql.trim());}// }return;}/*** 此处插入方法说明。 创建日期:(01-6-18 17:41:48)*/public void putValue(String sql, Vector v) {if (sql == null || sql.trim().length() == 0 || v == null)return;// synchronized (getSQLCache().getHt()) {if (sql != null && v != null) {if (mrulist == null) {mrulist = new LinkedList();} else if (mrulist.size() >= cacheSize) {getSQLCache().getHt().remove(mrulist.getLast());mrulist.removeLast();}getSQLCache().getHt().put(sql.trim(), v);mrulist.addFirst(sql.trim());}// }return;}/*** 此处插入方法说明。 创建日期:(2001-7-25 17:01:00)* * @param sql* java.lang.String*/public void remove(String sql) {if (sql != null) {getSQLCache().getHt().remove(sql.trim());if (mrulist == null) {mrulist = new LinkedList();} else {try {mrulist.remove(sql.trim());} catch (Exception e) {Logger.debug(e);}}}}public HashMap getRefDataValue(String key) {if (key == null || key.trim().length() == 0)return null;if (getSQLCache().getHt().containsKey(key.trim())) {if (mrulist == null) {mrulist = new LinkedList();} else {mrulist.remove(key.trim());}mrulist.addFirst(key.trim());return (HashMap) getSQLCache().getHt().get(key.trim());} elsereturn null;}/*** */public void putValue(String key, HashMap hm) {if (key == null || key.trim().length() == 0 || hm == null)return;if (mrulist == null) {mrulist = new LinkedList();} else if (mrulist.size() >= cacheSize) {getSQLCache().getHt().remove(mrulist.getLast());mrulist.removeLast();}getSQLCache().getHt().put(key, hm);mrulist.addFirst(key);return;}public LRUMap getLRUMap() {return lruMap;}public java.util.HashMap getRecentRecordht() {if (recentRecordht == null)recentRecordht = new HashMap();return recentRecordht;}}
package nc.ui.bd.ref;import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;public class LRUMap extends LinkedHashMap implements Cloneable,Serializable {private static final long serialVersionUID = 1L;private int lruSize = 500;public LRUMap(int initSize) {super(initSize, 1f, true);this.lruSize = initSize;}@Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {if (size() > lruSize)return true;elsereturn false;}
}
上一篇:电脑删除的文件如何找回?
下一篇:dayjs 笔记