Filtering Activity Instances for every User Role

This chapter demonstrates how to filter activity instances for every role (grant) the user is assigned to.

Example API

To provide a solution, you can use the following code snippets:

  1. Use the UserQuery to retrieve the attribute account with a specific login name. Get all users and create an activity query.
       UserQuery userQuery = new UserQuery();
    userQuery.getFilter().add(UserQuery.ACCOUNT.isEqual("<username>"));
       
  2. Get the user:
    Users users = queryService.getAllUsers(userQuery);
    User user = (User) users.get(0);
  3. Get user grants:
    List grants = user.getAllGrants();
  4. Use ActivityInstanceQuery to create the activity query:
    ActivityInstanceQuery activityQuery = new ActivityInstanceQuery();
  5. Add an OR filter to the activity query:
    FilterOrTerm orFilter = activityQuery.getFilter().addOrTerm();
  6. Extract single roles and build the query using the PerformingParticipantFilter:
    for (int x = 0; x < grants.size(); x++) {
       String grant = ((Grant) grants.get(x)).getId();
       PerformingParticipantFilter performerFilter = PerformingParticipantFilter.forModelParticipant(grant);
       orFilter.or(performerFilter);
    }
  7. Find the activities for user roles:
    ActivityInstances activeInstances = queryService.getAllActivityInstances(activityQuery);
  8. Get single activity information:
    for (int y = 0; y < activeInstances.getSize(); y++) {
       ActivityInstance activity = (ActivityInstance) activeInstances.get(y);
       System.out.println(y +") Activity OID: "+ activity.getOID() +" / Performer: "+ activity.getPerformedByName());
    }

Alternative Solution

Another option is to use the following code:

  1. Use the ServiceFactoryLocator to create the query service:
    ServiceFactory serviceFactory = ServiceFactoryLocator.get("motu", "motu");
    QueryService queryService = serviceFactory.getQueryService();
  2. Use ActivityInstanceQuery to create the activity query:
    ActivityInstanceQuery activityQuery = new ActivityInstanceQuery();
  3. Add an OR filter to the activity query using the PerformingParticipantFilter and the PerformingUserFilter:
    FilterOrTerm orFilter = activityQuery.getFilter().addOrTerm();
    orFilter.add(PerformingParticipantFilter.ANY_FOR_USER);
    orFilter.add(PerformingUserFilter.CURRENT_USER);
  4. Find the activities for user roles:
    ActivityInstances activeInstances = queryService.getAllActivityInstances(activityQuery);
  5. Get single activity information:
    for (int y = 0; y < activeInstances.getSize(); y++) {
    	ActivityInstance activity = (ActivityInstance) activeInstances.get(y);
    	System.out.println(y +") Activity OID: "+ activity.getOID() +" / Performer: "+ activity.getPerformedByName());
    }