·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » 一个用数据库实现的工作流

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

类别: 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,              &nbs
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1