Adding search and sort capabilities

The GET operation on the /activities endpoint on the Activities API is a basic operation that returns the list of activities in Guidewire PolicyCenter. However, suppose that you want the API to (optionally) restrict the returned activity list by assigned user and to (optionally) specify a primary sort column and direction. To provide this functionality, you need to update the following items:
  • The GET operation for /activities in the API Swagger schema
  • The getActivities method in handler class ExampleActivitiesApiHandler

Adding additional parameters to the Swagger schema

Guidewire defines the API schema in file activityAPI-1.0.swagger.yaml in the following location in the Studio Project window:
  • configuration > config > Integration > apis > mc > activityapi

The following code example adds sortBy and sortDirection parameters to the GET /activities operation. It uses several enum constructions to define the behavior of the parameters.

/activities
  get:
    summary: "Returns a list of activities"
    description: "Returns a list of activities"
    operationId: getActivities
    parameters:
    - name: assignedUser
      in: query
      type: string
    - name: sortBy
      in: query
      type: string
      enum: ["assignedUser", "escalated", "priority", "status", "subject", "targetDate"]
      default: "priority"
    - name: sortDirection
      in: query
      type: string
      enum: ["asc", "desc"]
      default: "asc"
    responses:
      '200':
        description: "Returns a list of activities"
        schema:
          type: array
          items:
            $ref: "activities#/definitions/ActivitySummary"

Adding functionality to handler class method getActivities

The following code sample updates the ExampleActivitiesApiHandler.getActivities method to provide sort and search capabilities. This class exists in the following directory in the Studio Project window:
  • configuration > gsrc > mc > activityapi
function getActivities(assignedUser : String, sortBy : String, sortDirection : String) : List<TransformResult> {

  var query = Query.make(Activity)
  var credential = Query.make(Credential)
        .compare(Credential#UserName, Relop.Equals, assignedUser)
        .select().AtMostOneRow

  if (credential == null) {
    throw new BadInputException("No user was found with username " + assignedUser)
  }

  var user = Query.make(User)
        .compare(User#Credential, Relop.Equals, credential)
        .select().AtMostOneRow
  
  query.compare(Activity#AssignedUser, Relop.Equals, user)

  var resultSet = query.select()

  var sortColumn : IQuerySelectColumn
  switch(sortBy) {
    case "assignedUser":
      sortColumn = QuerySelectColumns.path(Paths.make(Activity#AssignedUser))
      break
    case "escalated":
      sortColumn = QuerySelectColumns.path(Paths.make(Activity#Escalated))
      break
    case "priority":
      sortColumn = QuerySelectColumns.path(Paths.make(Activity#Priority))
      break
    case "status":
      sortColumn = QuerySelectColumns.path(Paths.make(Activity#Status))
      break
    case "subject":
      sortColumn = QuerySelectColumns.path(Paths.make(Activity#Subject))
      break
    case "targetDate":
      sortColumn = QuerySelectColumns.path(Paths.make(Activity#TargetDate))
      break
    default:
      throw new IllegalArgumentException("Unexpected sortBy argument " + sortBy)
  }

  if ("desc" == sortDirection) {
    resultSet.orderByDescending(sortColumn)
  } else {
    resultSet.orderBy(sortColumn)
  }

  var mapper = JsonConfigAccess.getMapper("mc.activityapi.activityAPI-1.0", "ActivitySummary")
  return mapper.transformObjects(resultSet)
}

Testing search and sort

It is possible to test the updated search and sort functionality in Swagger UI. If running the application server on a local machine, navigate to the following URL:
  • localhost:8180/pc/resources/swagger-ui
Enter the URL for the Activities API in the Explore field and test out the GET /activities operation. With the updated getActivities method, the Swagger UI now shows the following interactive parameter fields:
Field Test case
assignedUser Enter a user name to filter the list of returned activities so that it only includes activities assigned to the designated user.
sortBy Chose a value from the drop-down list sort activity list by a specific parameter.
sortDirection Chose a value from the drop-down list to determine the list sort order (either ascending or descending).