IBM BPM, V8.0.1, All platforms > Programming IBM BPM > Developing using the web service API > Samples

Retrieving user tasks

The following sample shows how to use the web API to retrieve tasks for a process participant from the process server.

package teamworks.samples.scenarios;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import teamworks.samples.client.ClientUtilities;
import teamworks.samples.client.repository.ClientRepository;
import teamworks.samples.client.repository.ClientRepositorySynchronizer;
import teamworks.samples.client.repository.SimpleClientRepository;

import com.lombardisoftware.webapi.SavedSearch;
import com.lombardisoftware.webapi.Search;
import com.lombardisoftware.webapi.SearchColumn;
import com.lombardisoftware.webapi.SearchColumnMetaData;
import com.lombardisoftware.webapi.SearchCondition;
import com.lombardisoftware.webapi.SearchOrdering;
import com.lombardisoftware.webapi.SearchOrder;
import com.lombardisoftware.webapi.SearchResultRow;
import com.lombardisoftware.webapi.SearchResults;
import com.lombardisoftware.webapi.SearchableType;
import com.lombardisoftware.webapi.Task;
import com.lombardisoftware.webapi.TaskStatus;
import com.lombardisoftware.webapi.UserConfiguration;
import com.lombardisoftware.webapi.WebAPI;

/**
 * This scenario demonstrates using the WebAPI to retrieve tasks for * the user from the Teamworks server.
 * 
 */
public class RetrieveUserTasks extends Scenario {
    
