Package org.fireflow.engine.persistence

Examples of org.fireflow.engine.persistence.IPersistenceService


  /* (non-Javadoc)
   * @see org.fireflow.engine.taskinstance.ITaskInstanceManager#claimWorkItem(java.lang.String, java.lang.String)
   */
  public final IWorkItem claimWorkItem(String workItemId,
      String taskInstanceId) throws EngineException, KernelException {
    IPersistenceService persistenceService = rtCtx.getPersistenceService();

    persistenceService.lockTaskInstance(taskInstanceId);

    IWorkItem workItem = persistenceService.findWorkItemById(workItemId);

    if (workItem == null)
      return null;

    if (workItem.getState().intValue() != IWorkItem.INITIALIZED) {
      TaskInstance thisTaskInst = (TaskInstance) workItem
          .getTaskInstance();
      throw new EngineException(thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Claim work item failed. The state of the work item is "
              + workItem.getState());
    }
    if (workItem.getTaskInstance().getState().intValue() != ITaskInstance.INITIALIZED
        && workItem.getTaskInstance().getState().intValue() != ITaskInstance.RUNNING) {
      TaskInstance thisTaskInst = (TaskInstance) workItem
          .getTaskInstance();
      throw new EngineException(thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Claim work item failed .The state of the correspond task instance is "
              + workItem.getTaskInstance().getState());
    }

    if (workItem.getTaskInstance().isSuspended()) {
      TaskInstance thisTaskInst = (TaskInstance) workItem
          .getTaskInstance();
      throw new EngineException(thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Claim work item failed .The  correspond task instance is suspended");
    }

    // 0、首先修改workitem的状态
    ((WorkItem) workItem).setState(IWorkItem.RUNNING);
    ((WorkItem) workItem).setClaimedTime(rtCtx.getCalendarService()
        .getSysDate());
    persistenceService.saveOrUpdateWorkItem(workItem);

    // 1、如果不是会签,则删除其他的workitem
    if (FormTask.ANY.equals(workItem.getTaskInstance()
        .getAssignmentStrategy())) {
      persistenceService.deleteWorkItemsInInitializedState(workItem
          .getTaskInstance().getId());
    }

    // 2、将TaskInstance的canBeWithdrawn字段改称false。即不允许被撤销
    TaskInstance taskInstance = (TaskInstance) workItem.getTaskInstance();
    taskInstance.setCanBeWithdrawn(false);
    persistenceService.saveOrUpdateTaskInstance(taskInstance);

    return workItem;

  }
View Full Code Here


          process, thisTaskInst.getTaskId(),
          "Complete work item failed. The correspond task instance [id="
              + thisTaskInst.getId() + "] is suspended");
    }

    IPersistenceService persistenceService = rtCtx.getPersistenceService();

    ((WorkItem) workItem).setComments(comments);
    ((WorkItem) workItem).setState(IWorkItem.COMPLETED);
    ((WorkItem) workItem).setEndTime(rtCtx.getCalendarService()
        .getSysDate());
    persistenceService.saveOrUpdateWorkItem(workItem);

    // 触发AFTER_WORKITEM_COMPLETE事件
    TaskInstanceEvent e = new TaskInstanceEvent();
    e.setSource(workItem.getTaskInstance());
    e.setWorkflowSession(((IWorkflowSessionAware) workItem)
View Full Code Here

    // if (!isInSameLine) throw new
    // EngineException("Jumpto refused because of the  FORM-type-task count of the target activitgy  is NOT 1; the count is "+count);
    // }

    // 3)检查当前的 taskinstance是否可以结束
    IPersistenceService persistenceService = rtCtx.getPersistenceService();

    Integer aliveWorkItemCount = persistenceService
        .getAliveWorkItemCountForTaskInstance(thisTaskInst.getId());
    if (aliveWorkItemCount != null && aliveWorkItemCount > 1) {
      throw new EngineException(
          thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Jumpto refused because of current taskinstance can NOT be completed. some workitem of this taskinstance is in runing state or initialized state");

    }

    // 4)检查当前的activity instance是否可以结束
    if (Activity.ALL.equals(workItem.getTaskInstance().getActivity()
        .getCompletionStrategy())) {

      Integer aliveTaskInstanceCount4ThisActivity = persistenceService
          .getAliveTaskInstanceCountForActivity(workItem
              .getTaskInstance().getProcessInstanceId(), workItem
              .getTaskInstance().getActivityId());
      if (aliveTaskInstanceCount4ThisActivity.intValue() > 1) {// 大于2表明当前Activity不可以complete
        throw new EngineException(
View Full Code Here

    // if (!isInSameLine) throw new
    // EngineException("Jumpto refused because of the  FORM-type-task count of the target activitgy  is NOT 1; the count is "+count);
    // }

    // 3)检查当前的 taskinstance是否可以结束
    IPersistenceService persistenceService = rtCtx.getPersistenceService();

    Integer aliveWorkItemCount = persistenceService
        .getAliveWorkItemCountForTaskInstance(thisTaskInst.getId());
    if (aliveWorkItemCount != null && aliveWorkItemCount > 1) {
      throw new EngineException(
          thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Jumpto refused because of current taskinstance can NOT be completed. some workitem of this taskinstance is in runing state or initialized state");

    }

    // 4)检查当前的activity instance是否可以结束
    if (Activity.ALL.equals(workItem.getTaskInstance().getActivity().getCompletionStrategy())) {

      Integer aliveTaskInstanceCount4ThisActivity = persistenceService
          .getAliveTaskInstanceCountForActivity(workItem
              .getTaskInstance().getProcessInstanceId(), workItem
              .getTaskInstance().getActivityId());
      if (aliveTaskInstanceCount4ThisActivity.intValue() > 1) {// 大于1表明当前Activity不可以complete
        throw new EngineException(
            thisTaskInst.getProcessInstanceId(),
            thisTaskInst.getWorkflowProcess(),
            thisTaskInst.getTaskId(),
            "Jumpto refused because of current activity instance can NOT be completed. some task instance of this activity instance is in runing state or initialized state");
      }
    }
   
    //4)首先检查目标状态M是否存在冲突,如果存在冲突则不允许跳转;如果不存在冲突,则需要调整token
    List<IToken> allTokens = persistenceService.findTokensForProcessInstance(thisTaskInst.getProcessInstanceId(), null);
    WorkflowProcess thisProcess = thisTaskInst.getWorkflowProcess();//找到当前的工作里模型
    List<String> aliveActivityIdsAfterJump = new ArrayList<String>();//计算跳转后,哪些activity节点复活
    aliveActivityIdsAfterJump.add(targetActivityId);
   
    for (int i=0;allTokens!=null && i<allTokens.size();i++){
      IToken tokenTmp = allTokens.get(i);
      IWFElement workflowElement = thisProcess.findWFElementById(tokenTmp.getNodeId()); //找到拥有此token的工作流元素
      if ((workflowElement instanceof Activity) && !workflowElement.getId().equals(thisActivityId)){
        //注意:不能自己跳转到自己,同时此工作流元素是activity类型
        aliveActivityIdsAfterJump.add(workflowElement.getId());
       
        if (thisProcess.isReachable(targetActivityId, workflowElement.getId())
          || thisProcess.isReachable(workflowElement.getId(), targetActivityId)){
          throw new EngineException(
              thisTaskInst.getProcessInstanceId(),
              thisTaskInst.getWorkflowProcess(),
              thisTaskInst.getTaskId(),
              "Jumpto refused because of the business-logic conflict!");

        }
      }
    }

    //所有检查结束,开始执行跳转操作
   
    INetInstance netInstance = rtCtx.getKernelManager().getNetInstance(
        workflowProcess.getId(),
        workItem.getTaskInstance().getVersion());
    if (netInstance == null) {
      throw new EngineException(thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Not find the net instance for workflow process [id="
              + workflowProcess.getId() + ", version="
              + workItem.getTaskInstance().getVersion() + "]");
    }
    Object obj = netInstance.getWFElementInstance(targetActivityId);
    IActivityInstance targetActivityInstance = (IActivityInstance) obj;
    if (targetActivityInstance == null) {
      throw new EngineException(thisTaskInst.getProcessInstanceId(),
          thisTaskInst.getWorkflowProcess(),
          thisTaskInst.getTaskId(),
          "Not find the activity instance  for activity[process id="
              + workflowProcess.getId() + ", version="
              + workItem.getTaskInstance().getVersion()
              + ",activity id=" + targetActivityId + "]");
    }

    if (rtCtx.isEnableTrace()) {

      ProcessInstanceTrace trace = new ProcessInstanceTrace();
      trace.setProcessInstanceId(workItem.getTaskInstance()
          .getProcessInstanceId());
      trace.setStepNumber(workItem.getTaskInstance().getStepNumber() + 1);
      trace.setType(ProcessInstanceTrace.JUMPTO_TYPE);
      trace.setFromNodeId(workItem.getTaskInstance().getActivityId());
      trace.setToNodeId(targetActivityId);
      trace.setEdgeId("");
      rtCtx.getPersistenceService().saveOrUpdateProcessInstanceTrace(
          trace);
    }

    //调整token布局
    List<Synchronizer> allSynchronizersAndEnds = new ArrayList<Synchronizer>();
    allSynchronizersAndEnds.addAll(thisProcess.getSynchronizers());
    allSynchronizersAndEnds.addAll(thisProcess.getEndNodes());
    for (int i=0;i<allSynchronizersAndEnds.size();i++){
      Synchronizer synchronizer = allSynchronizersAndEnds.get(i);

      int volumn = 0;
      if (synchronizer instanceof EndNode){
        volumn = synchronizer.getEnteringTransitions().size();
      }else{
        volumn = synchronizer.getEnteringTransitions().size()*synchronizer.getLeavingTransitions().size();
      }     
      IToken tokenTmp =  new Token();
      tokenTmp.setNodeId(synchronizer.getId());
      tokenTmp.setAlive(false);
      tokenTmp.setProcessInstanceId(thisTaskInst.getProcessInstanceId());
      tokenTmp.setStepNumber(-1);

      List<String> incomingTransitionIds = new ArrayList<String>();
      boolean reachable = false;
      List<Transition> enteringTrans = synchronizer.getEnteringTransitions();   
      for (int m=0;m<aliveActivityIdsAfterJump.size();m++){
        String aliveActivityId = aliveActivityIdsAfterJump.get(m);
        if (thisProcess.isReachable(aliveActivityId, synchronizer.getId())){         
          Transition trans = null;
          reachable = true;
          for (int j=0;j<enteringTrans.size();j++){
            trans = enteringTrans.get(j);
            Node fromNode = trans.getFromNode();
            if (thisProcess.isReachable(aliveActivityId, fromNode.getId())){
              if (!incomingTransitionIds.contains(trans.getId())){
                incomingTransitionIds.add(trans.getId());
              }
            }
          }     
        }
      }
      if (reachable){
        tokenTmp.setValue(volumn-(incomingTransitionIds.size()*volumn/enteringTrans.size()))
       
        IToken virtualToken = getJoinInfo(allTokens,synchronizer.getId()); //获取一个虚拟的综合性token
       
        if (virtualToken!=null){
          persistenceService.deleteTokensForNode(thisTaskInst.getProcessInstanceId(), synchronizer.getId());
        }
       
        if (tokenTmp.getValue()!=0){
          tokenTmp.setProcessInstance(thisTaskInst.getAliveProcessInstance());
          persistenceService.saveOrUpdateToken(tokenTmp);
        }
      }
    }
   
    this.completeWorkItem(workItem, targetActivityInstance, comments);
View Full Code Here

    public void onNodeInstanceEventFired(NodeInstanceEvent e)
            throws KernelException, EngineException {
      //同步器节点的监听器触发条件,是在离开这个节点的时候
        if (e.getEventType() == NodeInstanceEvent.NODEINSTANCE_LEAVING) {
            ISynchronizerInstance syncInst = (ISynchronizerInstance) e.getSource();
            IPersistenceService persistenceService = this.rtCtx.getPersistenceService();
            //删除同步器节点的token
            persistenceService.deleteTokensForNode(e.getToken().getProcessInstanceId(), syncInst.getSynchronizer().getId());

        }
    }
View Full Code Here

            dynamicAssignmentHandler.assign((IAssignable) taskInstance, part.getName());

        } else {

          IPersistenceService persistenceService = runtimeContext.getPersistenceService();
          //从数据库中查询任务信息
          List<ITaskInstance> taskInstanceList = persistenceService.findTaskInstancesForProcessInstance(taskInstance.getProcessInstanceId(), taskInstance.getActivityId());
          ITaskInstance theLastCompletedTaskInstance = null;

          for (int i=0;taskInstanceList!=null && i<taskInstanceList.size();i++){
            ITaskInstance tmp = taskInstanceList.get(i);
            if (tmp.getId().equals(taskInstance.getId()))continue;
                if (!tmp.getTaskId().equals(taskInstance.getTaskId()))continue;
                if (tmp.getState().intValue()!=ITaskInstance.COMPLETED)continue;
            if ( theLastCompletedTaskInstance==null){
              theLastCompletedTaskInstance = tmp;
            }else{
              if (theLastCompletedTaskInstance.getStepNumber().intValue()<tmp.getStepNumber().intValue()){
                theLastCompletedTaskInstance=tmp;
              }
            }
          }

          //如果是循环且LoopStrategy==REDO,则分配个上次完成该工作的操作员
            if (theLastCompletedTaskInstance!=null && (FormTask.REDO.equals(formTask.getLoopStrategy()) || currentSession.isInWithdrawOrRejectOperation())) {
              List<IWorkItem> workItemList = persistenceService.findCompletedWorkItemsForTaskInstance(theLastCompletedTaskInstance.getId());
              ITaskInstanceManager taskInstanceMgr = runtimeContext.getTaskInstanceManager();
                for (int k = 0; k < workItemList.size(); k++) {
                    IWorkItem completedWorkItem = workItemList.get(k);

                    IWorkItem newFromWorkItem = taskInstanceMgr.createWorkItem(currentSession,processInstance,taskInstance, completedWorkItem.getActorId());
View Full Code Here

     */
    public void onNodeInstanceEventFired(NodeInstanceEvent e)
            throws KernelException {
        if (e.getEventType() == NodeInstanceEvent.NODEINSTANCE_FIRED) {
            //保存token,并创建taskinstance
            IPersistenceService persistenceService = rtCtx.getPersistenceService();
            //TODO wmj2003 这里是插入还是更新token
            persistenceService.saveOrUpdateToken(e.getToken());
            //触发activity节点,就要创建新的task
            rtCtx.getTaskInstanceManager().createTaskInstances(e.getToken(), (IActivityInstance) e.getSource());
        } else if (e.getEventType() == NodeInstanceEvent.NODEINSTANCE_COMPLETED) {
          //TODO  wmj2003 因为什么原因未处理呢? 归档任务?
//      RuntimeContext.getInstance()
View Full Code Here

    public void onNodeInstanceEventFired(NodeInstanceEvent e)
            throws KernelException {
      //同步器节点的监听器触发条件,是在离开这个节点的时候
        if (e.getEventType() == NodeInstanceEvent.NODEINSTANCE_LEAVING) {
            ISynchronizerInstance syncInst = (ISynchronizerInstance) e.getSource();
            IPersistenceService persistenceService = this.rtCtx.getPersistenceService();
            //删除同步器节点的token
            persistenceService.deleteTokensForNode(e.getToken().getProcessInstanceId(), syncInst.getSynchronizer().getId());

        }     
      //如果节点实例结束,就触发
        if (e.getEventType() == NodeInstanceEvent.NODEINSTANCE_COMPLETED) {
            // 执行ProcessInstance的complete操作
View Full Code Here

            throw new EngineException(taskInstance.getProcessInstanceId(), parentWorkflowProcess,
                    taskInstance.getTaskId(),
                    "系统中没有Id为" + Subflow.getWorkflowProcessId() + "的流程定义");
        }
       
        IPersistenceService persistenceService = runtimeContext.getPersistenceService();
        //更改任务的状态和开始时间
        ((TaskInstance) taskInstance).setState(ITaskInstance.RUNNING);
        ((TaskInstance) taskInstance).setStartedTime(runtimeContext.getCalendarService().getSysDate());
        //TODO wmj2003 应该是update TaskInstance
        persistenceService.saveOrUpdateTaskInstance(taskInstance);


        IProcessInstance subProcessInstance = currentSession.createProcessInstance(subWorkflowProcess.getName(),taskInstance);

        //初始化流程变量,从父实例获得初始值
View Full Code Here

                                thisTaskInstance.getWorkflowProcess(),
                                thisTaskInstance.getTaskId(),
                                "Reject operation refused!Because the from activityId equals "+IToken.FROM_START_NODE );
        }
        //----added by wmj2003 20090915 ---end---
    IPersistenceService persistenceService = this.rtCtx
        .getPersistenceService();
    List<ITaskInstance> siblingTaskInstancesList = null;

    siblingTaskInstancesList = persistenceService
        .findTaskInstancesForProcessInstanceByStepNumber(workItem
            .getTaskInstance().getProcessInstanceId(),
            thisTaskInstance.getStepNumber());

    // 如果执行了split操作,则不允许reject
    if (siblingTaskInstancesList.size() > 1) {
      throw new EngineException(
          thisTaskInstance.getProcessInstanceId(),
          thisTaskInstance.getWorkflowProcess(),
          thisTaskInstance.getTaskId(),
          "Reject operation refused!Because the process instance has taken a split operation.");
    }

    // 检查From Activity中是否有ToolTask和SubflowTask
    List<String> fromActivityIdList = new ArrayList<String>();
    StringTokenizer tokenizer = new StringTokenizer(thisTaskInstance
        .getFromActivityId(), IToken.FROM_ACTIVITY_ID_SEPARATOR);
    while (tokenizer.hasMoreTokens()) {
      fromActivityIdList.add(tokenizer.nextToken());
    }
    WorkflowProcess workflowProcess = workItem.getTaskInstance()
        .getWorkflowProcess();
    for (int i = 0; i < fromActivityIdList.size(); i++) {
      String fromActivityId = (String) fromActivityIdList.get(i);
      Activity fromActivity = (Activity) workflowProcess
          .findWFElementById(fromActivityId);
      List<Task> fromTaskList = fromActivity.getTasks();
      for (int j = 0; j < fromTaskList.size(); j++) {
        Task task =  fromTaskList.get(j);
        if (Task.TOOL.equals(task.getType())
            || Task.SUBFLOW.equals(task.getType())) {
          throw new EngineException(
              thisTaskInstance.getProcessInstanceId(),
              thisTaskInstance.getWorkflowProcess(),
              thisTaskInstance.getTaskId(),
              "Reject operation refused!The previous activity contains tool-task or subflow-task");

        }
      }
    }
    // 恢复所有的FromTaskInstance
    INetInstance netInstance = rtCtx.getKernelManager().getNetInstance(
        workflowProcess.getId(),
        workItem.getTaskInstance().getVersion());
    if (netInstance == null) {
      throw new EngineException(thisTaskInstance.getProcessInstanceId(),
          thisTaskInstance.getWorkflowProcess(), thisTaskInstance
              .getTaskId(),
          "Not find the net instance for workflow process [id="
              + workflowProcess.getId() + ", version="
              + workItem.getTaskInstance().getVersion() + "]");
    }

    // 执行reject操作。

    IWorkflowSession session = ((IWorkflowSessionAware) workItem)
        .getCurrentWorkflowSession();
    session.setWithdrawOrRejectOperationFlag(true);
    int newStepNumber = thisTaskInstance.getStepNumber() + 1;
    try {
      // 首先将本WorkItem和TaskInstance cancel掉。
      workItem.setComments(comments);
      ((WorkItem) workItem).setState(IWorkItem.CANCELED);
      ((WorkItem) workItem).setEndTime(rtCtx.getCalendarService()
          .getSysDate());
      rtCtx.getPersistenceService().saveOrUpdateWorkItem(workItem);

      persistenceService.abortTaskInstance(thisTaskInstance);

      // 删除本环节的token
      persistenceService.deleteTokensForNode(thisTaskInstance
          .getProcessInstanceId(), thisTaskInstance.getActivityId());

      IActivityInstance fromActivityInstance = null;
      for (int i = 0; i < fromActivityIdList.size(); i++) {
        String fromActivityId = (String) fromActivityIdList.get(i);
        Object obj = netInstance.getWFElementInstance(fromActivityId);
        fromActivityInstance = (IActivityInstance) obj;
        Token newToken = new Token();
        ((Token) newToken).setAlive(true);
        ((Token) newToken).setNodeId(fromActivityId);
        newToken.setProcessInstanceId(thisTaskInstance
            .getProcessInstanceId());
        newToken.setProcessInstance(((TaskInstance) thisTaskInstance)
            .getAliveProcessInstance());
        newToken.setFromActivityId(thisTaskInstance.getActivityId());
        newToken.setStepNumber(newStepNumber);
        newToken.setValue(0);
        persistenceService.saveOrUpdateToken(newToken);

        this.createTaskInstances(newToken, fromActivityInstance);

        if (rtCtx.isEnableTrace()) {
          ProcessInstanceTrace trace = new ProcessInstanceTrace();
          trace.setProcessInstanceId(thisTaskInstance
              .getProcessInstanceId());
          trace.setStepNumber(newStepNumber);
          trace.setType(ProcessInstanceTrace.REJECT_TYPE);
          trace.setFromNodeId(thisActivity.getId());
          trace.setToNodeId(fromActivityId);
          trace.setEdgeId("");
          rtCtx.getPersistenceService()
              .saveOrUpdateProcessInstanceTrace(trace);
        }
      }

      ITransitionInstance theLeavingTransitionInstance = (ITransitionInstance) fromActivityInstance
          .getLeavingTransitionInstances().get(0);
      ISynchronizerInstance synchronizerInstance = (ISynchronizerInstance) theLeavingTransitionInstance
          .getLeavingNodeInstance();
      if (synchronizerInstance.getEnteringTransitionInstances().size() > fromActivityIdList
          .size()) {
        Token supplementToken = new Token();
        ((Token) supplementToken).setAlive(false);
        ((Token) supplementToken).setNodeId(synchronizerInstance
            .getSynchronizer().getId());
        supplementToken.setProcessInstanceId(thisTaskInstance
            .getProcessInstanceId());
        supplementToken
            .setProcessInstance(((TaskInstance) thisTaskInstance)
                .getAliveProcessInstance());
        supplementToken.setFromActivityId("EMPTY(created by reject)");
        supplementToken
            .setStepNumber(thisTaskInstance.getStepNumber() + 1);
        supplementToken.setValue(synchronizerInstance.getVolume()
            - theLeavingTransitionInstance.getWeight()
            * fromActivityIdList.size());
        persistenceService.saveOrUpdateToken(supplementToken);
      }
    } finally {
      session.setWithdrawOrRejectOperationFlag(false);
    }
  }
View Full Code Here

TOP

Related Classes of org.fireflow.engine.persistence.IPersistenceService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.