一个用数据库实现的工作流

类别:Java 点击:0 评论:0 推荐:

package com.highcom.workflow.dao.jdbc;

import org.springframework.dao.*;

import org.springframework.jdbc.core.*;
import org.springframework.jdbc.core.support.*;

import java.sql.*;

import java.util.*;
import com.highcom.workflow.domain.*;
import com.highcom.workflow.dao.*;
import com.highcom.seqgen.*;


public class WorkflowDaoJdbcImpl extends JdbcDaoSupport implements WorkflowDao {
    private SequenceService sequenceService = null;
    private String WORKFLOW_TEMPLATE_SEQ =
            "com.highcom.workflow.template";
    private String WORKFLOW_TEMPLATE_NODE_SEQ =
            "com.highcom.workflow.template.node";
    private String WORKFLOW_TEMPLATE_NODE_MAN_SEQ =
            "com.highcom.workflow.template.node.man";
    //
    private String WORKFLOW_SEQ =
            "com.highcom.workflow";
    private String WORKFLOW_NODE_SEQ =
            "com.highcom.workflow.node";
    private String WORKFLOW_NODE_MAN_SEQ =
            "com.highcom.workflow.node.man";
    //
    public WorkflowDaoJdbcImpl() {
    }

    /**
     * 新建立一个工作流模板
     * @param template WorkflowTemplate
     */
    public void addNewTemplate(WorkflowTemplate template) {
        String id = sequenceService.getValue(WORKFLOW_TEMPLATE_SEQ);
        this.getJdbcTemplate().update("insert into workflow_template(id,name,description,createDate,createMan,status,defaultworkflow) values(?,?,?,?,?,?,?)",
                                      new Object[] {
                                      id, template.getName(),
                                      template.getDescription(),
                                      template.getCreateDate(),
                                      template.getCreateMan(),
                                      new Integer(template.getStatus()),
                                      new Integer(template.getDefaultWorkflow())
        },
                new int[] {
                Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE,
                Types.VARCHAR, Types.INTEGER, Types.INTEGER
        });
    }

    public void addNewTemplateNode(WorkflowTemplateNode flowNode) {
        String id = this.sequenceService.getValue(this.
                                                  WORKFLOW_TEMPLATE_NODE_SEQ);
        int maxseq = getTemplateNodeMaxSequence(flowNode.getWorkId());
        maxseq += 1;
        this.getJdbcTemplate().update("insert into workflow_template_flow(id,template_id,sequence,name,description) values(?,?,?,?,?)",
                                      new Object[] {
                                      id, flowNode.getWorkId(),
                                      new Integer(maxseq),
                                      flowNode.getName(),
                                      flowNode.getDescription()
        },
                new int[] {
                Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
                Types.VARCHAR
        });

        //插入人员信息
        if (flowNode.getMans() != null) {
            List mans = flowNode.getMans();

            for (int i = 0; i < mans.size(); i++) {
                String man_id = this.sequenceService.getValue(this.
                        WORKFLOW_TEMPLATE_NODE_MAN_SEQ);
                String account_id = (String) mans.get(i);
                this.getJdbcTemplate().update("insert into workflow_template_man(id,template_flow_id,account_id) values(?,?,?)",
                                              new Object[] {man_id, id,
                                              account_id},
                                              new int[] {Types.VARCHAR,
                                              Types.VARCHAR,
                                              Types.VARCHAR});
            }
        }
    }

