Activiti Developer Series: Script Tasks

 

Activiti Developer Series: Script Tasks

 

 

Lesson 1 / Lab Exercise 1: Script Tasks Overview

Script Tasks Overview

Please click the video below.

 

 

Please proceed to the next topic.

How Labs are Formatted for this Module

Please click the video below.

 

 

Please proceed to the next topic.

Hello World Lab 1: Demonstration

Please click the video below.

Please proceed to the next topic.

Development Environment (VM) Configuration Presentation

Please click the video below.

Activiti Profile Login for this Virtual Machine:

Username: [email protected]

Password: demo1

Maven Restoral Procedure:

  1. Stop Maven!
  2. Navigate to directory: /home/alfresco
  3. Verify that the file activitiservicetasksarchive.tar.gz is present in this directory.
  4. Decompress this files in its current location by running the command, tar -xzvf activitiservicetasksarchive.tar.gz
  5. Your Maven project should be back to normal defaults. You will need to begin the Labs once again.

 

Car Parts Application and Process References in Activiti:

Within Activiti, we have preconfigured:

  • an application labeled Car Parts Application
  • a process labeled Car Parts Application

‚Äč

Please review the Virtual Machine specifications below and when done, proceed to the next topic.

 

Virtual Machine Specifications

 

Hardware:

Operating System:

Software:

 

 

POSTGRES Database Schema for the Lab Exercises

The following tables have been created in your virtual machine and pre-populated with some sample data.

 

DB Name: car_parts_provider database

Db Table Name: orders

 

 

DB Name: car_parts_shop

Db Table Name: parts

 

 

DB Name: car_parts_shop

Db Table Name: purchase

 

Once you have completed your review of the specifications, please proceed to the next topic.

Hello World Lab 1: Parameters

To complete this lab exercise you will need to perform the following:

  • Start up the Activiti server by changing into the correct project directory, and executing the correct Maven command.
  • Navigate to localhost:8080/activiti-app to verify that your Activiti instance is running correctly.
  • Login to Activiti using the following credentials:
  • Navigate into your Kickstart App in Activiti.
  • Create a new BPMN Process Model in Kickstart that includes a Start Event, an End Event, and a single Script Task. Make sure you give your process model a title and that you also give your script task a name.
  • Select your Script Task.
  • Select the “Script” attribute in the bottom pane. You should see a dialogue box come up in the window with the title “Change value for ‘Script’”.
  • In the box, type print(“hello world”); This is a line of Javascript code that tells the computer to print the words “Hello World” to the console.
  • Click “Save” in the dialog box to save your Javascript code for your Script Task.
  • In the lower pane, specify the script format by selecting the “Script Format” attribute and typing in “javascript”.
  • Click the checkmark at the top-left corner of the pane to make sure your process model can validate.
  • Save your process model by clicking the Save button at the top-left corner of the pane.
  • If you want to validate the message, deploy your process model using an App in Activiti and check your terminal for the logged message.

If you get lost, you may review the demonstration at any time and follow along, step-by-step.

Congrats! If you were able to get through the above steps, you have completed Lab 1!

Knowledge Check Question 1: Choose the ONE accurate description of a script task.

  • A script task is executed manually in a process by a User.
  • Script tasks are external scripts that reside outside of the BPMN process.
  • When a process execution arrives at a script task, its corresponding script is executed.
  • Script tasks have no known entity in a BPMN process. They are presented as an expression.

Knowledge Check Question 2: Use the dropdown menu to complete the statement.

A script task is interpreted at  which negates the need to deploy code that would require a server restart.

Knowledge Check Question 3: Select the ONE correct answer to complete the statement. Scripts are actually written in Activiti by___________________.

  • modifying the script property of a Script Task object from within the visual editor of a business process model.
  • developing the script in an IDE then importing it into Activiti.
  • opening the Javascript or Groovy editor.
  • developing the script in a terminal window within a specific Activiti directory.

Knowledge Check Question 4: Select the ONE correct answer. Which attribute with in the Activiti Visual Editor to you select to write the script for your script task?

  • Variables attrribute
  • Name attribute
  • Script format attribute
  • Script attribute

Knowledge Check Question 5: Select the ONE correct answer. You are building a Javascript Script Task and you need to enter a designated format into the Script format attribute.

  • Javascript
  • Groovy
  • Java
  • JSR223

Lesson Summary

Please click the video below.

 

 

Please proceed to the next lesson.