    /**
     * Scenario 1: Uses the ExecuteSavedSearch API to retrieve the user's tasks.
     */
    public void testScenario1() throws Exception {
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();

        // Retrieve the UserConfiguration which contains a number         // of pieces of information batched together in one call 
        UserConfiguration userConfiguration = webAPI.getUserConfiguration();

        // The user chooses the saved search - Inbox shows tasks that have a new or received status 
        SavedSearch savedSearch = ClientUtilities.findByName(
                userConfiguration.getSavedSearches(),                 "Inbox");
        
        // Modify the search to return results organized by Task instead         // of by Process Instance
        Search search = savedSearch.getSearch();
        search.setOrganizedByType(SearchableType._Task);
        
        // Execute the Inbox saved search, retrieving the first 25 results 
        SearchResults results = webAPI.executeSearch(savedSearch.getSearch(), 25, null);

        // Print out the column headers
        for(SearchColumnMetaData column : results.getColumns()) {
            System.out.print(column.getType() + " " + column.getName());
            System.out.print("\t");
        }         System.out.println();
        
        // Print out the rows
        for(SearchResultRow row : results.getRows()) {
            for(Object value : row.getValues()) {
                System.out.print(value != null ? value : "-");
                System.out.print("\t");
            }             System.out.println();
        }     } 
    /**
     * Scenario 2: Uses the ExecuteSearch API to retrieve the user's tasks.
     */
    public void testScenario2() throws Exception {
        
        // Create a new search instance         Search search = new Search();

        // Set it to be organized by Task - the other possibility is ProcessInstance
        search.setOrganizedByType("Task");

        // Specify the columns to return - these are the same columns as are defined
        // in the stock Inbox search
        search.setColumns(new SearchColumn[] {
                new SearchColumn("ProcessInstance", "Name", null),                 new SearchColumn("Process", "Name", null),                 new SearchColumn("ProcessInstance", "DueDate", null),                 new SearchColumn("Task", "Subject", null),                 new SearchColumn("Task", "Priority", null),                 new SearchColumn("Task", "DueDate", null),                 new SearchColumn("Task", "Status", null),                
        
        // Set the conditions - search only tasks with status of new or received
        search.setConditions(new SearchCondition[] {
                new SearchCondition(new SearchColumn("Task", "Status", null),                         "EQUALS",                         TaskStatus._New_or_Received)
        
        // Set the results to be ordered by task priority descending (highest
        // priority to lowest) and then by due date ascending (closest due
        // date first)
        search.setOrderBy(new SearchOrdering[] {
                new SearchOrdering(new SearchColumn("Task", "Priority", null),                         SearchOrder.DESCENDING),                 new SearchOrdering(new SearchColumn("Task", "DueDate", null),                         SearchOrder.ASCENDING)                        
                
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();

        // Execute the Inbox saved search, retrieving the first 25 results 
        SearchResults results = webAPI.executeSearch(search, 25, null);

        // Print out the column headers
        for(SearchColumnMetaData column : results.getColumns()) {
            System.out.print(column.getType() + " " + column.getName());
            System.out.print("\t");
        }         System.out.println();
        
        // Print out the rows
        for(SearchResultRow row : results.getRows()) {
            for(Object value : row.getValues()) {
                System.out.print(value != null ? value : "-");
                System.out.print("\t");
            }             System.out.println();
        }     }     
    /**
     * Scenario 3: Use the getTasksForSavedSearch API to retrieve the user's tasks      */
    public void testScenario3() throws Exception {
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();

        // Retrieve the UserConfiguration which contains a number         // of pieces of information batched together in one call 
        UserConfiguration userConfiguration = webAPI.getUserConfiguration();
        
        // The user chooses the saved search - Inbox shows tasks that have a new or received status 
        SavedSearch savedSearch = ClientUtilities.findByName(
                userConfiguration.getSavedSearches(),                 "Inbox");
        
        // Retrieve all of task objects for the saved search 
        Task[] tasks = webAPI.getTasksForSavedSearch(savedSearch.getId());
                                
        // Print out the first twenty-five results         System.out.println("Process Instance Name\t" +
                "Process Name\t" +
                "Process Instance DueDate\t" + 
                "Task Subject\t" + 
                "Task Priority\t" + 
                "Task DueDate\t" + 
                "Task Status\t");
        
        int max = Math.min(tasks.length, 25);
        for(int i = 0; i < max; ++i) {
            Task task = tasks[i];
            
            System.out.print(task.getProcessInstance().getName());
            System.out.print("\t");
            
            System.out.print(task.getProcessInstance().getProcess().getName());
            System.out.print("\t");
            
            System.out.print(task.getSubject());
            System.out.print("\t");
            
            System.out.print(task.getPriority());
            System.out.print("\t");
            
            System.out.print(task.getDueDate());
            System.out.print("\t");
            
            System.out.print(task.getStatus());
            System.out.print("\t");

            System.out.println();
        }        
    }     
    /**
     * Scenario 4: Use the synchronizeTaskLists API to retrieve the user's tasks.
     * This operation is intended for clients that may be occasionally connected
     * to the network and would like to continue operating when disconnected.
     */
    public void testScenario4() throws Exception {
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();

        // Retrieve the UserConfiguration which contains a number         // of pieces of information batched together in one call 
        UserConfiguration userConfiguration = webAPI.getUserConfiguration();
        
        // Include all saved searches except for the History saved search.
        Set<Long> savedSearchIds = new TreeSet<Long>();
        for(SavedSearch savedSearch : userConfiguration.getSavedSearches()) {
            if (!savedSearch.getName().equals("History")) {
                savedSearchIds.add(savedSearch.getId());
            }         } 
        // Create a client repository instance.  In more sophisticated
        // offline capable clients, the ClientRepository implementation
        // would be backed by a file or database.
        ClientRepository clientRepository = new SimpleClientRepository();
        
        // Create a synchronizer to do the actual work of synchronizing
        // the repository with the server         ClientRepositorySynchronizer synchronizer = 
            new ClientRepositorySynchronizer(clientRepository, webAPI, savedSearchIds);
        
        // Synchronize
        synchronizer.run();
        
        
        // The user chooses the saved search - Inbox shows tasks that have a new or received status 
        SavedSearch savedSearch = ClientUtilities.findByName(
                userConfiguration.getSavedSearches(),                 "Inbox");

        // Retrieve all of task objects for the saved search 
        List<Long> taskIds = clientRepository.loadTaskIdsForSavedSearch(savedSearch.getId());
                                
        // Print out the first twenty-five results         System.out.println("Process Instance Name\t" +
                "Process Name\t" +
                "Process Instance DueDate\t" + 
                "Task Subject\t" + 
                "Task Priority\t" + 
                "Task DueDate\t" + 
                "Task Status\t");
        
        int max = Math.min(taskIds.size(), 25);
        for(int i = 0; i < max; ++i) {                
            Task task = clientRepository.loadTask(taskIds.get(i));
            
            System.out.print(task.getProcessInstance().getName());
            System.out.print("\t");
            
            System.out.print(task.getProcessInstance().getProcess().getName());
            System.out.print("\t");
            
            System.out.print(task.getSubject());
            System.out.print("\t");
            
            System.out.print(task.getPriority());
            System.out.print("\t");
            
            System.out.print(task.getDueDate());
            System.out.print("\t");
            
            System.out.print(task.getStatus());
            System.out.print("\t");

            System.out.println();
        }        
    }     }

Samples