Skip to main content

Workflow Example 2

Create Workflow Definition

Let create a workflow that monitors specified folders and when a video file appears in some of them transcodes it with selected transcoding template.

image.png

LAPIS firesemits BPMN signals related to objects'an life-cycleobject's andlifecycle. The workflow will monitor two ofspecific them - objectCreated (fired when new object appears in the system) and objectUpdated (fired when existing object is modified) - bysignals using Signal Start Events.:

When
  1. objectCreated – Triggered when a new object is createdadded to the system.

  2. objectUpdated – Triggered when an existing object is modified.

When an object is created, the workflow will verify whether it is linked to one of the monitored folders. Similarly, when an existing object is modified, the workflow will check if it is linked to oneany of the monitored folders, similarly when an existing object is modified workflow will check if it is linked to some of the monitored folders,folders.

Let assume we have some folders:

image.png

For this example we will monitor "Folder 1"1" and "Folder 2"2". We will need their IDs which we can get by displaying the ID column:

image.png

We will also use a Transcoding Template holding the transcoding parameters, so get its ID, too:

image.png

Now let's create a new workflow definition as shown:

  1. Set general properties Name, ID and Executable:
    image.png

  2. Important is also to add the following "Extension Property"Property", so that workflow history is not flooded with all workflow instances that are created for allany createobject andcreation updateor events:modification:
    image.png

  3. Set the properties for Object Created signal start event:

    image.png


  4. Set the properties for Object Updated signal start event:

    image.png


  5. Set the following properties to object created Yes flow:


    image.png

    The script is:
    def folders = [
      '679d0faa3063823d2ba9a45c@1', // ID of "Folder 1"
      '679d0fb73063823d2ba9a45e@1', // ID of "Folder 2"
    ]
    
    def containers = (object.get('containers') ?: []).collect { it.id.toString() }
    
    def addedToAnyFolder = folders.intersect(containers)
    
    (object.unwrap().isInstanceOf('Video')) && !['draft', 'newObjectDraft'].contains(object.get('lifeCycleStatus')) && object.get('containers') != null && addedToAnyFolder
    Replace/add IDs of the folders you want to be monitored in folders variable.
     

    The object variable is defined for the workflow instances that start with objectCreated and/or objectUpdated signal events. withWith object.get() you can get any property value from the object (querying for non existing or undefined properties' values will return null), so similarly you may check for other object's properties and act as needed. In this example containers property holds IDs of folders (and/or other containers) to which this object is linked.


    The last script statement must evaluate to Boolean which sets the outcome for condition check. In this example it checks if the newly created object is of type 'Video', if it is not in the process of creation (autosaves during entering data for the new object will have such state), and finally the needed condition (object is linked to one of the monitored folders in this example).

  6. Set the following properties to object updated Yes flow:

    image.png

    The script is:
    def folders = [
      '679d0faa3063823d2ba9a45c@1', // ID of "Folder 1"
      '679d0fb73063823d2ba9a45e@1', // ID of "Folder 2"
    ]
    
    def originalContainers = (originalObject.get('containers') ?: []).collect { it.id.toString() }
    def containers = (object.get('containers') ?: []).collect { it.id.toString() }
    
    def intersectOriginal = folders.intersect(originalContainers) ?: []
    def intersectNew = folders.intersect(containers) ?: []
    
    def addedToAnyFolder = intersectOriginal.size() == 0 && intersectNew.size() > 0
    
    execution.hasVariable('originalObject') && (object.unwrap().isInstanceOf('Video')) && !['draft', 'newObjectDraft'].contains(object.get('lifeCycleStatus')) && addedToAnyFolder
    Match folders variable to the previous script. (Note that there is way to set this variable ones in the workflow and use it on other places, but for simplicity we have put it in both scripts).

  7. Set the following properties to the scripting task:

    image.png

    The script is:
    def operationId = Context.operationHelpers.getOperationByName('TranscodeOperation', godUser).id
    def templateId = '679d218d3063823d2ba9a46d@1' // ID of the transcoding template
    Context.operations.executeOperationById(operationId, [object.id].toImmutableList(), helpers.getSubject(subject), [  templateId: helpers.relationFromString(templateId),
    ].toImmutableMap(), null)

    Change the templateId with your own.

You can download the BPMN of the above example from here.

Test the new workflow
  1. Upload a video file to one of the monitored folders:

    image.png


  2. The transcoding should start after upload is finished:

    image.png


  3. You can also drag-drop an existing video in one of the monitored folders and the workflow should start the transcoding for it.