778 lines
25 KiB
Java
778 lines
25 KiB
Java
package com.amarsoft.app.base.util;
|
||
|
||
import java.text.MessageFormat;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.Iterator;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
import com.amarsoft.app.base.businessobject.BusinessObject;
|
||
import com.amarsoft.app.base.businessobject.BusinessObjectQuery;
|
||
import com.amarsoft.are.ARE;
|
||
import com.amarsoft.are.jbo.BizObject;
|
||
import com.amarsoft.are.jbo.BizObjectClass;
|
||
import com.amarsoft.are.jbo.BizObjectKey;
|
||
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.jbo.ql.ElementIterator;
|
||
import com.amarsoft.are.jbo.ql.JBOAttribute;
|
||
import com.amarsoft.are.lang.DataElement;
|
||
import com.amarsoft.are.lang.StringX;
|
||
import com.amarsoft.awe.dw.ui.util.StringMatch;
|
||
|
||
/**
|
||
* JBO工具类
|
||
* @author syang,ghShi
|
||
* @date 2013/11/18
|
||
*/
|
||
public class JBOHelper {
|
||
|
||
private static Pattern SQL_WHERE_PATTERN = Pattern.compile("\\w+=:(\\w+)",Pattern.CASE_INSENSITIVE);
|
||
/**
|
||
* 插入全条记录到表中
|
||
* @param tx 事务对象
|
||
* @param jboClass JBO类名
|
||
* @param 被拷贝的记录对象
|
||
* @param 是否要改变某些参数
|
||
* @param keys 需要改变的参数列表
|
||
* @param values 需要改变参数值列表包
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static BizObject copyObject(JBOTransaction tx,String jboClass,BizObject bo, boolean change,String[] keys,Object... values) throws JBOException{
|
||
BizObjectManager manager = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(manager);
|
||
BizObject bonew = manager.newObject();
|
||
bonew.setAttributesValue(bo);
|
||
|
||
|
||
if(change)
|
||
for(int i=0;i<keys.length;i++){
|
||
bonew.setAttributeValue(keys[i], values[i]);
|
||
}
|
||
manager.saveObject(bonew);
|
||
return bonew;
|
||
|
||
}
|
||
/**
|
||
* 插入到表中
|
||
* @param tx 事务对象
|
||
* @param jboClass JBO类名
|
||
* @param keys 需要插入的参数列表
|
||
* @param values 参数值列表包
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static void executeInsert(JBOTransaction tx,String jboClass,String[] keys,Object... values) throws JBOException{
|
||
BizObjectManager manager = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(manager);
|
||
BizObject bo = manager.newObject();
|
||
for(int i=0;i<keys.length;i++){
|
||
bo.setAttributeValue(keys[i], values[i]);
|
||
}
|
||
|
||
manager.saveObject(bo);
|
||
|
||
}
|
||
/**
|
||
* delete表
|
||
* @param tx 事务对象
|
||
* @param jboClass JBO类名
|
||
* @param Clause 完整delete语句
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
public static int executeDelete(JBOTransaction tx,String jboClass,String Clause,Object... values){
|
||
return executeUpdate(tx, jboClass, Clause, values);
|
||
}
|
||
/**
|
||
* 更新表
|
||
* @param tx 事务对象
|
||
* @param jboClass JBO类名
|
||
* @param Clause 完整update语句
|
||
* @param values 参数值列表包含set后的参数
|
||
* @return
|
||
*/
|
||
public static int executeUpdate(JBOTransaction tx,String jboClass,String Clause,Object... values){
|
||
int result = 0;
|
||
try {
|
||
BizObjectQuery query = genQuery(tx,jboClass,Clause,values);
|
||
result = query.executeUpdate();
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上更新/删除JBO对象出错,查询条件[{1}]", jboClass,Clause),e);
|
||
}
|
||
return result;
|
||
}
|
||
/**
|
||
* 获取查询数量
|
||
* @param tx 事务对象
|
||
* @param needUpdate 查出来的对象是否需要更新
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
public static int queryCount(JBOTransaction tx,boolean needUpdate,String jboClass,String whereClause,Object... values){
|
||
int count = 0;
|
||
try {
|
||
BizObjectQuery query = genQuery(tx,jboClass,whereClause,values);
|
||
count = query.getTotalCount();
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上查找JBO对象出错,查询条件[{1}]", jboClass,whereClause),e);
|
||
}
|
||
return count;
|
||
}
|
||
|
||
/**
|
||
* 获取查询数量
|
||
* @param jboClass JBO类名
|
||
* @param whereClause where条件
|
||
* @param values 参数值列表
|
||
* @return
|
||
*/
|
||
public static int queryCount(String jboClass,String whereClause,Object... values){
|
||
return queryCount(null,true,jboClass,whereClause,values);
|
||
}
|
||
|
||
/**
|
||
* 查找单个对象
|
||
* @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 jboClass
|
||
* @return
|
||
*/
|
||
public static BizObject getNewObject(String jboClass){
|
||
BizObjectManager bm;
|
||
BizObject bo = null;
|
||
try {
|
||
bm = JBOFactory.getBizObjectManager(jboClass);
|
||
bo = bm.newObject();
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类[{0}]上查找JBO对象出错", jboClass),e);
|
||
}
|
||
return 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);
|
||
}
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error(MessageFormat.format("在JBO类{0}上,找不到属性{1}", bizObject.getBizObjectClass().getAbsoluteName(),name),e);
|
||
}
|
||
return value;
|
||
}
|
||
/**根据jboclass和条件返回一个BizObject对象
|
||
* where中的参数和para中的参数顺序要一一对应
|
||
* 传入的from和where子句和显示模板配置一样
|
||
* @param mainJboClass主jbo
|
||
* @param fromCase
|
||
* @param whereCondition
|
||
* @param paramstr
|
||
* @param update是否更新
|
||
* @param fields指定查询字段(id,name)
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static BizObject getBizObjectByCondition(String mainJboClass,String fromCase,String whereCondition,String paramstr,boolean update,String fields) throws Exception{
|
||
StringBuffer sb = new StringBuffer();
|
||
sb.append(" select ");
|
||
if(StringX.isSpace(mainJboClass)){
|
||
throw new Exception("主jbo对象不能为空!");
|
||
}
|
||
if(StringX.isSpace(fromCase)){
|
||
throw new Exception("from子句不能为空!");
|
||
}
|
||
if(StringX.isSpace(mainJboClass)){
|
||
throw new Exception("查询条件对象不能为空!");
|
||
}
|
||
if(StringX.isSpace(fields)){
|
||
sb.append(" * ");
|
||
}else{
|
||
sb.append(fields);
|
||
}
|
||
sb.append(" from ");
|
||
sb.append(fromCase);
|
||
sb.append(" where ").append(whereCondition);
|
||
BizObjectManager bom = JBOFactory.getBizObjectManager(mainJboClass);
|
||
BizObjectQuery query = bom.createQuery(sb.toString());
|
||
//解析参数
|
||
ArrayList<String> al = new ArrayList<String>();
|
||
al = StringMatch.getContents(al, whereCondition, "\\:[a-zA-Z0-9_]+");
|
||
String[] arrParam = paramstr.split("\\,",al.size());
|
||
for(int i=0;i<al.size();i++){
|
||
query.setParameter(al.get(i).toString().substring(1), arrParam[i]);
|
||
}
|
||
return query.getSingleResult(update);
|
||
}
|
||
/**根据jboclass和条件返回一个BizObject对象
|
||
* where中的参数和para中的参数顺序要一一对应
|
||
* 传入的from和where子句和显示模板配置一样
|
||
* 默认:查询全部字段属性且结果集不可被更新
|
||
* @param mainJboClass
|
||
* @param fromCase
|
||
* @param whereCondition
|
||
* @param paramstr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static BizObject getBizObjectByCondition(String mainJboClass,String fromCase,String whereCondition,String paramstr) throws Exception{
|
||
return getBizObjectByCondition(mainJboClass, fromCase, whereCondition, paramstr,false, "*");
|
||
}
|
||
|
||
/**根据jboclass和条件返回一个List<BizObject>对象
|
||
* where中的参数和para中的参数顺序要一一对应
|
||
* 传入的from和where子句和显示模板配置一样
|
||
* @param mainJboClass主jbo
|
||
* @param fromCase
|
||
* @param whereCondition
|
||
* @param paramstr
|
||
* @param update是否更新
|
||
* @param fields指定查询字段(id,name)
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static List<BizObject> getListByCondition(String mainJboClass,String fromCase,String whereCondition,String paramstr,boolean update,String fields) throws Exception{
|
||
StringBuffer sb = new StringBuffer();
|
||
sb.append(" select ");
|
||
if(StringX.isSpace(mainJboClass)){
|
||
throw new Exception("主jbo对象不能为空!");
|
||
}
|
||
if(StringX.isSpace(fromCase)){
|
||
throw new Exception("from子句不能为空!");
|
||
}
|
||
if(StringX.isSpace(mainJboClass)){
|
||
throw new Exception("查询条件对象不能为空!");
|
||
}
|
||
if(StringX.isSpace(fields)){
|
||
sb.append(" * ");
|
||
}else{
|
||
sb.append(fields);
|
||
}
|
||
sb.append(" from ");
|
||
sb.append(fromCase);
|
||
sb.append(" where ").append(whereCondition);
|
||
BizObjectManager bom = JBOFactory.getBizObjectManager(mainJboClass);
|
||
BizObjectQuery query = bom.createQuery(sb.toString());
|
||
//解析参数
|
||
ArrayList<String> al = new ArrayList<String>();
|
||
al = StringMatch.getContents(al, whereCondition, "\\:[a-zA-Z0-9_]+");
|
||
String[] arrParam = paramstr.split("\\,",al.size());
|
||
for(int i=0;i<al.size();i++){
|
||
query.setParameter(al.get(i).toString().substring(1), arrParam[i]);
|
||
}
|
||
return query.getResultList(update);
|
||
}
|
||
/**根据jboclass和条件返回一个List<BizObject>对象
|
||
* where中的参数和para中的参数顺序要一一对应
|
||
* 传入的from和where子句和显示模板配置一样
|
||
* 默认:查询全部字段属性且结果集不可被更新
|
||
* @param mainJboClass
|
||
* @param fromCase
|
||
* @param whereCondition
|
||
* @param paramstr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static List<BizObject> getListByCondition(String mainJboClass,String fromCase,String whereCondition,String paramstr) throws Exception{
|
||
return getListByCondition(mainJboClass, fromCase, whereCondition, paramstr,false, "*");
|
||
}
|
||
|
||
/**执行hql方法获取一个jbo对象
|
||
* @param jboClass
|
||
* @param hql
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static BizObject getBizObjectByQL(String jboClass,String hql) throws JBOException{
|
||
BizObjectManager bom = JBOFactory.getBizObjectManager(jboClass);
|
||
return bom.createQuery(hql).getSingleResult(false);
|
||
}
|
||
/**执行hql方法获取一个jbo列表
|
||
* @param jboClass
|
||
* @param hql
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static List<BizObject> getListByQL(String jboClass,String hql) throws JBOException{
|
||
BizObjectManager bom = JBOFactory.getBizObjectManager(jboClass);
|
||
return bom.createQuery(hql).getResultList(false);
|
||
}
|
||
|
||
/**
|
||
* 根据JBOClass和条件进行更新操作
|
||
* setClause和whereClause格式与正常JBO查询语句格式相同
|
||
* whereClause中的参数和parastr中的参数顺序要一一对应
|
||
* parastr多个参数之间以逗号分割
|
||
* @param tx
|
||
* @param JboClass
|
||
* @param setClause
|
||
* @param whereClause
|
||
* @param parastr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static Object updateByCondition(JBOTransaction tx,String JboClass,String setClause,String whereClause,String paramstr) throws Exception{
|
||
StringBuffer sb = new StringBuffer();
|
||
sb.append(" update O ");
|
||
if(StringX.isSpace(JboClass)){
|
||
throw new Exception("主jbo对象不能为空!");
|
||
}
|
||
if(StringX.isSpace(setClause)){
|
||
throw new Exception("更新字段不能为空!");
|
||
}
|
||
if(StringX.isSpace(whereClause)){
|
||
throw new Exception("查询条件不能为空!");
|
||
}
|
||
sb.append(" set ").append(setClause);
|
||
sb.append(" where ").append(whereClause);
|
||
BizObjectManager bom = JBOFactory.getBizObjectManager(JboClass);
|
||
BizObjectQuery query = bom.createQuery(sb.toString());
|
||
//解析参数
|
||
ArrayList<String> al = new ArrayList<String>();
|
||
al = StringMatch.getContents(al, setClause+","+whereClause, "\\:[a-zA-Z0-9_]+");
|
||
String[] arrParam = paramstr.split("\\,");
|
||
for(int i=0;i<al.size();i++){
|
||
query.setParameter(al.get(i).toString().substring(1), arrParam[i]);
|
||
}
|
||
query.executeUpdate();
|
||
return "SUCCESS";
|
||
}
|
||
|
||
/**
|
||
* 根据JBOClass和条件进行删除操作
|
||
* whereClause格式与正常JBO查询语句格式相同
|
||
* whereClause中的参数和parastr中的参数顺序要一一对应
|
||
* parastr多个参数之间以逗号分割
|
||
* @param tx
|
||
* @param JboClass
|
||
* @param whereClause
|
||
* @param parastr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static Object deleteByCondition(JBOTransaction tx,String JboClass,String whereClause,String paramstr) throws Exception{
|
||
StringBuffer sb = new StringBuffer();
|
||
sb.append(" delete from O where");
|
||
if(StringX.isSpace(JboClass)){
|
||
throw new Exception("主jbo对象不能为空!");
|
||
}
|
||
if(StringX.isSpace(whereClause)){
|
||
throw new Exception("查询条件对象不能为空!");
|
||
}
|
||
sb.append(whereClause);
|
||
BizObjectManager bom = JBOFactory.getBizObjectManager(JboClass);
|
||
BizObjectQuery query = bom.createQuery(sb.toString());
|
||
//解析参数
|
||
ArrayList<String> al = new ArrayList<String>();
|
||
al = StringMatch.getContents(al, whereClause, "\\:[a-zA-Z0-9_]+");
|
||
String[] arrParam = paramstr.split("\\,");
|
||
for(int i=0;i<al.size();i++){
|
||
query.setParameter(al.get(i).toString().substring(1), arrParam[i]);
|
||
}
|
||
query.executeUpdate();
|
||
return "SUCCESS";
|
||
}
|
||
|
||
/**
|
||
* 根据JBOClass和参数进行插入操作
|
||
* parastr多个参数之间以逗号分割
|
||
* value多个值之间以逗号分隔
|
||
* @param tx
|
||
* @param JboClass
|
||
* @param whereClause
|
||
* @param parastr
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static String insertByCondition(JBOTransaction tx,String JboClass,String parastr,String value) throws Exception{
|
||
|
||
String []recParam = parastr.split(",");
|
||
String []recValue = value.split(",");
|
||
try{
|
||
BizObjectManager bom =JBOFactory.getBizObjectManager(JboClass);
|
||
tx.join(bom);
|
||
BizObject bo = bom.newObject();
|
||
for(int i=0;i<recParam.length;i++){
|
||
bo.setAttributeValue(recParam[i], recValue[i]);
|
||
}
|
||
bom.saveObject(bo);
|
||
}catch(JBOException e){
|
||
ARE.getLog().error(MessageFormat.format("插入操作失败,插入表名称:", JboClass),e);
|
||
}
|
||
return "SUCCESS";
|
||
}
|
||
/**
|
||
* 生成jbo查询,以Map方式指定参数值
|
||
* @param tx
|
||
* @param jboClass
|
||
* @param jbosql
|
||
* @param parameterString
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static BizObjectQuery getQuery(String jboClass,String jbosql,List<DataElement> parameterList,JBOTransaction tx) throws JBOException{
|
||
Map<String,Object> parameterMap = new HashMap<String,Object>();
|
||
for(DataElement parameter:parameterList){
|
||
parameterMap.put(parameter.getName(), parameter.getValue());
|
||
}
|
||
return JBOHelper.getQuery(jboClass, jbosql, parameterMap,tx);
|
||
}
|
||
|
||
|
||
/**
|
||
* 生成jbo查询,以Map方式指定参数值
|
||
* @param tx
|
||
* @param jboClass
|
||
* @param jbosql
|
||
* @param parameterString
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static BizObjectQuery getQuery(String jboClass,String jbosql,Map<String,Object> parameters,JBOTransaction tx) throws JBOException{
|
||
BizObjectManager bizManager = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(bizManager);
|
||
BizObjectQuery q= JBOHelper.getQuery(bizManager, jboClass, jbosql, parameters);
|
||
return q;
|
||
}
|
||
|
||
/**
|
||
* 生成jbo查询,以Map方式指定参数值
|
||
* @param tx
|
||
* @param jboClass
|
||
* @param jbosql
|
||
* @param parameterString
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
private static BizObjectQuery getQuery(BizObjectManager bizManager,String jboClass,String jbosql,Map<String,Object> parameters) throws JBOException{
|
||
BizObjectQuery query = bizManager.createQuery(jbosql);
|
||
if(parameters!=null){
|
||
for(Iterator<String> it=parameters.keySet().iterator();it.hasNext();){
|
||
String key = it.next();
|
||
query.setParameter(key, (String)parameters.get(key));
|
||
}
|
||
}
|
||
return query;
|
||
}
|
||
|
||
public static BizObjectQuery getQuery(String jboClass,String jbosql,String parameterString,JBOTransaction tx) throws JBOException{
|
||
return getQuery(jboClass,jbosql,parameterString,";",",",tx);
|
||
}
|
||
|
||
|
||
public static BusinessObjectQuery getQuery(String jboClass,Map<String,String> queryAttributes,String whereString,String parameterString,JBOTransaction tx) throws JBOException{
|
||
return JBOHelper.getQuery(jboClass, queryAttributes, whereString, parameterString, ";", ",", tx);
|
||
}
|
||
/**
|
||
* 生成jbo查询,parameterString样式为para1=value1;para2 in a,b,c;para3 like value3%
|
||
* @param tx
|
||
* @param jboClass
|
||
* @param jbosql
|
||
* @param parameterString
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static BusinessObjectQuery getQuery(String jboClass,Map<String,String> queryAttributes,String whereString,String parameterString,String parametersplit,String insplitChar,JBOTransaction tx) throws JBOException{
|
||
String queryString = "";
|
||
Map<String,String[]> queryAttributeMap=new HashMap<String,String[]>();
|
||
queryString+="select ";
|
||
for(String attributeName:queryAttributes.keySet()){
|
||
String actualAttributeName = queryAttributes.get(attributeName);
|
||
if(StringX.isEmpty(actualAttributeName)){
|
||
actualAttributeName=attributeName;
|
||
}
|
||
String[] s=parseSelectAttribute(actualAttributeName);
|
||
|
||
queryAttributeMap.put(attributeName, s);
|
||
if(s[0].equalsIgnoreCase("O")){
|
||
queryString+=s[0]+"."+actualAttributeName+",";
|
||
}
|
||
else{
|
||
queryString+=s[0]+"."+actualAttributeName+" as v."+attributeName+",";
|
||
}
|
||
|
||
}
|
||
queryString=queryString.substring(0,queryString.length()-1);
|
||
queryString+= " "+whereString;
|
||
|
||
|
||
if(insplitChar==null||insplitChar.length()==0) insplitChar=",";
|
||
if(parametersplit==null||parametersplit.length()==0) parametersplit=";";
|
||
BizObjectQuery query = JBOHelper.getQuery(jboClass, queryString, parameterString, parametersplit, insplitChar, tx);
|
||
BusinessObjectQuery businessObjectQuery = new BusinessObjectQuery(query);
|
||
businessObjectQuery.init(queryAttributeMap, queryString);
|
||
return businessObjectQuery;
|
||
}
|
||
|
||
public static String[] parseSelectAttribute(String expression) throws JBOException{
|
||
String[] s=new String[2];
|
||
ElementIterator ei=new ElementIterator(expression);
|
||
if(ei.hasNext()){
|
||
JBOAttribute j= (JBOAttribute)ei.next();
|
||
if(ei.hasNext()){//超过两个element
|
||
s[0]="";
|
||
s[1]=expression;
|
||
}
|
||
else{
|
||
|
||
s[0]=j.getClassAlias();
|
||
s[1]=j.getName();
|
||
}
|
||
}
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* 生成jbo查询,parameterString样式为para1=value1;para2 in a,b,c;para3 like value3%
|
||
* @param tx
|
||
* @param jboClass
|
||
* @param jbosql
|
||
* @param parameterString
|
||
* @return
|
||
* @throws JBOException
|
||
*/
|
||
public static BizObjectQuery getQuery(String jboClass,String jbosql,String parameterString,String parametersplit,String insplitChar,JBOTransaction tx) throws JBOException{
|
||
if(insplitChar==null||insplitChar.length()==0) insplitChar=",";
|
||
if(parametersplit==null||parametersplit.length()==0) parametersplit=";";
|
||
|
||
BusinessObject[] parameterArray = parseJBOParamter(parameterString,parametersplit,insplitChar);
|
||
return JBOHelper.getQuery(jboClass, jbosql, parameterArray, tx);
|
||
}
|
||
|
||
public static BizObjectQuery getQuery(BizObjectKey[] bizObjectKeyArray,JBOTransaction tx) throws JBOException{
|
||
BizObjectClass clazz = bizObjectKeyArray[0].getBizObjectClass();
|
||
String[] keys = clazz.getKeyAttributes();
|
||
if(keys == null || keys.length <= 0) throw new JBOException("该对象【"+clazz.getRoot().getAbsoluteName()+"】未定义主键!");
|
||
String jbosql = "(";
|
||
int parameterCount =bizObjectKeyArray.length* keys.length;
|
||
BusinessObject[] parameterArray = new BusinessObject[parameterCount];
|
||
|
||
int m=0;
|
||
for(int j=0;j<bizObjectKeyArray.length;j++){
|
||
String keySql="(";
|
||
for(int i = 0; i < keys.length; i++){
|
||
if(i>0) keySql += " and ";
|
||
keySql += " "+keys[i]+"=:"+keys[i]+m;
|
||
BusinessObject parameter = BusinessObject.createBusinessObject();
|
||
parameter.setAttributeValue("Name", keys[i]+m);
|
||
parameter.setAttributeValue("Value", bizObjectKeyArray[j].getAttribute(keys[i]));
|
||
parameter.setAttributeValue("Operate", "=");
|
||
parameterArray[m]=parameter;
|
||
m++;
|
||
}
|
||
keySql+=")";
|
||
if(j>0)jbosql+=" or ";
|
||
jbosql+=keySql;
|
||
}
|
||
jbosql+=")";
|
||
return JBOHelper.getQuery(clazz.getRoot().getAbsoluteName(), jbosql, parameterArray, tx);
|
||
}
|
||
|
||
public static BizObjectQuery getQuery(String jboClass,String jbosql,BusinessObject[] parameterArray,JBOTransaction tx) throws JBOException{
|
||
for(BusinessObject parameter:parameterArray){
|
||
String parameterName = parameter.getString("Name");
|
||
if(StringX.isEmpty(parameterName)) continue;
|
||
String operator = parameter.getString("Operate");
|
||
if(StringX.isEmpty(operator)) operator="=";
|
||
operator=operator.trim();
|
||
|
||
if(operator.equalsIgnoreCase("in")) {
|
||
Object value = parameter.getObject("Value");
|
||
String insql="";
|
||
Object[] valueArray = (Object[])value;
|
||
for(int i=0;i<valueArray.length;i++){
|
||
insql +=","+":SYS_GEN_"+parameterName+"_"+i;
|
||
}
|
||
if(insql.length()>0) insql=insql.substring(1);
|
||
jbosql = StringHelper.replaceAllIgnoreCase(jbosql, ":"+parameterName, insql);
|
||
}
|
||
}
|
||
|
||
BizObjectManager manager = JBOFactory.getBizObjectManager(jboClass);
|
||
if(tx!=null)tx.join(manager);
|
||
BizObjectQuery query = manager.createQuery(jbosql);
|
||
|
||
for(BusinessObject parameter:parameterArray){
|
||
String parameterName = parameter.getString("Name");
|
||
|
||
if(StringX.isEmpty(parameterName)) continue;
|
||
String operator = parameter.getString("Operate");
|
||
if(StringX.isEmpty(operator)) operator="=";
|
||
operator=operator.trim();
|
||
|
||
if(operator.equalsIgnoreCase("in")) {
|
||
Object[] values = (Object[])parameter.getObject("Value");
|
||
for(int i=0;i<values.length;i++){
|
||
String parameterName_IN ="SYS_GEN_"+parameterName+"_"+i;
|
||
query.setParameter(parameterName_IN, (String)values[i]);
|
||
}
|
||
}
|
||
else{
|
||
DataElement dataElement = new DataElement(parameterName);
|
||
dataElement.setValue(parameter.getObject("Value"));
|
||
query.setParameter(dataElement);
|
||
}
|
||
}
|
||
return query;
|
||
}
|
||
|
||
public static BusinessObject[] parseJBOParamter(String parameterString,String splitChar,String insplitChar) throws JBOException{
|
||
if(parameterString==null||parameterString.length()==0) return new BusinessObject[0];
|
||
if(StringX.isEmpty(splitChar)) splitChar=";";
|
||
if(StringX.isEmpty(insplitChar)) insplitChar=",";
|
||
|
||
String[] s1= parameterString.split(splitChar);
|
||
BusinessObject[] paramters = new BusinessObject[s1.length];
|
||
|
||
int i=0;
|
||
for(String s2:s1){
|
||
s2 = s2.trim();
|
||
String parameterName="";
|
||
String parameterOperater="";
|
||
Object parameterValue=null;
|
||
|
||
if(s2.toUpperCase().endsWith(" IN")||s2.toUpperCase().indexOf(" IN ")>0){
|
||
if(s2.toUpperCase().endsWith(" IN"))
|
||
parameterName = s2.substring(0,s2.toUpperCase().indexOf(" IN")).trim();
|
||
else
|
||
parameterName = s2.substring(0,s2.toUpperCase().indexOf(" IN ")).trim();
|
||
parameterOperater = "in";
|
||
|
||
String inValueString = "";
|
||
if(s2.toUpperCase().endsWith(" IN"))
|
||
inValueString = s2.substring(s2.toUpperCase().indexOf(" IN")+3).trim();
|
||
else
|
||
inValueString = s2.substring(s2.toUpperCase().indexOf(" IN ")+4).trim();
|
||
parameterValue = inValueString.split(insplitChar);
|
||
}
|
||
else{
|
||
parameterName= s2.substring(0,s2.indexOf("=")).trim();
|
||
parameterOperater = "=";
|
||
parameterValue = s2.substring(s2.indexOf("=")+1).trim();
|
||
}
|
||
BusinessObject parameter = BusinessObject.createBusinessObject();
|
||
parameter.setAttributeValue("Name", parameterName);
|
||
parameter.setAttributeValue("Operate", parameterOperater);
|
||
parameter.setAttributeValue("Value", parameterValue);
|
||
paramters[i]=parameter;
|
||
i++;
|
||
}
|
||
return paramters;
|
||
}
|
||
|
||
}
|