View Javadoc

1   /**
2    * 
3    */
4   package eu.diversit.jbpm.spring;
5   
6   import java.util.List;
7   
8   import org.jbpm.api.ProcessDefinition;
9   import org.jbpm.api.ProcessDefinitionQuery;
10  import org.jbpm.api.RepositoryService;
11  import org.springframework.beans.factory.annotation.Autowired;
12  
13  /**
14   * Bean to lookup a ProcessDefinition by key.
15   * Can be used to lookup the current (last) deployed process definition version.
16   * 
17   * @author joostdenboer
18   */
19  public class JbpmProcessLookup {
20  
21  	@Autowired
22  	private RepositoryService repositoryService;
23  	
24  	private String processKey;
25  
26  	/**
27  	 * Get the current (last) version of the process with current process key.
28  	 * @return {@link ProcessDefinition}
29  	 */
30  	public ProcessDefinition getCurrent() {
31  		ProcessDefinitionQuery dfq = repositoryService.createProcessDefinitionQuery();
32  		List<ProcessDefinition> result = dfq.processDefinitionKey(processKey)
33  			.orderDesc(ProcessDefinitionQuery.PROPERTY_VERSION)
34  			.page(0, 1)
35  			.list();
36  		
37  		if(!result.isEmpty()) {
38  			return result.get(0);
39  		} else {
40  			return null;
41  		}
42  	}
43  	
44  	/**
45  	 * Get all versions of the process with current process key.
46  	 * @return List of {@link ProcessDefinition}
47  	 */
48  	public List<ProcessDefinition> getAllVersions() {
49  		
50  		List<ProcessDefinition> result = repositoryService.createProcessDefinitionQuery()
51  				.processDefinitionKey(processKey)
52  				.list();
53  		return result;
54  	}
55  	
56  	/**
57  	 * Get a process definition with given version.
58  	 * @param version int Specific version
59  	 * @return {@link ProcessDefinition} with current process key and given version.
60  	 */
61  	public ProcessDefinition getVersion(int version) {
62  		String id = String.format("%s-%d", processKey, version);
63  		
64  		ProcessDefinitionQuery dfq = repositoryService.createProcessDefinitionQuery();
65  		ProcessDefinition df = dfq.processDefinitionId(id).uniqueResult();
66  		return df;
67  	}
68  
69  	/* Setters / Getters for Spring injection */
70  	
71  	/**
72  	 * @return the processKey
73  	 */
74  	public String getProcessKey() {
75  		return processKey;
76  	}
77  
78  	/**
79  	 * @param processKey the processKey to set
80  	 */
81  	public void setProcessKey(String processKey) {
82  		this.processKey = processKey;
83  	}
84  	
85  }