Time for action – working with processes

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:

Time for action – working with processes

To resolve this error, we have to perform the following steps:

  1. If you get the preceding error, first of all, modify your 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:
    Time for action – working with processes
  2. Edit the 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>
  3. Your modified activiti-context.xml file should look as shown in the following screenshot:
    Time for action – working with processes
  4. After editing the 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.
  5. Now, restart the Tomcat server and from the RESTClient, you can retrieve process-definitions by setting the GET method along with the URL ../repository/process-definitions, as shown in the following screenshot:
    Time for action – working with processes
  6. On navigating to the preceding REST URL, you should be able to view the list of process definitions available within the repository as shown in the following screenshot:
    Time for action – working with processes
  7. To access 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;
      }
  8. To fetch the process definitions, we need to create a test method in the test file as follows:
    @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());
        }
      }
  9. On testing the method, we should get all the processes deployed in the repository. All the processes will be displayed, as shown in the following screenshot:
    Time for action – working with processes
  10. We can also fetch a specific process from the repository by providing the process ID with the GET method and the URL repository/process-definition/{processeId}, as shown in the following screenshot:
    Time for action – working with processes
  11. We will create an implementation class with the 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;
      }
  12. Now, we will create the 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());
      }
  13. So, on execution of the test case, we get the details of the process specified within it, as shown in the following screenshot:
    Time for action – working with processes
  14. Using the REST API, we can not only fetch the process definitions, but also update the processes by setting the Method type to PUT. We will create the 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;
      } 
  15. Create a test method 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());
    }
  16. On executing the test class, the Category parameter should be updated with the value provided in the updateProcess method, which will produce the output as shown in the following screenshot:
    Time for action – working with processes
  17. As we can update the process by setting the Method value to PUT, we can also manage the action parameter using the PUT method as follows:
    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;
    }
  18. In the preceding code, we have set the 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;
      }
  19. Using the REST API, we can also assign users to the business process. Now, we will see how to assign users to the business process. We will create the 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;
      }

What just happened?

So far, we have used the REST API to retrieve and edit the process using the PUT method. We have also gone through updating the action parameter by specifying an action to the process, such as suspending and activating the process or adding users to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset