208 lines
5.6 KiB
Java
208 lines
5.6 KiB
Java
package com.amarsoft.aims.util;
|
||
|
||
import java.text.MessageFormat;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
import com.amarsoft.are.ARE;
|
||
import com.amarsoft.are.jbo.BizObject;
|
||
import com.amarsoft.are.jbo.BizObjectManager;
|
||
import com.amarsoft.are.jbo.BizObjectQuery;
|
||
import com.amarsoft.are.jbo.JBOException;
|
||
import com.amarsoft.are.jbo.JBOFactory;
|
||
import com.amarsoft.are.jbo.JBOTransaction;
|
||
import com.amarsoft.are.lang.DataElement;
|
||
|
||
/**
|
||
* JBO工具类
|
||
* @author syang
|
||
* @date 2013/11/18
|
||
*/
|
||
public class JBOHelper {
|
||
private static Pattern SQL_WHERE_PATTERN = Pattern.compile("\\w+=:(\\w+)",Pattern.CASE_INSENSITIVE);
|
||
|
||
/**
|
||
* 查找单个对象
|
||
* @param tx 事务对象
|
||
* @param needUpdate 查出来的对象是否需要更新
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
public static BizObject querySingle(JBOTransaction tx,boolean needUpdate,String jboClass,String whereClause,Object... values){
|
||
BizObject bizObject = null;
|
||
try {
|
||
BizObjectQuery query = genQuery(tx,jboClass,whereClause,values);
|
||
bizObject = query.getSingleResult(needUpdate);
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上查找JBO对象出错,查询条件[{1}]", jboClass,whereClause),e);
|
||
}
|
||
return bizObject;
|
||
}
|
||
|
||
/**
|
||
* 查找单个对象(不需要事务,查出来的对象可以被更新)
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
public static BizObject querySingle(String jboClass,String whereClause,Object... values){
|
||
return querySingle(null,true,jboClass,whereClause,values);
|
||
}
|
||
|
||
/**
|
||
* 创建JBO新对象
|
||
* @param tx
|
||
* @param jboClass
|
||
* @return
|
||
*/
|
||
public static BizObject createBizObject(JBOTransaction tx,String jboClass){
|
||
BizObjectManager bm;
|
||
BizObject bo = null;
|
||
try {
|
||
bm = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(bm);
|
||
bo = bm.newObject();
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上创建BO对象出错", jboClass),e);
|
||
}
|
||
return bo;
|
||
}
|
||
/**
|
||
* 创建JBO新对象
|
||
* @param tx
|
||
* @param jboClass
|
||
* @return
|
||
*/
|
||
public static BizObject createBizObject(String jboClass){
|
||
return createBizObject(null,jboClass);
|
||
}
|
||
|
||
/**
|
||
* 保存JBO对象
|
||
* @param tx
|
||
* @param jboClass
|
||
* @param bo
|
||
* @return
|
||
*/
|
||
public static BizObject saveBizObject(JBOTransaction tx,String jboClass,BizObject bo){
|
||
BizObjectManager bm;
|
||
try {
|
||
bm = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(bm);
|
||
bm.saveObject(bo);
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上创建BO对象出错", jboClass),e);
|
||
}
|
||
return bo;
|
||
}
|
||
|
||
|
||
/**
|
||
* 保存JBO对象
|
||
* @param jboClass
|
||
* @param bo
|
||
* @return
|
||
*/
|
||
public static BizObject saveBizObject(String jboClass,BizObject bo){
|
||
return saveBizObject(null,jboClass,bo);
|
||
}
|
||
|
||
/**
|
||
* 查询一个JBO列表
|
||
* @param tx 事务对象
|
||
* @param needUpdate 查出来的对象是否需要更新
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
@SuppressWarnings("unchecked")
|
||
public static List<BizObject> queryList(JBOTransaction tx,boolean needUpdate,String jboClass,String whereClause,Object... values){
|
||
List<BizObject> jboList = null;
|
||
try {
|
||
BizObjectQuery query = genQuery(tx,jboClass,whereClause,values);
|
||
jboList = query.getResultList(needUpdate);
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上查找JBO对象出错,查询条件[{1}]", jboClass,whereClause),e);
|
||
}
|
||
return jboList;
|
||
}
|
||
|
||
/**
|
||
* 查询一个JBO列表(不需要事务,查出来的对象可以被更新)
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
public static List<BizObject> queryList(String jboClass,String whereClause,Object... values){
|
||
return queryList(null,true,jboClass,whereClause,values);
|
||
}
|
||
|
||
/**
|
||
* 从JBO的SQL where子语句中解析出参数
|
||
* @param paraNames
|
||
* @param s
|
||
*/
|
||
private static void parseParameterNames(List<String> paraNames,String s){
|
||
Matcher matcher = SQL_WHERE_PATTERN.matcher(s);
|
||
if(matcher.find()&&matcher.groupCount()>0){
|
||
String name = matcher.group(1);
|
||
paraNames.add(name);
|
||
parseParameterNames(paraNames,matcher.replaceFirst(""));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 生成查询条件
|
||
* @param tx 事务对象
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
private static BizObjectQuery genQuery(JBOTransaction tx,String jboClass,String whereClause,Object... values) throws JBOException{
|
||
BizObjectManager manager = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(manager);
|
||
BizObjectQuery query = manager.createQuery(whereClause);
|
||
List<String> paraNames = new ArrayList<String>();
|
||
parseParameterNames(paraNames,whereClause);
|
||
|
||
for(int i=0;i<paraNames.size();i++){
|
||
DataElement element = new DataElement(paraNames.get(i));
|
||
element.setValue(values[i]);
|
||
query.setParameter(element);
|
||
}
|
||
return query;
|
||
}
|
||
|
||
/**
|
||
* 获取JBO成员值,如果该项不存在JBO上,则只记录日记,不抛出异常
|
||
* @param bizObject 被查找的JBO类
|
||
* @param name 属性名称
|
||
* @return 属性值对象
|
||
*/
|
||
public static DataElement getAttribute(BizObject bizObject,String name){
|
||
DataElement value = null;
|
||
try {
|
||
if(null != bizObject){
|
||
value = bizObject.getAttribute(name);
|
||
}else{
|
||
throw new NullPointerException("查找JBO属性,JBO对象为空");
|
||
}
|
||
} catch (JBOException e) {
|
||
String msg = MessageFormat.format("在JBO类{0}上,找不到属性{1}", bizObject.getBizObjectClass().getAbsoluteName(),name);
|
||
ARE.getLog().error(msg,e);
|
||
throw new NullPointerException(msg);
|
||
}
|
||
return value;
|
||
}
|
||
|
||
}
|