Lesson 2: Script Tasks in Detail

When to use a Script Task

Please click the video below.

 

 
 

Please proceed to the next topic.

Javascript vs. Groovy

Please click the video below.

 

 

Please proceed to the next topic.

Demonstration Video: Script Task Development using JavaScript

Please click the video below.

 

 

Please proceed to the next topic.

Demonstration Video: Script Task Development using Groovy

Please click the video below.

 

 

Please proceed to the next topic.

Script Tasks Best Practices

Click the audio above the appropriate paragraph.

 

Introduction

“Just because you can, doesn’t mean you should!”

While each programming language comes with their own unique considerations, there are “best practices” that apply across many languages. 

 

Keep it simple

Use Script Tasks for:

  • well-defined

or

  • atomic tasks

 

Use of powerful scripting languages could:

  • lead to over engineering

or

  • lead to a maintenance nightmare.

 

Don’t Reinvent the Wheel

Don't: rely on script tasks to do things that currently exist in Alfresco Activiti.
Do: use Script tasks to execute a customized solution

 

 

 

 

 

 

 

 

Code at Your Own Risk

You could do serious harm to your system such as:

  • writing files but not closing output streams.
  • leaving database connections open.
  • unintentionally write code that leaks memory or isn’t thread-safe.
  • adversely affect your Alfresco Activiti server.

 

Please proceed to the next topic.

Knowledge Check Question 6: Choose the one correct answer. What is the major advantage of using Script Tasks in a BPMN process?

  • They are easy to code
  • They take up no process memory
  • They require no Server restart upon deployment
  • They are built using a Spring framework

Knowledge Check Question 7: Use the drop-down menus to fill-in the blanks.

You should consider  and  when deciding to use Script Tasks in your BPMN process.

Knowledge Check Question 8: Select all that apply. When should you consider using a Script Task?

  • When you have a single task you need to accomplish
  • When you have code limitations
  • When you need to use an API
  • When you have multiple tasks you need to accomplish
  • When you don't need access to an API
  • When you need to rapid prototype
  • All of the above

Knowledge Check Question 9: Select two languages primarily used to create Script Tasks.

  • Groovy
  • Python
  • JavaScript
  • Ruby
  • Perl

Knowledge Check Question 10: Select two answers. Primarily the best practice is to use Script Tasks for:

  • well-defined tasks
  • tasks immediately before a Service Task
  • use script tasks for multiple tasks
  • atomic tasks
  • many tasks at once in a single script
  • non-database functions

Lesson Summary

Please click the video below.

 

 

Please proceed to the next topic.

Lab Exercise 2: Advanced Script Task Techniques

Lab Overview

In lab 2 you will be building off the concepts you previously learned by creating a new process model and accompanying app as shown below. Hover over the various BPMN components to learn more about them:

Lab Demonstration

Please click video below.

 

 

Please proceed to the next topic.

Lab Guidance

