package com.augur.wf.shark.common.util;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.dbcp.BasicDataSource;
import com.augur.wf.shark.common.Exception.*;
import java.util.List;
import java.util.ArrayList;
import com.augur.wf.shark.common.Db.DbConnectionManager;
import com.augur.wf.shark.SharkProviderImpl;
import com.augurit.pysz.login.po.Usertable;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
import java.io.RandomAccessFile;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
/**
*
* @author Administrator
*
*/
public class CodeFormula {
public CodeFormula() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static final String poolName = "idb";
public static final int apply_code = 1;
public static final int get_code = 2;
public static final int putback_code =3 ;
public static final int refuse_code =4;
public static final int abort_code =5;
private static List getBeforeCode(String procId,String activityId,int codeType)throws WfException{
Connection con = DbConnectionManager.getInstance().getConnection(
poolName);
Statement st = null;
List codes = new ArrayList();
try {
st = con.createStatement();
String iSql = "select code from activitycode a,ASSIGNMENTEVENTAUDITS b where a.FIRETYPE=0 and a.procdefid=b.PROCESSDEFINITIONID and a.actdefid=ACTIVITYDEFINITIONID and b.PROCESSID='"+procId+"' and b.ACTIVITYID='"+activityId+"' and codetype='"+codeType+"'";
System.out.println(iSql);
ResultSet rs = st.executeQuery(iSql);
if (rs.next()) {
codes.add(rs.getString(1));
}
} catch (Exception e) {
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"获取节点代码失败!"+activityId,
ErrorInfo.getTrace(e, e.getStackTrace()));
} finally {
DbConnectionManager.getInstance().freeConnection(poolName, con);
}
return codes;
}
private static List getAfterCode(String procId,String activityId,int codeType)throws WfException{
Connection con = DbConnectionManager.getInstance().getConnection(
poolName);
Statement st = null;
List codes = new ArrayList();
try {
st = con.createStatement();
String iSql = "select distinct code from activitycode a,ASSIGNMENTEVENTAUDITS b where a.FIRETYPE=1 and a.procdefid=b.PROCESSDEFINITIONID and a.actdefid=ACTIVITYDEFINITIONID and b.PROCESSID='"+procId+"' and b.ACTIVITYID='"+activityId+"' and codetype='"+codeType+"'";
System.out.println(iSql);
ResultSet rs = st.executeQuery(iSql);
if (rs.next()) {
codes.add(rs.getString(1));
}
} catch (Exception e) {
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"获取节点代码失败!"+activityId,
ErrorInfo.getTrace(e, e.getStackTrace()));
} finally {
DbConnectionManager.getInstance().freeConnection(poolName, con);
}
return codes;
}
private static void updValue(String formId,String tableName,String fieldName,String value)throws WfException{
Connection con = DbConnectionManager.getInstance().getConnection(
poolName);
Statement st = null;
try {
st = con.createStatement();
String iSql = "update "+tableName+" set "+fieldName+"='"+value+"' where formid='"+formId+"'";
System.out.println(iSql);
st.execute(iSql);
} catch (Exception e) {
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"更新代码失败!",
ErrorInfo.getTrace(e, e.getStackTrace()));
} finally {
DbConnectionManager.getInstance().freeConnection(poolName, con);
}
}
private static String getValue(String formId,String tableName,String fieldName)throws WfException{
Connection con = DbConnectionManager.getInstance().getConnection(
poolName);
Statement st = null;
String value = null;
try {
st = con.createStatement();
String iSql = "select "+fieldName+" from "+tableName+" where applyid='"+formId+"'";
ResultSet rs = st.executeQuery(iSql);
if (rs.next()) {
value = rs.getString(1);
}
} catch (Exception e) {
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"获取值失败!",
ErrorInfo.getTrace(e, e.getStackTrace()));
} finally {
DbConnectionManager.getInstance().freeConnection(poolName, con);
}
return value;
}
//private static void get
private static void parseCode(String procId,String activityId,String code,HttpServletRequest request) throws WfException{
try{
String funName = code.substring(0, 6);
///设置流程变量代码解释 setVal opinion view.opinion or setVal opinion db.table1.opinion
if ("setVal".equals(funName)) {
String[] strs = code.split(" ");
String valName = strs[1].trim(); //代码前部分-流程变量名
String valValue = strs[2].trim(); //代码后部分
String[] valueDefs = valValue.split("\\."); //解释代码后部分
String dataDefType =valueDefs[0]; //数据来源类型 view为页面,data 为数据库
String defFieldName = null;
String value = null;
//从页面获取数据才设置变量 格式: setVal
if ("view".equals(dataDefType)) {
defFieldName = valueDefs[1];
value = request.getParameter(
defFieldName);
}
//从数据库中获取数据才设置变量����ȡ
else if ("data".equals(dataDefType)) {
String tableName = valueDefs[1];
defFieldName = valueDefs[2];
String formId = ExProcessManager.getExprocessRelaDataId(
procId);
value = getValue(formId,tableName,defFieldName);
}
else if("cons".equals(dataDefType)){
value = valueDefs[1];
}
Usertable user =(Usertable) request.getSession().getAttribute("login_user");
SharkProviderImpl.variableSet(SharkProviderImpl.connect(user.getUserid(),user.getPasswd()), activityId, valName,
value);
}
if("updFld".equals(funName)){ //updFld tablea.jiaofei data.tableb.jiaofei
String[] strs = code.split(" ");
String valName = strs[1].trim(); //代码前部分-流程变量名
String valValue = strs[2].trim(); //代码后部分
String[] fldDef = valName.split("\\.");
String[] valueDefs = valValue.split("\\."); //解释代码后部分
String dataDefType =valueDefs[0]; //数据来源类型 view为页面,data 为数据库
String defFieldName = null;
String value = null;
String formId = ExProcessManager.getExprocessRelaDataId(
procId);
//从页面获取数据才设置变量 格式: setVal
if ("view".equals(dataDefType)) {
defFieldName = valueDefs[1];
value = request.getParameter(
defFieldName);
}
//从数据库中获取数据才设置变量����ȡ
else if ("data".equals(dataDefType)) {
String tableName = valueDefs[1];
defFieldName = valueDefs[2];
value = getValue(formId,tableName,defFieldName);
}
else if("cons".equals(dataDefType)){
value = valueDefs[1];
}
updValue(formId,fldDef[0],fldDef[1],value);
}
}
catch(Exception e){
e.printStackTrace();
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"解释代码失败!"+code,
ErrorInfo.getTrace(e, e.getStackTrace()));
}
}
public static void parseBeforeCode(String procId,String activityId,int codeType,HttpServletRequest request)throws WfException{
try{
List codes = getBeforeCode(procId,activityId,codeType);
for(int i=0;i<codes.size();i++){
parseCode(procId,activityId,codes.get(i).toString(),request);
}
}
catch(WfException e){
throw e;
}
catch(Exception e1){
throw new WfException();
}
}
public static void parseAfterCode(String procId,String activityId,int codeType,HttpServletRequest request)throws WfException{
try{
List codes = getAfterCode(procId,activityId,codeType);
for(int i=0;i<codes.size();i++){
parseCode(procId,activityId,codes.get(i).toString(),request);
}
}
catch(WfException e){
throw e;
}
catch(Exception e1){
throw new WfException();
}
}
public static String limitControls(String procdefId,String activityId,String url)throws WfException{
Connection con = DbConnectionManager.getInstance().getConnection(
poolName);
Statement st = null;
String value="";
try {
st = con.createStatement();
String iSql = "";
///
if(activityId!=null&&!"null".equals(activityId)){
iSql = "select distinct a.controltype,a.controlname,a.viewtype,c.formname from activitycontrollimits a,activitycontrol c,ASSIGNMENTEVENTAUDITS b where a.limitno=c.limitno and c.actdefid=b.activitydefinitionid and b.ACTIVITYID='"+activityId+"' and url='"+url+"'" ;
}
///进去发起页面 activityid is null
else
iSql = "select distinct a.controltype,a.controlname,a.viewtype,c.formname from activitycontrollimits a,activitycontrol c where a.limitno=c.limitno and procdefid='"+procdefId+"' and actdefid='null' and url='"+url+"'" ;
System.out.println(iSql);
ResultSet rs = st.executeQuery(iSql);
while (rs.next()) {
int cType = rs.getInt(3);
switch(cType){
case 0:value = value+"document.all['"+rs.getString(2)+"'].disabled=true;";
break;
case 1:
if(rs.getInt(1)==3)
value = value="self.location='commons/noallow.jsp'";
else
value = value+"document.all['"+rs.getString(2)+"'].style.display='none';";
break;
//case 2:value= value+"document.all['"+rs.getString(2)+"'].style.display='block';";
// break;
default:
value = value+"document.all['"+rs.getString(2)+"'].disabled=false;";
break;
}
}
// if(value.trim().length()==0)
// value="self.location='/commons/noallow.jsp'";
} catch (Exception e) {
e.printStackTrace();
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"生成校验串失败!",
ErrorInfo.getTrace(e, e.getStackTrace()));
} finally {
DbConnectionManager.getInstance().freeConnection(poolName, con);
}
value= "<script language='javascript'>"+value;
value = value+"</script>";
return value;
}
public static String parseClientControl(String activityId) throws WfException{
Connection con = DbConnectionManager.getInstance().getConnection(
poolName);
Statement st = null;
String value= "";
try {
st = con.createStatement();
String iSql = "select distinct a.procdefid,a.actdefid,a.controltype,a.name,a.value,a.label,a.todefid from activitycodecontrol a,ASSIGNMENTEVENTAUDITS b where a.actdefid=b.activitydefinitionid and b.ACTIVITYID='"+activityId+"'" ;
System.out.println(iSql);
ResultSet rs = st.executeQuery(iSql);
while (rs.next()) {
int cType = rs.getInt(3);
switch(cType){
case 0:value = value+"<input type='checkbox' name='"+rs.getString(4)+"' value='"+rs.getString(5)+"'>"+rs.getString(6);
break;
case 1:value = value+rs.getString(6)+"<input type='text' name='"+rs.getString(4)+"' value='"+rs.getString(5)+"'>";
break;
case 2:value= value+"<input type='radio' onclick=getUsers('"+rs.getString(7)+"') touserid='"+rs.getString(7)+"' name='"+rs.getString(4)+"' value='"+rs.getString(5)+"'>"+rs.getString(6);
break;
default:
value = value+"<input type='checkbox' onclick=getUsers('"+rs.getString(7)+"') touserid='"+rs.getString(7)+"' name='"+rs.getString(4)+"' value='"+rs.getString(5)+"'>"+rs.getString(5);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new WfException(ErrorInfo.EXT_PROC_MANAGER_ERROR,"解释客户端变量失败!",
ErrorInfo.getTrace(e, e.getStackTrace()));
} finally {
DbConnectionManager.getInstance().freeConnection(poolName, con);
}
return value;
}
public static void main(String[] args){
try{
// CodeFormula.parseCode(null,null,"setVal laid data.tablea.filed", null);
try{
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.gjt.mm.mysql.Driver");
ds.setUrl("jdbc:mysql://localhost/sample");
ds.setUsername("root");
ds.setPassword("dddd");
ds.setInitialSize(2);
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial","org.apache.naming.java.javaURLContextFactory");
InitialContext ctx=new InitialContext();
ctx.bind("tomcatDS",ds);
ds = (BasicDataSource)ctx.lookup("tomcatDS");
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
String strSql = "select * from user";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
System.out.println("id="+rs.getString(1));
}
rs.close();
stmt.close();
con.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
private void jbInit() throws Exception {
}
}
解析数据库表上的Code字段的值样式:
updFld exprocess.BUSSSTATE cons.12001
setVal outopin cont.test