📄 qobjectdb.java
字号:
package com.cloudwebsoft.framework.base;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import cn.js.fan.db.*;
import cn.js.fan.util.*;
import cn.js.fan.web.SkinUtil;
import com.cloudwebsoft.framework.db.Connection;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import com.cloudwebsoft.framework.util.LogUtil;
/**
* <p>Title: QObject (Quick Object)</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public abstract class QObjectDb implements java.io.Serializable {
public QDBTable table;
public String cacheGroup;
public String cacheGroupCount;
public boolean isInitFromConfig = true;
public PrimaryKey primaryKey;
public boolean loaded = false;
public ResultRecord resultRecord;
public QObjectDb() {
// 从配置中读取queryCreate,queryLoad, querySave, cacheGroup
init();
}
public void init() {
// connName = Global.defaultDB;
initDB();
if (isInitFromConfig)
initFromConfig();
cacheGroup = this.getClass().getName();
cacheGroupCount = cacheGroup + "_count";
}
public void initDB() {
// isInitFromConfig = false;
}
public void initFromConfig() {
QDBConfig dc = new QDBConfig();
// LogUtil.getInstance(getClass()).info("initFromConfig:" + getClass().getName());
table = dc.getQDBTable(getClass().getName());
if (table == null) {
// LogUtil.getLog(this.getClass()).error("initFromConfig2: cann't find table '" + getClass().getName() + "' defination in config file.");
throw new IllegalArgumentException(
"initFromConfig2: cann't find table '" + getClass().getName() +
"' defination in config file.");
}
// 注意需通过clone产生一个新的实例,否则因为如果是自缓存中获得的,则dt.getPrimaryKey()引用的是同一个实例
this.primaryKey = (PrimaryKey) table.getPrimaryKey().clone();
this.objCachable = table.isObjCachable();
this.listCachable = table.isListCachable();
}
public QDBTable getTable() {
return table;
}
public String getCacheKey(PrimaryKey pk) {
return table.getName() + "_" + pk.toString();
}
public QObjectDb getQObjectDb(Object primaryKeyValue) {
PrimaryKey pk = new PrimaryKey();
pk.setValue(primaryKeyValue);
// System.out.println(getClass() + " getQObjectDb:" + pk + " getCacheKey(pk)=" + getCacheKey(pk));
QObjectDb obj = null;
if (objCachable) {
try {
obj = (QObjectDb) QCache.getInstance().getFromGroup(getCacheKey(
pk),
cacheGroup);
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error("getQObject:" +
e.getMessage());
}
// System.out.println(getClass() + " getQObjectDb:" + pk + " obj=" + obj);
if (obj == null) {
// obj = new QObjectDb();
obj = getQObjectDbRaw(primaryKeyValue);
if (obj != null && obj.isLoaded()) {
try {
QCache.getInstance().putInGroup(getCacheKey(pk),
cacheGroup, obj);
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error(
"getQObject2: key=" + getCacheKey(primaryKey) +
" group=" + cacheGroup + " " + e.getMessage());
LogUtil.getLog(this.getClass()).error(StrUtil.trace(e));
}
}
}
} else {
obj = getQObjectDbRaw(primaryKeyValue);
}
// System.out.println(getClass() + " getQObjectDb2:" + obj.primaryKey);
return obj;
}
public QObjectDb getQObjectDbRaw(Object primaryKeyValue) {
QObjectDb obj = null;
try {
obj = (QObjectDb) getClass().newInstance();
} catch (Exception e) {
LogUtil.getLog(getClass()).error("getQObjectDbRaw:" + e.getMessage());
}
// logger.info("obj=" + obj + " pk=" + pk.getValue() + " group=" + group);
obj.primaryKey.setValue(primaryKeyValue);
// System.out.println(getClass() + " getQObjectDbRaw:" + obj.primaryKey);
try {
obj.load();
} catch (SQLException e) {
LogUtil.getLog(getClass()).error("getQObject2:" + primaryKey +
" trace:" + StrUtil.trace(e));
}
if (!obj.isLoaded())
obj = null;
return obj;
}
public boolean load() throws SQLException {
JdbcTemplate jt = new JdbcTemplate(new Connection(table.getConnName()));
ResultIterator ri = null;
Object[] params = primaryKey.toObjectArray();
// LogUtil.getLog(getClass()).error("load: params=" + params);
ri = jt.executeQuery(table.getQueryLoad(), params);
if (ri.hasNext()) {
resultRecord = (ResultRecord) ri.next();
loaded = true;
return true;
}
return false;
}
public boolean create(ParamChecker paramChecker) throws ResKeyException,
ErrMsgException {
// 解析queryCreate语句
String[] fields = getFieldsFromQueryCreate();
int len = fields.length;
// 填充ResultRecord,这样填充的ResultRecord,row中字段的排列顺序与load()出来的并不一致,而与queryCreate中的排列顺序一致
// 只是为了便于在创建后,得到创建的记录中含有哪些值
HashMap mapIndex = new HashMap();
Vector row = new Vector();
Object[] params = new Object[len];
for (int i = 0; i < len; i++) {
params[i] = paramChecker.getValue(fields[i]);
mapIndex.put(fields[i].toUpperCase(), new Integer(i + 1));
row.addElement(params[i]);
}
resultRecord = new ResultRecord(row, mapIndex);
return create(new JdbcTemplate(new Connection(table.getConnName())),
params);
}
public boolean create(JdbcTemplate jt, Object[] params) throws
ResKeyException {
boolean re = false;
try {
re = jt.executeUpdate(table.getQueryCreate(), params) == 1;
} catch (SQLException e) {
LogUtil.getLog(getClass()).error("create:" + StrUtil.trace(e));
throw new ResKeyException(SkinUtil.ERR_DB);
}
if (re) {
// 刷新缓存
refreshCreate();
}
return re;
}
/**
*
* @param sql String
* @return int -1 表示sql语句不合法
*/
public long getQObjectCount(String sql) {
// 根据sql语句得出计算总数的sql查询语句
String query = cn.js.fan.db.SQLFilter.getCountSql(sql);
Long count = null;
if (listCachable) {
try {
count = (Long) QCache.getInstance().getFromGroup(query,
cacheGroupCount);
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error(e.getMessage());
}
// If already in cache, return the count.
if (count != null) {
return count.longValue();
}
}
// Otherwise, we have to load the count from the db.
long docCount = 0;
Connection conn = new Connection(table.getConnName());
ResultSet rs = null;
try {
rs = conn.executeQuery(query);
if (rs.next())
docCount = rs.getLong(1);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
// Add the count to cache
if (listCachable) {
try {
QCache.getInstance().putInGroup(query, cacheGroupCount,
new Long(docCount));
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error(e.getMessage());
}
}
return docCount;
}
public void refresh(PrimaryKey pk) {
if (!objCachable)
return;
try {
if (!cacheGroup.equals("")) {
QCache.getInstance().remove(getCacheKey(pk),
cacheGroup);
} else {
QCache.getInstance().remove(getCacheKey(pk));
}
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error("refresh:" + e.getMessage());
}
}
public void refreshList() {
if (!listCachable)
return;
try {
// 当在修改OA中档案模块时,发现如果置用户为invalid,则在置用户职位时,因为user_sel.jsp中使用了getObjects,而当save时,并未刷新列表,就是因为未刷新列表
QCache.getInstance().invalidateGroup(cacheGroupCount);
QCache.getInstance().invalidateGroup(cacheGroup);
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error(e.getMessage());
}
}
public void refreshCreate() {
if (!listCachable)
return;
refreshList();
}
public void refreshSave(PrimaryKey pk) {
if (!objCachable)
return;
refresh(pk);
}
public void refreshDel(PrimaryKey pk) {
try {
if (objCachable)
refresh(pk);
if (listCachable) {
refreshList();
}
} catch (Exception e) {
LogUtil.getLog(this.getClass()).error("refreshDel:" + e.getMessage());
}
}
public boolean del() throws ResKeyException {
return del(new JdbcTemplate(new Connection(table.getConnName())));
}
public boolean del(JdbcTemplate jt) throws ResKeyException {
boolean re = false;
Object[] params = primaryKey.toObjectArray();
try {
re = jt.executeUpdate(table.getQueryDel(), params) == 1;
} catch (SQLException e) {
LogUtil.getLog(getClass()).error("del:" + e.getMessage());
throw new ResKeyException(SkinUtil.ERR_DB);
}
// 刷新缓存
refreshDel(primaryKey);
return re;
}
public boolean save(JdbcTemplate jt, Object[] params) throws SQLException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -