基于Osworkflow的工作流开发。

基本开发思路
一个流程:
  • 一个业务流程service,用于定义流程的名称及初始化执行ID。(如下PolicyWorkflowService )
  • 一个流程定义文件。
  • N个condition(用于做权限限制)。
  • N个function(用于执行业务函数)。

public class PolicyWorkflowService extends WorkflowService {

    public static String WORKFLOW_NAME = "policy";
    public static int INITIALIZE_ACTION_ID = 100;

    public PolicyPublishWorkflowService() {
        super(WORKFLOW_NAME, INITIALIZE_ACTION_ID);
    }
}

也可以作为Spring的Bean配置出来。


WorkflowService用于提供工作流引擎的基础服务。
所有业务流程service必须继承它。
此类不能直接提供给外部使用。
/**
 * 工作流程服务类
 * 
 * @author kiral
 * @日期 2008-1-15
 */
public class WorkflowService{

    public static final Logger logger = Logger.getLogger(WorkflowService.class);

    private String workflowName;

    private Workflow workflow;

    private int initActionId;

    protected WorkflowService() {
    }

    protected WorkflowService(String workflowName, int initActionId) {
        this.workflowName = workflowName;
        this.initActionId = initActionId;
    }

    /**
     * 初始化工作流程
     * 
     * @param userName
     * @param inputs
     * @return
     * @throws Exception
     * @deprecated 使用doInitialize()替换
     */
    @SuppressWarnings("unchecked")
    @Deprecated
    public long doInitialize(String userName) {
        return doInitialize();
    }

    @SuppressWarnings("unchecked")
    public long doInitialize(Map input) {
        long wf_id = -1;
        try {
            wf_id = workflow.initialize(workflowName, initActionId, input);
        } catch (InvalidRoleException e) {
            logger.info("当前用户不能初始化工作流");
        } catch (Exception e) {
            throw new RuntimeException("初始化工作流出现问题:", e);
        }
        return wf_id;
    }

    /**
     * 初始化工作流程
     */
    @SuppressWarnings("unchecked")
    public long doInitialize() {
        return doInitialize(new HashMap());
    }
    
    /**
     * 执行动作
     * 
     * @deprecated 使用doAction(workflowId,actionId,inputs)替换
     */
    @SuppressWarnings("unchecked")
    @Deprecated
    public void doAction(final String userName, final long workflowId, final int actionId,
            final Map inputs) {
        doAction(workflowId, actionId, inputs);
    }
    
    

    /**
     * 执行动作
     * 
     * @param userName
     * @param workflowId
     * @param actionId
     * @param inputs
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public void doAction(final long workflowId, final int actionId, final Map inputs) {
        try {
            workflow.setContext();
            workflow.doAction(workflowId, actionId, inputs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 流程是否结束
     * @return true结束 
     */
    public boolean hasEnd(String workflowId)
    {
        return queryCurrentStep(workflowId)==null?true:false;
    }

    

    /**
     * 执行动作
     * 
     * @param userName
     * @param workflowId
     * @param actionId
     * @param inputs
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public void doAction(final long workflowId, final int actionId) {
        doAction(workflowId, actionId, null);
    }

    public void doAction(final String workflowId, final int actionId) {
        Assert.hasLength(workflowId, "workflowId不能为空");
        doAction(Long.parseLong(workflowId), actionId, null);
    }

    @SuppressWarnings("unchecked")
    public void doAction(final String workflowId, final int actionId, final String operationId) {
        Map inputs = new HashMap();
        inputs.put("operationId", inputs);
        doAction(Long.parseLong(workflowId), actionId, inputs);
    }
    
    /**
     * 查询当前步骤的描述信息
     * 
     * @param workflowId
     * @param userName
     * @return
     * @deprecated 使用queryCurrentStepDescriptor(long workflowId)替换
     */
    @SuppressWarnings("unchecked")
    @Deprecated
    public StepDescriptor queryCurrentStepDescriptor(long workflowId, String userName) {
        return queryCurrentStepDescriptor(workflowId);
    }

    /**
     * 查询当前步骤的描述信息
     * 
     * @param workflowId
     * @param userName
     * @return
     */
    @SuppressWarnings("unchecked")
    public StepDescriptor queryCurrentStepDescriptor(long workflowId) {
        List list = workflow.getCurrentSteps(workflowId);
        if (list == null || list.isEmpty()) {
            return null;
        } else {
            return queryStepDescriptor(((Step) list.get(0)).getStepId());
        }
    }

    public StepDescriptor queryCurrentStepDescriptor(String workflowId) {
        return queryCurrentStepDescriptor(Long.parseLong(workflowId));
    }

    /**
     * 查询当前步骤
     * 
     * @param workflowId
     * @deprecated 使用queryCurrentStep(long workflowId)替换
     */
    @SuppressWarnings("unchecked")
    @Deprecated
    public Step queryCurrentStep(long workflowId, String userName) {
        return queryCurrentStep(workflowId);
    }

    /**
     * 查询当前步骤
     * 
     * @param workflowId
     * @return
     */
    @SuppressWarnings("unchecked")
    public Step queryCurrentStep(long workflowId) {
        List list = workflow.getCurrentSteps(workflowId);
        return list.isEmpty() ? null : ((Step) list.get(0));
    }
    
    public Step queryCurrentStep(String workflowId) {
        return queryCurrentStep(Long.parseLong(workflowId));
    }

