We are comfortable with fetching deployments from the repositories. Now, we will become familiar with fetching the process-definitions
folder from the repository; you can paste the URL http://localhost:8080/activiti-rest/service/repository/process-definitions
in RESTClient. If an error 400 occurs, as shown in the following screenshot, we will have to modify the activiti-rest
folder:
To resolve this error, we have to perform the following steps:
activiti-context.xml
file located in the ../apache-tomcat-7.0.37/webapps/activiti-rest/WEB-INF/classes
folder, as shown in the following screenshot:actviti-context.xml
file by placing the following code in the processEngineConfiguration
bean:<property name="customFormTypes"> <list> <bean class="org.activiti.explorer.form.UserFormType"/> <bean class="org.activiti.explorer.form.ProcessDefinitionFormType"/> <bean class="org.activiti.explorer.form.MonthFormType"/> </list> </property>
activiti-context.xml
file should look as shown in the following screenshot:activity-context.xml
file, you need to add the activity-explorer{version}.jar
file to the ../apache-tomcat-7.0.37/webapps/activiti-rest/WEB-INF/lib
folder.process-definitions
by setting the GET method along with the URL ../repository/process-definitions
, as shown in the following screenshot:process-definitions
within a class, we need to create a Process_Deployment
class in which we will create a method to fetch the process definitions, as shown in the following code:private static String REST_URI = "http://localhost:8080/activiti-rest/service"; private static ClientResource getClientResource(String uri) { ClientResource resource = new ClientResource(uri); resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "kermit", "kermit"); return resource; } public static JSONArray getProcessDefinitions() throws JSONException, IOException { String uri = REST_URI + "/repository/process-definitions"; Representation response = getClientResource(uri).get(MediaType.ALL); JSONObject object = new JSONObject(response.getText()); if (object != null) { JSONArray processArray = (JSONArray) object.get("data"); return processArray; } return null; }
@Test public void processDefinitons() throws Exception { JSONArray processArray = Process_Deployment.getProcessDefinitions(); for (int i = 0; i < processArray.length(); i++) { JSONObject jsonObject = (JSONObject) processArray.get(i); assertNotNull(jsonObject); System.out.println(jsonObject.toString()); } }
repository/process-definition/{processeId}
, as shown in the following screenshot:getProcessDefinitionById
method in the Process_Deployment
class, which will retrieve the process from the repository based on the process ID provided, as shown in the following code:public static JSONObject getProcessDefinitionById(String processId) throws JSONException, IOException { String uri = REST_URI + "/repository/process-definitions/"+ processId; Representation response = getClientResource(uri).get(MediaType.APPLICATION_JSON); JSONObject object = new JSONObject(response.getText()); if (object != null) { return object; } return null; }
processDefinitionById
method in the Test REST API class to test the results, as shown in the following code:@Test public void processDefinitionById() throws Exception { JSONObject jsonObject = Process_Deployment.getProcessDefinitionById("createTimersProcess:1:37"); assertNotNull(jsonObject); System.out.println(jsonObject.toString()); }
updateProcess
method to update the process in the Process_Deployment
class and provide the processId
argument for which we want to perform the update, as shown in the following code:public static JSONObject updateProcess(String processId) throws JSONException, IOException { String uri = REST_URI + "/repository/process-definitions/" + processId; JSONObject updateData = new JSONObject(); updateData.put("category", "Updated Category"); Representation response = getClientResource(uri).put(updateData); JSONObject object = new JSONObject(response.getText()); if (object != null) { return object; } return null; }
update
in the Test REST API class file to perform the testing of the update
method, as shown in the following code:@Test public void update() throws Exception{ System.out.println("Before Updating Category"); JSONObject object1 = Process_Deployment. getProcessDefinitionById ("vacationRequest:1:33"); assertNotNull(object1); System.out.println(object1.toString()); System.out.println("After Updating Category"); JSONObject object2 = Process_Deployment.UpdateProcess("vacationRequest:1:33"); assertNotNull(object2); System.out.println(object2.toString()); }
Category
parameter should be updated with the value provided in the updateProcess
method, which will produce the output as shown in the following screenshot:public static JSONObject suspendProcessDefination(String id) throws JSONException, IOException { String uri = REST_URI + "/repository/process-definitions/" + id; JSONObject my_data = new JSONObject(); my_data.put("action", "suspend"); my_data.put("includeProcessInstances", "false"); Representation response = getClientResource(uri).put(my_data); JSONObject object = new JSONObject(response.getText()); if (object != null) { return object; } return null; }
action
parameter to suspend the process. This action will suspend the process so that it cannot be executed. If you want to activate the process, you need to change the value of the action
parameter from suspend
to activate
, as shown in the following code:public static JSONObject activateProcessDefination(String id) throws JSONException, IOException { String uri = REST_URI + "/repository/process-definitions/"+ id; JSONObject my_data = new JSONObject(); my_data.put("action", "activate"); my_data.put("includeProcessInstances", "true"); Representation response = getClientResource(uri).put(my_data); JSONObject object = new JSONObject(response.getText()); if (object != null) { return object; } return null; }
addCandidate
method in the Process_Deployment
class as follows:public static JSONObject addCandidate(String id) throws JSONException, IOException { String uri = REST_URI + "/repository/process-definitions/" + id + "/identitylinks"; JSONObject my_data = new JSONObject(); my_data.put("user", "kermit"); Representation response = getClientResource(uri).post(my_data); JSONObject object = new JSONObject(response.getText()); if (object != null) { return object; } return null; }