Lab Parameters

  • Make sure Alfresco Activiti is running via the command line. If it isn't, start it up and go to http://localhost:8080/activiti-app to verify.
  • Login to Activiti using the following credentials:
  • Open the Kickstart App in your running Alfresco Activiti instance.
  • Create a new process model as shown in the Overview section for this lab.
  • Open the Kickstart App on the Alfresco Activiti homepage. Create a new process model using the BPMN Editor and name it "Simple Message Process." Fill out the dialog box with the correct settings and click Save.
  • Select your initial Start Event. Under the "referenced form" property in the bottom pane, click the New Form button to create a new form.
  • Title your new form "Message Form." Enter a description if you wish, and then click the Create Form button.
  • In the Form Creation panel that appears, drag a "Text" module into the main pane. Then, drag a "Checkbox" module over as well.
  • Edit the Text box by clicking the pencil icon. Change the Label to read "Message."
  • Edit the Checkbox by clicking the pencil icon. Change the Label to read "Use Groovy?" This will allow the user to decide if they would prefer to use Groovy (if checked) or Javascript (if unchecked) in order to code their Script Task in the process model.
  • Save the form.
  • From your Start Event back in your process model, add an Exclusive Gateway.
  • From the new Exclusive Gateway, use Sequence Flows to connect two separate Script Tasks to the process model. 
  • Title each of your new Script Tasks with the coding language they will use (i.e.-- "Groovy Task" or "Javascript Task"). Make sure you set the "Script Format" property to match.
  • Assign a flow condition (in the Properties pane at the bottom of the screen) to each Sequence Flow pointing to your new Script Tasks. Select "Simple" for your condition type, "Form Condition" for your 'Depends on' field, and then select the appropriate Boolean logic for each of your tasks.
  • For reference, this would be "Use Groovy? equal true" for Groovy and "Use Groovy? not equal true" for Javascript.
  • Before adding your actual scripts to your Script Tasks, continue ahead in your BPMN process model and join the outcome of your Exclusive Gateway using another gateway. Then, from the new Exclusive Gateway, add a User Task and title it "Confirmation Task."
  • For this User Task, create another form from the "Referenced Form" property in the bottom pane. Title the new form "MessageConfirm" and save. In the Form Creation screen that follows, use a Display Text module to display a message to the user indicating what message was sent from your previous form.  When you are finished, save your form and close out of it. Review the demonstration at the 5:30 timestamp if you don't know how to do this.
  • Now, add an End Event to your form from your User Task.
  • Finally, you'll need to create your Groovy and Javascript scripts for each of the Script Tasks. Enter your script content in the "Script" property on the bottom pane. For successful deployment, you will need to meet the following milestones: 
    1. Retrieve the message entered into the first form you created, titled "Message Form." You may use the .getVariable() method to do this.
    2. (Optional) Retrieve the current user ID.  You may use the implicitly-available Spring bean userInfoBean to do this. The .getCurrentUser() method may also be useful to you .
    3. Print the message and, if you decided it was necessary, the current user information.
  • At the top-left of your process model editor window, click the checkmark icon to validate your process model. If it validates correctly, click the Save button right next to it on the left to save your process model.
  • Navigate to the "Apps" tab on the top of the window in Alfresco Activiti.
  • Create a new App, and include your updated process model. Then, save and publish your new App.
  • Return to the Alfresco Activiti homepage.
  • Select your new App to open it.
  • Click the 'Start+' button at the top of your screen to run a new process instance.
  • When the form appears as part of the process, select any part you wish.
  • Click the "Start Process" button at the bottom of the dialog box when you have filled out the form.
  • If you see the correct message in your console upon completion of the process, congratulations! You have completed Lab 2.


NOTE: If you get lost, you may review the demonstration at any time and follow along, step-by-step. The "Solution Files" may also be copy-pasted at any time from the Desktop/Class/Activiti/Script Tasks/Solutions/Lab 2 directory. There are two solution files for the Script Tasks  lab2.groovy.txt and lab2.js.txt.

Auto Parts Scenario Introduction and Overview

Scenario Introduction and Overview

Please click the audio toolbar below for narration!

 

 

In this next lab you will advance your existing development skills by adding Listeners to an existing  scenario based BPMN workflow. The scenario you will be using is based on an Auto Parts ordering system.

The workflow is actually broken-out by conditions in the Auto Parts order process and they are:

  1.  All items requested are in stock
  2. None of the requested items are in stock
  3. Some of the requested items are in stock

 

 

Specific process steps (reference numbers) for each condition:

 

All items requested are in stock:

  • (Step 1) The start event uses a custom form (test1) that allows users to view and select parts.
  • (Step 2) The parts selected are available in our local parts inventory.
  • (Step 3) The selected parts are reserved and the database is updated.
  • (Step 4) An email is sent to the workflow process initiator stating that the requested items are in stock.
  • (Step 5) A document is created that reports on the current inventory conditions.
  • (Step 6) The process ends.

None of the requested items are in stock:

  • (Step 1)The start event uses a custom form (test1) that allows users to view and select parts.
  •  (Step 7) None of the selected parts are in stock in our local parts inventory.
  •  (Step 8) The parts supplier db is checked, the parts are ordered and the db is updated. 
  • (Step 9) An email is sent to the workflow process initiator stating that the items are not in stock in the local inventory.
  • (Step 10) A document is created that reports on the current inventory conditions.
  • (Step 11) The process ends.

Some of the requested items are in stock:

  • (Step 1)The start event uses a custom form (test1) that allows users to view and select parts.
  • (Step 12) Some of the parts requested are in stock in the local parts inventory. 
  • (Step 13) The selected parts are reserved and the database is updated.
  • (Step 14) The remaining parts that have been selected and that are out of stock in the local inventory are ordered through the parts supplier.
  • (Step 15) The parts supplier db is checked, the parts are ordered and the db is updated. 
  • (Step 16) An email is sent notifying the workflow process initiator that some of the items are in stock.
  • (Step 17) A document is created that reports on the current inventory conditions.
  • (Step 18) The process ends.