    @SuppressWarnings("unchecked")
    public StepDescriptor queryStepDescriptor(int stepId) {
        return getWorkflowDescriptor().getStep(stepId);
    }

    /**
     * 指定Action是否是当前将要执行的action
     */
    public boolean isCurrentAction(long workflowId, String userName, int actionId) {
        Map<Integer, String> actionMap = findAvailableActions(workflowId, userName);
        return actionMap.get(actionId) == null ? false : true;
    }

    /**
     * 得到用户指定流程的有效行动
     * 
     * @param workflowId
     * @param userName
     * @return actionID和名称的键值对
     */
    public Map<Integer, String> findAvailableActions(long workflowId, String userName) {
        Map<Integer, String> result = new HashMap<Integer, String>();
        int[] availableActions = workflow.getAvailableActions(workflowId, null);
        WorkflowDescriptor wd = workflow
                .getWorkflowDescriptor(workflow.getWorkflowName(workflowId));
        for (int i = 0; i < availableActions.length; i++) {
            int actionId = availableActions[i];
            result.put(actionId, wd.getAction(actionId).getName());
        }
        return result;
    }

    public Map<Integer, String> findAvailableActions(String workflowId, String userName) {
        Assert.hasLength(workflowId, "workflowId不能为空");
        return findAvailableActions(Long.parseLong(workflowId), userName);
    }

    public String findActionName(int actionId) {
        ActionDescriptor actionDescriptor = getWorkflowDescriptor().getAction(actionId);
        return actionDescriptor == null ? null : actionDescriptor.getName();
    }

    /**
     * 查询所有步骤
     */
    @SuppressWarnings("unchecked")
    public List<Step> findSteps(long workflowId) {
        Assert.isTrue(workflowId > 0, "workflowId必须大于零");
        List<Step> result = new ArrayList<Step>();
        List currentSteps = workflow.getCurrentSteps(workflowId);
        List historySteps = workflow.getHistorySteps(workflowId);
        result.addAll(currentSteps);
        result.addAll(historySteps);
        return result;
    }
    
    @SuppressWarnings("unchecked")
    public List<SimpleStep> findCurrentSteps(long workflowId) {
        Assert.isTrue(workflowId > 0, "workflowId必须大于零");
        return workflow.getCurrentSteps(workflowId);
    }
    
    @SuppressWarnings("unchecked")
    public List<Step> findHistorySteps(long workflowId) {
        Assert.isTrue(workflowId > 0, "workflowId必须大于零");
        return workflow.getHistorySteps(workflowId);
    }

    /**
     * 获得工作流描述
     * 
     */
    public WorkflowDescriptor getWorkflowDescriptor() {
        return workflow.getWorkflowDescriptor(workflowName);
    }

    /**
     * 查询工作流描述
     * 
     */
    public WorkflowDescriptor queryWorkflowDescriptor(long workflowId) {
        return workflow.getWorkflowDescriptor(workflow.getWorkflowName(workflowId));
    }

    public void setWorkflow(IscsmWorkflow workflow) {
        this.workflow = workflow;
    }

}



Workflow类,在这里你可以设置流程的执行者。
public class Workflow extends AbstractWorkflow {
    
    public void setContext() {
        String caller;
        try {
            caller = “你系统的用户”;
        } catch (Exception e) {
            caller = "";
        }
        super.context = new BasicWorkflowContext(caller);
    }
    
}


AbstractFunction类,所有Function的基类,继承他能得到很多工作流引擎自带的基础属性。如调用者,流程ID..等
import java.util.Map;

import com.opensymphony.workflow.FunctionProvider;
import com.opensymphony.workflow.basic.BasicWorkflowContext;
import com.opensymphony.workflow.spi.WorkflowEntry;

public abstract class AbstractFunction implements FunctionProvider {
    
    protected long workflowId;

    protected String caller;

    protected int actionId;

    private WorkflowService workflowService;
    
    @SuppressWarnings("unchecked")
    protected void set(Map transientVars,Map args) {
        WorkflowEntry workflowEntry = (WorkflowEntry) transientVars.get("entry");
        workflowId = workflowEntry.getId();
        BasicWorkflowContext context = (BasicWorkflowContext) transientVars.get("context");
        caller = context.getCaller();
        actionId = (Integer) transientVars.get("actionId");
    }
    

    public void setWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

}


使用自定义的condition来实现,用自己的权限系统控制流程
public class UserRightCondition implements Condition {

    private UserRoleDAO userRoleDAO;

    @SuppressWarnings("unchecked")
    public boolean passesCondition(Map transientVars, Map args, PropertySet ps)
            throws StoreException {
        String actionCode = (String) args.get("actionCode");
        //在流程定义文件里配置能够执行当前流程的角色PK或者,权限PK。
          //然后在这里判断当前caller是否有对应的权限和角色,如果有返回true

        return false;
    }
}


一些内置的condition
  • OSUserGroupCondition -  使用 OSUser来判断调用者是否在参数"group"中。 
  • StatusCondition -  判断当前步骤的状态是否与参数"status"相同。
  • AllowOwnerOnlyCondition -  如果调用者是指定的步骤的所有者的话,那么只返回
  • true,如果没有指明步骤的话,就返回当前步骤。
  • DenyOwnerCondition 与 AllowOwnerOnlyCondition 功能相反。
使用如下:
<condition type="class">                                               
<arg name="class.name">com.opensymphony.workflow.util.AllowOwnerOnlyCondition</arg> 
<condition>

你可能感兴趣的:(spring,工作,bean,workflow)