package com.vci.server.bof.service;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutputStream;
|
import java.math.BigDecimal;
|
import java.sql.Connection;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.sql.Statement;
|
import java.sql.Timestamp;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.xml.rpc.holders.LongHolder;
|
|
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.hibernate.HibernateException;
|
import org.hibernate.SQLQuery;
|
import org.hibernate.Session;
|
import org.omg.CORBA.StringHolder;
|
|
import com.vci.corba.omd.atm.AttribItem;
|
import com.vci.corba.omd.btm.BtmItem;
|
import com.vci.corba.omd.data.AttributeValue;
|
import com.vci.corba.omd.data.BusinessObject;
|
import com.vci.corba.omd.data.LinkObject;
|
import com.vci.corba.omd.data.RevisionDataInfo;
|
import com.vci.corba.omd.data.VersionDataInfo;
|
import com.vci.corba.omd.lcm.TransitionVO;
|
import com.vci.corba.omd.ltm.LinkType;
|
import com.vci.corba.omd.vrm.VersionRule;
|
import com.vci.corba.common.VCIError;
|
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
|
import com.vci.server.base.utility.OmdHelper;
|
import com.vci.server.bof.objects.BtmItemWrap;
|
import com.vci.server.bof.utils.RevisionComputer;
|
import com.vci.server.bof.utils.ServerObjectUtil;
|
import com.vci.server.cache.OMCacheProvider;
|
import com.vci.common.log.ServerWithLog4j;
|
import com.vci.corba.framework.data.UserInfo;
|
|
public class BOFactoryServices extends BOFactoryBaseService {
|
|
private static BOFactoryServices instance = null;
|
private int BATCHSIZE = 2000;
|
|
|
private BOFactoryServices() {
|
|
}
|
|
public static synchronized BOFactoryServices getInstance() {
|
if (instance == null) {
|
instance = new BOFactoryServices();
|
}
|
|
return instance;
|
}
|
|
/**
|
* 创建新对象
|
*
|
* @param bo
|
* @param isRevision,是否生版本
|
* @param isUpdateLastRevStatus,是否在生版时更新历史版本的islastrev属性为0,即变更历史版本为非最新版本
|
* @param isNewVersion,是否升版次
|
* @return
|
* @throws Exception
|
* @throws Throwable
|
*/
|
public BusinessObject createBusinessObject(BusinessObject bo,
|
boolean isRevision, boolean isNewVersion) throws Exception, Throwable {
|
Session session = HibernateSessionFactory.getSession();
|
|
BtmItemWrap btw = new BtmItemWrap();
|
//btw.btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(bo.btmName);
|
btw.btmItem = OMCacheProvider.getBizType(bo.btName);
|
btw.tableName = OmdHelper.getBTTableName(bo.btName);
|
|
String sql = null;
|
|
// 创建新版本时,更改其他版本信息
|
if (btw.btmItem.revLevel > 0 && isRevision) {
|
if (btw.btmItem.revInput) {
|
//sql = getBOCurrentInputRevisionSql(tableName, bo.nameoid, bo.versionValue);
|
sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=?", btw.tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
query.setString(1, bo.revisionValue);
|
|
List<?> queryList = query.list();
|
Object array = (Object) queryList.get(0);
|
int revsionCount = ((BigDecimal) array).intValue();
|
if (revsionCount > 0) {
|
throw new VCIError("P0010SOF-00012", new String[0]);
|
}
|
} else {
|
//sql = getBOCurrentAutoRevisionSql(tableName, bo.nameoid, bo.versionValue, bo.versionSeq);
|
sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=? and REVISIONSEQ=?", btw.tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
query.setString(1, bo.versionValue);
|
query.setInteger(2, (int)bo.versionSeq);
|
|
List<?> queryList = query.list();
|
Object array = (Object) queryList.get(0);
|
int revsionCount = ((BigDecimal) array).intValue();
|
if (revsionCount > 0) {
|
RevisionDataInfo info = getNextRevisionValueObject(
|
btw.tableName, bo.nameoid, bo.revisionRule,
|
btw.btmItem.revInput, bo.revisionValue);
|
bo.revisionValue = info.revisionVal;
|
bo.revisionSeq = info.revisionSeq;
|
}
|
}
|
//sql = getUpdateRevisionSql(bo.nameoid, tableName);
|
sql = String.format("update %s set ISLASTR='0' where NAMEOID=?", btw.tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
|
//SQLQuery query = session.createSQLQuery(sql);
|
query.executeUpdate();
|
}
|
// 创建新版次是,更改其他版次信息
|
if (btw.btmItem.revLevel > 1 && isNewVersion) {
|
//sql = getTypeCurrentVersionSql(tableName, bo.revisionid, bo.nameoid, bo.versionValue, bo.versionSeq);
|
|
sql = String.format("select count(*) as count from %s where REVISIONOID=? and NAMEOID=? and VERSIONVALUE=? and VERSIONSEQ=?", btw.tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.revisionid);
|
query.setString(1, bo.nameoid);
|
query.setString(2, bo.revisionValue);
|
query.setInteger(3, (int)bo.revisionSeq);
|
|
List<?> queryList = query.list();
|
Object array = (Object) queryList.get(0);
|
int versionCount = ((BigDecimal) array).intValue();
|
if (versionCount > 0) {
|
VersionDataInfo vdInfo = getNextVersionValue(btw.tableName,
|
bo.revisionid, bo.nameoid,
|
Integer.valueOf(bo.versionRule).intValue());
|
bo.versionValue = vdInfo.versionVal;
|
bo.versionSeq = vdInfo.versionSeq;
|
}
|
|
//sql = getUpdateVersionSql(bo.nameoid, bo.revisionid, tableName);
|
sql = String.format("update %s set ISLASTV='0' where NAMEOID=? and REVISIONOID=?", btw.tableName);
|
query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
query.setString(1, bo.revisionid);
|
|
//query = session.createSQLQuery(sql);
|
query.executeUpdate();
|
}
|
|
// 保持新创建对象
|
btw.mapAttr = getBOAttributeItemMap(bo.btName);
|
|
//sql = getCreateBOSql(bo, tableName, attrItemMap);
|
sql = getCreateBOSql(btw);
|
//sql = getCreateBOSql(bo, tableName, Arrays.asList(boItem.apNameArray));
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareCreateBOPsmt(query, bo, btw);
|
query.executeUpdate();
|
|
return bo;
|
}
|
|
/**
|
* 创建新对象
|
*
|
* @param bo
|
* @param isRevision,是否生版本
|
* @param isUpdateLastRevStatus,是否在生版时更新历史版本的islastrev属性为0,即变更历史版本为非最新版本
|
* @param isNewVersion,是否升版次
|
* @return
|
* @throws Exception
|
* @throws Throwable
|
*/
|
public BusinessObject createBusinessObject(BusinessObject bo, boolean isRevision,
|
boolean isNewVersion, Map<String, BtmItemWrap> mapBtw) throws Exception, Throwable {
|
Session session = HibernateSessionFactory.getSession();
|
|
BtmItemWrap btw = null;
|
BtmItem boItem = null;
|
|
if (mapBtw.containsKey(bo.btName)){
|
btw = mapBtw.get(bo.btName);
|
boItem = btw.btmItem;
|
}
|
else {
|
btw = new BtmItemWrap();
|
//boItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(bo.btmName);
|
boItem = OMCacheProvider.getBizType(bo.btName);
|
btw.btmItem = boItem;
|
mapBtw.put(bo.btName, btw);
|
}
|
|
if (btw.tableName == null)
|
btw.tableName = OmdHelper.getBTTableName(bo.btName);
|
|
String sql = null;
|
|
// 创建新版本时,更改其他版本信息
|
if (boItem.revLevel > 0 && isRevision) {
|
if (boItem.revInput) {
|
checkBOCurrentInputRevisionSql(session, bo, btw.tableName);
|
//sql = getBOCurrentInputRevisionSql(tableName, bo.nameoid, bo.versionValue);
|
// sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=?", tableName);
|
//
|
// SQLQuery query = session.createSQLQuery(sql);
|
// query.setString(0, bo.nameoid);
|
// query.setString(1, bo.revisionValue);
|
//
|
// List<?> queryList = query.list();
|
// Object array = (Object) queryList.get(0);
|
// int revsionCount = ((BigDecimal) array).intValue();
|
// if (revsionCount > 0) {
|
// throw new VCIError("P0010SOF-00012", new String[0]);
|
// }
|
} else {
|
getBOCurrentAutoRevision(session, bo, btw.tableName);
|
//sql = getBOCurrentAutoRevisionSql(tableName, bo.nameoid, bo.versionValue, bo.versionSeq);
|
//SQLQuery query = session.createSQLQuery(sql);
|
// sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=? and REVISIONSEQ=?", tableName);
|
// SQLQuery query = session.createSQLQuery(sql);
|
// query.setString(0, bo.nameoid);
|
// query.setString(1, bo.revisionValue);
|
// query.setInteger(2, bo.revisionSeq);
|
//
|
// List<?> queryList = query.list();
|
// Object array = (Object) queryList.get(0);
|
// int revsionCount = ((BigDecimal) array).intValue();
|
// if (revsionCount > 0) {
|
// RevisionDataInfo info = getNextRevisionValueObject(
|
// tableName, bo.nameoid, bo.revisionRule,
|
// boItem.revInput, bo.revisionValue);
|
// bo.revisionValue = info.revisionVal;
|
// bo.revisionSeq = info.revisionSeq;
|
// }
|
}
|
//sql = getUpdateRevisionSql(bo.nameoid, tableName);
|
//SQLQuery query = session.createSQLQuery(sql);
|
sql = String.format("update %s set ISLASTR='0' where NAMEOID=?", btw.tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
|
query.executeUpdate();
|
}
|
// 创建新版次是,更改其他版次信息
|
if (boItem.revLevel > 1 && isNewVersion) {
|
getTypeCurrentVersion(session, bo, btw.tableName);
|
//sql = getTypeCurrentVersionSql(tableName, bo.revisionid, bo.nameoid, bo.versionValue, bo.versionSeq);
|
//SQLQuery query = session.createSQLQuery(sql);
|
|
// sql = String.format("select count(*) as count from %s where REVISIONOID=? and NAMEOID=? and VERSIONVALUE=? and VERSIONSEQ=?", tableName);
|
// SQLQuery query = session.createSQLQuery(sql);
|
// query.setString(0, bo.revisionid);
|
// query.setString(1, bo.nameoid);
|
// query.setString(2, bo.revisionValue);
|
// query.setInteger(3, bo.revisionSeq);
|
//
|
// List<?> queryList = query.list();
|
// Object array = (Object) queryList.get(0);
|
// int versionCount = ((BigDecimal) array).intValue();
|
// if (versionCount > 0) {
|
// VersionDataInfo vdInfo = getNextVersionValue(tableName,
|
// bo.revisionid, bo.nameoid,
|
// Integer.valueOf(bo.versionRule).intValue());
|
// bo.versionValue = vdInfo.versionVal;
|
// bo.versionSeq = vdInfo.versionSeq;
|
// }
|
|
sql = String.format("update %s set ISLASTV='0' where NAMEOID=? and REVISIONOID=?", btw.tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
query.setString(1, bo.revisionid);
|
|
//sql = getUpdateVersionSql(bo.nameoid, bo.revisionid, tableName);
|
//query = session.createSQLQuery(sql);
|
query.executeUpdate();
|
}
|
|
// 保持新创建对象
|
|
Map<String, AttribItem> mapAttr = null;
|
if (btw.mapAttr == null) {
|
mapAttr = getBOAttributeItemMap(bo.btName);
|
btw.mapAttr = mapAttr;
|
} else {
|
mapAttr = btw.mapAttr;
|
}
|
|
//sql = getCreateBOSql(bo, tableName, mapAttr);
|
sql = getCreateBOSql(btw);
|
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareCreateBOPsmt(query, bo, btw);
|
query.executeUpdate();
|
|
return bo;
|
}
|
|
/**
|
* 批量存储BO对象
|
*
|
* @param bos
|
* @param isRevision
|
* @param isNewVersion
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
*/
|
public BusinessObject[] batchCreateBusinessObject(BusinessObject[] bos,
|
boolean isRevision, boolean isNewVersion) throws Exception, Throwable {
|
Session session = HibernateSessionFactory.getSession();
|
|
Connection connection = HibernateSessionFactory.getSessionConnection();
|
|
ServerWithLog4j.logger.debug("===batchCreateBusinessObject Begin");
|
|
Map<String, BtmItemWrap> mapBtw = new HashMap<String, BtmItemWrap>();
|
//Map<String, String> mapSQL = new HashMap<String, String>();
|
|
//Map<String, String> mapTableName = new HashMap<String, String>();
|
List<BusinessObject> lstBO = null;
|
Map<String, List<BusinessObject>> mapBOs = new HashMap<String, List<BusinessObject>>();
|
Map<String, List<BusinessObject>> mapUpRevs = new HashMap<String, List<BusinessObject>>();
|
Map<String, List<BusinessObject>> mapUpVers = new HashMap<String, List<BusinessObject>>();
|
|
String sql = null;
|
|
for (BusinessObject bo : bos) {
|
|
BtmItemWrap btw = null;
|
BtmItem boItem = null;
|
|
if (mapBtw.containsKey(bo.btName)) {
|
btw = mapBtw.get(bo.btName);
|
boItem = btw.btmItem;
|
} else {
|
btw = new BtmItemWrap();
|
//boItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(bo.btmName);
|
boItem = OMCacheProvider.getBizType(bo.btName);
|
btw.btmItem = boItem;
|
mapBtw.put(bo.btName, btw);
|
}
|
|
if (btw.mapAttr == null) {
|
btw.mapAttr = getBOAttributeItemMap(bo.btName);
|
}
|
|
if (btw.tableName == null){
|
btw.tableName = OmdHelper.getBTTableName(bo.btName);
|
}
|
|
// 创建新版本时,更改其他版本信息
|
if (isRevision && boItem.revLevel > 0) {
|
if (boItem.revInput) {
|
// sql = getBOCurrentInputRevisionSql(tableName, bo.nameoid,
|
// bo.versionValue);
|
checkBOCurrentInputRevisionSql(session, bo, btw.tableName);
|
} else {
|
// sql = getBOCurrentAutoRevisionSql(tableName, bo.nameoid,
|
// bo.versionValue, bo.versionSeq);
|
getBOCurrentAutoRevision(session, bo, btw.tableName);
|
}
|
|
// sql = getUpdateRevisionSql(bo.nameoid, tableName);
|
sql = String.format("update %s set ISLASTR='0' where NAMEOID=?", btw.tableName);
|
|
if (mapUpRevs.containsKey(sql))
|
lstBO = mapUpRevs.get(sql);
|
else {
|
lstBO = new ArrayList<BusinessObject>();
|
mapUpRevs.put(sql, lstBO);
|
}
|
lstBO.add(bo);
|
|
// SQLQuery query = session.createSQLQuery(sql);
|
// query.setString(0, bo.nameoid);
|
//
|
// query.executeUpdate();
|
}
|
// 创建新版次是,更改其他版次信息
|
if (isNewVersion && boItem.revLevel > 1) {
|
// sql = getTypeCurrentVersionSql(tableName, bo.revisionid,
|
// bo.nameoid, bo.versionValue, bo.versionSeq);
|
// SQLQuery query = session.createSQLQuery(sql);
|
|
getTypeCurrentVersion(session, bo, btw.tableName);
|
|
sql = String.format("update %s set ISLASTV='0' where NAMEOID=? and REVISIONOID=?", btw.tableName);
|
if (mapUpVers.containsKey(sql))
|
lstBO = mapUpVers.get(sql);
|
else {
|
lstBO = new ArrayList<BusinessObject>();
|
mapUpVers.put(sql, lstBO);
|
}
|
lstBO.add(bo);
|
|
// sql = getUpdateVersionSql(bo.nameoid, bo.revisionid,
|
// tableName);
|
// query = session.createSQLQuery(sql);
|
// query.executeUpdate();
|
}
|
|
// 保持新创建对象
|
|
if (mapBOs.containsKey(bo.btName)) {
|
lstBO = mapBOs.get(bo.btName);
|
} else {
|
lstBO = new ArrayList<BusinessObject>();
|
mapBOs.put(bo.btName, lstBO);
|
}
|
lstBO.add(bo);
|
}
|
|
if (mapUpRevs.size() > 0) {
|
ServerWithLog4j.logger.debug("batchCreateBusinessObject Update Revision");
|
Iterator<String> it = mapUpRevs.keySet().iterator();
|
while (it.hasNext()) {
|
sql = it.next();
|
ServerWithLog4j.logger.debug(sql);
|
|
lstBO = mapUpRevs.get(sql);
|
PreparedStatement pst = connection.prepareStatement(sql);
|
int i = 0;
|
for (i = 0; i < lstBO.size(); i++) {
|
pst.setString(1, lstBO.get(i).nameoid);
|
pst.addBatch();
|
if ((i + 1) % BATCHSIZE == 0) {
|
pst.executeBatch();
|
}
|
}
|
|
pst.executeBatch();
|
|
if (pst != null) {
|
pst.close();
|
pst = null;
|
}
|
}
|
}
|
|
if (mapUpVers.size() > 0) {
|
ServerWithLog4j.logger.debug("batchCreateBusinessObject Update Version");
|
|
Iterator<String> it = mapUpVers.keySet().iterator();
|
while (it.hasNext()) {
|
sql = it.next();
|
ServerWithLog4j.logger.debug(sql);
|
|
lstBO = mapUpVers.get(sql);
|
PreparedStatement pst = connection.prepareStatement(sql);
|
int i = 0;
|
for (i = 0; i < lstBO.size(); i++) {
|
pst.setString(1, lstBO.get(i).nameoid);
|
pst.setString(2, lstBO.get(i).revisionid);
|
pst.addBatch();
|
if ((i + 1) % BATCHSIZE == 0) {
|
pst.executeBatch();
|
}
|
}
|
pst.executeBatch();
|
|
if (pst != null) {
|
pst.close();
|
pst = null;
|
}
|
}
|
}
|
|
if (mapBOs.size() > 0) {
|
ServerWithLog4j.logger.debug("batchCreateBusinessObject Create Business Object");
|
|
BtmItemWrap btw = null;
|
String btmName = "";
|
|
Iterator<String> it = mapBOs.keySet().iterator();
|
while (it.hasNext()) {
|
btmName = it.next();
|
if (mapBtw.containsKey(btmName)) {
|
btw = mapBtw.get(btmName);
|
}
|
|
if (btw == null || btw.mapAttr == null)
|
continue;
|
|
sql = getCreateBOSql(btw);
|
|
ServerWithLog4j.logger.debug(sql);
|
|
lstBO = mapBOs.get(btmName);
|
PreparedStatement pst = connection.prepareStatement(sql);
|
int i = 0;
|
for (i = 0; i < lstBO.size(); i++) {
|
BusinessObject bo = lstBO.get(i);
|
|
prepareCreateBOPsmt(pst, bo, btw);
|
pst.addBatch();
|
if ((i + 1) % BATCHSIZE == 0) {
|
pst.executeBatch();
|
}
|
}
|
|
pst.executeBatch();
|
|
if (pst != null) {
|
pst.close();
|
pst = null;
|
}
|
}
|
}
|
|
ServerWithLog4j.logger.debug("===batchCreateBusinessObject end;");
|
return bos;
|
}
|
|
/**
|
* 保存链接以及链接关联的对象
|
*
|
* @param bos
|
* @param lo
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
*/
|
public boolean createBusinessObject(BusinessObject[] bos, LinkObject lo, LongHolder ts)
|
throws Exception, Throwable {
|
Timestamp cTime = new Timestamp(System.currentTimeMillis());
|
String currentTime = cTime.toString();
|
|
ts.value = cTime.getTime();
|
// 执行LO保存
|
LOFactoryService loService = LOFactoryService.getInstance();
|
loService.createLinkObject(lo);
|
|
// batchSaveBusinessObject(bos, currentTime);
|
|
// 按业务类型进行分组,
|
// 每组业务类型待创建保存的数据进行批量处理
|
Map<String, List<BusinessObject>> map = getGroupedBOsMap(bos);
|
Iterator<String> its = map.keySet().iterator();
|
while(its.hasNext()){
|
String key = its.next();
|
List<BusinessObject> boList = map.get(key);
|
// 调用单一业务类型的进行批量处理
|
batchSaveBusinessObjectWithSameBtm(boList.toArray(new BusinessObject[]{}), currentTime);
|
}
|
|
return true;
|
}
|
|
/**
|
* 批量保存对象以及链接
|
*
|
* @param bos
|
* @param los
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
* @throws PlmBtmError
|
* @throws PlmomdError
|
*/
|
public boolean batchCreateBusinessObjectWithLink(BusinessObject[] bos,
|
LinkObject[] los, LongHolder ts) throws Exception, Throwable {
|
|
ServerWithLog4j.logger.debug("===batchCreateBusinessObjectWithLink begin Create LinkObjects====");
|
// 执行LO保存
|
Timestamp cTime = new Timestamp(System.currentTimeMillis());
|
String currentTime = cTime.toString();
|
ts.value = cTime.getTime();
|
|
LOFactoryService loService = LOFactoryService.getInstance();
|
loService.batchCreateLinkObject(los);
|
// for (int i = 0; i < los.length; i++) {
|
// loService.createLinkObject(los[i]);
|
// }
|
|
// batchSaveBusinessObject(bos, currentTime);
|
|
ServerWithLog4j.logger.debug("===batchCreateBusinessObjectWithLink begin Create BusinessObject====");
|
|
// 按业务类型进行分组,
|
// 每组业务类型待创建保存的数据进行批量处理
|
Map<String, List<BusinessObject>> map = getGroupedBOsMap(bos);
|
Iterator<String> its = map.keySet().iterator();
|
while(its.hasNext()){
|
String key = its.next();
|
List<BusinessObject> boList = map.get(key);
|
// 调用单一业务类型的进行批量处理
|
batchSaveBusinessObjectWithSameBtm(boList.toArray(new BusinessObject[]{}), currentTime);
|
}
|
|
ServerWithLog4j.logger.debug("===batchCreateBusinessObjectWithLink end");
|
|
return true;
|
}
|
|
/**
|
* 返回按业务类型进行分组后的 map
|
* @param bos 待批量处理的 bos
|
* @return
|
*/
|
private Map<String, List<BusinessObject>> getGroupedBOsMap(BusinessObject[] bos){
|
Map<String, List<BusinessObject>> map = new HashMap<String, List<BusinessObject>>();
|
for(BusinessObject bo : bos){
|
if(map.containsKey(bo.btName)){
|
map.get(bo.btName).add(bo);
|
} else {
|
List<BusinessObject> list = new ArrayList<BusinessObject>();
|
list.add(bo);
|
map.put(bo.btName, list);
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 批量保存业务对象和链接对象,保存时不进行检查
|
*
|
* @param bos
|
* @param los
|
* @param ts
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
*/
|
public boolean batchCreateBusinessObjectWithLinkNoCheck(BusinessObject[] bos,
|
LinkObject[] los, StringHolder ts) throws Exception, Throwable {
|
// 执行LO保存
|
Timestamp cTime = new Timestamp(System.currentTimeMillis());
|
String currentTime = cTime.toString();
|
ts.value = currentTime.toString();
|
|
LOFactoryService loService = LOFactoryService.getInstance();
|
loService.batchCreateLinkObjectNoCheck(los, currentTime);
|
|
// batchSaveBusinessObject(bos, currentTime);
|
|
// 按业务类型进行分组,
|
// 每组业务类型待创建保存的数据进行批量处理
|
Map<String, List<BusinessObject>> map = getGroupedBOsMap(bos);
|
Iterator<String> its = map.keySet().iterator();
|
while(its.hasNext()){
|
String key = its.next();
|
List<BusinessObject> boList = map.get(key);
|
// 调用单一业务类型的进行批量处理
|
batchSaveBusinessObjectWithSameBtm(boList.toArray(new BusinessObject[]{}), currentTime);
|
}
|
|
return true;
|
}
|
|
/**
|
* 批量创建同一个业务类型的数据
|
* @param bos 同一业务类型待创建保存的BOS
|
* @param currentTime
|
* @throws Exception
|
* @throws Throwable
|
*/
|
private void batchSaveBusinessObjectWithSameBtm(BusinessObject[] bos, String currentTime) throws Exception, Throwable {
|
if (bos == null || bos.length == 0)
|
return;
|
|
BtmItemWrap btw = null;
|
|
// Map<String, String> tableMap = new HashMap<String, String>();
|
// Map<String, Map<String, AttribItem>> boAttrMap = new HashMap<String, Map<String, AttribItem>>();
|
//Map<String, Boolean> clobMap = new HashMap<String, Boolean>();
|
//boolean isHasClob = false;
|
PreparedStatement pst = null;
|
|
String btmName = bos[0].btName;
|
|
btw = new BtmItemWrap();
|
btw.tableName = OmdHelper.getBTTableName(btmName);
|
//btw.btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(btmName);
|
btw.btmItem = OMCacheProvider.getBizType(btmName);
|
btw.mapAttr = getBOAttributeItemMap(btmName);
|
|
String sql = getCreateBOSql(btw);
|
|
pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
|
int i = 0;
|
for (i = 0; i < bos.length; i++) {
|
|
// 为 pst 设置参数值
|
prepareCreateBOPsmt(pst, bos[i], btw);
|
pst.addBatch();
|
|
// 是否达到批量执行阀值
|
if((i + 1) % BATCHSIZE == 0){
|
// 达到则执行
|
pst.executeBatch();
|
}
|
}
|
|
pst.executeBatch();
|
|
closePreparedStatement(pst);
|
}
|
|
/**
|
* 批量存储bos
|
* @param bos
|
* @throws Throwable
|
* @throws Exception
|
*/
|
private void batchSaveBusinessObject(BusinessObject[] bos, String currentTime) throws Exception, Throwable {
|
Map<String, BtmItemWrap> mapBtw = new HashMap<String, BtmItemWrap>();
|
//Map<String, String> tableMap = new HashMap<String, String>();
|
//Map<String, String> initSqlMap = new HashMap<String, String>();
|
//Map<String, Map<String, AttribItem>> boAttrMap = new HashMap<String, Map<String, AttribItem>>();
|
Map<String, Boolean> clobMap = new HashMap<String, Boolean>();
|
//ArrayList<String> insertSqlList = new ArrayList<String>();
|
Map<String, List<BusinessObject>> mapBOs = new HashMap<String, List<BusinessObject>>();
|
List<BusinessObject> lstBO = null;
|
|
boolean isHasClob = false;
|
|
BtmItemWrap btw = null;
|
for (int i = 0; i < bos.length; i++) {
|
if (mapBtw.containsKey(bos[i].btName)){
|
btw = mapBtw.get(bos[i].btName);
|
} else {
|
btw = new BtmItemWrap();
|
btw.tableName = OmdHelper.getBTTableName(bos[i].btName);
|
//btw.btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(bos[i].btmName);
|
btw.btmItem = OMCacheProvider.getBizType(bos[i].btName);
|
btw.mapAttr = getBOAttributeItemMap(bos[i].btName);
|
|
mapBtw.put(bos[i].btName, btw);
|
}
|
|
// if (tableMap.get(bos[i].btmName) == null) {
|
// String tableName = this.getTableName(bos[i].btmName);
|
// tableMap.put(bos[i].btmName, tableName);
|
// }
|
// if (boAttrMap.get(bos[i].btmName) == null) {
|
// Map<String, AttribItem> attrItemMap = getBOAttributeItemMap(bos[i].btmName);
|
// boAttrMap.put(bos[i].btmName, attrItemMap);
|
// }
|
if (clobMap.get(bos[i].btName) == null) {
|
isHasClob = isTypeHasClobAttr(btw.mapAttr);
|
clobMap.put(bos[i].btName, isHasClob);
|
} else {
|
isHasClob = clobMap.get(bos[i].btName);
|
}
|
|
if (isHasClob) {
|
String createSql = getCreateBOSql(bos[i], btw.tableName, btw.mapAttr);
|
SQLQuery query = HibernateSessionFactory.getSession().createSQLQuery(createSql);
|
prepareCreateBOPsmt(query, bos[i], btw);
|
query.executeUpdate();
|
} else {
|
if (mapBOs.containsKey(bos[i].btName)){
|
lstBO = mapBOs.get(bos[i].btName);
|
} else {
|
lstBO = new ArrayList<BusinessObject>();
|
mapBOs.put(bos[i].btName, lstBO);
|
}
|
|
lstBO.add(bos[i]);
|
|
//String createSql = constructCreateSql(bos[i], btw.tableName, currentTime, initSqlMap, btw.mapAttr);
|
//ServerWithLog4j.logger.debug(createSql);
|
//insertSqlList.add(createSql);
|
}
|
}
|
|
Iterator<String> its = mapBOs.keySet().iterator();
|
while (its.hasNext()){
|
String key = its.next();
|
|
if (mapBtw.containsKey(key)) {
|
btw = mapBtw.get(key);
|
}
|
if (btw == null || btw.mapAttr == null)
|
continue;
|
|
String sql = getCreateBOSql(btw);
|
|
ServerWithLog4j.logger.debug(sql);
|
|
lstBO = mapBOs.get(key);
|
|
|
PreparedStatement pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
int i = 0;
|
for (i = 0; i < lstBO.size(); i++) {
|
BusinessObject bo = lstBO.get(i);
|
|
prepareCreateBOPsmt(pst, bo, btw);
|
pst.addBatch();
|
if ((i + 1) % BATCHSIZE == 0) {
|
pst.executeBatch();
|
}
|
}
|
pst.executeBatch();
|
|
if (pst != null) {
|
pst.close();
|
pst = null;
|
}
|
}
|
|
// Statement st = HibernateSessionFactory.getSessionConnection().createStatement();
|
// int size = insertSqlList.size();
|
// for (int i = 0; i < size; i++) {
|
// st.addBatch(insertSqlList.get(i));
|
// if ((i + 1) % BATCHSIZE == 0) {
|
// st.executeBatch();
|
// }
|
// }
|
//
|
// if (bos.length % BATCHSIZE != 0) {
|
// st.executeBatch();
|
// }
|
//
|
// if (st != null) {
|
// st.close();
|
// st = null;
|
// }
|
}
|
|
// private void closePreparedStatementMap(Map<String, PreparedStatement> pstMap) throws SQLException{
|
// Iterator<String> its = pstMap.keySet().iterator();
|
// while(its.hasNext()){
|
// String key = its.next();
|
// PreparedStatement pst = pstMap.get(key);
|
// closePreparedStatement(pst);
|
// }
|
// }
|
|
private void closePreparedStatement(PreparedStatement pst) throws SQLException{
|
if(pst != null){
|
pst.clearBatch();
|
pst.clearParameters();
|
pst.close();
|
}
|
}
|
|
private boolean isTypeHasClobAttr(Map<String, AttribItem> attrItemMap) {
|
Iterator<String> itor = attrItemMap.keySet().iterator();
|
while (itor.hasNext()) {
|
String key = itor.next();
|
AttribItem item = attrItemMap.get(key);
|
if (item.vtDataType.equals("VTClob")) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
public String constructCreateSql(BusinessObject bo, String tableName, long currentTime,
|
Map<String, String> initSqlMap, Map<String, AttribItem> attrItemMap) {
|
String initSql = null;
|
// if (initSqlMap.containsKey(tableName)) {
|
// initSql = initSqlMap.get(tableName);
|
// } else {
|
initSql = getInsertInitSql(bo, tableName, currentTime, attrItemMap);
|
// initSqlMap.put(tableName, initSql);
|
// }
|
// 对象特有属性
|
// AttribItem[] attrItems = getBOAttributeItem(bo.btmName);
|
StringBuilder sbSql = new StringBuilder(initSql);
|
// 对象固有属性
|
sbSql.append("'").append(bo.oid).append("'").append(",'")
|
.append(bo.revisionid).append("'").append(",'")
|
.append(bo.nameoid).append("'").append(",'").append(bo.btName)
|
.append("'").append(",").append(bo.isLastR).append(",")
|
.append(bo.isFirstR).append(",").append(bo.isLastV).append(",")
|
.append(bo.isFirstV).append(",'").append(bo.creator)
|
.append("'").append(",to_timestamp('").append(currentTime)
|
.append("','").append(DATEFORMATTER).append("')").append(",'")
|
.append(bo.creator).append("'").append(",to_timestamp('")
|
.append(currentTime).append("','").append(DATEFORMATTER)
|
.append("')")
|
|
.append(",'").append(bo.revisionRule).append("'").append(",'")
|
.append(bo.versionRule).append("'").append(",")
|
.append(bo.revisionSeq).append(",'").append(bo.revisionValue)
|
.append("'").append(",").append(bo.versionSeq).append(",'")
|
.append(bo.versionValue).append("'").append(",'")
|
.append(bo.lctId).append("'").append(",'").append(bo.lcStatus)
|
.append("'").append(",to_timestamp('").append(currentTime)
|
.append("','").append(DATEFORMATTER).append("')")
|
|
.append(",'").append(bo.id).append("'").append(",'")
|
.append(bo.name).append("'").append(",'")
|
.append(bo.description).append("'").append(",'")
|
.append(bo.creator).append("'").append(",'").append(bo.creator)
|
.append("'").append(",to_timestamp('").append(currentTime)
|
.append("','").append(DATEFORMATTER).append("')").append(",'")
|
.append(bo.creator).append("'").append(",to_timestamp('")
|
.append(currentTime).append("','").append(DATEFORMATTER)
|
.append("')").append(",'").append(bo.fromVersion)
|
.append("'");
|
for (int i = 0; i < bo.newAttrValList.length; i++) {
|
if (!attrItemMap.containsKey(bo.newAttrValList[i].attrName.toLowerCase())) {
|
continue;
|
}
|
AttributeValue abValue = bo.newAttrValList[i];
|
AttribItem abItem = attrItemMap.get(abValue.attrName.toLowerCase());
|
sbSql.append(",").append(getSqlAccordingType(abItem, bo.newAttrValList[i].attrVal));
|
}
|
|
bo.createTime = currentTime;
|
bo.modifyTime = currentTime;
|
bo.ts = currentTime;
|
// bo.checkinTime = currentTime;
|
// bo.checkoutTime = currentTime;
|
sbSql.append(")");
|
String sql = sbSql.toString();
|
return sql;
|
|
}
|
|
private String getInsertInitSql(BusinessObject bo, String tableName, long currentTime, Map<String, AttribItem> attrItemMap) {
|
StringBuilder sbFields = new StringBuilder(
|
"OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
+ "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
+ "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");
|
// + "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME,");
|
AttributeValue[] attrValList = bo.newAttrValList;
|
for (int i = 0; i < attrValList.length; i++) {
|
if (!attrItemMap.containsKey(attrValList[i].attrName.toLowerCase())) {
|
continue;
|
}
|
sbFields.append(",");
|
sbFields.append(attrValList[i].attrName);
|
}
|
StringBuilder sbSql = new StringBuilder().append("INSERT INTO ")
|
.append(tableName).append("(").append(sbFields.toString())
|
.append(")").append(" VALUES (");
|
return sbSql.toString();
|
}
|
|
private String getInsertInitSql(BtmItemWrap btw) {
|
StringBuilder sbFields = new StringBuilder(
|
"OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
+ "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
+ "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");
|
//+ "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME");
|
//AttributeValue[] attrValList = bo.newAttrValList;
|
for (String field : btw.btmItem.apNameArray) {
|
sbFields.append(",");
|
sbFields.append(field);
|
}
|
StringBuilder sbSql = new StringBuilder().append("INSERT INTO ")
|
.append(btw.tableName).append("(").append(sbFields.toString())
|
.append(")").append(" VALUES (");
|
return sbSql.toString();
|
}
|
|
/**
|
* 构建BO的创建SQL
|
*
|
* @param bo
|
* @return
|
*/
|
private String getCreateBOSql(BusinessObject bo, String tableName, Map<String, AttribItem> attrItemMap) {
|
String sql = "";
|
StringBuilder sbFields = new StringBuilder(
|
"OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
+ "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
+ "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION"); /*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
AttributeValue[] attrValList = bo.newAttrValList;
|
for (int i = 0; i < attrValList.length; i++) {
|
if (!attrItemMap.containsKey(attrValList[i].attrName.toLowerCase())) {
|
continue;
|
}
|
sbFields.append(",");
|
sbFields.append(attrValList[i].attrName);
|
}
|
|
String[] allFields = sbFields.toString().split(",");
|
StringBuilder sbValues = new StringBuilder();
|
for (int i = 0; i < allFields.length; i++) {
|
sbValues.append("?");
|
if (i != allFields.length - 1) {
|
sbValues.append(",");
|
}
|
}
|
StringBuilder sbSql = new StringBuilder().append("INSERT INTO ")
|
.append(tableName).append("(").append(sbFields.toString())
|
.append(")").append(" VALUES (").append(sbValues.toString())
|
.append(")");
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
/**
|
* 构建BO的创建SQL
|
*
|
* @param bo
|
* @return
|
*/
|
private String getCreateBOSql(BtmItemWrap btm) {
|
String sql = "";
|
StringBuilder sbFields = new StringBuilder(
|
"OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
+ "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
+ "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION"); /*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
//AttributeValue[] attrValList = btm.apNameArray;
|
|
String sField;
|
for (int i = 0; i < btm.btmItem.apNameArray.length; i++) {
|
sField = btm.btmItem.apNameArray[i];
|
// if (!attrItemMap.containsKey(sField.toLowerCase())) {
|
// continue;
|
// }
|
sbFields.append(",");
|
sbFields.append(sField);
|
}
|
|
String[] allFields = sbFields.toString().split(",");
|
StringBuilder sbValues = new StringBuilder();
|
for (int i = 0; i < allFields.length; i++) {
|
sbValues.append("?");
|
if (i != allFields.length - 1) {
|
sbValues.append(",");
|
}
|
}
|
|
StringBuilder sbSql = new StringBuilder().append("INSERT INTO ")
|
.append(btm.tableName).append("(").append(sbFields.toString())
|
.append(")").append(" VALUES (").append(sbValues.toString())
|
.append(")");
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
// private String getCreateBOSql(BusinessObject bo, String tableName, List<String> lstAttrItem) {
|
// String sql = "";
|
// StringBuilder sbFields = new StringBuilder(
|
// "OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
// + "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
// + "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION"); /*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
// AttributeValue[] attrValList = bo.newAttrValList;
|
// for (int i = 0; i < attrValList.length; i++) {
|
// if (!lstAttrItem.contains(attrValList[i].attrName.toLowerCase())) {
|
// continue;
|
// }
|
// sbFields.append(",");
|
// sbFields.append(attrValList[i].attrName);
|
// }
|
//
|
// String[] allFields = sbFields.toString().split(",");
|
// StringBuilder sbValues = new StringBuilder();
|
// for (int i = 0; i < allFields.length; i++) {
|
// sbValues.append("?");
|
// if (i != allFields.length - 1) {
|
// sbValues.append(",");
|
// }
|
// }
|
// StringBuilder sbSql = new StringBuilder().append("INSERT INTO ")
|
// .append(tableName).append("(").append(sbFields.toString())
|
// .append(")").append(" VALUES (").append(sbValues.toString())
|
// .append(")");
|
// sql = sbSql.toString();
|
// return sql;
|
// }
|
|
/**
|
* 准备创建数据
|
*
|
* @param psmt
|
* @param bo
|
* @throws Throwable
|
* @throws Exception
|
* @throws PlmbtwError
|
*/
|
private void prepareCreateBOPsmt(SQLQuery query, BusinessObject bo, BtmItemWrap btw)
|
throws Exception, Throwable {
|
|
Map<String, String> mapValue = new HashMap<String, String>();
|
for (AttributeValue av : bo.newAttrValList){
|
mapValue.put(av.attrName.toLowerCase(), av.attrVal);
|
}
|
|
int index = 0;
|
|
long curTime = System.currentTimeMillis();
|
Timestamp currentTime = new Timestamp(curTime);
|
// 对象固有属性
|
query.setString(index++, bo.oid);
|
query.setString(index++, bo.revisionid);
|
query.setString(index++, bo.nameoid);
|
query.setString(index++, bo.btName);
|
query.setInteger(index++, bo.isLastR ? 1 : 0);
|
query.setInteger(index++, bo.isFirstR ? 1 : 0);
|
query.setInteger(index++, bo.isLastV ? 1 : 0);
|
query.setInteger(index++, bo.isFirstV ? 1 : 0);
|
//query.setString(index++, bo.creator);
|
query.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //需要后台统一赋值 modify by zhonggy 2014-12-30
|
query.setTimestamp(index++, currentTime);
|
//query.setString(index++, bo.creator);
|
query.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //需要后台统一赋值 modify by zhonggy 2014-12-30
|
query.setTimestamp(index++, currentTime);
|
|
query.setString(index++, bo.revisionRule);
|
query.setString(index++, bo.versionRule);
|
query.setInteger(index++, bo.revisionSeq);
|
query.setString(index++, bo.revisionValue);
|
query.setInteger(index++, bo.versionSeq);
|
query.setString(index++, bo.versionValue);
|
query.setString(index++, bo.lctId);
|
query.setString(index++, bo.lcStatus);
|
query.setTimestamp(index++, currentTime);
|
|
query.setString(index++, bo.id);
|
query.setString(index++, bo.name);
|
query.setString(index++, bo.description);
|
query.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //需要后台统一赋值 modify by zhonggy 2014-12-30
|
/*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
/*
|
query.setString(index++, bo.creator);
|
query.setTimestamp(index++, currentTime);
|
query.setString(index++, bo.creator);
|
query.setTimestamp(index++, currentTime);
|
*/
|
query.setString(index++, bo.fromVersion);
|
// 对象特有属性
|
String value = null;
|
for (String field : btw.btmItem.apNameArray){
|
AttribItem abItem = btw.mapAttr.get(field.toLowerCase());
|
|
if (mapValue.containsKey(field.toLowerCase()))
|
value = mapValue.get(field.toLowerCase());
|
else
|
value = null;
|
setQueryValueAccordingDataType(query, abItem, index++, value);
|
}
|
// for (int i = 0; i < bo.newAttrValList.length; i++) {
|
// if (!attrItemMap.containsKey(bo.newAttrValList[i].attrName.toLowerCase())) {
|
// continue;
|
// }
|
// AttributeValue abValue = bo.newAttrValList[i];
|
// AttribItem abItem = attrItemMap.get(abValue.attrName.toLowerCase());
|
// setQueryValueAccordingDataType(query, abItem, index++, bo.newAttrValList[i].attrVal);
|
// }
|
|
bo.createTime = curTime;
|
bo.modifyTime = curTime;
|
bo.ts = curTime;
|
bo.creator = HibernateSessionFactory.getVciSessionInfo().userName;
|
bo.modifier = HibernateSessionFactory.getVciSessionInfo().userName;
|
bo.owner = HibernateSessionFactory.getVciSessionInfo().userName;
|
/*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
//bo.checkinTime = currentTime.toString();
|
//bo.checkoutBy = bo.creator;
|
//bo.checkoutTime = currentTime.toString();
|
}
|
|
// private void prepareCreateBOPsmt(PreparedStatement pst, BusinessObject bo, Map<String, AttribItem> attrItemMap)
|
// throws Exception, Throwable {
|
// int index = 1;
|
// Timestamp currentTime = new Timestamp(System.currentTimeMillis());
|
// // 对象固有属性
|
// pst.setString(index++, bo.oid);
|
// pst.setString(index++, bo.revisionid);
|
// pst.setString(index++, bo.nameoid);
|
// pst.setString(index++, bo.btmName);
|
// pst.setInt(index++, bo.isLastR);
|
// pst.setInt(index++, bo.isFirstR);
|
// pst.setInt(index++, bo.isLastV);
|
// pst.setInt(index++, bo.isFirstV);
|
// //query.setString(index++, bo.creator);
|
// pst.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //需要后台统一赋值 modify by zhonggy 2014-12-30
|
// pst.setTimestamp(index++, currentTime);
|
// //query.setString(index++, bo.creator);
|
// pst.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //需要后台统一赋值 modify by zhonggy 2014-12-30
|
// pst.setTimestamp(index++, currentTime);
|
//
|
// pst.setString(index++, bo.revisionRule);
|
// pst.setString(index++, bo.versionRule);
|
// pst.setInt(index++, bo.revisionSeq);
|
// pst.setString(index++, bo.revisionValue);
|
// pst.setInt(index++, bo.versionSeq);
|
// pst.setString(index++, bo.versionValue);
|
// pst.setString(index++, bo.lctId);
|
// pst.setString(index++, bo.lcStatus);
|
// pst.setTimestamp(index++, currentTime);
|
//
|
// pst.setString(index++, bo.id);
|
// pst.setString(index++, bo.name);
|
// pst.setString(index++, bo.description);
|
// pst.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //需要后台统一赋值 modify by zhonggy 2014-12-30
|
// /*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
// /*
|
// query.setString(index++, bo.creator);
|
// query.setTimestamp(index++, currentTime);
|
// query.setString(index++, bo.creator);
|
// query.setTimestamp(index++, currentTime);
|
// */
|
// pst.setString(index++, bo.copyFromVersion);
|
// // 对象特有属性
|
// for (int i = 0; i < bo.newAttrValList.length; i++) {
|
// if (!attrItemMap.containsKey(bo.newAttrValList[i].attrName.toLowerCase())) {
|
// continue;
|
// }
|
// AttributeValue abValue = bo.newAttrValList[i];
|
// AttribItem abItem = attrItemMap.get(abValue.attrName.toLowerCase());
|
// setQueryValueAccordingDataType(pst, abItem, index++, bo.newAttrValList[i].attrVal);
|
// }
|
//
|
// bo.createTime = currentTime.toString();
|
// bo.lastModifyTime = currentTime.toString();
|
// bo.ts = currentTime.toString();
|
// bo.creator = HibernateSessionFactory.getVciSessionInfo().userName;
|
// bo.lastModifier = HibernateSessionFactory.getVciSessionInfo().userName;
|
// bo.owner = HibernateSessionFactory.getVciSessionInfo().userName;
|
// /*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
// //bo.checkinTime = currentTime.toString();
|
// //bo.checkoutBy = bo.creator;
|
// //bo.checkoutTime = currentTime.toString();
|
// }
|
//
|
private void prepareCreateBOPsmt(PreparedStatement pst, BusinessObject bo, BtmItemWrap btm)
|
throws Exception, Throwable {
|
int index = 1;
|
|
long curTime = System.currentTimeMillis();
|
Timestamp currentTime = new Timestamp(curTime);
|
|
String userName = HibernateSessionFactory.getVciSessionInfo().userName;
|
if (!StringUtils.isBlank(userName)) {
|
bo.creator = userName;
|
bo.modifier = userName;
|
bo.owner = userName;
|
}
|
|
// 对象固有属性
|
pst.setString(index++, bo.oid);
|
pst.setString(index++, bo.revisionid);
|
pst.setString(index++, bo.nameoid);
|
pst.setString(index++, bo.btName);
|
pst.setInt(index++, bo.isLastR ? 1 : 0);
|
pst.setInt(index++, bo.isFirstR ? 1 : 0);
|
pst.setInt(index++, bo.isLastV ? 1 : 0);
|
pst.setInt(index++, bo.isFirstV ? 1 : 0);
|
|
pst.setString(index++, bo.creator);
|
pst.setTimestamp(index++, currentTime);
|
|
pst.setString(index++, bo.modifier);
|
pst.setTimestamp(index++, currentTime);
|
|
pst.setString(index++, bo.revisionRule);
|
pst.setString(index++, bo.versionRule);
|
pst.setInt(index++, bo.revisionSeq);
|
pst.setString(index++, bo.revisionValue);
|
pst.setInt(index++, bo.versionSeq);
|
pst.setString(index++, bo.versionValue);
|
pst.setString(index++, bo.lctId);
|
pst.setString(index++, bo.lcStatus);
|
pst.setTimestamp(index++, currentTime);
|
|
pst.setString(index++, bo.id);
|
pst.setString(index++, bo.name);
|
pst.setString(index++, bo.description);
|
pst.setString(index++, bo.owner);
|
|
/*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
/*
|
query.setString(index++, bo.creator);
|
query.setTimestamp(index++, currentTime);
|
query.setString(index++, bo.creator);
|
query.setTimestamp(index++, currentTime);
|
*/
|
pst.setString(index++, bo.fromVersion);
|
|
BtmItem btmItem = btm.btmItem;
|
Map<String, AttribItem> mapAttrItem = btm.mapAttr;
|
String sField, sValue;
|
Map<String, String> mapValue = new HashMap<String, String>();
|
for (int i = 0; i < bo.newAttrValList.length; i++)
|
{
|
mapValue.put(bo.newAttrValList[i].attrName.toLowerCase(), bo.newAttrValList[i].attrVal);
|
}
|
|
// 对象特有属性
|
for (int i = 0; i < btmItem.apNameArray.length; i++) {
|
|
sField = btmItem.apNameArray[i].toLowerCase();
|
|
AttribItem abItem = mapAttrItem.get(sField);
|
|
if (mapValue.containsKey(sField))
|
sValue = mapValue.get(sField);
|
else
|
sValue = "";
|
|
setQueryValueAccordingDataType(pst, abItem, index++, sValue);
|
}
|
|
// for (int i = 0; i < bo.newAttrValList.length; i++) {
|
// if (!mapAttrItem.containsKey(bo.newAttrValList[i].attrName.toLowerCase())) {
|
// continue;
|
// }
|
// AttributeValue abValue = bo.newAttrValList[i];
|
// AttribItem abItem = mapAttrItem.get(abValue.attrName.toLowerCase());
|
// setQueryValueAccordingDataType(pst, abItem, index++, bo.newAttrValList[i].attrVal);
|
// }
|
bo.createTime = curTime;
|
bo.modifyTime = curTime;
|
bo.ts = curTime;
|
bo.creator = HibernateSessionFactory.getVciSessionInfo().userName;
|
bo.modifier = HibernateSessionFactory.getVciSessionInfo().userName;
|
bo.owner = HibernateSessionFactory.getVciSessionInfo().userName;
|
/*在新建保存时不需要默认保存检入检出人与检入检出时间 by zhonggy 2014-12-30 */
|
//bo.checkinTime = currentTime.toString();
|
//bo.checkoutBy = bo.creator;
|
//bo.checkoutTime = currentTime.toString();
|
}
|
//
|
// private String getUpdateRevisionSql(String nameOid, String boName) {
|
// StringBuilder buffer = new StringBuilder();
|
// buffer.append("update ").append(boName).append(" set ISLASTR = '0'")
|
// .append(" where NAMEOID = '").append(nameOid).append("'");
|
// return buffer.toString();
|
// }
|
//
|
// private String getUpdateVersionSql(String nameOid, String revisionOid,
|
// String boName) {
|
// StringBuilder buffer = new StringBuilder();
|
// buffer.append("update ").append(boName).append(" set ISLASTV = '0'")
|
// .append(" where NAMEOID = '").append(nameOid).append("'")
|
// .append(" and REVISIONOID = '").append(revisionOid).append("'");
|
// return buffer.toString();
|
// }
|
//
|
// private String getTypeCurrentVersionSql(String boName, String revisionOid,
|
// String nameOid, String versionVal, int versionSeq) {
|
// StringBuilder strBuffer = new StringBuilder();
|
// strBuffer.append("select count(*) as count from ").append(boName)
|
// .append(" where REVISIONOID = '").append(revisionOid)
|
// .append("'").append(" AND NAMEOID = '").append(nameOid)
|
// .append("'").append(" and VERSIONVALUE = '").append(versionVal)
|
// .append("'").append(" and VERSIONSEQ = ").append(versionSeq);
|
//
|
// return strBuffer.toString();
|
// }
|
//
|
/*
|
* 获取当前有效的版本号
|
*/
|
private void getTypeCurrentVersion(Session session, BusinessObject bo, String tableName) throws NumberFormatException, HibernateException, SQLException {
|
|
String sql = String.format("select count(*) as count from %s where REVISIONOID=? and NAMEOID=? and VERSIONVALUE=? and VERSIONSEQ=?", tableName);
|
PreparedStatement ps = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
//SQLQuery query = session.createSQLQuery(sql);
|
ps.setString(1, bo.revisionid);
|
ps.setString(2, bo.nameoid);
|
ps.setString(3, bo.revisionValue);
|
ps.setInt(4, bo.revisionSeq);
|
|
ResultSet rs = ps.executeQuery();
|
int versionCount = 0;
|
if (rs.next()) {
|
versionCount = rs.getInt("count");
|
}
|
rs.close();
|
ps.close();
|
|
// SQLQuery query = session.createSQLQuery(sql);
|
// query.setString(0, bo.revisionid);
|
// query.setString(1, bo.nameoid);
|
// query.setString(2, bo.revisionValue);
|
// query.setInteger(3, bo.revisionSeq);
|
//
|
// List<?> queryList = query.list();
|
// Object array = (Object) queryList.get(0);
|
// int versionCount = ((BigDecimal) array).intValue();
|
if (versionCount > 0) {
|
VersionDataInfo vdInfo = getNextVersionValue(tableName,
|
bo.revisionid, bo.nameoid,
|
Integer.valueOf(bo.versionRule).intValue());
|
bo.versionValue = vdInfo.versionVal;
|
bo.versionSeq = vdInfo.versionSeq;
|
}
|
}
|
|
|
// private String getBOCurrentAutoRevisionSql(String boName, String nameOid,
|
// String revisionVal, int revisionSeq) {
|
// StringBuilder strBuffer = new StringBuilder();
|
// strBuffer.append("select count(*) as count from ").append(boName)
|
// .append(" where NAMEOID = '").append(nameOid).append("'")
|
// .append(" and REVISIONVALUE = '").append(revisionVal)
|
// .append("'").append(" and REVISIONSEQ = ").append(revisionSeq);
|
//
|
// return strBuffer.toString();
|
// }
|
|
/*
|
* 检查自动版本是否有效
|
*/
|
private void getBOCurrentAutoRevision(Session session, BusinessObject bo, String tableName)
|
throws Exception {
|
String sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=? and REVISIONSEQ=?", tableName);
|
PreparedStatement ps = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
//SQLQuery query = session.createSQLQuery(sql);
|
ps.setString(1, bo.nameoid);
|
ps.setString(2, bo.revisionValue);
|
ps.setInt(3, bo.revisionSeq);
|
|
ResultSet rs = ps.executeQuery();
|
int revsionCount = 0;
|
if (rs.next()) {
|
revsionCount = rs.getInt("count");
|
}
|
rs.close();
|
ps.close();
|
|
if (revsionCount > 0) {
|
RevisionDataInfo info = getNextRevisionValueObject(
|
tableName, bo.nameoid, bo.revisionRule,
|
false, bo.revisionValue);
|
bo.revisionValue = info.revisionVal;
|
bo.revisionSeq = info.revisionSeq;
|
}
|
}
|
|
// private String getBOCurrentInputRevisionSql(String tableName, String nameOid, String revisionVal) {
|
// StringBuilder strBuffer = new StringBuilder();
|
// strBuffer.append("select count(*) as count from ").append(tableName)
|
// .append(" where NAMEOID = '").append(nameOid).append("'")
|
// .append(" and REVISIONVALUE = '").append(revisionVal)
|
// .append("'");
|
//
|
// return strBuffer.toString();
|
// }
|
|
/*
|
* 检查对象版本是否有效
|
*/
|
private void checkBOCurrentInputRevisionSql(Session session, BusinessObject bo, String tableName)
|
throws VCIError, HibernateException, SQLException{
|
String sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=?", tableName);
|
PreparedStatement ps = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
ps.setString(1, bo.nameoid);
|
ps.setString(2, bo.revisionValue);
|
|
ResultSet rs = ps.executeQuery();
|
int revsionCount = 0;
|
if (rs.next()) {
|
revsionCount = rs.getInt("count");
|
}
|
rs.close();
|
ps.close();
|
if (revsionCount > 0) {
|
throw new VCIError("P0010SOF-00012", new String[0]);
|
}
|
// SQLQuery query = session.createSQLQuery(sql);
|
// query.setString(0, bo.nameoid);
|
// query.setString(1, bo.revisionValue);
|
//
|
// List<?> queryList = query.list();
|
// Object array = (Object) queryList.get(0);
|
// int revsionCount = ((BigDecimal) array).intValue();
|
// if (revsionCount > 0) {
|
// throw new VCIError("P0010SOF-00012", new String[0]);
|
// }
|
}
|
|
|
/**
|
* 关闭stamement连接
|
*
|
* @param psmt
|
*/
|
private void close(Statement psmt) {
|
try {
|
if (psmt != null) {
|
psmt.close();
|
psmt = null;
|
}
|
} catch (SQLException e) {
|
;
|
}
|
}
|
|
private void close(ResultSet rs) {
|
try {
|
if (rs != null) {
|
rs.close();
|
rs = null;
|
}
|
} catch (SQLException e) {
|
;
|
}
|
}
|
|
/**
|
* 更改BO的属性值
|
*
|
* @param bo
|
* @return
|
* @throws Exception
|
*/
|
public boolean updateBuinessObject(BusinessObject bo, Map<String, String> attrMap)
|
throws Exception {
|
Session session = HibernateSessionFactory.getSession();
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
String sql = getUpdateBOSql(bo, tableName, attrMap);
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareUpdateBOPsmt(query, bo, attrMap, null);
|
int result = query.executeUpdate();
|
if (result < 1)
|
return false;
|
return true;
|
}
|
|
public boolean updateBuinessObject(BusinessObject bo, Map<String, String> attrMap, Map<String, AttribItem> attrNameMap)
|
throws Exception {
|
Session session = HibernateSessionFactory.getSession();
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
String sql = getUpdateBOSql(bo, tableName, attrMap);
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareUpdateBOPsmt(query, bo, attrMap, attrNameMap);
|
int result = query.executeUpdate();
|
if (result < 1)
|
return false;
|
return true;
|
}
|
|
/**
|
* 批量更新对象属性
|
*
|
* @param bos
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
*/
|
public boolean batchUpdateBusinessObject(BusinessObject[] bos)
|
throws Exception, Throwable {
|
ServerWithLog4j.logger.debug("===batchUpdateBusinessObject begin");
|
|
//按业务类型进行分组
|
Map<String, Map<String, List<BusinessObject>>> btmBoMap = new HashMap<String, Map<String, List<BusinessObject>>>();
|
Map<String, String> tableMap = new HashMap<String, String>();
|
List<BusinessObject> lstBO = null;
|
Map<String, List<BusinessObject>> btmMap = null;
|
String btmName;
|
String tableName = null;
|
String sql = null;
|
Map<String, Map<String, String>> mapBtmAttr = new HashMap<String, Map<String, String>>();
|
Map<String, AttribItem> attrNameMap = new HashMap<String, AttribItem>();
|
|
for (int i = 0; i < bos.length; i++) {
|
btmName = bos[i].btName;
|
|
if (btmBoMap.containsKey(btmName)) {
|
btmMap = btmBoMap.get(btmName);
|
} else {
|
btmMap = new HashMap<String, List<BusinessObject>>();
|
btmBoMap.put(btmName, btmMap);
|
}
|
|
if (tableMap.get(btmName) != null) {
|
tableName = tableMap.get(btmName);
|
} else {
|
tableName = OmdHelper.getBTTableName(btmName);
|
tableMap.put(btmName, tableName);
|
}
|
|
if (!mapBtmAttr.containsKey(btmName)) {
|
Map<String, String> attrMap = this.getBOAttributeItemDefaultValMap(btmName, attrNameMap);
|
mapBtmAttr.put(btmName, attrMap);
|
}
|
|
sql = getUpdateBOSql(bos[i], tableName, mapBtmAttr.get(btmName));
|
if (btmMap.containsKey(sql)) {
|
lstBO = btmMap.get(sql);
|
} else {
|
lstBO = new ArrayList<BusinessObject>();
|
btmMap.put(sql, lstBO);
|
}
|
lstBO.add(bos[i]);
|
}
|
|
ServerWithLog4j.logger.debug("===batch exec sql");
|
|
Iterator<String> btmItor = btmBoMap.keySet().iterator();
|
while (btmItor.hasNext()) {
|
btmName = btmItor.next();
|
ServerWithLog4j.logger.debug("btmName=" + btmName);
|
|
Map<String, List<BusinessObject>> cbtmMap = btmBoMap.get(btmName);
|
Iterator<String> sqlitor = cbtmMap.keySet().iterator();
|
while (sqlitor.hasNext()) {
|
sql = sqlitor.next();
|
ServerWithLog4j.logger.debug(sql);
|
|
lstBO = cbtmMap.get(sql);
|
batchUpdateBusinessObject(btmName, sql, lstBO, mapBtmAttr, attrNameMap);
|
}
|
}
|
|
ServerWithLog4j.logger.debug("===batchUpdateBusinessObject end;");
|
|
return true;
|
}
|
|
private void batchUpdateBusinessObject(String btmName, String sql, List<BusinessObject> boList,
|
Map<String, Map<String, String>> boAttrMap, Map<String, AttribItem> attrNameMap) throws Exception {
|
|
Connection conn = HibernateSessionFactory.getSessionConnection();
|
|
PreparedStatement pst = conn.prepareStatement(sql);
|
int i = 0;
|
for (i = 0; i < boList.size(); i++) {
|
prepareUpdateBOPsmt(pst, boList.get(i), boAttrMap.get(boList.get(i).btName), attrNameMap);
|
pst.addBatch();
|
if ((i + 1) % BATCHSIZE == 0) {
|
pst.executeBatch();
|
}
|
}
|
pst.executeBatch();
|
|
if (pst != null) {
|
pst.close();
|
pst = null;
|
}
|
}
|
|
private void prepareUpdateBOPsmt(PreparedStatement pst, BusinessObject bo, Map<String, String> attrMap,
|
Map<String, AttribItem> attrNameMap) throws Exception {
|
int index = 1;
|
//query.setString(index++, bo.lastModifier);
|
pst.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //业务对象保存修改人后台统一处理 modify by zhonggy 2014-12-30
|
|
long curTime = System.currentTimeMillis();
|
Timestamp newTime = new Timestamp(curTime);
|
pst.setTimestamp(index++,newTime );
|
|
for (int i = 0; i < bo.newAttrValList.length; i++) {
|
String attrName = bo.newAttrValList[i].attrName.toLowerCase();
|
|
if (!isBOAttribute(attrName, attrMap)) {
|
continue;
|
}
|
AttribItem abItem = null;
|
if (attrNameMap == null || !attrNameMap.containsKey(attrName)) {
|
//abItem = ServerServiceProvider.getOMDService().getAttributeService().getAttribItemByName(attrName);
|
abItem = OMCacheProvider.getAttribute(attrName);
|
attrNameMap.put(attrName, abItem);
|
} else {
|
abItem = attrNameMap.get(attrName);
|
}
|
|
setQueryValueAccordingDataType(pst, abItem, index++, bo.newAttrValList[i].attrVal);
|
}
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
pst.setTimestamp(index++, newTime);
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
// 对象固有属性
|
pst.setString(index++, bo.oid);
|
pst.setTimestamp(index++, new Timestamp(bo.ts));
|
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
bo.modifyTime=curTime;
|
bo.ts=curTime;
|
bo.modifier = HibernateSessionFactory.getVciSessionInfo().userName;
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
}
|
|
/**
|
* 准备更改PO属性的SQL语句
|
*
|
* @param bo
|
* @return
|
*/
|
private String getUpdateBOSql(BusinessObject bo, String tableName, Map<String, String> attrMap) {
|
String sql = "";
|
|
StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
.append(tableName).append(" set LastModifier = ?,LastModifyTime = ?");
|
//.append(tableName).append(" set REVISIONVALUE = ?,VERSIONVALUE = ?,LCSTATUS = ?,LastModifier = ?,LastModifyTime = ?");
|
for (int i = 0; i < bo.newAttrValList.length; i++) {
|
String field = bo.newAttrValList[i].attrName;
|
if (!isBOAttribute(field, attrMap)) {
|
continue;
|
}
|
sbSql.append(",");
|
sbSql.append(bo.newAttrValList[i].attrName).append("=?");
|
}
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
sbSql.append(" ,TS = ?");
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
sbSql.append(" WHERE OID = ? and TS = ?");
|
sql = sbSql.toString();
|
|
ServerWithLog4j.logger.debug(sql);
|
|
return sql;
|
}
|
|
/**
|
* 准备更改属性值
|
*
|
* @param psmt
|
* @param bo
|
* @throws Exception
|
*/
|
private void prepareUpdateBOPsmt(SQLQuery query, BusinessObject bo, Map<String, String> attrMap, Map<String, AttribItem> attrNameMap)
|
throws Exception {
|
int index = 0;
|
//query.setString(index++, bo.lastModifier);
|
query.setString(index++, HibernateSessionFactory.getVciSessionInfo().userName); //业务对象保存修改人后台统一处理 modify by zhonggy 2014-12-30
|
|
long curTime = System.currentTimeMillis();
|
Timestamp newTime = new Timestamp(curTime);
|
query.setTimestamp(index++,newTime );
|
|
if (attrNameMap == null) {
|
attrNameMap = new HashMap<String, AttribItem>();
|
}
|
|
List<String> lstAttr = new ArrayList<String>();
|
for (AttributeValue av : bo.newAttrValList) {
|
String name = av.attrName.toLowerCase();
|
if (!attrNameMap.containsKey(name) && !lstAttr.contains(name)) {
|
//lstAttr.add(name);
|
AttribItem aitem = OMCacheProvider.getAttribute(name);
|
attrNameMap.put(aitem.name, aitem);
|
}
|
}
|
|
//AttribItem[] abItems = ServerServiceProvider.getOMDService().getAttributeService().getAttribItemsByNames(lstAttr.toArray(new String[0]));
|
// for (AttribItem aitem : abItems) {
|
// }
|
|
for (int i = 0; i < bo.newAttrValList.length; i++) {
|
if (!isBOAttribute(bo.newAttrValList[i].attrName, attrMap)) {
|
continue;
|
}
|
|
AttribItem abItem = attrNameMap.get(bo.newAttrValList[i].attrName.toLowerCase());
|
|
// if (attrNameMap.get(bo.newAttrValList[i].attrName.toLowerCase()) == null) {
|
// ;
|
// attrNameMap.put(abItem.name, abItem);
|
// } else {
|
// abItem = attrNameMap.get(bo.newAttrValList[i].attrName.toLowerCase());
|
// }
|
//System.out.println("===============" + bo.newAttrValList[i].attrName + "===================");
|
|
setQueryValueAccordingDataType(query, abItem, index++, bo.newAttrValList[i].attrVal);
|
}
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
query.setTimestamp(index++, newTime);
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
// 对象固有属性
|
query.setString(index++, bo.oid);
|
query.setTimestamp(index++, new Timestamp(bo.ts));
|
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
bo.modifyTime=curTime;
|
bo.ts=curTime;
|
bo.modifier = HibernateSessionFactory.getVciSessionInfo().userName;
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
}
|
|
/**
|
* 按类型删除BO对象
|
*
|
* @param bo
|
* @param type
|
* , 1:版次对象;2:版本对象;3:主对象
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
*/
|
public boolean deleteBusinessObject(BusinessObject bo, int type)
|
throws Exception, Throwable {
|
Session session = HibernateSessionFactory.getSession();
|
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
|
// 删除对象
|
String sql = getDeleteBOSql(bo, type, tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareDeleteBOPsmt(query, bo, type);
|
query.executeUpdate();
|
|
// 删除链接
|
// TODO 获取对象关联的链接
|
List<String> fromLinkList = new ArrayList<String>();
|
List<String> toLinkList = new ArrayList<String>();
|
//LinkType[] linkTypes = ServerServiceProvider.getOMDService().getLinkTypeService().getLinkTypes();
|
LinkType[] linkTypes = OMCacheProvider.getLinkTypes();
|
getRealtedLinks(bo.btName, fromLinkList, toLinkList, linkTypes);
|
|
LOFactoryService loServer = LOFactoryService.getInstance();
|
// 删除所有FROM端对象
|
loServer.deleteLinksByFromObj(fromLinkList.toArray(new String[0]), bo, type);
|
// 删除所有TO端对象
|
loServer.deleteLinksbyToObj(toLinkList.toArray(new String[0]), bo, type);
|
|
updateObjectLastRevision(tableName, bo);
|
|
return true;
|
}
|
|
private Map<String, String> execBatchDeleteBusinessObject(BusinessObject[] bos, int type) throws HibernateException, SQLException {
|
|
Map<String, String> tableMap = new HashMap<String, String>();
|
Statement st = HibernateSessionFactory.getSessionConnection().createStatement();
|
String tableName;
|
String sql;
|
|
int i = 0;
|
for (i = 0; i < bos.length; i++) {
|
if (tableMap.get(bos[i].btName) != null) {
|
tableName = tableMap.get(bos[i].btName);
|
} else {
|
tableName = OmdHelper.getBTTableName(bos[i].btName);
|
tableMap.put(bos[i].btName, tableName);
|
}
|
|
// 删除对象
|
sql = getBatchDeleteBOSql(bos[i], type, tableName);
|
st.addBatch(sql);
|
if ((i + 1) % BATCHSIZE == 0) {
|
st.executeBatch();
|
}
|
}
|
|
st.executeBatch();
|
|
if (st != null) {
|
st.close();
|
st = null;
|
}
|
|
return tableMap;
|
}
|
|
private void execBatchDeleteBORelatedLO(BusinessObject[] bos, int type) throws Exception, Throwable {
|
List<String> csqlList = null;
|
List<String> sqlList = new ArrayList<String>();
|
//LinkType[] linkTypes = ServerServiceProvider.getOMDService().getLinkTypeService().getLinkTypes();
|
LinkType[] linkTypes = OMCacheProvider.getLinkTypes();
|
|
Map<String, String> tableMap = new HashMap<String, String>();
|
|
for (int i = 0; i < bos.length; i++) {
|
List<String> fromLinkList = new ArrayList<String>();
|
List<String> toLinkList = new ArrayList<String>();
|
getRealtedLinks(bos[i].btName, fromLinkList, toLinkList, linkTypes);
|
LOFactoryService loServer = LOFactoryService.getInstance();
|
csqlList = new ArrayList<String>();
|
for (int j = 0; j < fromLinkList.size(); j++) {
|
loServer.getBatchDeleteLinksByFromObj(fromLinkList.toArray(new String[0]), bos[i], type, csqlList, tableMap);
|
}
|
sqlList.addAll(csqlList);
|
csqlList = new ArrayList<String>();
|
for (int j = 0; j < toLinkList.size(); j++) {
|
loServer.getBatchDeleteLinksByToObj(toLinkList.toArray(new String[0]), bos[i], type, csqlList, tableMap);
|
}
|
sqlList.addAll(csqlList);
|
}
|
|
Statement st = HibernateSessionFactory.getSessionConnection().createStatement();
|
|
int i = 0;
|
for (i = 0; i < sqlList.size(); i++) {
|
st.addBatch(sqlList.get(i));
|
if ((i + 1) % BATCHSIZE == 0) {
|
st.executeBatch();
|
}
|
}
|
|
st.executeBatch();
|
|
if (st != null) {
|
st.close();
|
st = null;
|
}
|
}
|
|
private void execBatchDeleteBOUpdateVersion(BusinessObject[] bos, Map<String, String> tableMap) throws HibernateException, SQLException {
|
Map<String, List<BusinessObject>> typeBos = new HashMap<String, List<BusinessObject>>();
|
List<BusinessObject> boList = null;
|
for (int i = 0; i < bos.length; i++) {
|
if (typeBos.get(bos[i].btName) != null) {
|
boList = typeBos.get(bos[i].btName);
|
} else {
|
boList = new ArrayList<BusinessObject>();
|
typeBos.put(bos[i].btName, boList);
|
}
|
boList.add(bos[i]);
|
}
|
|
Iterator<String> itor = typeBos.keySet().iterator();
|
Map<String, Object[]> objMap = new HashMap<String, Object[]>();
|
while (itor.hasNext()) {
|
String key = itor.next();
|
boList = typeBos.get(key);
|
batchGetLastRevisionObject(key, tableMap, boList, objMap);
|
}
|
String sql;
|
Statement st = HibernateSessionFactory.getSessionConnection().createStatement();
|
Iterator<String> nameoidItor = objMap.keySet().iterator();
|
int i = 0;
|
while (nameoidItor.hasNext()) {
|
i++;
|
String nameoid = nameoidItor.next();
|
Object[] obj = objMap.get(nameoid);
|
sql = String.format("update %s set ISLASTR = 1,ISLASTV=1 where oid='%s'", tableMap.get((String)obj[4]), (String)obj[0]);
|
if (sql == null || sql.trim().equals("")) {
|
continue;
|
}
|
st.addBatch(sql);
|
if ((i + 1) % BATCHSIZE == 0) {
|
st.executeBatch();
|
}
|
}
|
|
st.executeBatch();
|
|
if (st != null) {
|
st.close();
|
st = null;
|
}
|
}
|
|
/**
|
* 将剩余的最大版本、最大版次标记为最新的版本、版次
|
* @param tableName
|
* @param bo
|
* @return
|
*/
|
private boolean updateObjectLastRevision(String tableName, BusinessObject bo) {
|
String sql = "select * from( select oid from " + tableName + " t where t.nameoid = ? order by t.revisionseq desc, t.versionseq desc) c where rownum = 1";
|
Session session = HibernateSessionFactory.getSession();
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
|
ServerWithLog4j.logger.debug("updateObjectLastRevision Query Last Revision BO sql=" + sql);
|
|
List<?> queryList = query.list();
|
if (queryList == null || queryList.size() < 1) {
|
return true;
|
}
|
|
// Object[] array = (Object[]) queryList.get(0);
|
// String oid = (String)array[0];
|
Object array = (Object) queryList.get(0);
|
String oid = (String)array;
|
|
sql = "update " + tableName + " set ISLASTR = ?,ISLASTV=? where oid = ?";
|
ServerWithLog4j.logger.debug("updateObjectLastRevision sql=" + sql + "; oid=" + oid);
|
|
query = session.createSQLQuery(sql);
|
query.setInteger(0, 1);
|
query.setInteger(1, 1);
|
query.setString(2, oid);
|
query.executeUpdate();
|
|
return true;
|
}
|
|
private void batchGetLastRevisionObject(String key, Map<String, String> tableMap, List<BusinessObject> boList, Map<String, Object[]> objMap) {
|
List<Object[]> objList = new ArrayList<Object[]>();
|
Session session = HibernateSessionFactory.getSession();
|
StringBuffer buffer = new StringBuffer();
|
buffer.append("select oid, nameoid, revisionseq, versionseq, btmname from ").append(tableMap.get(key)).append(" t where t.nameoid in (");
|
for (int i = 0; i < boList.size(); i++) {
|
if (i % 500 > 0) {
|
buffer.append(",");
|
}
|
buffer.append("'").append(boList.get(i).nameoid).append("'");
|
if ((i + 1) % 500 == 0) {
|
buffer.append(") order by nameoid, revisionseq desc, versionseq desc");
|
SQLQuery query = session.createSQLQuery(buffer.toString());
|
List<?> queryList = query.list();
|
if (queryList != null && queryList.size() >= 1) {
|
for (int j = 0; j < queryList.size(); j++) {
|
objList.add((Object[])queryList.get(j));
|
}
|
}
|
buffer = new StringBuffer();
|
if ((i + 1) != boList.size()) {
|
buffer.append("select oid, nameoid, revisionseq, versionseq, btmname from ").append(tableMap.get(key)).append(" t where t.nameoid in (");
|
}
|
}
|
}
|
if (buffer.toString().length() > 0) {
|
buffer.append(") order by nameoid, revisionseq desc, versionseq desc");
|
SQLQuery query = session.createSQLQuery(buffer.toString());
|
List<?> queryList = query.list();
|
if (queryList != null && queryList.size() >= 1) {
|
for (int j = 0; j < queryList.size(); j++) {
|
objList.add((Object[])queryList.get(j));
|
}
|
}
|
}
|
|
for (int i = 0; i < objList.size(); i++) {
|
Object[] obj = objList.get(i);
|
//String oid = (String)obj[0];
|
String nameoid = (String)obj[1];
|
int revisionseq = ((BigDecimal)obj[2]).intValue();
|
int versionseq = ((BigDecimal)obj[3]).intValue();
|
if (objMap.get(nameoid) != null) {
|
Object[] cobj = objMap.get(nameoid);
|
int crevisionseq = ((BigDecimal)cobj[2]).intValue();
|
int cversionseq = ((BigDecimal)cobj[3]).intValue();
|
if (revisionseq > crevisionseq) {
|
objMap.put(nameoid, obj);
|
} else if (revisionseq > crevisionseq && versionseq > cversionseq) {
|
objMap.put(nameoid, obj);
|
}
|
} else {
|
objMap.put(nameoid, obj);
|
}
|
}
|
}
|
|
private String getUpdateObjectLastRevisionSql(String tableName, BusinessObject bo) {
|
String sql = "select * from (select oid from " + tableName + " t where t.nameoid = ? order by t.revisionseq desc, t.versionseq desc) c where rownum = 1";
|
Session session = HibernateSessionFactory.getSession();
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, bo.nameoid);
|
|
List<?> queryList = query.list();
|
if (queryList == null || queryList.size() < 1) {
|
return "";
|
}
|
|
Object array = (Object) queryList.get(0);
|
String oid = (String)array;
|
sql = "update " + tableName + " set ISLASTR = 1,ISLASTV=1 where oid = '" + oid + "'";
|
|
return sql;
|
}
|
|
/**
|
* 按类型批量删除BO对象
|
*
|
* @param bos
|
* @param type
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
*/
|
public boolean batchDeleteBusinessObject(BusinessObject[] bos, int type)
|
throws Exception, Throwable {
|
long startTime = System.currentTimeMillis();
|
Map<String, String> tableMap = execBatchDeleteBusinessObject(bos, type);
|
execBatchDeleteBORelatedLO(bos, type);
|
execBatchDeleteBOUpdateVersion(bos, tableMap);
|
long endTime = System.currentTimeMillis();
|
ServerWithLog4j.logger.debug("batchDeleteBusinessObject duration: " + (endTime - startTime));
|
return true;
|
}
|
|
/**
|
* 构建删除BO的SQL语句
|
*
|
* @param bo
|
* @return
|
*/
|
private String getDeleteBOSql(BusinessObject bo, int type, String tableName) {
|
String sql = "";
|
|
StringBuilder sbSql = new StringBuilder().append("DELETE ")
|
.append(tableName).append(" WHERE ");
|
if (type == 1) {
|
sbSql.append("OID = ?");
|
// 2013-12-03 删除增加TS判断逻辑,对于版本和对象ts已经没有意义
|
sbSql.append(" and TS = ?");
|
// 2013-12-03 删除增加TS判断逻辑
|
} else if (type == 2) {
|
sbSql.append("REVISIONOID = ?");
|
} else if (type == 3) {
|
sbSql.append("NAMEOID = ?");
|
}
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
private String getBatchDeleteBOSql(BusinessObject bo, int type, String tableName) {
|
String sql = "";
|
|
StringBuilder sbSql = new StringBuilder().append("DELETE ")
|
.append(tableName).append(" WHERE ");
|
if (type == 1) {
|
sbSql.append("OID = '" + bo.oid + "'");
|
// 2013-12-03 删除增加TS判断逻辑,对于版本和对象ts已经没有意义
|
sbSql.append(" and TS = to_timestamp('").append(bo.ts).append("','").append(DATEFORMATTER).append("')");
|
// 2013-12-03 删除增加TS判断逻辑
|
} else if (type == 2) {
|
sbSql.append("REVISIONOID = '" + bo.revisionid + "'");
|
} else if (type == 3) {
|
sbSql.append("NAMEOID = '" + bo.nameoid + "'");
|
}
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
/**
|
* 准备删除BO的数据
|
*
|
* @param psmt
|
* @param bo
|
* @throws SQLException
|
*/
|
private void prepareDeleteBOPsmt(SQLQuery query, BusinessObject bo, int type)
|
throws SQLException {
|
int index = 0;
|
|
// 对象固有属性
|
String id = "";
|
if (type == 1) {
|
id = bo.oid;
|
} else if (type == 2) {
|
id = bo.revisionid;
|
} else if (type == 3) {
|
id = bo.nameoid;
|
}
|
query.setString(index++, id);
|
// 2013-12-03 删除增加TS判断逻辑
|
if (type == 1) {
|
query.setTimestamp(index++, new Timestamp(bo.ts));
|
}
|
// 2013-12-03 删除增加TS判断逻辑
|
}
|
|
private void getRealtedLinks(String boName, List<String> fromList, List<String> toList, LinkType[] linkTypes) throws Exception, Throwable {
|
for (LinkType linkType : linkTypes) {
|
if (ArrayUtils.contains(linkType.btmItemsFrom, boName)) {
|
fromList.add(linkType.name);
|
}
|
|
if (ArrayUtils.contains(linkType.btmItemsTo, boName)) {
|
toList.add(linkType.name);
|
}
|
}
|
}
|
|
/**
|
* 根据对象ID获取对象的指定属性列值
|
*
|
* @param oid
|
* @param btmName
|
* @param queryColumns
|
* @return
|
* @throws Throwable
|
* @throws Exception
|
* @throws PlmbtwError
|
*/
|
public BusinessObject getInfoBusinessObject(String oid, String btmName)
|
throws Exception, Throwable {
|
|
AttribItem[] attrItems = getBOAttributeItem(btmName);
|
|
String tableName = OmdHelper.getBTTableName(btmName);
|
|
//Session session = HibernateSessionFactory.getSession();
|
String sql = getSelectBOSql(oid, tableName, attrItems);
|
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
ResultSet rs = stmt.executeQuery(sql);
|
|
BusinessObject bo = new BusinessObject();
|
if (rs.next()) {
|
bo = ServerObjectUtil.createBOByRS(attrItems, rs);
|
}
|
close(rs);
|
close(stmt);
|
// SQLQuery query = session.createSQLQuery(sql);
|
// List<?> queryList = query.list();
|
// if (queryList == null || queryList.size() < 1) {
|
// return new BusinessObject();
|
// }
|
//
|
// Object[] array = (Object[]) queryList.get(0);
|
// ServerBusinessObject sbo = new ServerBusinessObject();
|
// String[] queryColumns = sbo.getQueryColumnAccordingToSQL(sql, "SELECT", "FROM " + tableName);
|
// String attrType = "";
|
// int j = 0;
|
// for (int i = 0; i < array.length; i++) {
|
// if (i < array.length - attrItems.length) {
|
// attrType = "";
|
// } else {
|
// attrType = attrItems[j++].vtDataType;
|
// }
|
// sbo.setAttributeValue(queryColumns[i], attrType, array[i]);
|
// }
|
//
|
// BusinessObject bo = sbo.getBusinessObject();
|
|
return bo;
|
}
|
|
public BusinessObject[] getLastRevisionBusinessObject(String oid, String btmName)
|
throws Exception, Throwable {
|
AttribItem[] attrItems = getBOAttributeItem(btmName);
|
|
String tableName = OmdHelper.getBTTableName(btmName);
|
|
Session session = HibernateSessionFactory.getSession();
|
String sql = getLastVersionSelectBOSql(oid, tableName, attrItems);
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
ResultSet rs = stmt.executeQuery(sql);
|
|
BusinessObject bo = new BusinessObject();
|
if (rs.next()) {
|
bo = ServerObjectUtil.createBOByRS(attrItems, rs);
|
}
|
|
// SQLQuery query = session.createSQLQuery(sql);
|
// List<?> queryList = query.list();
|
// if (queryList == null || queryList.size() < 1) {
|
// return new BusinessObject[0];
|
// }
|
//
|
// Object[] array = (Object[]) queryList.get(0);
|
// ServerBusinessObject sbo = new ServerBusinessObject();
|
// String[] queryColumns = sbo.getQueryColumnAccordingToSQL(sql, "SELECT",
|
// "FROM " + tableName);
|
// String attrType = "";
|
// int j = 0;
|
// for (int i = 0; i < array.length; i++) {
|
// if (i < array.length - attrItems.length) {
|
// attrType = "";
|
// } else {
|
// attrType = attrItems[j++].vtDataType;
|
// }
|
// sbo.setAttributeValue(queryColumns[i], attrType, array[i]);
|
// }
|
//
|
// BusinessObject bo = sbo.getBusinessObject();
|
|
BusinessObject[] bos = new BusinessObject[1];
|
bos[0] = bo;
|
return bos;
|
}
|
|
/**
|
* 获取最新发布有效版的对象的oid/revisionoid/nameoid/btmname
|
* @param nameoid
|
* @return
|
*/
|
public String[] getLastReleasedBOId(String nameoid) {
|
Session session = HibernateSessionFactory.getSession();
|
String sql = "select * from PLRELEASEDOBJ p where p.nameoid = ?";
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, nameoid);
|
List<?> queryList = query.list();
|
if (queryList == null || queryList.size() < 1) {
|
return null;
|
}
|
|
Object[] array = (Object[]) queryList.get(0);
|
String[] values = new String[4];
|
for (int i = 0; i < array.length; i++) {
|
values[i] = (String)array[i];
|
}
|
|
return values;
|
}
|
/**
|
* 构建查询SQL语句
|
*
|
* @param oid
|
* @param btmName
|
* @param queryColumns
|
* @return
|
*/
|
private String getSelectBOSql(String oid, String btmName,
|
AttribItem[] attrItems) {
|
String sql = "";
|
StringBuilder sbFields = new StringBuilder(
|
"SELECT OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
+ "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
+ "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");
|
//+ "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME");
|
for (int i = 0; i < attrItems.length; i++) {
|
sbFields.append(",");
|
sbFields.append(attrItems[i].name);
|
}
|
sbFields.append(" FROM ").append(btmName).append(" p where p.OID = '")
|
.append(oid).append("'");
|
sql = sbFields.toString();
|
return sql;
|
}
|
|
private String getLastVersionSelectBOSql(String nameoid, String btmName,
|
AttribItem[] attrItems) {
|
String sql = "";
|
StringBuilder sbFields = new StringBuilder(
|
"SELECT OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
|
+ "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
|
+ "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");
|
//+ "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME");
|
for (int i = 0; i < attrItems.length; i++) {
|
sbFields.append(",");
|
sbFields.append(attrItems[i].name);
|
}
|
sbFields.append(" FROM ").append(btmName).append(" p where p.OID in ")
|
.append("(select r.oid from PLRELEASEDOBJ r where r.nameoid = '")
|
.append(nameoid).append("')");
|
sql = sbFields.toString();
|
return sql;
|
}
|
|
|
|
/**
|
* check in 对象
|
*
|
* @param bo
|
* @return
|
* @throws SQLException
|
*/
|
public boolean checkInBusinessObject(BusinessObject bo) throws SQLException {
|
Session session = HibernateSessionFactory.getSession();
|
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
|
String sql = getCheckInBOSql(bo, tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareCheckInBOPsmt(query, bo);
|
query.executeUpdate();
|
|
// 删除检出备份数据
|
deleteUndoCheckoutData(bo);
|
|
return true;
|
}
|
|
public boolean batchCheckInBusinessObject(BusinessObject[] bos)
|
throws SQLException {
|
for (BusinessObject bo : bos) {
|
checkInBusinessObject(bo);
|
}
|
|
return true;
|
}
|
|
private String getCheckInBOSql(BusinessObject bo, String tableName) {
|
String sql = "";
|
|
StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
.append(tableName).append(" set ");
|
sbSql.append("LASTMODIFIER = ?").append(",LASTMODIFYTIME = ?")
|
.append(",CHECKINBY = ?").append(",CHECKINTIME = ?")
|
.append(",CHECKOUTBY = ?").append(",CHECKOUTTIME= ?");
|
sbSql.append(" WHERE OID = ?");
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
private void prepareCheckInBOPsmt(SQLQuery query, BusinessObject bo)
|
throws SQLException {
|
int index = 0;
|
|
query.setString(index++, bo.modifier);
|
query.setTimestamp(index++, new Timestamp(System.currentTimeMillis()));
|
query.setString(index++, bo.modifier);
|
query.setTimestamp(index++, new Timestamp(System.currentTimeMillis()));
|
query.setString(index++, "");
|
query.setString(index++, "");
|
query.setString(index++, bo.oid);
|
}
|
|
/**
|
* 检查对象
|
*
|
* @param bo
|
* @return
|
* @throws SQLException
|
* @throws IOException
|
*/
|
public boolean checkoutBusinessObject(BusinessObject bo)
|
throws SQLException, IOException {
|
|
Session session = HibernateSessionFactory.getSession();
|
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
|
String modifier = bo.modifier;
|
// 查询OID对应的数据
|
String sql = getSelectForUpdateBOSql(bo.oid, tableName);
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
ResultSet rs = stmt.executeQuery(sql);
|
String checkoutBy = null;
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
while (rs.next()) {
|
int count = rs.getMetaData().getColumnCount();
|
checkoutBy = rs.getString("CHECKOUTBY");
|
for (int i = 0; i < count; i++) {
|
map.put(rs.getMetaData().getColumnName(i + 1),
|
rs.getObject(i + 1));
|
}
|
}
|
close(rs);
|
close(stmt);
|
|
// 判断是否可以检出
|
if (checkoutBy != null) {
|
return false;
|
}
|
// 记录检出信息,为取消检出回滚数据做准备
|
sql = "insert into PLATFORM_CHECKOUT values (?,?)";
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareBackupCheckOutBOPsmt(query, map, bo);
|
query.executeUpdate();
|
// 执行检出
|
//bo.checkoutBy = modifier;
|
sql = getCheckOutBOSql(bo, tableName);
|
query = session.createSQLQuery(sql);
|
prepareCheckOutBOPsmt(query, bo);
|
query.executeUpdate();
|
|
return true;
|
}
|
|
/**
|
* 批量检出对象
|
*
|
* @param bos
|
* @return
|
* @throws SQLException
|
* @throws IOException
|
*/
|
public boolean batchCheckoutBusinessObject(BusinessObject[] bos)
|
throws SQLException, IOException {
|
for (BusinessObject bo : bos) {
|
checkoutBusinessObject(bo);
|
}
|
|
return true;
|
}
|
|
private String getSelectForUpdateBOSql(String oid, String btmName) {
|
String sql = "";
|
StringBuilder sbFields = new StringBuilder("SELECT * ");
|
sbFields.append(" FROM ").append(btmName).append(" p where p.OID = '")
|
.append(oid).append("'").append(" for update");
|
sql = sbFields.toString();
|
return sql;
|
}
|
|
private String getCheckOutBOSql(BusinessObject bo, String tableName) {
|
String sql = "";
|
|
StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
.append(tableName).append(" set ");
|
sbSql.append("CHECKOUTBY = ?").append(",CHECKOUTTIME = ?");
|
sbSql.append(" WHERE OID = ?");
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
private void prepareCheckOutBOPsmt(SQLQuery query, BusinessObject bo)
|
throws SQLException {
|
int index = 0;
|
|
query.setString(index++, bo.modifier);
|
query.setTimestamp(index++, new Timestamp(System.currentTimeMillis()));
|
query.setString(index++, bo.oid);
|
}
|
|
private void prepareBackupCheckOutBOPsmt(SQLQuery query,
|
HashMap<String, Object> map, BusinessObject bo) throws IOException,
|
SQLException {
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ObjectOutputStream ops = new ObjectOutputStream(baos);
|
ops.writeObject(map);
|
ops.close();
|
baos.close();
|
|
int index = 0;
|
query.setString(index++, bo.oid);
|
query.setBinary(index++, baos.toByteArray());
|
baos = null;
|
}
|
|
/**
|
* 撤销检出对象
|
*
|
* @param bo
|
* @return
|
* @throws SQLException
|
* @throws IOException
|
* @throws ClassNotFoundException
|
*/
|
public boolean undoCheckoutBusinessObject(BusinessObject bo)
|
throws SQLException, IOException, ClassNotFoundException {
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
|
// 获取回滚的迁出数据
|
String sql = getSelectUndoCheckoutSQL(bo);
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
ResultSet rs = stmt.executeQuery(sql);
|
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
|
while (rs.next()) {
|
byte[] props = rs.getBytes("CHECKOUTVALUE");
|
ByteArrayInputStream baos = new ByteArrayInputStream(props);
|
ObjectInputStream ops = new ObjectInputStream(baos);
|
Object ob = ops.readObject();
|
if (ob != null) {
|
map = (HashMap<String, Object>) ob;
|
}
|
ops.close();
|
baos.close();
|
baos = null;
|
}
|
close(rs);
|
close(stmt);
|
|
ArrayList<Object> list = new ArrayList<Object>();
|
// 撤销回滚迁出数据
|
sql = getUndoCheckoutSQL(bo, map, list, tableName);
|
PreparedStatement psmt = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
prepareUndoCheckOutBOPsmt(psmt, list);
|
psmt.execute();
|
close(stmt);
|
// 删除检出备份数据
|
deleteUndoCheckoutData(bo);
|
|
return true;
|
}
|
|
/**
|
* 批量撤销检出对象
|
*
|
* @param bos
|
* @return
|
* @throws SQLException
|
* @throws IOException
|
* @throws ClassNotFoundException
|
*/
|
public boolean batchUnCheckOutBusinessObject(BusinessObject[] bos)
|
throws SQLException, IOException, ClassNotFoundException {
|
for (BusinessObject bo : bos) {
|
undoCheckoutBusinessObject(bo);
|
}
|
|
return true;
|
}
|
|
private String getSelectUndoCheckoutSQL(BusinessObject bo) {
|
StringBuilder insertSql = new StringBuilder();
|
insertSql
|
.append("select checkoutvalue from PLM_BOM_CHECKOUT where oid = ");
|
insertSql.append("'").append(bo.oid).append("'");
|
return insertSql.toString();
|
}
|
|
/**
|
* 从数据库检出备份表中删除已经备份的数据。
|
*
|
* @param bo
|
*/
|
private void deleteUndoCheckoutData(BusinessObject bo) {
|
String sql = "delete PLM_BOM_CHECKOUT where oid = '" + bo.oid + "'";
|
SQLQuery query = HibernateSessionFactory.getSession().createSQLQuery(
|
sql);
|
query.executeUpdate();
|
}
|
|
private String getUndoCheckoutSQL(BusinessObject bo,
|
HashMap<String, Object> map, ArrayList<Object> list,
|
String tableName) {
|
StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
.append(tableName).append(" set ");
|
Iterator<String> itor = map.keySet().iterator();
|
boolean isFirst = true;
|
while (itor.hasNext()) {
|
String key = (String) itor.next();
|
if (key.equals("OID")) {
|
continue;
|
}
|
|
if (isFirst) {
|
isFirst = false;
|
} else {
|
sbSql.append(",");
|
}
|
sbSql.append(key).append(" = ?");
|
list.add(map.get(key));
|
}
|
sbSql.append(" WHERE OID = '").append(bo.oid).append("'");
|
return sbSql.toString();
|
}
|
|
private void prepareUndoCheckOutBOPsmt(PreparedStatement stmt,
|
ArrayList<Object> list) throws SQLException {
|
int index = 1;
|
int size = list.size();
|
for (int i = 0; i < size; i++) {
|
stmt.setObject(index++, list.get(i));
|
}
|
}
|
|
/**
|
* 状态跃迁
|
*
|
* @param bo
|
* @param destination,跃迁状态
|
* @param releaseStatus,发布状态
|
* @return
|
* @throws SQLException
|
*/
|
public boolean transferBusinessObject(BusinessObject bo, String destination, String releaseStatus)
|
throws SQLException {
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
|
String sql = getTransitionBOSql(bo, tableName);
|
SQLQuery query = HibernateSessionFactory.getSession().createSQLQuery(sql);
|
prepareTransitionBOQuery(query, bo, destination);
|
query.executeUpdate();
|
|
//将发布版本的对象存储到发布对象表中
|
if (releaseStatus.trim().equals("") || !releaseStatus.equals(destination) || !bo.isLastR || !bo.isLastV) {
|
return true;
|
}
|
//删除历时发布数据
|
String deleteSql = "delete from PLRELEASEDOBJ where NAMEOID = ?";
|
query = HibernateSessionFactory.getSession().createSQLQuery(
|
deleteSql);
|
prepareDeleteHistoryReleasedObjQuery(query, bo);
|
query.executeUpdate();
|
|
String createSql = "insert into PLRELEASEDOBJ values(?, ?, ?, ?)";
|
query = HibernateSessionFactory.getSession().createSQLQuery(createSql);
|
prepareCreateReleasedObjQuery(query, bo);
|
query.executeUpdate();
|
|
return true;
|
}
|
|
private String getTransitionBOSql(BusinessObject bo, String tableName) {
|
String sql = "";
|
|
StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
.append(tableName).append(" set ");
|
sbSql.append("LASTMODIFIER = ?").append(", LASTMODIFYTIME = ?")
|
.append(", LCStatus = ?");
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
sbSql.append(",TS = ?");
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
sbSql.append(" WHERE OID = ?");
|
sql = sbSql.toString();
|
return sql;
|
}
|
|
private void prepareTransitionBOQuery(SQLQuery query, BusinessObject bo,
|
String destination) throws SQLException {
|
int index = 0;
|
|
query.setString(index++, bo.modifier);
|
|
long curTime = System.currentTimeMillis();
|
Timestamp nowtime =new Timestamp(curTime);
|
query.setTimestamp(index++, nowtime);
|
query.setString(index++, destination);
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
query.setTimestamp(index++, nowtime);
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
query.setString(index++, bo.oid);
|
bo.lcStatus = destination;
|
bo.ts = curTime;
|
bo.modifyTime = curTime;
|
}
|
|
private void prepareDeleteHistoryReleasedObjQuery(SQLQuery query, BusinessObject bo) throws SQLException {
|
int index = 0;
|
query.setString(index++, bo.nameoid);
|
}
|
|
private void prepareCreateReleasedObjQuery(SQLQuery query, BusinessObject bo) throws SQLException {
|
int index = 0;
|
query.setString(index++, bo.oid);
|
query.setString(index++, bo.revisionid);
|
query.setString(index++, bo.nameoid);
|
query.setString(index++, bo.btName);
|
}
|
|
public boolean batchTransferBusinessObject(BusinessObject[] bos,
|
TransitionVO[] vos, String[] releaseStatus) throws HibernateException, SQLException {
|
batchUpdateLifeCycleStatus(bos, vos);
|
|
List<Integer> releaseList = new ArrayList<Integer>();
|
//将发布版本的对象存储到发布对象表中
|
for (int i = 0; i < bos.length; i++) {
|
if (!releaseStatus[i].trim().equals("") && releaseStatus[i].equals(vos[i].destination) && bos[i].isLastR && bos[i].isLastV) {
|
releaseList.add(i);
|
}
|
}
|
if (releaseList.size() == 0) {
|
return true;
|
}
|
|
batchCreateLatestRelaseObject(bos, releaseList);
|
|
return true;
|
}
|
|
private void batchUpdateLifeCycleStatus(BusinessObject[] bos,
|
TransitionVO[] vos) throws HibernateException, SQLException {
|
// Timestamp cTime = new Timestamp(System.currentTimeMillis());
|
// String currentTime = cTime.toString();
|
long currentTime = System.currentTimeMillis();
|
|
Map<String, String> tableMap = new HashMap<String, String>();
|
ArrayList<String> transferSqlList = new ArrayList<String>();
|
for (int i = 0; i < bos.length; i++) {
|
String tableName = tableMap.get(bos[i].btName);
|
if (tableName == null) {
|
tableName = OmdHelper.getBTTableName(bos[i].btName);
|
tableMap.put(bos[i].btName, tableName);
|
}
|
transferSqlList.add(getTransferSql(bos[i], tableName, vos[i].destination, currentTime));
|
}
|
|
Statement st = HibernateSessionFactory.getSessionConnection().createStatement();
|
int size = transferSqlList.size();
|
int i = 0;
|
for (i = 0; i < size; i++) {
|
st.addBatch(transferSqlList.get(i));
|
if ((i + 1) % BATCHSIZE == 0) {
|
st.executeBatch();
|
}
|
}
|
|
st.executeBatch();
|
|
if (st != null) {
|
st.close();
|
st = null;
|
}
|
}
|
|
private void batchCreateLatestRelaseObject(BusinessObject[] bos, List<Integer> releaseList) throws HibernateException, SQLException {
|
ArrayList<String> deleteSqlList = new ArrayList<String>();
|
List<String> insertSqlList = new ArrayList<String>();
|
for (int i = 0; i < releaseList.size(); i++) {
|
StringBuffer deleteSql = new StringBuffer("delete from PLRELEASEDOBJ where NAMEOID = '").append(bos[releaseList.get(i)].nameoid).append("'");
|
deleteSqlList.add(deleteSql.toString());
|
StringBuffer insertSql = new StringBuffer("insert into PLRELEASEDOBJ values(")
|
.append("'").append(bos[releaseList.get(i)].oid).append("'")
|
.append(",'").append(bos[releaseList.get(i)].revisionid).append("'")
|
.append(",'").append(bos[releaseList.get(i)].nameoid).append("'")
|
.append(",'").append(bos[releaseList.get(i)].btName).append("'")
|
.append(")");
|
insertSqlList.add(insertSql.toString());
|
}
|
|
Statement st = HibernateSessionFactory.getSessionConnection().createStatement();
|
int size = deleteSqlList.size();
|
|
int i = 0;
|
for (i = 0; i < size; i++) {
|
st.addBatch(deleteSqlList.get(i));
|
if ((i + 1) % BATCHSIZE == 0) {
|
st.executeBatch();
|
}
|
}
|
|
st.executeBatch();
|
|
size = insertSqlList.size();
|
for (i = 0; i < size; i++) {
|
st.addBatch(insertSqlList.get(i));
|
if ((i + 1) % BATCHSIZE == 0) {
|
st.executeBatch();
|
}
|
}
|
|
st.executeBatch();
|
|
if (st != null) {
|
st.close();
|
st = null;
|
}
|
}
|
|
private String getTransferSql(BusinessObject bo, String tableName, String destination, long currentTime) {
|
StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
.append(tableName).append(" set ")
|
.append("LASTMODIFIER = '").append(bo.modifier).append("'")
|
.append(", LASTMODIFYTIME = to_timestamp('").append(currentTime).append("','").append(DATEFORMATTER).append("')")
|
.append(", LCStatus = '").append(destination).append("'")
|
.append(",TS = to_timestamp('").append(currentTime).append("','").append(DATEFORMATTER).append("')")
|
.append(" WHERE OID = '").append(bo.oid).append("'");
|
bo.ts = currentTime;
|
bo.lcStatus = destination;
|
bo.modifyTime = currentTime;
|
return sbSql.toString();
|
}
|
|
/**
|
* 变更所有者
|
*
|
* @param bo
|
* @return
|
* @throws SQLException
|
*/
|
public boolean changeBusinessObjectOwner(BusinessObject bo,
|
UserInfo userInfo) throws SQLException {
|
String tableName = OmdHelper.getBTTableName(bo.btName);
|
|
Session session = HibernateSessionFactory.getSession();
|
|
String sql = getChangeBOOnwerSql(bo, tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
prepareChangeBOOnwerQuery(query, bo, userInfo);
|
query.executeUpdate();
|
|
return true;
|
}
|
|
/**
|
* 批量更改对象所有者
|
*
|
* @param bos
|
* @param userList
|
* @return
|
* @throws SQLException
|
*/
|
public boolean batchChangeBusinessObjectOwner(BusinessObject[] bos,
|
UserInfo[] userList) throws SQLException {
|
for (int i = 0; i < bos.length; i++) {
|
changeBusinessObjectOwner(bos[i], userList[i]);
|
}
|
|
return true;
|
}
|
|
private String getChangeBOOnwerSql(BusinessObject bo, String tableName) {
|
String sql = String.format("UPDATE %s set LASTMODIFIER=?, LASTMODIFYTIME=?, OWNER=?, TS=? WHERE OID=?", tableName);
|
|
// StringBuilder sbSql = new StringBuilder().append("UPDATE ")
|
// .append(tableName).append(" set ");
|
// sbSql.append("LASTMODIFIER = ?").append(", LASTMODIFYTIME = ?")
|
// .append(", OWNER = ?");
|
// // 2013-12-03增加 更新"TS"逻辑(gaonb)
|
// sbSql.append(",TS = ?");
|
// // 2013-12-03增加 更新"TS"逻辑(gaonb)
|
// sbSql.append(" WHERE OID = ?");
|
// sql = sbSql.toString();
|
return sql;
|
}
|
|
private void prepareChangeBOOnwerQuery(SQLQuery query, BusinessObject bo,
|
UserInfo userInfo) throws SQLException {
|
int index = 0;
|
|
query.setString(index++, bo.modifier);
|
query.setTimestamp(index++, new Timestamp(System.currentTimeMillis()));
|
query.setString(index++, userInfo.userName);
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
query.setTimestamp(index++, new Timestamp(System.currentTimeMillis()));
|
// 2013-12-03增加 更新"TS"逻辑(gaonb)
|
query.setString(index++, bo.oid);
|
}
|
|
public VersionDataInfo getNextVersionValue(String tableName,
|
String revisionOid, String nameOid, int versionType)
|
throws HibernateException, SQLException {
|
VersionDataInfo info = new VersionDataInfo();
|
|
// 查询OID对应的数据
|
String sql = getTypeLatestVersionSql(tableName, revisionOid, nameOid);
|
PreparedStatement pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
|
pst.setString(1, revisionOid);
|
pst.setString(2, nameOid);
|
|
ResultSet rs = pst.executeQuery();
|
while (rs.next()) {
|
info.versionVal = rs.getString("VERSIONVALUE");
|
info.versionSeq = rs.getShort("VERSIONSEQ");
|
}
|
close(rs);
|
close(pst);
|
info = cumputeNextVersion(info, versionType);
|
return info;
|
}
|
|
private String getTypeLatestVersionSql(String tableName,
|
String revisionOid, String nameOid) {
|
String sql = String.format("SELECT * FROM (select VERSIONVALUE, VERSIONSEQ from %s " +
|
"where REVISIONOID=? and NAMEOID=? order by versionseq desc) where rownum = 1", tableName);
|
|
return sql;
|
// StringBuilder strBuffer = new StringBuilder();
|
// strBuffer.append("select * from (")
|
// .append("select VERSIONVALUE, VERSIONSEQ from ")
|
// .append(tableName).append(" where REVISIONOID = '")
|
// .append(revisionOid).append("'").append(" AND NAMEOID = '")
|
// .append(nameOid).append("'")
|
// .append(" order by versionseq desc")
|
// .append(") where rownum = 1");
|
//
|
// return strBuffer.toString();
|
}
|
|
/**
|
* 计算下一个版次号
|
*
|
* @param info
|
* @param versionType
|
* @return
|
*/
|
public VersionDataInfo cumputeNextVersion(VersionDataInfo info,
|
int versionType) {
|
if (versionType == 0 || versionType == 2) {
|
info.versionSeq = (short)(info.versionSeq + 1);
|
info.versionVal = String.valueOf(Integer.valueOf(info.versionVal) + 1);
|
} else {
|
info.versionSeq = (short)(info.versionSeq + 1);
|
RevisionComputer computer = new RevisionComputer();
|
info.versionVal = computer.cumputeLowChar(info.versionVal);
|
}
|
return info;
|
}
|
|
public RevisionDataInfo getNextRevisionValueObject(String btName,
|
String nameOid, String revisionRule, boolean revInput,
|
String revisionVal) throws Exception {
|
|
String tableName = OmdHelper.getBTTableName(btName);
|
|
Session session = HibernateSessionFactory.getSession();
|
if (revInput) {
|
//String sql = getBOCurrentInputRevisionSql(tableName, nameOid, revisionVal);
|
//SQLQuery query = session.createSQLQuery(sql);
|
String sql = String.format("select count(*) as count from %s where NAMEOID=? and REVISIONVALUE=?", tableName);
|
SQLQuery query = session.createSQLQuery(sql);
|
query.setString(0, nameOid);
|
query.setString(1, revisionVal);
|
|
List<?> queryList = query.list();
|
Object[] array = (Object[]) queryList.get(0);
|
int revsionCount = ((BigDecimal) array[0]).intValue();
|
if (revsionCount > 0) {
|
throw new VCIError("P0010SOF-00012", new String[0]);
|
}
|
}
|
RevisionDataInfo info = getNextRevisionValue(tableName, nameOid, revisionRule, revInput, revisionVal);
|
return info;
|
}
|
|
/**
|
* 计算下一个版本号
|
* @param tableName:表名称
|
* @param nameOid:对象id
|
* @param revisionRule:版本规则
|
* @param revInput:是否手动填写
|
* @return
|
* @throws Exception
|
* @throws PlmomdError
|
*/
|
public RevisionDataInfo getNextRevisionValue(String tableName, String nameOid, String revisionRule,
|
boolean revInput, String revisionVal) throws Exception {
|
String sql = getTypeLatestRevisionSql(tableName, nameOid, revisionRule);
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
ResultSet rs = stmt.executeQuery(sql);
|
RevisionDataInfo info = new RevisionDataInfo();
|
while (rs.next()) {
|
info.revisionVal = rs.getString("REVISIONVALUE");
|
info.revisionSeq = rs.getShort("REVISIONSEQ");
|
}
|
close(rs);
|
close(stmt);
|
if (revInput) {
|
info.revisionSeq = (short)(info.revisionSeq + 1);
|
info.revisionVal = revisionVal;
|
} else {
|
info = cumputeNextRevision(info, revisionRule);
|
}
|
|
return info;
|
}
|
|
/**
|
* 直接通过计算获取对应的新版本值
|
* @param bo
|
* @return
|
* @throws Exception
|
*/
|
public RevisionDataInfo getNextRevisionValue(String revisonValue, short revisonSeq, String revisionRule) throws Exception {
|
RevisionDataInfo info = new RevisionDataInfo();
|
info.revisionVal = revisonValue;
|
info.revisionSeq = revisonSeq;
|
info = cumputeNextRevision(info, revisionRule);
|
|
return info;
|
}
|
|
public String getTypeLatestRevisionSql(String tableName, String nameOid,
|
String revisionRule) {
|
StringBuilder strBuffer = new StringBuilder();
|
strBuffer.append("select * from (")
|
.append("select REVISIONVALUE, REVISIONSEQ from ")
|
.append(tableName).append(" where NAMEOID = '").append(nameOid)
|
.append("'").append(" order by REVISIONSEQ desc")
|
.append(") where rownum = 1");
|
|
return strBuffer.toString();
|
}
|
|
private RevisionDataInfo cumputeNextRevision(RevisionDataInfo info,
|
String revisionRule) throws Exception {
|
|
//VersionRule versionRule = ServerServiceProvider.getOMDService().getVerRuleService().getVersionRule(revisionRule);
|
VersionRule versionRule = OMCacheProvider.getVersionRule(revisionRule);
|
|
String prefix = versionRule.prefixion;
|
String suffix = versionRule.suffix;
|
String currentVal = revertRevisionVal(info.revisionVal, prefix, suffix);
|
RevisionComputer computer = new RevisionComputer();
|
boolean isValidChar = computer.isNumberAndChar(currentVal);
|
if (!isValidChar) {
|
throw new VCIError("P0010SOF-00013", new String[0]);
|
}
|
String nextVal = computer.computerNextRevision(currentVal,
|
versionRule.jumpCharacter);
|
if (nextVal.equals(currentVal)) {
|
throw new VCIError("P0010SOF-00014", new String[0]);
|
}
|
info.revisionVal = new StringBuilder().append(prefix).append(nextVal)
|
.append(suffix).toString();
|
info.revisionSeq += 1;
|
return info;
|
}
|
|
/**
|
* 将版本值的前后缀去除
|
*
|
* @param str
|
* @param prefix
|
* @param suffix
|
* @return
|
*/
|
private String revertRevisionVal(String str, String prefix, String suffix) {
|
String current = str.substring(prefix.length());
|
current = current.substring(0, current.length() - suffix.length());
|
return current;
|
}
|
|
/**
|
* 根据查询语句和条件获取查询结果,只查询一列
|
* @param sql
|
* @param conditions
|
* @return
|
* @throws HibernateException
|
* @throws SQLException
|
*/
|
public String[] getClssficationValue(String sql, Map<String, String> conditions) throws HibernateException, SQLException {
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
Iterator<String> itor = conditions.keySet().iterator();
|
while (itor.hasNext()) {
|
String key = itor.next();
|
String value = conditions.get(key);
|
if (sql.indexOf("%:" + key + "%") >= 0) {
|
sql = sql.replaceAll("%:" + key + "%", "'%" + value + "%'");
|
} else if (sql.indexOf("%:" + key) >= 0) {
|
sql = sql.replaceAll("%:" + key, "'%" + value + "'");
|
} else if (sql.indexOf(":" + key + "%") >= 0) {
|
sql = sql.replaceAll(":" + key + "%", "'" + value + "%'");
|
} else if (sql.indexOf(":" + key) >= 0) {
|
sql = sql.replaceAll(":" + key, "'" + value + "'");
|
}
|
}
|
ResultSet rs = stmt.executeQuery(sql);
|
List<String> list = new ArrayList<String>();
|
while (rs.next()) {
|
list.add(rs.getString(1));
|
}
|
close(rs);
|
close(stmt);
|
return list.toArray(new String[0]);
|
}
|
|
/**
|
* 根据查询语句获取符合要求的多列查询结果
|
* @param sql
|
* @param conditions
|
* @return
|
* @throws SQLException
|
*/
|
public String[][] getSqlQueryResult(String sql, Map<String, String> conditions) throws SQLException {
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
Iterator<String> itor = conditions.keySet().iterator();
|
while (itor.hasNext()) {
|
String key = itor.next();
|
String value = conditions.get(key);
|
if (sql.indexOf("%:" + key + "%") >= 0) {
|
sql = sql.replaceAll("%:" + key + "%", "'%" + value + "%'");
|
} else if (sql.indexOf("%:" + key) >= 0) {
|
sql = sql.replaceAll("%:" + key, "'%" + value + "'");
|
} else if (sql.indexOf(":" + key + "%") >= 0) {
|
sql = sql.replaceAll(":" + key + "%", "'" + value + "%'");
|
} else if (sql.indexOf(":" + key) >= 0) {
|
sql = sql.replaceAll(":" + key, "'" + value + "'");
|
}
|
}
|
ResultSet rs = stmt.executeQuery(sql);
|
String str = sql.substring("select".length() + 1);
|
str = str.substring(0, str.indexOf(" from "));
|
int queryColSize = str.split(",").length;
|
List<String[]> list = new ArrayList<String[]>();
|
while (rs.next()) {
|
String[] values = new String[queryColSize];
|
for (int i = 0; i < queryColSize; i++) {
|
values[i] = rs.getString(i + 1);
|
}
|
list.add(values);
|
}
|
close(rs);
|
close(stmt);
|
return list.toArray(new String[0][]);
|
}
|
|
public String[][] getCustomSqlValue(String[] sqls) throws HibernateException, SQLException {
|
Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
|
String sql = "";
|
List<String[]> list = new ArrayList<String[]>();
|
for (int i = 0; i < sqls.length; i++) {
|
if (i % 100 == 0 && i != 0) {
|
ResultSet rs = stmt.executeQuery(sql);
|
while (rs.next()) {
|
String[] strArray = new String[2];
|
strArray[0] = String.valueOf(rs.getInt(1));
|
strArray[1] = rs.getString(2);
|
list.add(strArray);
|
}
|
close(rs);
|
sql = "";
|
} else if (i % 100 != 0){
|
sql += " union ";
|
}
|
sql += sqls[i];
|
}
|
if (sql.length() != 0) {
|
ResultSet rs = stmt.executeQuery(sql);
|
while (rs.next()) {
|
String[] strArray = new String[2];
|
strArray[0] = String.valueOf(rs.getInt(1));
|
strArray[1] = rs.getString(2);
|
list.add(strArray);
|
}
|
close(rs);
|
close(stmt);
|
}
|
return list.toArray(new String[0][]);
|
}
|
}
|