Updating an activity
So far, the Activities API provides a few basic
GET and
POST operations for activities. However, it is common to want to update
an application resource as well. This action requires a PATCH operation on
the specified resource. The newly designed API adds a PATCH
/activities/{activityId} operation that provides the following functionality:- It marks the schema properties on the
ActivityDetailJSON object that are not for update as read-only - It marks the schema properties on the
ActivityDetailJSON object that are non-nullable as required - It adds a
checksumproperty to theActivityDetailJSON object that maps to the bean version of the activity - It returns the result as a
PATCH /activities/{activityId}operation
Adding read-only properties to the JSON schema
The JSON schema file for the Activities API, activityAPI-1.0.schema.json, exists in the following directory in the Guidewire Studio Project window:
The following example schema updates the JSON
ActivityDetail object to
mark some properties as read-only, as well as, add a checksum
property."ActivityDetail" : {
"type" : "object",
"properties" : {
"approvalRationale" : {
"type" : "string"
},
"assignedUser" : {
"$ref" : "#/definitions/AssignedUser",
"readOnly" : true
},
"checksum" : {
"type" : "string"
},
"description" : {
"type" : "string"
},
"escalationDate" : {
"type" : "string",
"format" : "date-time"
},
"mandatory" : {
"type" : "boolean"
},
"priority" : {
"type" : "string",
"x-gw-type" : "typekey.Priority"
},
"publicId" : {
"type" : "string",
"readOnly" : true
},
"relatedAccount" : {
"$ref" : "#/definitions/RelatedAccount",
"readOnly" : true
},
"relatedContact" : {
"$ref" : "#/definitions/RelatedContact",
"readOnly" : true
},
"relatedJob" : {
"$ref" : "#/definitions/RelatedJob",
"readOnly" : true
},
"relatedPolicy" : {
"$ref" : "#/definitions/RelatedPolicy",
"readOnly" : true
},
"relatedPolicyPeriod" : {
"$ref" : "#/definitions/RelatedPolicyPeriod",
"readOnly" : true
},
"status" : {
"type" : "string",
"x-gw-type" : "typekey.ActivityStatus",
"readOnly" : true
},
"subject" : {
"type" : "string"
},
"targetDate" : {
"type" : "string",
"format" : "date-time"
}
}
}Adding a PATCH operation to the API schema
The API Swagger schema file, activityAPI-1.0.swagger.yaml, exists in
the following directory in Guidewire Studio Project window:
The following code sample illustrates how to add a new
PATCH operation to
the /activities/{activityId} endpoint in the Swagger
schema./activities/{activityId}:
patch:
summary: "Updates the details of a single activity"
description: "Updates the details of a single activity"
operationId: updateActivity
parameters:
- $ref: "#/parameters/activityId"
- name: body
in: body
required: true
schema:
$ref: "activities#/definitions/ActivityDetail"
responses:
'200':
description: "Returns details of the updated activity"
schema:
$ref: "activities#/definitions/ActivityDetail"Adding 'checksum' to the JSON mapper file
The integration mapper file, activityAPI-1.0.mapping.json, exists in
the following directory in the Studio Project window:
To add the new
checksum property to the mapper file, add the following
code underneath properties in the ActivityDetail
definition."checksum" : {
"path" : "Activity.BeanVersion.toString()"
}Creating the necessary JSON wrapper classes
The API handler code uses wrapper classes for the JSON objects that the code manipulates.
You must create the necessary wrapper classes before you update the Activity API handler
class. See Generate schema wrapper classes for details.
Important: You must do this step before you start to update the
handler class updateActivity method.
Adding method updateActivity to the handler class
To provide functionality for the PATCH /activity/{activityId} operation,
you need to add a method to support the new functionality to the
ExampleActivitiesApiHandler handler class. As the
operationId for the PATCH operation is
updateActivity, you need to add an updateActivity
method to the handler class.
The following code sample illustrates how to construct an updateActivity method on ExampleActivitiesApiHandler class.
function updateActivity(activityId : String, body : JsonObject) : TransformResult {
var activity = loadActivityById(activityId)
var activityDetail = ActivityDetail.wrap(body)
gw.transaction.Transaction.runWithNewBundle(\b -> {
activity = b.add(activity)
activity.ApprovalRationale = activityDetail.approvalRationale ?: activity.ApprovalRationale
activity.Description = activityDetail.description ?: activity.Description
activity.EscalationDate = activityDetail.escalationDate ?: activity.EscalationDate
activity.Mandatory = activityDetail.mandatory ?: activity.Mandatory
activity.Priority = activityDetail.priority ?: activity.Priority
activity.Subject = activityDetail.subject ?: activity.Subject
activity.TargetDate = activityDetail.targetDate ?: activity.TargetDate
})
var mapper = JsonConfigAccess.getMapper("mc.activityapi.activityAPI-1.0", "ActivityDetail")
return mapper.transformObject(activity)
}
Next steps
You must do the following to test your work:
- Run
codegento generate the necessary Java wrapper classes for the JSON objects used by the class methods. - Recompile your work.
- Restart the application server.
