Example work queue that bulk inserts its work items

The following Gosu code is an example of a custom work queue that uses bulk insert to write work items for a batch to its work queue. This custom work queue class derives from the base class BulkInsertWorkQueuBase instead of from WorkQueueBase. The code provides an implementation of the buildBulkInsertSelect method instead of findTargets for its writer logic. The code provides an implementation of the processWorkItemMethod for its worker logic.

Bulk insert work queues like this one use the StandardWorkItem entity type for its work queue table. To use a custom work queue, you must implement createBatchProcess method to tell PolicyCenter in which table to insert the work items created from the SELECT statement.

The unit of work for this work queue is an activity that has not been viewed for five days or more. The process sends an email to the user assigned to the activity, and then it sets the escalation date on the activity to the current date. The process updates entity data, so the processWorkItemMethod uses the runWithNewBundle API.

uses gw.api.database.Query
uses gw.api.database.Relop
uses gw.api.email.EmailUtil
uses gw.processes.BulkInsertWorkQueueBase
 
/**
 * An example of a process implemented as a work queue that sends email
 * to assignees of activities that have not been viewed for five days or more.
 */
class MyOptomizedActivityEmailWorkQueue extends BulkInsertWorkQueueBase <Activity, StandardWorkItem> {
 
  /**
   * Let the base class register this type of custom batch processing by
   * passing the batch processing type, the entity type the work queue item, and
   * the target type for the units of work.
   */
  
  construct() {
    super ( BatchProcessType.TC_NOTIFYUNVIEWEDACTIVITIES, StandardWorkItem, Activity )
  }
    
  /**
   * Select the units of work for a batch run: activities that have not been
   * viewed for five days or more.
   */
  override function buildBulkInsertSelect(builder : Object, args: List<Object>) {
    excludeDuplicatesOnStandardWorkItem(builder, true);     // if using StandardWorkItem
    extractSourceQuery(builder).compare( 
          Activity#LastViewedDate.PropertyInfo.Name, Relop.LessThanOrEquals,
          java.util.Date.Today.addBusinessDays(-5) )
  }
   
  /**
   * Process a unit of work: an activity not viewed for five days or more
   */
  override function processWorkItem(workItem: StandardWorkItem) {
    // Extract an object referenct to the unit of work: an Activity instance
    var activity = extractTarget(workItem) // Convert the ID of the target to an object reference
        
    if (activity.AssignedByUser.Contact.EmailAddress1 != null) {
      // Send an email to the user assigned to the activity
      EmailUtil.sendEmailWithBody(null, activity.AssignedUser.Contact,                     // To:
            null,                                                                          // From:
            "Activity not viewed for five days",                                           // Subject:
             "See activity" + activity.Subject + ", due on " + activity.TargetDate + ".")  // Body:
    }
  }
}