    /**
     * 生成新的工作流实例 调用程序需要本身设置这些信息 1)本身信息 2)结点信息
     * 3)结点人员信息
     * status:-1,待启动,-2:强制终止,-3:退回到原始状态(需要修改),0:正在运行,1:已经完成.
     * flow_id:当前运行结点id.-9999:已经没有当前结点.
     *
     * @param workflow Workflow
     * @return String
     */
    public String addNewWorkflow(Workflow workflow) {
        //
        String id = sequenceService.getValue(WORKFLOW_SEQ);
        this.getJdbcTemplate().update(
                "insert into workflow_work(id,doc_id,status," +
                "flow_id,start_date,end_date,v_comment) values(?,?,?,?,?,?,?)",
                new Object[] {
                id, workflow.getDocId(),
                new Integer(workflow.getStatus()),
                workflow.getFlowId(), workflow.getStartDate(),
                workflow.getEndDate(),
                workflow.getComment()},
                new int[] {
                Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
                Types.VARCHAR, Types.DATE, Types.DATE,
                Types.VARCHAR});
        //插入结点信息,由结点插入来插入人员信息
        List nodes = workflow.getWorkflowNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.size(); i++) {
                WorkflowNode node = (WorkflowNode) nodes.get(i);
                addNewWorkflowNode(node);
            }

        }
        return id;
    }

    /**
     * 通过某一个流程结点
     * 1)除了更新此流程的状态以外,还需要查看是否是最后一个结点
     * 如果是最后一个结点,则完成此流程.
     * 否则,更新流程状态为下一个结点,启动下一个结点
     * @param updateWorkFlowNode WorkflowNode
     * @param man_id String
     */
    public void approveWorkFlowNode(WorkflowNode updateWorkFlowNode,
                                    String man_id) {
        //需要更新的工作流结点
        WorkflowNode wfNode = updateWorkFlowNode;
        String wf_node_id = wfNode.getId();

        //更新结点
        updateWorkflowNode(wf_node_id, wfNode);
        //得到应该运行的下一个结点
        WorkflowNode shouldRunNode = getAfterShouldRunningWorkflowNode(wfNode.
                getWorkId(), wfNode.getSequence());
        //
        if (shouldRunNode == null) {
            //已经没有下一个结点,工作流应该完成
            Workflow wf = this.getWorkflowById(wfNode.getWorkId());
            //
            wf.setComment("正常完成");
            wf.setStatus(Workflow.WORKFLOW_STATUS_FINISHED);
            java.sql.Timestamp endDate = new Timestamp(System.currentTimeMillis());
            wf.setEndDate(endDate);
            //

            //更新工作流为完成状态
            updateWorkflow(wf.getId(), wf);
        } else {
            //还存在下一个结点,工作流应该转移到下一个结点
            Workflow wf = this.getWorkflowById(wfNode.getWorkId());
            //设置工作流当前结点
            wf.setFlowId(shouldRunNode.getId());
            //更新当前结点为运行状态
            shouldRunNode.setStatus(WorkflowNode.WORKFLOW_NODE_STATUS_RUNNING);
            java.sql.Timestamp startDate = new Timestamp(System.
                    currentTimeMillis());
            shouldRunNode.setStartDate(startDate);
            //更新结点
            updateWorkflowNode(shouldRunNode.getId(), shouldRunNode);
            //更新工作流
            updateWorkflow(wf.getId(), wf);
        }
    }

    /**
     * 拒绝通过某一个结点
     * @param updateWorkFlowNode WorkflowNode
     * @param man_id String
     */
    public void declineWorkFlowNode(WorkflowNode updateWorkFlowNode,
                                    String man_id) {
        WorkflowNode wfNode = updateWorkFlowNode;
        String wf_node_id = wfNode.getId();
        //更新结点
        updateWorkflowNode(wf_node_id, wfNode);
        //
        WorkflowNode shouldRunNode = getBeforeShouldRunningWorkflowNode(wfNode.
                getWorkId(), wfNode.getSequence());
        if (shouldRunNode == null) {
            //已经退回到编辑状态
            Workflow wf = this.getWorkflowById(wfNode.getWorkId());
            //
            wf.setComment("重新编辑");
            //标识工作流为退回编辑状态
            wf.setStatus(Workflow.WORKFLOW_STATUS_BACKED);
            updateWorkflow(wf.getId(), wf);
        } else {
            //退回到前一个结点
            Workflow wf = this.getWorkflowById(wfNode.getWorkId());
            wf.setFlowId(shouldRunNode.getId());
            //-2表示这个结点是因为下一个结点没有通过而返回的
            //重审'[批结点
            shouldRunNode.setStatus(WorkflowNode.WORKFLOW_NODE_STATUS_BACKED);
            //
            java.sql.Timestamp startDate = new Timestamp(System.
                    currentTimeMillis());
            shouldRunNode.setStartDate(startDate);
            updateWorkflowNode(shouldRunNode.getId(), shouldRunNode);
            //
            updateWorkflow(wf.getId(), wf);
        }

    }

    /**
     * 删除与指定结点关联的所有人员记录
     * @param nodeId String
     */
    public void deleteMansOfNode(String nodeId) {
        this.getJdbcTemplate().update(
                "delete from workflow_template_man where template_flow_id=?",
                new Object[] {nodeId}, new int[] {Types.VARCHAR});
    }

    /**
     * 删除工作流模版,将一同删除结点与结点的人员信息记录
     * @param id String
     */
    public void deleteTemplate(String id) {
        this.getJdbcTemplate().update(
                "delete from workflow_template where id=?",
                new Object[] {id}, new int[] {Types.VARCHAR});
        deleteTemplateNodesOfTemplate(id);
    }

    public void deleteTemplateNode(String id) {
        ////删除结点需要调整序号
        WorkflowTemplateNode templateNode = this.getTemplateNodeById(id);
        String template_id = templateNode.getWorkId();
        int maxseq = getTemplateNodeMaxSequence(template_id);
        int currentId = templateNode.getSequence();
        if (currentId < maxseq) {
            this.getJdbcTemplate().update("update workflow_template_flow set sequence=sequence-1 where template_id=? and sequence>?",
                                          new Object[] {template_id,
                                          new Integer(currentId)},
                                          new int[] {Types.VARCHAR,
                                          Types.INTEGER});
        }
        //
        this.deleteMansOfNode(id);
        this.getJdbcTemplate().update(
                "delete from workflow_template_flow where id=?",
                new Object[] {id},
                new int[] {Types.VARCHAR});

    }

    public void deleteTemplateNodesOfTemplate(String template_id) {
        WorkflowTemplateNode[] nodes = this.getTemplateNodesOfTemplate(
                template_id);

        if (nodes != null) {
            for (int i = 0; i < nodes.length; i++) {
                WorkflowTemplateNode node = nodes[i];
                deleteMansOfNode(node.getId());
            }
        }

        this.getJdbcTemplate().update(
                "delete from workflow_template_flow where template_id=?",
                new Object[] {template_id}, new int[] {Types.VARCHAR});
    }

    /**
     * 删除工作流实例
     * @param id String
     */
    public void deleteWorkflow(String id) {
        this.getJdbcTemplate().update("delete from workflow_work where id=?",
                                      new Object[] {id},
                                      new int[] {Types.VARCHAR});
        //
        WorkflowNode[] wfNodes = this.getWorkflowNodesOfWork(id);
        if (wfNodes != null) {
            for (int i = 0; i < wfNodes.length; i++) {
                this.deleteWorkflowNode(wfNodes[i].getId());
            }
        }
    }

    public void finishWorkFlow(String id) {
    }

    public String[] getAccountIdsOfTempalteNode(String template_node_id) {
        List mans = this.getTemplateNodeMans(template_node_id);

        if (mans != null) {
            return (String[]) mans.toArray(new String[mans.size()]);
        }

        return null;
    }

    public WorkflowNode[] getAllCurrentWorkflowNode() {
        return null;
    }

    public WorkflowTemplate[] getAllTemplates(int flag) {
        if ((flag != -1) && (flag != 0) && (flag != 1)) {
            return null;
        }

        List list = null;

        if (flag != -1) {
            list = this.getJdbcTemplate().query(
                    "select * from workflow_template where status=? order by createDate",
                    new Object[] {new Integer(flag)},
                    new int[] {Types.INTEGER},
                    new RowMapper() {
                public Object mapRow(ResultSet rs, int _int) throws
                        SQLException {
                    WorkflowTemplate temp = new WorkflowTemplate();
                    temp.setId(rs.getString("id"));
                    temp.setName(rs.getString("name"));
                    temp.setDescription(rs.getString("description"));
                    temp.setCreateMan(rs.getString("createMan"));
                    temp.setCreateDate(rs.getTimestamp("createDate"));
                    temp.setStatus(rs.getInt("status"));
                    return temp;
                }
            });
        } else {
            list = this.getJdbcTemplate().query(
                    "select * from workflow_template order by createDate",
                    new RowMapper() {
                public Object mapRow(ResultSet rs, int _int) throws
                        SQLException {
                    WorkflowTemplate temp = new WorkflowTemplate();
                    temp.setId(rs.getString("id"));
                    temp.setName(rs.getString("name"));
                    temp.setDescription(rs.getString("description"));
                    temp.setCreateMan(rs.getString("createMan"));
                    temp.setCreateDate(rs.getTimestamp("createDate"));
                    temp.setStatus(rs.getInt("status"));

                    return temp;
                }
            });
        }

        if ((list != null) && (list.size() > 0)) {
            return (WorkflowTemplate[]) list.toArray(new WorkflowTemplate[list.
                    size()]);
        }

        return null;
    }

    /**
     * 得到指定工作流的当前结点
     * @param work_id String
     * @return WorkflowNode
     */
    public WorkflowNode getCurrentNode(String work_id) {
        Object obj = this.getJdbcTemplate().query(
                "select flow_id from workflow_work where id=?",
                new Object[] {work_id},
                new int[] {Types.VARCHAR},
                new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    return rs.getString("flow_id");
                }
                return null;
            }
        });
        if (obj != null) {
            String flow_id = (String) obj;
            //
            return this.getWorkflowNodeById(flow_id);
        }
        return null;
    }

    public WorkflowNode getCurrentNode(Workflow workFlow) {
        return null;
    }

    public WorkflowNode[] getCurrentWorkflowNodeByMan(String mam_id) {
        return null;
    }

    public WorkflowTemplate getTemplateById(String id) {
        Object obj = this.getJdbcTemplate().query(
                "select * from workflow_template where id=?",
                new Object[] {id}, new int[] {Types.VARCHAR},
                new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    WorkflowTemplate temp = new WorkflowTemplate();
                    temp.setId(rs.getString("id"));
                    temp.setName(rs.getString("name"));
                    temp.setDescription(rs.getString("description"));
                    temp.setCreateMan(rs.getString("createMan"));
                    temp.setCreateDate(rs.getTimestamp("createDate"));
                    temp.setStatus(rs.getInt("status"));

                    return temp;
                }

                return null;
            }
        });

        if (obj != null) {
            return (WorkflowTemplate) obj;
        }

        return null;
    }

    public WorkflowTemplate getTemplateByName(String name) {
        return null;
    }

    public WorkflowTemplateNode getTemplateNodeById(String id) {
        Object obj = this.getJdbcTemplate().query(
                "select * from workflow_template_flow where id=?",
                new Object[] {id}, new int[] {Types.VARCHAR},
                new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    WorkflowTemplateNode wfnode = new WorkflowTemplateNode();
                    wfnode.setId(rs.getString("id"));
                    wfnode.setName(rs.getString("name"));
                    wfnode.setDescription(rs.getString("description"));
                    wfnode.setWorkId(rs.getString("template_id"));
                    wfnode.setSequence(rs.getInt("sequence"));

                    return wfnode;
                }

                return null;
            }
        });

        if (obj != null) {
            return (WorkflowTemplateNode) obj;
        }

        return null;
    }

    public WorkflowTemplateNode getTemplateNodeByName(String name) {
        return null;
    }

    public List getTemplateNodeMans(String template_flow_id) {
        List list = this.getJdbcTemplate().query(
                "select account_id from workflow_template_man where template_flow_id=? ",
                new Object[] {template_flow_id}, new int[] {Types.VARCHAR},
                new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {
                return rs.getString("account_id");
            }
        });

        return list;
    }

    public List getWorkflowNodeMans(String work_flow_id) {
        List list = this.getJdbcTemplate().query(
                "select account_id from workflow_man where flow_id=? ",
                new Object[] {work_flow_id}, new int[] {Types.VARCHAR},
                new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {
                return rs.getString("account_id");
            }
        });

        return list;
    }

    public int getTemplateNodeMaxSequence(String template_id) {
        Object obj = this.getJdbcTemplate().query("select max(sequence) as maxsequence from workflow_template_flow where template_id=?",
                                                  new Object[] {template_id},
                                                  new int[] {Types.VARCHAR},
                                                  new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    int maxnum = rs.getInt("maxsequence");

                    return new Integer(maxnum);
                }

                return new Integer(0);
            }
        });

        return ((Integer) obj).intValue();
    }

    public WorkflowTemplateNode[] getTemplateNodesOfTemplate(String template_id) {
        List list = this.getJdbcTemplate().query(
                "select * from workflow_template_flow where template_id=? order by sequence",
                new Object[] {template_id}, new int[] {Types.VARCHAR},
                new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {
                WorkflowTemplateNode tnode = new WorkflowTemplateNode();
                tnode.setId(rs.getString("id"));
                tnode.setDescription(rs.getString("description"));
                tnode.setName(rs.getString("name"));
                tnode.setSequence(rs.getInt("sequence"));
                tnode.setWorkId(rs.getString("template_id"));

                return tnode;
            }
        });

        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                WorkflowTemplateNode tnode = (WorkflowTemplateNode) list.get(i);
                List mans = this.getTemplateNodeMans(tnode.getId());
                tnode.setMans(mans);
            }

            return (WorkflowTemplateNode[]) list.toArray(new
                    WorkflowTemplateNode[
                    list.size()]);
        }

        return null;
    }

    public Workflow getWorkflowById(String id) {
        Object obj = this.getJdbcTemplate().query("select id,doc_id,status," +
                                                  "flow_id,start_date,end_date,comment from workflow_work where id=?",
                                                  new Object[] {id},
                                                  new int[] {Types.VARCHAR},
                                                  new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    Workflow wf = new Workflow();
                    wf.setId(rs.getString("id"));
                    wf.setDocId(rs.getString("doc_id"));
                    wf.setStatus(rs.getInt("status"));
                    wf.setComment(rs.getString("comment"));
                    wf.setStartDate(rs.getTimestamp("start_date"));
                    wf.setEndDate(rs.getTimestamp("end_date"));
                    wf.setFlowId(rs.getString("flow_id"));
                    return wf;
                }
                return null;
            }
        });
        if (obj != null) {
            return (Workflow) obj;
        }
        return null;
    }

    public Workflow getWorkflowByName(String id) {
        return null;
    }

    /**
     * 根据状态得到工作流
     * @param status int
     * @return Workflow[]
     */
    public Workflow[] getWorkflowByStatus(int status) {
        List list = this.getJdbcTemplate().query("select id,doc_id,status," +
                                                 "flow_id,start_date,end_date,comment from workflow_work where status=? order by start_date desc",
                                                 new Object[] {new Integer(
                status)},
                                                 new int[] {Types.INTEGER},
                                                 new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {
                Workflow wf = new Workflow();
                wf.setId(rs.getString("id"));
                wf.setDocId(rs.getString("doc_id"));
                wf.setStatus(rs.getInt("status"));
                wf.setComment(rs.getString("comment"));
                wf.setStartDate(rs.getTimestamp("start_date"));
                wf.setEndDate(rs.getTimestamp("end_date"));
                wf.setFlowId(rs.getString("flow_id"));
                return wf;

            }
        });
        if (list != null && list.size() > 0) {
            return (Workflow[]) list.toArray(new Workflow[list.size()]);
        }
        return null;
    }

    public void setSequenceService(SequenceService sequenceService) {
        this.sequenceService = sequenceService;
    }

    public void terminateWorkFlow(Workflow workFlow) {
    }

    public void updateTemplate(String id, WorkflowTemplate template) {
        this.getJdbcTemplate().update(
                "update workflow_template set name=?,description=?,status=? where id=?",
                new Object[] {
                template.getName(), template.getDescription(),
                new Integer(template.getStatus()), id
        },
                new int[] {
                Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR});
    }

    public void updateTemplateNode(String id, WorkflowTemplateNode flowNode) {
        this.getJdbcTemplate().update(
                "update workflow_template_flow set name=?,description=?,sequence=? Where id=?",
                new Object[] {
                flowNode.getName(), flowNode.getDescription(),
                new Integer(flowNode.getSequence()), id
        },
                new int[] {
                Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR});
    }

    /**
     * 更新工作流
     * @param id String
     * @param workflow Workflow
     */
    public void updateWorkflow(String id, Workflow workflow) {
        this.getJdbcTemplate().update(
                "update workflow_work set doc_id=?,status=?," +
                "flow_id=?,start_date=?,end_date=?,comment=? from workflow_work where id=?",
                new Object[] {workflow.getDocId(),
                new Integer(workflow.getStatus()),
                workflow.getFlowId(), workflow.getStartDate(),
                workflow.getEndDate(), workflow.getComment(),
                id}, new int[] {Types.VARCHAR, Types.INTEGER,
                Types.VARCHAR, Types.TIMESTAMP,
                Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR});
    }

    public void addNewTemplateNodeMan(String template_node_id,
                                      String account_id) {
        String id = this.sequenceService.getValue(this.
                                                  WORKFLOW_TEMPLATE_NODE_MAN_SEQ);
        this.getJdbcTemplate().update("insert into workflow_template_man(id,template_flow_id,account_id) values(?,?,?)",
                                      new Object[] {id, template_node_id,
                                      account_id}, new int[] {Types.VARCHAR,
                                      Types.VARCHAR, Types.VARCHAR});
    }

    public void deleteTemplateNodeMan(String template_node_id,
                                      String account_id) {
        this.getJdbcTemplate().update(
                "delete from workflow_template_man where template_flow_id=? and account_id=?",
                new Object[] {template_node_id,
                account_id}, new int[] {Types.VARCHAR, Types.VARCHAR});

    }

    /**
     *
     * @param flowNode WorkflowNode
     * @return String
     */
    public String addNewWorkflowNode(WorkflowNode flowNode) {
        String id = this.sequenceService.getValue(this.WORKFLOW_NODE_SEQ);
        //
        this.getJdbcTemplate().update(
                "insert into workflow_flow(id,work_id,status," +
                "sequence,name,description,comment,start_date,end_date,audit_man) values(?,?,?,?,?,?,?,?,?,?)",
                new Object[] {
                id, flowNode.getWorkId(), new Integer(flowNode.getStatus()),
                new Integer(flowNode.getSequence()),
                flowNode.getName(), flowNode.getDescription(),
                flowNode.getComment(),
                flowNode.getStartDate(), flowNode.getEndDate(),
                flowNode.getAuditMan()},
                new int[] {
                Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER,
                Types.VARCHAR,
                Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.DATE,
                Types.VARCHAR});

        //插入人员信息
        if (flowNode.getMans() != null) {
            List mans = flowNode.getMans();

            for (int i = 0; i < mans.size(); i++) {
                String man_id = this.sequenceService.getValue(this.
                        WORKFLOW_NODE_MAN_SEQ);
                String account_id = (String) mans.get(i);
                this.getJdbcTemplate().update(
                        "insert into workflow_man(id,flow_id,account_id) values(?,?,?)",
                        new Object[] {man_id, id, account_id},
                        new int[] {Types.VARCHAR, Types.VARCHAR,
                        Types.VARCHAR});
            }
        }
        return id;
    }

    public WorkflowNode[] getWorkflowNodesOfWork(String work_id) {

        List list = this.getJdbcTemplate().query("select id,work_id,comment,status,sequence,name,description,start_date,end_date,audit_man from workflow_flow where " +
                                                 "work_id=?",
                                                 new Object[] {work_id},
                                                 new int[] {Types.VARCHAR},
                                                 new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {
                WorkflowNode wfNode = new WorkflowNode();
                wfNode.setId(rs.getString("id"));
                wfNode.setWorkId(rs.getString("work_id"));
                wfNode.setComment(rs.getString("comment"));
                wfNode.setStatus(rs.getInt("status"));
                wfNode.setSequence(rs.getInt("sequence"));
                wfNode.setName(rs.getString("name"));
                wfNode.setDescription(rs.getString("description"));
                wfNode.setStartDate(rs.getTimestamp("start_date"));
                wfNode.setEndDate(rs.getTimestamp("end_date"));
                wfNode.setAuditMan(rs.getString("audit_man"));
                return wfNode;

            }
        });
        if (list != null && list.size() > 0) {
            return (WorkflowNode[]) list.toArray(new WorkflowNode[list.size()]);
        }
        return null;
    }

    public void deleteWorkflowNode(String flow_id) {
        this.getJdbcTemplate().update("delete from workflow_flow where id=?",
                                      new Object[] {flow_id},
                                      new int[] {Types.VARCHAR});
        this.getJdbcTemplate().update(
                "delete from workflow_man where flow_id=?",
                new Object[] {flow_id},
                new int[] {Types.VARCHAR});
    }

    /**
     *
     * @return Workflow
     */
    public Workflow[] getAllRunningWorkflow() {
        return this.getWorkflowByStatus(Workflow.WORKFLOW_STATUS_RUNNING);
    }

    public WorkflowNode getWorkflowNodeById(String node_id) {
        Object obj = this.getJdbcTemplate().query(
                "select id,work_id,comment,status," +
                "sequence,name,description,start_date,end_date,audit_man from workflow_flow where id=?",
                new Object[] {node_id}, new int[] {Types.VARCHAR},
                new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    WorkflowNode wfNode = new WorkflowNode();
                    wfNode.setId(rs.getString("id"));
                    wfNode.setWorkId(rs.getString("work_id"));
                    wfNode.setComment(rs.getString("comment"));
                    wfNode.setStatus(rs.getInt("status"));
                    wfNode.setSequence(rs.getInt("sequence"));
                    wfNode.setName(rs.getString("name"));
                    wfNode.setDescription(rs.getString("description"));
                    wfNode.setStartDate(rs.getTimestamp("start_date"));
                    wfNode.setEndDate(rs.getTimestamp("end_date"));
                    wfNode.setAuditMan(rs.getString("audit_man"));
                    return wfNode;

                }
                return null;
            }
        });
        if (obj != null) {
            return (WorkflowNode) obj;
        }
        return null;
    }

    /**
     * 得到指定结点所包含的所有人员
     * @param node_id String
     * @return String[]
     */
    public String[] getMansOfWorkflowNode(String node_id) {
        List list = this.getJdbcTemplate().query(
                "select account_id from workflow_man where flow_id=?",
                new Object[] {node_id}, new int[] {Types.VARCHAR},
                new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {
                return rs.getString("account_id");
            }
        });
        if (list != null && list.size() > 0) {
            return (String[]) list.toArray(new String[list.size()]);
        }
        return null;
    }

    public WorkflowNode[] getWorkflowNodeByStatus(int status) {
        List list = this.getJdbcTemplate().query(
                "select b.flow_id,a.work_id,a.comment,a.status," +
                "a.sequence,a.name,a.description,a.start_date,a.end_date,a.audit_man from workflow_flow a" +
                ",workflow_work b where a.id=b.flow_id and b.status=? order by a.start_date desc",
                new Object[] {new Integer(status)}, new int[] {Types.INTEGER},
                new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {

                WorkflowNode wfNode = new WorkflowNode();
                wfNode.setId(rs.getString("flow_id"));
                wfNode.setWorkId(rs.getString("work_id"));
                wfNode.setComment(rs.getString("comment"));
                wfNode.setStatus(rs.getInt("status"));
                wfNode.setSequence(rs.getInt("sequence"));
                wfNode.setName(rs.getString("name"));
                wfNode.setDescription(rs.getString("description"));
                wfNode.setStartDate(rs.getTimestamp("start_date"));
                wfNode.setEndDate(rs.getTimestamp("end_date"));
                wfNode.setAuditMan(rs.getString("audit_man"));
                return wfNode;
            }
        });
        if (list != null && list.size() > 0) {
            return (WorkflowNode[]) list.toArray(new WorkflowNode[list.size()]);
        }
        return null;
    }

    public WorkflowNode[] getAllRunningWorkflowNode() {
        return this.getWorkflowNodeByStatus(Workflow.WORKFLOW_STATUS_RUNNING,
                                            WorkflowNode.
                                            WORKFLOW_NODE_STATUS_RUNNING);
    }

    public Workflow getWorkflowByDocId(String doc_id) {

        Object obj = this.getJdbcTemplate().query("select id,doc_id,status," +
                                                  "flow_id,start_date,end_date,comment from workflow_work where doc_id=?",
                                                  new Object[] {doc_id},
                                                  new int[] {Types.VARCHAR},
                                                  new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    Workflow wf = new Workflow();
                    wf.setId(rs.getString("id"));
                    wf.setDocId(rs.getString("doc_id"));
                    wf.setStatus(rs.getInt("status"));
                    wf.setComment(rs.getString("comment"));
                    wf.setStartDate(rs.getTimestamp("start_date"));
                    wf.setEndDate(rs.getTimestamp("end_date"));
                    wf.setFlowId(rs.getString("flow_id"));
                    return wf;
                }
                return null;
            }
        });
        if (obj != null) {
            return (Workflow) obj;
        }
        return null;

    }

    /**
     *
     * @param flow_id String
     * @param wfNode WorkflowNode
     */
    public void updateWorkflowNode(String flow_id, WorkflowNode wfNode) {
        this.getJdbcTemplate().update(
                "update workflow_flow set work_id=?,comment=?,status=?," +
                "sequence=?,name=?,description=?,start_date=?,end_date=?,audit_man=? where id=?",
                new Object[] {wfNode.getWorkId(), wfNode.getComment(),
                new Integer(wfNode.getStatus()), new Integer(wfNode.getSequence()),
                wfNode.getName(), wfNode.getDescription(), wfNode.getStartDate(),
                wfNode.getEndDate(), wfNode.getAuditMan(), flow_id},
                new int[] {Types.VARCHAR, Types.VARCHAR,
                Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR,
                Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR});
    }

    /**
     * 得到应该运行的下一个结点
     *
     * @param workflow_id String
     * @param currentSeq int
     * @return WorkflowNode
     */
    public WorkflowNode getAfterShouldRunningWorkflowNode(String workflow_id,
            int currentSeq) {
        Object obj = this.getJdbcTemplate().query(
                "select id,work_id,comment,status," +
                "sequence,name,description,start_date,end_date,audit_man from workflow_flow where " +
                "work_id=?  and sequence=(select min(sequence) from workflow_flow where work_id=? and sequence>?)",
                new Object[] {workflow_id, workflow_id, new Integer(currentSeq)},
                new int[] {Types.VARCHAR,
                Types.VARCHAR, Types.INTEGER},
                new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    WorkflowNode wfNode = new WorkflowNode();
                    wfNode.setId(rs.getString("id"));
                    wfNode.setWorkId(rs.getString("work_id"));
                    wfNode.setComment(rs.getString("comment"));
                    wfNode.setStatus(rs.getInt("status"));
                    wfNode.setSequence(rs.getInt("sequence"));
                    wfNode.setName(rs.getString("name"));
                    wfNode.setDescription(rs.getString("description"));
                    wfNode.setStartDate(rs.getTimestamp("start_date"));
                    wfNode.setEndDate(rs.getTimestamp("end_date"));
                    wfNode.setAuditMan(rs.getString("audit_man"));
                    return wfNode;

                }
                return null;
            }
        });
        if (obj != null) {
            return (WorkflowNode) obj;
        }
        return null;
    }

    /**
     * 在一个结点被拒绝的情况下,得到应该返回的上一个结点
     *
     * @param workflow_id String
     * @param currentSeq int
     * @return WorkflowNode
     */
    public WorkflowNode getBeforeShouldRunningWorkflowNode(String workflow_id,
            int currentSeq) {
        Object obj = this.getJdbcTemplate().query(
                "select id,work_id,comment,status," +
                "sequence,name,description,start_date,end_date,audit_man from workflow_flow where " +
                "work_id=?  and sequence=(select max(sequence) from workflow_flow where work_id=? and sequence<?)",
                new Object[] {workflow_id, workflow_id, new Integer(currentSeq)},
                new int[] {Types.VARCHAR,
                Types.VARCHAR, Types.INTEGER},
                new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    WorkflowNode wfNode = new WorkflowNode();
                    wfNode.setId(rs.getString("id"));
                    wfNode.setWorkId(rs.getString("work_id"));
                    wfNode.setComment(rs.getString("comment"));
                    wfNode.setStatus(rs.getInt("status"));
                    wfNode.setSequence(rs.getInt("sequence"));
                    wfNode.setName(rs.getString("name"));
                    wfNode.setDescription(rs.getString("description"));
                    wfNode.setStartDate(rs.getTimestamp("start_date"));
                    wfNode.setEndDate(rs.getTimestamp("end_date"));
                    wfNode.setAuditMan(rs.getString("audit_man"));
                    return wfNode;

                }
                return null;
            }
        });
        if (obj != null) {
            return (WorkflowNode) obj;
        }
        return null;
    }

    public WorkflowNode[] getWorkflowNodeByStatus(int work_status,
                                                  int node_status) {
        List list = this.getJdbcTemplate().query(
                "select b.flow_id,a.work_id,a.comment,a.status," +
                "a.sequence,a.name,a.description,a.start_date,a.end_date,a.audit_man from workflow_flow a" +
                ",workflow_work b where a.id=b.flow_id and b.status=? and a.status=? order by a.start_date desc",
                new Object[] {new Integer(work_status), new Integer(node_status)},
                new int[] {Types.INTEGER, Types.INTEGER},
                new RowMapper() {
            public Object mapRow(ResultSet rs, int _int) throws SQLException {

                WorkflowNode wfNode = new WorkflowNode();
                wfNode.setId(rs.getString("flow_id"));
                wfNode.setWorkId(rs.getString("work_id"));
                wfNode.setComment(rs.getString("comment"));
                wfNode.setStatus(rs.getInt("status"));
                wfNode.setSequence(rs.getInt("sequence"));
                wfNode.setName(rs.getString("name"));
                wfNode.setDescription(rs.getString("description"));
                wfNode.setStartDate(rs.getTimestamp("start_date"));
                wfNode.setEndDate(rs.getTimestamp("end_date"));
                wfNode.setAuditMan(rs.getString("audit_man"));
                return wfNode;
            }
        });
        if (list != null && list.size() > 0) {
            return (WorkflowNode[]) list.toArray(new WorkflowNode[list.size()]);
        }
        return null;

    }

    public WorkflowNode[] getAllBackedWorkflowNode() {
        WorkflowNode[] wfNodes = getWorkflowNodeByStatus(Workflow.
                WORKFLOW_STATUS_RUNNING,
                WorkflowNode.WORKFLOW_NODE_STATUS_BACKED);
        return wfNodes;
    }

    public WorkflowNode[] getAllReEditWorkflowNode() {
        WorkflowNode[] wfNodes = getWorkflowNodeByStatus(Workflow.
                WORKFLOW_STATUS_BACKED,
                WorkflowNode.WORKFLOW_NODE_STATUS_DECLINED);
        return wfNodes;
    }
    /**
     * 取得默认的工作流模板
     * @return WorkflowTemplate
     */
    public WorkflowTemplate getDefaultWorkflow() {
        Object obj = this.getJdbcTemplate().query(
                      "select * from workflow_template where defaultworkflow=?",
                      new Object[] {new Integer(0)}, new int[] {Types.INTEGER},
                      new ResultSetExtractor() {
                  public Object extractData(ResultSet rs) throws SQLException,
                          DataAccessException {
                      if (rs.next()) {
                          WorkflowTemplate temp = new WorkflowTemplate();
                          temp.setId(rs.getString("id"));
                          temp.setName(rs.getString("name"));
                          temp.setDescription(rs.getString("description"));
                          temp.setCreateMan(rs.getString("createMan"));
                          temp.setCreateDate(rs.getTimestamp("createDate"));
                          temp.setStatus(rs.getInt("status"));
                          temp.setDefaultWorkflow(rs.getInt("defaultworkflow"));
                          return temp;
                      }
                      return null;
                  }
              });
              if (obj != null) {
                  return (WorkflowTemplate) obj;
              }
        return null;
    }
}

本文地址:http://com.8s8s.com/it/it10197.htm