wfprocesscaseinfodao.java

来自「公司自己开发的工作流引擎」· Java 代码 · 共 224 行

JAVA
224
字号
package cn.com.iaspec.workflow.engine.dao;

import java.sql.*;
import java.util.*;
import org.apache.log4j.*;
import cn.com.iaspec.workflow.db.*;
import cn.com.iaspec.workflow.*;
import cn.com.iaspec.workflow.vo.db.*;

public class WfProcessCaseInfoDAO{
  private static Logger logger=Logger.getLogger(WfProcessCaseInfoDAO.class);
  public WfProcessCaseInfoDAO(){
  }

  /**
   * 查询LaProcessCaseInfo信息
   * @param info LaProcessCaseInfo
   * @throws SQLException
   * @return List
   */
  public List query(WfProcessCaseInfo info)
      throws SQLException{
    Connection conn=null;
    Statement stm=null;
    try{
      conn=WorkflowDBConnectionManager.getInstance().getConnection();
      stm=conn.createStatement();
      String sql=" select * from wf_process_case_info ";
      StringBuffer sbWhere=getQueryString(info);
      sql=sql+sbWhere.toString();
      logger.info("......query sql:"+sql);
      ResultSet rs=stm.executeQuery(sql);
      List list=this.resultsetToProcessCaseInfo(rs);
      return list;
    }
    finally{
      if(stm!=null){
        stm.close();
      }
      if(conn!=null){
        conn.close();
      }

    }
  }

  /**
   * 将list存贮的LaProcessCaseInfo信息,首先进行检查,如果数据库中不存在该记录,则往表wf_process_case_info插入记录
   * @param list List
   * @throws Exception
   */
  public void insert(List list)
      throws Exception{
    logger.info("begin insert(List list)...");
    if(list==null){
      return;
    }
    for(int i=0;i<list.size();i++){
      WfProcessCaseInfo info=(WfProcessCaseInfo)list.get(i);
      //检查是否已经插入记录
      if(!this.checkIfExist(info.getCaseRegisterId(),info.getProInstId())){
        insert(info);
      }
    }
  }

  /**
   * 往表wf_process_case_info插入记录
   * @param info LaProcessCaseInfo
   * @throws Exception
   */
  public void insert(WfProcessCaseInfo info)
      throws Exception{
    logger.info("begin insert(LaProcessCaseInfo info)...");
    Connection conn=null;
    Statement stm=null;
    try{
      conn=WorkflowDBConnectionManager.getInstance().getConnection();
      stm=conn.createStatement();
      StringBuffer sbInsertValue=this.getStrInsertValues(info);
      String sql=" insert into wf_process_case_info ("+this.getStrFields()+") "+
          " values ("+sbInsertValue.toString()+")";
      logger.info("......insert sql:"+sql);
      stm.execute(sql);
    }
    finally{
      WorkflowDBConnectionManager.getInstance().close(stm,false);
      WorkflowDBConnectionManager.getInstance().close(conn);
    }
  }

  /**
   * 检查是否在数据库中是否已经存在记录
   * @param registerId String
   * @param proInstId long
   * @throws SQLException
   * @return boolean
   */
  public boolean checkIfExist(String registerId,long proInstId)
      throws SQLException{
    WfProcessCaseInfo info=new WfProcessCaseInfo();
    info.setCaseRegisterId(registerId);
    info.setProInstId(proInstId);
    List list=this.query(info);
    if(list!=null&&list.size()>0){
      return true;
    }
    else{
      return false;
    }
  }

  private List resultsetToProcessCaseInfo(ResultSet rs)
      throws SQLException{
    List list=new ArrayList();
    while(rs.next()){
      WfProcessCaseInfo info=new WfProcessCaseInfo();
      info.setCaseRegisterId(rs.getString("case_register_id"));
      info.setFlowType(rs.getInt("flow_type"));
      info.setParentProInstId(rs.getLong("parent_pro_inst_id"));
      info.setProInstId(rs.getLong("pro_inst_id"));
      list.add(info);
    }
    return list;
  }

  /**
   * 取得查询条件
   * @param info LaProcessCaseInfo
   * @return StringBuffer
   */
  private StringBuffer getQueryString(WfProcessCaseInfo info){
    StringBuffer sb=new StringBuffer();
    if(info!=null){
      if(info.getCaseRegisterId()!=null&&!info.getCaseRegisterId().equals("")){
        sb.append(" case_register_id = '"+info.getCaseRegisterId()+"' ");
        sb.append(" and ");
      }
      if(info.getFlowType()!=WorkflowConstant.INT_INIT_VALUE){
        sb.append(" flow_type = "+info.getFlowType()+" ");
        sb.append(" and ");
      }
      if(info.getParentProInstId()!=WorkflowConstant.LONG_INIT_VALUE){
        sb.append(" parent_pro_inst_id = "+info.getParentProInstId()+" ");
        sb.append(" and ");
      }
      if(info.getProInstId()!=WorkflowConstant.LONG_INIT_VALUE){
        sb.append(" pro_inst_id = "+info.getProInstId()+" ");
        sb.append(" and ");
      }
    }
    if(sb.length()>0){
      //增加查询条件关键字
      sb.insert(0," where ");
      //去除最后一个" AND "操作符
      sb.delete(sb.length()-5,sb.length());
    }
    return sb;
  }

  /**
   * 得到本表内所有字段名,以","分开
   * @throws Exception
   * @return String
   */
  private String getStrFields()
      throws Exception{
    return "case_register_id,flow_type,parent_pro_inst_id,pro_inst_id";
  }

  /**
   * 根据info取得insert语句
   * @param info LaProcessCaseInfo
   * @throws Exception
   * @return StringBuffer
   */
  private StringBuffer getStrInsertValues(WfProcessCaseInfo info)
      throws Exception{
    StringBuffer sbSQL=new StringBuffer();
    if(info.getCaseRegisterId()!=null&&!info.getCaseRegisterId().equals("")){
      sbSQL.append("'");
      sbSQL.append(info.getCaseRegisterId());
      sbSQL.append("'");
      sbSQL.append(",");
    }
    else{
      sbSQL.append("null");
      sbSQL.append(",");
    }

    if(info.getFlowType()!=WorkflowConstant.INT_INIT_VALUE){
      sbSQL.append(info.getFlowType());
      sbSQL.append(",");
    }
    else{
      sbSQL.append("null");
      sbSQL.append(",");
    }

    if(info.getParentProInstId()!=WorkflowConstant.LONG_INIT_VALUE){
      sbSQL.append(info.getParentProInstId());
      sbSQL.append(",");
    }
    else{
      sbSQL.append("null");
      sbSQL.append(",");
    }

    if(info.getProInstId()!=WorkflowConstant.LONG_INIT_VALUE){
      sbSQL.append(info.getProInstId());
      sbSQL.append(",");
    }
    else{
      sbSQL.append("null");
      sbSQL.append(",");
    }

    //删除最后一个逗号
    sbSQL.delete(sbSQL.length()-1,sbSQL.length());
    return sbSQL;
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?