jbpm4 驳回

jbpm工作流实现驳回

	/****************
	 * 任务驳回
	 * @param task
	 * @param destName
	 * @throws Exception
	 */
	public void reject(Task task,String destName) throws Exception{
		try {
			String active = task.getActivityName();
			ProcessInstance pi = getProcessInstanceByTask(task);
			String pdId = pi.getProcessDefinitionId();
			ProcessDefinition pImpl =  processEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionId(pdId).uniqueResult();
			addOutTransition((ProcessDefinitionImpl)pImpl, active, destName);
			taskService.completeTask(task.getId(), "to "+destName);
			removeOutTransition((ProcessDefinitionImpl)pImpl, active, destName);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
	}

	/******************
	 * 动态添加Transition
	 * @param pd
	 * @param sourceName
	 * @param destName
	 * @throws Exception
	 */
	public void addOutTransition(ProcessDefinitionImpl pd,String sourceName,String destName) throws Exception{
		EnvironmentFactory environmentFactory = (EnvironmentFactory)processEngine;
		EnvironmentImpl environment = null;
		try {
			environment = environmentFactory.openEnvironment();
			ActivityImpl sourceActivity = pd.findActivity(sourceName);
			ActivityImpl destActivity = pd.findActivity(destName);
			TransitionImpl transition = sourceActivity.createOutgoingTransition();
			transition.setName("to " + destName);
			transition.setDestination(destActivity);
			sourceActivity.addOutgoingTransition(transition);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		} finally {
			if (environment != null) {
				environment.close();
			}
		}
	}

	/***************
	 * 动态删除Transition
	 * @param pd
	 * @param sourceName
	 * @param destName
	 * @throws Exception
	 */
	public void removeOutTransition(ProcessDefinitionImpl pd,String sourceName,String destName) throws Exception{
		EnvironmentFactory environmentFactory = (EnvironmentFactory)processEngine;
		EnvironmentImpl environment = null;
		try {
			environment = environmentFactory.openEnvironment();
			ActivityImpl sourceActivity = pd.findActivity(sourceName);
			List<Transition> tranList = sourceActivity.getOutgoingTransitions();
			for (Transition transition : tranList) {
				if (destName.equals(transition.getDestination().getName())) {
					tranList.remove(transition);
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		} finally {
			if (environment != null) {
				environment.close();
			}
		}
	}

	public ProcessInstance getProcessInstanceByTask(Task task){
		return executionService.findProcessInstanceById(task.getExecutionId());
	}
This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s