Note: In the upcoming lab you will complete this scenario by adding the Script Task and testing the code you built.

Please proceed to Lab 3!

 

Lab Exercise 3: Script Task to Report Inventory Control Quantities

Overview

Please click the video below.

 

Please proceed to the next sub-topic.

Demonstration

Please click the video below.

 

 

Please proceed to the next topic.

Guidance

Lab Parameters

  • Make sure Alfresco Activiti is running via the command line. If it isn't, start it up and go to http://localhost:8080/activiti-app to verify.
  • Start the SMTP server using the fakeSMTP-2.0.jar located on the desktop by opening a new terminal window and running the command 'java -jar ~/Desktop/fakeSMTP-2.0.jar. Once the Fake SMTP Server dialog box appears, click on the 'Start Server' button.
  • Login to Activiti using the following credentials:
  •  Start the Kickstart App in your running Alfresco Activiti instance.
  • Select the provided "Car Parts Process."
  • Create and add a Script Task to the process model and title it "Log Transaction," just before the End Event that terminates the process.
  • Click on the newly-created Script Task to select it.
  • In the grey Properties panel at the bottom, type in "javascript" for your Script Format property.
  • Next, select the Script property. A dialog window should appear with a space to enter your code.
  • Write a block of code in the window that accomplishes the following:
    • Utilize the two implicit variables that come along with this application. execution is mapped to the current execution context; availableItemsInStock is given to you by the application earlier in the process.
    • Import Alfresco Activiti's Java SecurityUtils to get the current user information. This will allow you to use the current user's email for execution of the Script Task.
    • Then, you'll need to construct a JSON object that includes the following information about the user: current timestamp, tenant ID from the database (execution variable), the email address from the current user, the part ID from the database (execution variable), the unit price of the part from the database (execution variable), the quantity from the database (execution variable), and the given variable for availableItemsInStock.
    • Once you have constructed your JSON object, print your transaction object to the console and debug it if necessary.
    • Import the Java classes you'll need for writing information to files.
    • Define the file system location and file name of your transaction log.
    • Print the file path location to the console. Debug if necessary.
    • Use a try-catch-finally block to create and write the transaction log to a file. Review the previous demonstration if you don't know how to do this.
    • Finally, print a message to the console indicating that the Script Task was successful.
  • Click 'Save' in the dialog window to save your code.
  • At the top-left of your process model editor window, click the checkmark icon to validate your process model. If it validates correctly, click the Save button right next to it on the left to save your process model.
  • Navigate to the "Apps" tab on the top of the window in Alfresco Activiti.
  • Create a new App, and include your updated process model. Then, save and publish your new App.
  • Return to the Alfresco Activiti homepage.
  • Select your new App to open it.
  • Click the 'Start+' button at the top of your screen to run a new process instance.
  • When the form appears as part of the process, select any part you wish.
  • Click the "Start Process" button at the bottom of the dialog box when you have filled out the form.
  • Once the process is complete, navigate back to your terminal console to check for your Script Task message indicating that it executed successfully. If you get an error or the process did not complete successfully, debug your Script Task code and try again.
  • If you see the correct message in your console, congratulations! You have completed Lab 3.

 

If you get lost, you may review the demonstration at any time and follow along, step-by-step. The "Solution File" may also be copy-pasted at any time from the Desktop/Class/Activiti/Script Tasks/Solutions/Lab 3 directory. The file name for this lab is logTXScript.js.txt

Module Summary

Module Summary

Please click the audio toolbar below.

 

Let’s revisit the module key takeaways!

Lesson 1:

  • Provided an overview of a BPMN Script Tasks in Alfresco Activiti
  •  You should be able to describe at a high-level the:
    • characteristics
    • components
    • operations 

 

Lesson 2:

  • Discussed in detail script tasks and specifically their use and scripting language differences
  • Provided demonstrations that allowed you to see the contrast between the two primary scripting languages:
    • Groovy
    • JavaScript
  • Best practices for using or developing script tasks.

 

Lab Exercises:

  • Provided a real world BPMN scenario modeled after a Car Parts Ordering process
  • You navigated through and built a script task 

 

Conclusion:

There was a good balance of:

  • conceptual learning
  • demonstration
  • practical exercises 

 

Thank you for your participation!

 

Please keep an eye on the Alfresco Activiti Developer Series for future focused topics.

 

You have completed this module.