/**
* Copyright (c) 2002 sinosoft Co. Ltd.
* All right reserved.
*/
package com.sinosoft.lis.pubfun;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.sinosoft.utility.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class PubSubmit
{
//传输数据类
private VData mInputData;
/** 错误处理类,每个需要错误处理的类中都放置该类 */
public CErrors mErrors = new CErrors();
/** 数据库连接 **/
private Connection conn = null;
/** 立即提交标志 **/
private final boolean commitFlag = true;
public PubSubmit()
{
}
/**
* 传输数据的公共方法
* 传入数据
* @param cInputData VData
* 数据操作标示
* 为了解决同时提交时的数据重复插入的问题,传入一个(select)sql如果查出数据则表示已经有数据,则报错退出
* @param cOperate String
* @return boolean
*/
public boolean submitData(VData cInputData, String cOperate)
{
//首先将数据在本类中做一个备份
mInputData = (VData) cInputData.clone();
if (!this.saveData())
{
return false;
}
mInputData = null;
return true;
}
/**
* 数据库操作
* @return: boolean
*/
private boolean saveData()
{
// System.out.println("---Start Save---");
//建立数据库连接
if (conn == null)
{
conn = DBConnPool.getConnection();
}
if (conn == null)
{
// @@错误处理
CError.buildErr(this, "数据库连接失败");
return false;
}
try
{
//开始事务,锁表
conn.setAutoCommit(false);
String action = ""; //操作方式,INSERT\UPDATE\DELETE
String className = ""; //类名
Object o = null; //Schema或Set对象
Object DBObject = null; //DB或DBSet对象
Method m = null; //方法
Constructor constructor = null; //构造函数
Class[] parameterC = new Class[1]; //调用方法的参数数组
Object[] parameterO = new Object[1]; //调用方法的对象数组
// System.out.println("mInputData.size():" + mInputData.size());
// System.out.println("mInputData :" + mInputData);
//通过MMap来传递每个Schema或Set的数据库操作方式,约定使用
MMap map = (MMap) mInputData.getObjectByObjectName("MMap", 0);
if (map != null && map.keySet().size() != 0)
{
Set set = map.keySet();
//Iterator iterator = map.keySet().iterator();
//while (iterator.hasNext()) {
for (int j = 0; j < set.size(); j++)
{
//获取操作对象Schema或Set或SQL
//o = iterator.next();
o = map.getOrder().get(String.valueOf(j + 1));
//获取操作方式
action = (String) map.get(o);
if (action == null)
{
continue;
}
// System.out.println("\n" + o.getClass().getName() +
// " Operate DB: " + action);
//构造相应的DB类名
className = o.getClass().getName();
// System.out.println("className :" + className);
// System.out.println("action :" + action);
if (className.endsWith("String"))
{
if (action.equals("UPDATE"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
if (!tExeSQL.execUpdateSQL(tSQL))
{
CError.buildErr(this, "执行更新语句失败");
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("DELETE"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
// System.out.println("执行SQL语句:" + tSQL);
if (!tExeSQL.execUpdateSQL(tSQL))
{
CError.buildErr(this, "执行删除语句失败");
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("INSERT"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
// System.out.println("执行SQL语句:" + tSQL);
if (!tExeSQL.execUpdateSQL(tSQL))
{
CError.buildErr(this,
"执行插入语句失败原因:" +
tExeSQL.mErrors.getError(0).
errorMessage);
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("SELECT")) //加入select方法,如果查出则表示有重复操作并报错退出
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
SSRS tSSRS = new SSRS();
System.out.println("执行SQL语句:" + tSQL);
tSSRS = tExeSQL.execSQL(tSQL);
if (tSSRS.getMaxRow()>0)
{
CError.buildErr(this, "该操作已经执行,请检查数据!");
conn.rollback();
conn.close();
return false;
}
continue;
}
//*****************************************************************************
//begin added by hutongzhao 2009-04-21
if (action.startsWith("SLIS_PACKAGE_PROCEDURE")){
String procName = (String)o;
Map procMap = this.callProcedureByJdbc(conn,procName);
String result = (String)procMap.get("RESULT");
if(result!=null && !result.equals("1")){
CError.buildErr(this, "执行存储过程失败!"+procName);
CError.buildErr(this, "执行存储过程失败原因!"+procMap.get("MESSAGE"));
try
{
conn.rollback();
conn.close();
}catch (Exception ex){
StringBuffer appString = new StringBuffer();
appString.append("异常信息:"+ex.getMessage());
appString.append("\n");
StackTraceElement elements[] = ex.getStackTrace();
for(int i = 0; i<elements.length; i++){
appString.append(elements[i].toString());
appString.append("\n");
}
System.out.println(appString.toString());
}finally{
conn.close();
}
System.out.println("PubSubmet中执行存储过程失败!"+procName);
return false;
}
continue;
}//end added by hutongzhao
//********************************************************************************
}
//增加对MMap的处理,edit by liyudong
else if (className.endsWith("MMap"))
{
//获取存储SQL及变量的MMap
MMap tMMap = (MMap) o;
if(tMMap != null && tMMap.keySet().size() != 0){
Set mset = tMMap.keySet();
ExeSQL tExeSQL = new ExeSQL(conn);
//循环处理MMap
for(int mcount = 0; mcount < mset.size(); mcount++){
String keyClass = tMMap.getOrder().get(String.valueOf(mcount + 1)).getClass().getName();
System.out.println("keyClass:" + keyClass);
String tSQL = "";
String[] tvars = {};
if(keyClass.endsWith("String")){
tSQL = (String) tMMap.getOrder().get(String.valueOf(mcount + 1));
tvars = (String[]) tMMap.get(tSQL);
}else if(keyClass.endsWith("StringBuffer")){
StringBuffer tSQLBuffer = (StringBuffer)tMMap.getOrder().get(String.valueOf(mcount + 1));
tvars = (String[]) tMMap.get(tSQLBuffer);
tSQL = tSQLBuffer.toString();
}
if (!action.equals("SELECT"))
{
if (!tExeSQL.execUpdateSQL(tSQL,tvars))
{
CError.buildErr(this, "执行更新语句失败");
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("SELECT"))
{
SSRS tSSRS = new SSRS();
System.out.println("执行SQL语句:" + tSQL);
tSSRS = tExeSQL.execSQL(tSQL,tvars);
if (tSSRS.getMaxRow()>0)
{
CError.buildErr(this, "该操作已经执行,请检查数据!");
conn.rollback();
conn.close();
return false;
}
continue;
}
}
continue;
}
}
else if (className.endsWith("Schema"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") + 1,
className.lastIndexOf("S")) +
"DB";
}
else if (className.endsWith("BLSet"))
{
className = "com.sinosoft.lis.vdb." +
className.
substring(className.lastIndexOf(".") + 1,
className.lastIndexOf("B")) +
"DBSet";
}
else if (className.endsWith("Set"))
{
className = "com.sinosoft.lis.vdb." +
className.
substring(className.lastIndexOf(".") + 1,
className.lastIndexOf("S")) +
"DBSet";
}
Class DBClass = Class.forName(className);
//选择构造函数,构造相同事务的DB或DBSet对象
parameterC[0] = Connection.class;
constructor = DBClass.getConstructor(parameterC);
parameterO[0] = conn;
DBObject = constructor.newInstance(parameterO);
//给DB对象付值,将传入的Schema或Set对象的内容复制到DB中
parameterC[0] = o.getClass();
if (o.getClass().getName().endsWith("Schema"))
{
m = DBObject.getClass().getMethod("setSchema",
parameterC);
}
else if (o.getClass().getName().endsWith("Set"))
{
m = DBObject.getClass().getMethod("set", parameterC);
}
parameterO[0] = o;
m.invoke(DBObject, parameterO);
//进行数据库操作
if (action.equals("INSERT"))
{
m = DBObject.getClass().getMethod("insert", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行插入语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("UPDATE"))
{
m = DBObject.getClass().getMethod("update", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行更新语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("DELETE"))
{
m = DBObject.getClass().getMethod("delete", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行删除语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("DELETE&INSERT"))
{
//DELETE
m = DBObject.getClass().getMethod("delete", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行删除,插入语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
//INSERT
m = DBObject.getClass().getMethod("insert", null);
b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行插入语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("BLOBINSERT"))
{ //add by Alex at 2005.1.12
//first,insert a record with a empty_blob , at the same time prepare the param for the second step
String tSQL = "";
//用于第二步update时的参数
String pWhereSQL = " and ";
String pTabName = "";
String pUpdateField = "";
String[] parmStrArr = getBlobInsertStr(o, tSQL,
pWhereSQL, pTabName,
pUpdateField);
// COracleBlob tCOracleBlob = new COracleBlob();
// if (!tCOracleBlob.InsertBlankBlobRecord(parmStrArr[0], conn)) {
// CError.buildErr(this, "执行插入blob语句失败");
// try {
// conn.rollback();
// } catch (Exception e) {}
// conn.close();
// System.out.println(
// "COracleBlob.InsertBlankBlobRecord" +
// " " + action + " Failed");
// return false;
// }
//second,using UpdateBlob method to update the empty_blob with a InputStream object from the schema
String aClassName = o.getClass().getName();
Schema s = (Schema) o;
m = o.getClass().getMethod("get" + parmStrArr[2], null);
InputStream ins = (InputStream) m.invoke(o, null);
// if (!tCOracleBlob.UpdateBlob(ins, parmStrArr[1],
// parmStrArr[2] , parmStrArr[3], conn)) {
// CError.buildErr(this, "执行更新blob语句失败");
// try {
// conn.rollback();
// } catch (Exception e) {}
// conn.close();
// ins.close();
// System.out.println("COracleBlob.UpdateBlob" +
// " " + action + " Failed");
// return false;
// }
// CBlob tCBlob = new CBlob();
if (!CBlob.BlobInsert(ins, parmStrArr, conn))
{
CError.buildErr(this, "执行更新blob语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
ins.close();
System.out.println("tCBlob.BlobInsert" +
" " + action + " Failed");
return false;
}
ins.close();
}
else if (action.equals("BLOBUPDATE"))
{ //add by Alex at 2005.1.12
//first,prepare the param for the UpdateBlob
String tSQL = "";
//用于第二步update时的参数
String pWhereSQL = " and ";
String pTabName = "";
String pUpdateField = "";
String[] parmStrArr = getBlobInsertStr(o, tSQL,
pWhereSQL, pTabName,
pUpdateField);
//second,using UpdateBlob method to update the blobfield with a InputStream object from the schema
// COracleBlob tCOracleBlob = new COracleBlob();
String aClassName = o.getClass().getName();
Schema s = (Schema) o;
m = o.getClass().getMethod("get" + parmStrArr[2], null);
InputStream ins = (InputStream) m.invoke(o, null);
// if (!tCOracleBlob.UpdateBlob(ins, parmStrArr[1],
// parmStrArr[2], parmStrArr[3], conn))
// {
// CError.buildErr(this, "执行更新blob语句失败");
// try
// {
// conn.rollback();
// }
// catch (Exception e)
// {}
// conn.close();
// ins.close();
// System.out.println("COracleBlob.UpdateBlob" +
// " " + action + " Failed");
// return false;
// }
// CBlob tCBlob = new CBlob();
if (!CBlob.BlobUpdate(ins, parmStrArr, conn))
{
CError.buildErr(this, "执行更新blob语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
ins.close();
System.out.println("tCBlob.BlobUpdate" +
" " + action + " Failed");
return false;
}
ins.close();
}
else if (action.equals("BLOBDELETE"))
{ //add by Alex at 2005.1.12
// System.out.println(SysConst.DBTYPE);
m = DBObject.getClass().getMethod("delete", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行删除语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
} //end of while
}
//数据提交:为保正事务一致性所有数据准备完毕后一次性提交.
if (commitFlag)
{
conn.commit();
conn.close();
conn = null;
// System.out.println("---End Committed---");
}
else
{
System.out.println(
"---End Datebase Operation, but not Commit in AutoBLS---");
}
}
catch (Exception e)
{
e.printStackTrace();
// @@错误处理
CError.buildErr(this, e.toString());
try {
conn.rollback();
} catch (Exception ex) { }
try {
conn.close();
} catch (Exception ex) { }
conn = null;
return false;
}
return true;
}
/**
* @auther hutongzhao 2009-04-22
* 功能:JDBC调用存储过程
* @param Connection 数据库连接对象
* @param procName 欲执行的存储过程
* @return Map 这个对象中有两个key。
* 一个是key=RESULT,记录执行存储过程返回的结果,RESULT="1"表示执行成功,RESULT="0"表示实行失败;
* 一个是key=MESSAGE,记录执行存储过程返回的成功或失败的详细信息;
* @throws SQLException
*/
public Map callProcedureByJdbc(Connection con,String procName) throws SQLException
{
Map map = new HashMap();
map.put("BOOLEAN", new Boolean(false));
CallableStatement callablestatement = null;
try{
System.out.println("procName = "+procName);
procName = "{call "+procName+" }";
//String procName = "{call PKG_SLIS_CS_DIVIDEND_EXTEND.p_process_dividend_surrender(?,?,?,?,?,?,?)}";
callablestatement = con.prepareCall(procName);
//欲执行package的过程最后两个参数为输出参数
callablestatement.registerOutParameter(1, java.sql.Types.VARCHAR);
callablestatement.registerOutParameter(2, java.sql.Types.VARCHAR);
System.out.println("executed procName = "+procName);
callablestatement.execute();
String result = callablestatement.getString(1);
String message = callablestatement.getString(2);
map.put("RESULT", result);
map.put("MESSAGE", message);
System.out.println("procure's result = "+result);
System.out.println("procure's message = "+message);
map.put("BOOLEAN", new Boolean(true));
}
catch(Exception ex ){
StringBuffer appString = new StringBuffer();
appString.append("异常信息:"+ex.getMessage());
appString.append("\n");
StackTraceElement elements[] = ex.getStackTrace();
for(int i = 0; i<elements.length; i++){
appString.append(elements[i].toString());
appString.append("\n");
}
System.out.println(appString.toString());
map.put("RESULT", "0");
map.put("MESSAGE", ex.getMessage());
}
finally{
if(callablestatement!=null){
callablestatement.close();
System.out.println("procedure CallableStatement colsed ");
}
}
return map;
}
/**
* 获取Blob操作所需信息公共方法
* @param o Object
* @param tSQL String
* @param pTabName String
* @param pUpdateField String
* @param pWhereSQL String
* @return String[]
*/
private static String[] getBlobInsertStr(Object o, String tSQL, String pTabName,
String pUpdateField, String pWhereSQL)
{
String aClassName = o.getClass().getName();
if (aClassName.endsWith("Schema"))
{
Schema s = (Schema) o;
String[] pk = s.getPK();
pTabName = aClassName.substring(aClassName.
lastIndexOf(".") + 1,
aClassName.lastIndexOf("S"));
//String ColPart = "( ";
String ValPart = "values(";
String mark = "'";
int nFieldCount = s.getFieldCount();
int jj = 0;
//------------------------------------------------add by tangsheng 2009-12-01------------------------------------------------
//此处之所以需要为insert语句增加col的顺序指定,是因为在保全批单合并需求中,为lpedorprint表增加了字段,字段顺序发生变化导致数据插入失败,于是增加了col的顺序指定
String ColPart = " ( ";
for(int i = 0;i<nFieldCount;i++){
if(i==0)
ColPart+=s.getFieldName(i);
else
ColPart+=","+s.getFieldName(i);
}
ColPart+=") ";
//---------------------------------------------------------end---------------------------------------------------------------
for (int i = 0; i < nFieldCount - 1; i++)
{ //只循环(nFieldCount-1)次,使blob字段不加入循环
String strFieldName = s.getFieldName(i);
String strFieldValue = s.getV(i);
for (int ii = 0; ii < pk.length; ii++)
{
if (strFieldName.equals(pk[ii]))
{
pWhereSQL += strFieldName + " = '" + strFieldValue +
"' and ";
}
}
int nFieldType = s.getFieldType(i);
boolean bFlag = false;
switch (nFieldType)
{
case Schema.TYPE_STRING:
case Schema.TYPE_DATE:
//TODO:需处理
if (!strFieldValue.equals("null"))
{
strFieldValue = mark + strFieldValue +
mark;
bFlag = true;
}else{
bFlag = true;
}
break;
case Schema.TYPE_DOUBLE:
case Schema.TYPE_FLOAT:
case Schema.TYPE_INT:
bFlag = true;
break;
default:
bFlag = false;
break;
}
if (bFlag)
{
jj++;
if (jj != 1)
{
//ColPart += ",";
ValPart += ",";
}
//ColPart += strFieldName;
ValPart += strFieldValue;
}
} // end of for
//ColPart += " )";
ValPart += ",";
ValPart += "empty_blob()"; //添加empty_blob
ValPart += ")";
tSQL = "insert into " + pTabName + " " + ColPart+
ValPart;
if (jj == 0)
{
tSQL = "";
}
pUpdateField = s.getFieldName(nFieldCount - 1);
if (pWhereSQL.lastIndexOf("and") != -1)
{
pWhereSQL = "and " +
pWhereSQL.substring(0, pWhereSQL.lastIndexOf("and"));
}
}
else
{
return null;
}
String[] returnStr = new String[4];
returnStr[0] = tSQL;
returnStr[1] = pTabName;
returnStr[2] = pUpdateField;
returnStr[3] = pWhereSQL;
return returnStr;
}
public static void main(String[] args)
{
PubSubmit pubSubmit1 = new PubSubmit();
// LPEdorPrintSchema tLPEdorPrintSchemaMain = new LPEdorPrintSchema();
// tLPEdorPrintSchemaMain.setEdorNo("410110001000065");
// tLPEdorPrintSchemaMain.setManageCom("86");
// tLPEdorPrintSchemaMain.setPrtFlag("N");
// tLPEdorPrintSchemaMain.setPrtTimes(0);
// tLPEdorPrintSchemaMain.setMakeDate(PubFun.getCurrentDate());
// tLPEdorPrintSchemaMain.setMakeTime(PubFun.getCurrentTime());
// tLPEdorPrintSchemaMain.setOperator("001");
// tLPEdorPrintSchemaMain.setModifyDate(PubFun.getCurrentDate());
// tLPEdorPrintSchemaMain.setModifyTime(PubFun.getCurrentTime());
// com.sinosoft.utility.XmlExport xmlexport = new com.sinosoft.utility.
// XmlExport();
// xmlexport.createDocument("", "");
// InputStream ins = xmlexport.getInputStream();
// tLPEdorPrintSchemaMain.setEdorInfo(ins);
VData mResult = new VData();//BLOBUPDATE
// MMap map = new MMap();//BLOBINSERT
// map.put(tLPEdorPrintSchemaMain, "BLOBDELETE");
// mResult.addElement(map);
// pubSubmit1.submitData(mResult, "");
MMap map = new MMap();//BLOBINSERT
//map.put("PKG_SLIS_CS_DIVIDEND_EXTEND.p_process_dividend_surrender('8051000000157618','0','8021000000001768',date'2009-04-24','admin',?,?)", "SLIS_PACKAGE_PROCEDURE_1");
// map.put("PKG_SLIS_CS_DIVIDEND_EXTEND.p_test('world',?,?)","SLIS_PACKAGE_PROCEDURE_test");
mResult.addElement(map);
pubSubmit1.submitData(mResult, "");
}
}
* Copyright (c) 2002 sinosoft Co. Ltd.
* All right reserved.
*/
package com.sinosoft.lis.pubfun;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.sinosoft.utility.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class PubSubmit
{
//传输数据类
private VData mInputData;
/** 错误处理类,每个需要错误处理的类中都放置该类 */
public CErrors mErrors = new CErrors();
/** 数据库连接 **/
private Connection conn = null;
/** 立即提交标志 **/
private final boolean commitFlag = true;
public PubSubmit()
{
}
/**
* 传输数据的公共方法
* 传入数据
* @param cInputData VData
* 数据操作标示
* 为了解决同时提交时的数据重复插入的问题,传入一个(select)sql如果查出数据则表示已经有数据,则报错退出
* @param cOperate String
* @return boolean
*/
public boolean submitData(VData cInputData, String cOperate)
{
//首先将数据在本类中做一个备份
mInputData = (VData) cInputData.clone();
if (!this.saveData())
{
return false;
}
mInputData = null;
return true;
}
/**
* 数据库操作
* @return: boolean
*/
private boolean saveData()
{
// System.out.println("---Start Save---");
//建立数据库连接
if (conn == null)
{
conn = DBConnPool.getConnection();
}
if (conn == null)
{
// @@错误处理
CError.buildErr(this, "数据库连接失败");
return false;
}
try
{
//开始事务,锁表
conn.setAutoCommit(false);
String action = ""; //操作方式,INSERT\UPDATE\DELETE
String className = ""; //类名
Object o = null; //Schema或Set对象
Object DBObject = null; //DB或DBSet对象
Method m = null; //方法
Constructor constructor = null; //构造函数
Class[] parameterC = new Class[1]; //调用方法的参数数组
Object[] parameterO = new Object[1]; //调用方法的对象数组
// System.out.println("mInputData.size():" + mInputData.size());
// System.out.println("mInputData :" + mInputData);
//通过MMap来传递每个Schema或Set的数据库操作方式,约定使用
MMap map = (MMap) mInputData.getObjectByObjectName("MMap", 0);
if (map != null && map.keySet().size() != 0)
{
Set set = map.keySet();
//Iterator iterator = map.keySet().iterator();
//while (iterator.hasNext()) {
for (int j = 0; j < set.size(); j++)
{
//获取操作对象Schema或Set或SQL
//o = iterator.next();
o = map.getOrder().get(String.valueOf(j + 1));
//获取操作方式
action = (String) map.get(o);
if (action == null)
{
continue;
}
// System.out.println("\n" + o.getClass().getName() +
// " Operate DB: " + action);
//构造相应的DB类名
className = o.getClass().getName();
// System.out.println("className :" + className);
// System.out.println("action :" + action);
if (className.endsWith("String"))
{
if (action.equals("UPDATE"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
if (!tExeSQL.execUpdateSQL(tSQL))
{
CError.buildErr(this, "执行更新语句失败");
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("DELETE"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
// System.out.println("执行SQL语句:" + tSQL);
if (!tExeSQL.execUpdateSQL(tSQL))
{
CError.buildErr(this, "执行删除语句失败");
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("INSERT"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
// System.out.println("执行SQL语句:" + tSQL);
if (!tExeSQL.execUpdateSQL(tSQL))
{
CError.buildErr(this,
"执行插入语句失败原因:" +
tExeSQL.mErrors.getError(0).
errorMessage);
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("SELECT")) //加入select方法,如果查出则表示有重复操作并报错退出
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") +
1, className.lastIndexOf("S")) +
"DB";
String tSQL = (String) o;
ExeSQL tExeSQL = new ExeSQL(conn);
SSRS tSSRS = new SSRS();
System.out.println("执行SQL语句:" + tSQL);
tSSRS = tExeSQL.execSQL(tSQL);
if (tSSRS.getMaxRow()>0)
{
CError.buildErr(this, "该操作已经执行,请检查数据!");
conn.rollback();
conn.close();
return false;
}
continue;
}
//*****************************************************************************
//begin added by hutongzhao 2009-04-21
if (action.startsWith("SLIS_PACKAGE_PROCEDURE")){
String procName = (String)o;
Map procMap = this.callProcedureByJdbc(conn,procName);
String result = (String)procMap.get("RESULT");
if(result!=null && !result.equals("1")){
CError.buildErr(this, "执行存储过程失败!"+procName);
CError.buildErr(this, "执行存储过程失败原因!"+procMap.get("MESSAGE"));
try
{
conn.rollback();
conn.close();
}catch (Exception ex){
StringBuffer appString = new StringBuffer();
appString.append("异常信息:"+ex.getMessage());
appString.append("\n");
StackTraceElement elements[] = ex.getStackTrace();
for(int i = 0; i<elements.length; i++){
appString.append(elements[i].toString());
appString.append("\n");
}
System.out.println(appString.toString());
}finally{
conn.close();
}
System.out.println("PubSubmet中执行存储过程失败!"+procName);
return false;
}
continue;
}//end added by hutongzhao
//********************************************************************************
}
//增加对MMap的处理,edit by liyudong
else if (className.endsWith("MMap"))
{
//获取存储SQL及变量的MMap
MMap tMMap = (MMap) o;
if(tMMap != null && tMMap.keySet().size() != 0){
Set mset = tMMap.keySet();
ExeSQL tExeSQL = new ExeSQL(conn);
//循环处理MMap
for(int mcount = 0; mcount < mset.size(); mcount++){
String keyClass = tMMap.getOrder().get(String.valueOf(mcount + 1)).getClass().getName();
System.out.println("keyClass:" + keyClass);
String tSQL = "";
String[] tvars = {};
if(keyClass.endsWith("String")){
tSQL = (String) tMMap.getOrder().get(String.valueOf(mcount + 1));
tvars = (String[]) tMMap.get(tSQL);
}else if(keyClass.endsWith("StringBuffer")){
StringBuffer tSQLBuffer = (StringBuffer)tMMap.getOrder().get(String.valueOf(mcount + 1));
tvars = (String[]) tMMap.get(tSQLBuffer);
tSQL = tSQLBuffer.toString();
}
if (!action.equals("SELECT"))
{
if (!tExeSQL.execUpdateSQL(tSQL,tvars))
{
CError.buildErr(this, "执行更新语句失败");
conn.rollback();
conn.close();
return false;
}
continue;
}
if (action.equals("SELECT"))
{
SSRS tSSRS = new SSRS();
System.out.println("执行SQL语句:" + tSQL);
tSSRS = tExeSQL.execSQL(tSQL,tvars);
if (tSSRS.getMaxRow()>0)
{
CError.buildErr(this, "该操作已经执行,请检查数据!");
conn.rollback();
conn.close();
return false;
}
continue;
}
}
continue;
}
}
else if (className.endsWith("Schema"))
{
className = "com.sinosoft.lis.db." +
className.
substring(className.lastIndexOf(".") + 1,
className.lastIndexOf("S")) +
"DB";
}
else if (className.endsWith("BLSet"))
{
className = "com.sinosoft.lis.vdb." +
className.
substring(className.lastIndexOf(".") + 1,
className.lastIndexOf("B")) +
"DBSet";
}
else if (className.endsWith("Set"))
{
className = "com.sinosoft.lis.vdb." +
className.
substring(className.lastIndexOf(".") + 1,
className.lastIndexOf("S")) +
"DBSet";
}
Class DBClass = Class.forName(className);
//选择构造函数,构造相同事务的DB或DBSet对象
parameterC[0] = Connection.class;
constructor = DBClass.getConstructor(parameterC);
parameterO[0] = conn;
DBObject = constructor.newInstance(parameterO);
//给DB对象付值,将传入的Schema或Set对象的内容复制到DB中
parameterC[0] = o.getClass();
if (o.getClass().getName().endsWith("Schema"))
{
m = DBObject.getClass().getMethod("setSchema",
parameterC);
}
else if (o.getClass().getName().endsWith("Set"))
{
m = DBObject.getClass().getMethod("set", parameterC);
}
parameterO[0] = o;
m.invoke(DBObject, parameterO);
//进行数据库操作
if (action.equals("INSERT"))
{
m = DBObject.getClass().getMethod("insert", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行插入语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("UPDATE"))
{
m = DBObject.getClass().getMethod("update", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行更新语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("DELETE"))
{
m = DBObject.getClass().getMethod("delete", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行删除语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("DELETE&INSERT"))
{
//DELETE
m = DBObject.getClass().getMethod("delete", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行删除,插入语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
//INSERT
m = DBObject.getClass().getMethod("insert", null);
b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行插入语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
else if (action.equals("BLOBINSERT"))
{ //add by Alex at 2005.1.12
//first,insert a record with a empty_blob , at the same time prepare the param for the second step
String tSQL = "";
//用于第二步update时的参数
String pWhereSQL = " and ";
String pTabName = "";
String pUpdateField = "";
String[] parmStrArr = getBlobInsertStr(o, tSQL,
pWhereSQL, pTabName,
pUpdateField);
// COracleBlob tCOracleBlob = new COracleBlob();
// if (!tCOracleBlob.InsertBlankBlobRecord(parmStrArr[0], conn)) {
// CError.buildErr(this, "执行插入blob语句失败");
// try {
// conn.rollback();
// } catch (Exception e) {}
// conn.close();
// System.out.println(
// "COracleBlob.InsertBlankBlobRecord" +
// " " + action + " Failed");
// return false;
// }
//second,using UpdateBlob method to update the empty_blob with a InputStream object from the schema
String aClassName = o.getClass().getName();
Schema s = (Schema) o;
m = o.getClass().getMethod("get" + parmStrArr[2], null);
InputStream ins = (InputStream) m.invoke(o, null);
// if (!tCOracleBlob.UpdateBlob(ins, parmStrArr[1],
// parmStrArr[2] , parmStrArr[3], conn)) {
// CError.buildErr(this, "执行更新blob语句失败");
// try {
// conn.rollback();
// } catch (Exception e) {}
// conn.close();
// ins.close();
// System.out.println("COracleBlob.UpdateBlob" +
// " " + action + " Failed");
// return false;
// }
// CBlob tCBlob = new CBlob();
if (!CBlob.BlobInsert(ins, parmStrArr, conn))
{
CError.buildErr(this, "执行更新blob语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
ins.close();
System.out.println("tCBlob.BlobInsert" +
" " + action + " Failed");
return false;
}
ins.close();
}
else if (action.equals("BLOBUPDATE"))
{ //add by Alex at 2005.1.12
//first,prepare the param for the UpdateBlob
String tSQL = "";
//用于第二步update时的参数
String pWhereSQL = " and ";
String pTabName = "";
String pUpdateField = "";
String[] parmStrArr = getBlobInsertStr(o, tSQL,
pWhereSQL, pTabName,
pUpdateField);
//second,using UpdateBlob method to update the blobfield with a InputStream object from the schema
// COracleBlob tCOracleBlob = new COracleBlob();
String aClassName = o.getClass().getName();
Schema s = (Schema) o;
m = o.getClass().getMethod("get" + parmStrArr[2], null);
InputStream ins = (InputStream) m.invoke(o, null);
// if (!tCOracleBlob.UpdateBlob(ins, parmStrArr[1],
// parmStrArr[2], parmStrArr[3], conn))
// {
// CError.buildErr(this, "执行更新blob语句失败");
// try
// {
// conn.rollback();
// }
// catch (Exception e)
// {}
// conn.close();
// ins.close();
// System.out.println("COracleBlob.UpdateBlob" +
// " " + action + " Failed");
// return false;
// }
// CBlob tCBlob = new CBlob();
if (!CBlob.BlobUpdate(ins, parmStrArr, conn))
{
CError.buildErr(this, "执行更新blob语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
ins.close();
System.out.println("tCBlob.BlobUpdate" +
" " + action + " Failed");
return false;
}
ins.close();
}
else if (action.equals("BLOBDELETE"))
{ //add by Alex at 2005.1.12
// System.out.println(SysConst.DBTYPE);
m = DBObject.getClass().getMethod("delete", null);
Boolean b = (Boolean) m.invoke(DBObject, null);
if (!b.booleanValue())
{
CError.buildErr(this, "执行删除语句失败");
try
{
conn.rollback();
}
catch (Exception e)
{}
conn.close();
System.out.println(DBObject.getClass().getName() +
" " + action + " Failed");
return false;
}
}
} //end of while
}
//数据提交:为保正事务一致性所有数据准备完毕后一次性提交.
if (commitFlag)
{
conn.commit();
conn.close();
conn = null;
// System.out.println("---End Committed---");
}
else
{
System.out.println(
"---End Datebase Operation, but not Commit in AutoBLS---");
}
}
catch (Exception e)
{
e.printStackTrace();
// @@错误处理
CError.buildErr(this, e.toString());
try {
conn.rollback();
} catch (Exception ex) { }
try {
conn.close();
} catch (Exception ex) { }
conn = null;
return false;
}
return true;
}
/**
* @auther hutongzhao 2009-04-22
* 功能:JDBC调用存储过程
* @param Connection 数据库连接对象
* @param procName 欲执行的存储过程
* @return Map 这个对象中有两个key。
* 一个是key=RESULT,记录执行存储过程返回的结果,RESULT="1"表示执行成功,RESULT="0"表示实行失败;
* 一个是key=MESSAGE,记录执行存储过程返回的成功或失败的详细信息;
* @throws SQLException
*/
public Map callProcedureByJdbc(Connection con,String procName) throws SQLException
{
Map map = new HashMap();
map.put("BOOLEAN", new Boolean(false));
CallableStatement callablestatement = null;
try{
System.out.println("procName = "+procName);
procName = "{call "+procName+" }";
//String procName = "{call PKG_SLIS_CS_DIVIDEND_EXTEND.p_process_dividend_surrender(?,?,?,?,?,?,?)}";
callablestatement = con.prepareCall(procName);
//欲执行package的过程最后两个参数为输出参数
callablestatement.registerOutParameter(1, java.sql.Types.VARCHAR);
callablestatement.registerOutParameter(2, java.sql.Types.VARCHAR);
System.out.println("executed procName = "+procName);
callablestatement.execute();
String result = callablestatement.getString(1);
String message = callablestatement.getString(2);
map.put("RESULT", result);
map.put("MESSAGE", message);
System.out.println("procure's result = "+result);
System.out.println("procure's message = "+message);
map.put("BOOLEAN", new Boolean(true));
}
catch(Exception ex ){
StringBuffer appString = new StringBuffer();
appString.append("异常信息:"+ex.getMessage());
appString.append("\n");
StackTraceElement elements[] = ex.getStackTrace();
for(int i = 0; i<elements.length; i++){
appString.append(elements[i].toString());
appString.append("\n");
}
System.out.println(appString.toString());
map.put("RESULT", "0");
map.put("MESSAGE", ex.getMessage());
}
finally{
if(callablestatement!=null){
callablestatement.close();
System.out.println("procedure CallableStatement colsed ");
}
}
return map;
}
/**
* 获取Blob操作所需信息公共方法
* @param o Object
* @param tSQL String
* @param pTabName String
* @param pUpdateField String
* @param pWhereSQL String
* @return String[]
*/
private static String[] getBlobInsertStr(Object o, String tSQL, String pTabName,
String pUpdateField, String pWhereSQL)
{
String aClassName = o.getClass().getName();
if (aClassName.endsWith("Schema"))
{
Schema s = (Schema) o;
String[] pk = s.getPK();
pTabName = aClassName.substring(aClassName.
lastIndexOf(".") + 1,
aClassName.lastIndexOf("S"));
//String ColPart = "( ";
String ValPart = "values(";
String mark = "'";
int nFieldCount = s.getFieldCount();
int jj = 0;
//------------------------------------------------add by tangsheng 2009-12-01------------------------------------------------
//此处之所以需要为insert语句增加col的顺序指定,是因为在保全批单合并需求中,为lpedorprint表增加了字段,字段顺序发生变化导致数据插入失败,于是增加了col的顺序指定
String ColPart = " ( ";
for(int i = 0;i<nFieldCount;i++){
if(i==0)
ColPart+=s.getFieldName(i);
else
ColPart+=","+s.getFieldName(i);
}
ColPart+=") ";
//---------------------------------------------------------end---------------------------------------------------------------
for (int i = 0; i < nFieldCount - 1; i++)
{ //只循环(nFieldCount-1)次,使blob字段不加入循环
String strFieldName = s.getFieldName(i);
String strFieldValue = s.getV(i);
for (int ii = 0; ii < pk.length; ii++)
{
if (strFieldName.equals(pk[ii]))
{
pWhereSQL += strFieldName + " = '" + strFieldValue +
"' and ";
}
}
int nFieldType = s.getFieldType(i);
boolean bFlag = false;
switch (nFieldType)
{
case Schema.TYPE_STRING:
case Schema.TYPE_DATE:
//TODO:需处理
if (!strFieldValue.equals("null"))
{
strFieldValue = mark + strFieldValue +
mark;
bFlag = true;
}else{
bFlag = true;
}
break;
case Schema.TYPE_DOUBLE:
case Schema.TYPE_FLOAT:
case Schema.TYPE_INT:
bFlag = true;
break;
default:
bFlag = false;
break;
}
if (bFlag)
{
jj++;
if (jj != 1)
{
//ColPart += ",";
ValPart += ",";
}
//ColPart += strFieldName;
ValPart += strFieldValue;
}
} // end of for
//ColPart += " )";
ValPart += ",";
ValPart += "empty_blob()"; //添加empty_blob
ValPart += ")";
tSQL = "insert into " + pTabName + " " + ColPart+
ValPart;
if (jj == 0)
{
tSQL = "";
}
pUpdateField = s.getFieldName(nFieldCount - 1);
if (pWhereSQL.lastIndexOf("and") != -1)
{
pWhereSQL = "and " +
pWhereSQL.substring(0, pWhereSQL.lastIndexOf("and"));
}
}
else
{
return null;
}
String[] returnStr = new String[4];
returnStr[0] = tSQL;
returnStr[1] = pTabName;
returnStr[2] = pUpdateField;
returnStr[3] = pWhereSQL;
return returnStr;
}
public static void main(String[] args)
{
PubSubmit pubSubmit1 = new PubSubmit();
// LPEdorPrintSchema tLPEdorPrintSchemaMain = new LPEdorPrintSchema();
// tLPEdorPrintSchemaMain.setEdorNo("410110001000065");
// tLPEdorPrintSchemaMain.setManageCom("86");
// tLPEdorPrintSchemaMain.setPrtFlag("N");
// tLPEdorPrintSchemaMain.setPrtTimes(0);
// tLPEdorPrintSchemaMain.setMakeDate(PubFun.getCurrentDate());
// tLPEdorPrintSchemaMain.setMakeTime(PubFun.getCurrentTime());
// tLPEdorPrintSchemaMain.setOperator("001");
// tLPEdorPrintSchemaMain.setModifyDate(PubFun.getCurrentDate());
// tLPEdorPrintSchemaMain.setModifyTime(PubFun.getCurrentTime());
// com.sinosoft.utility.XmlExport xmlexport = new com.sinosoft.utility.
// XmlExport();
// xmlexport.createDocument("", "");
// InputStream ins = xmlexport.getInputStream();
// tLPEdorPrintSchemaMain.setEdorInfo(ins);
VData mResult = new VData();//BLOBUPDATE
// MMap map = new MMap();//BLOBINSERT
// map.put(tLPEdorPrintSchemaMain, "BLOBDELETE");
// mResult.addElement(map);
// pubSubmit1.submitData(mResult, "");
MMap map = new MMap();//BLOBINSERT
//map.put("PKG_SLIS_CS_DIVIDEND_EXTEND.p_process_dividend_surrender('8051000000157618','0','8021000000001768',date'2009-04-24','admin',?,?)", "SLIS_PACKAGE_PROCEDURE_1");
// map.put("PKG_SLIS_CS_DIVIDEND_EXTEND.p_test('world',?,?)","SLIS_PACKAGE_PROCEDURE_test");
mResult.addElement(map);
pubSubmit1.submitData(mResult, "");
}
}