Work with Groovy
Basic Level Video Lessons
65 Groovy Coding Samples
https://www.youtube.com/playlist?list=PLOGAj7tCqHx84a5oHMfLhE3-vKMM_6_nD
Advanced Groovy: Functional Programming
Comprehensive Groovy Book to learn
Code examples from this book - https://github.com/Dierk/GroovyInAction
Additional Learning Resourses
https://leanpub.com/groovytutorial/read
http://www.groovy-lang.org/documentation.html#gettingstarted
http://www.groovy-lang.org/documentation.html
Using Groovy for WorkFusion Bot Tasks
Pass Objects Between Groovy <script> Blocks
There are several options to pass an object or variable between script blocks:
Use the Web-Harvest sys object:
sys.defineVariable("cc", simpleObject);define a variable** without type** or without def:
simpleObject = new SimpleClass();Use the putAt() method of Groovy Binding API:
binding.putAt("cc", simpleObject); // or simple syntax: putAt("new_column", export_info);If you need to create a Web-Harvest Node variable which can be passed to other plugins (except export), do not use the
putAt()method.
Example
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<script><![CDATA[
class SimpleClass {
def printString() {
println " Simple Class Content";
}
}
// static objects/variables having local scope, i.e. visible only inside <script> block where they were defined
def simpleObject = new SimpleClass();
simpleObject.printString();
// to make static object/variable available in other <script> block add it to global context object 'binding'
binding.putAt("cc", simpleObject);
// dynamic variables having GLOBAL scope, i.e. available between <script> blocks and down in WebHarvest as well
simpleGroovyString = " hello"
//creating a global variable using web-harvest sys object
def localString = " Var from script 1"
sys.defineVariable("global_var", localString);
]]></script>
<script></script>
<export include-original-data="true">
<single-column name="cc_var" value="${cc}" />
<single-column name="simplegroovystring_var" value="${simpleGroovyString}" />
<single-column name="global_var" value="${global_var}" />
</export>
</config>
Working with WebHarvest objects in Groovy
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<var-def name="jsonData">
<http url="https://pub_demo.s3.amazonaws.com/trainings/bakery.json"/>
</var-def>
<script><![CDATA[
import com.google.gson.JsonObject
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonParser
// http://web-harvest.sourceforge.net/doc/org/webharvest/runtime/variables/ListVariable.html
println("Class of WebHarvest var jsonData : " + jsonData.getClass());
println("Class of jsonData wrapped object : " + jsonData.getWrappedObject().getClass());
println("jsonData wrapped object : " + jsonData.getWrappedObject());
JsonArray items = new Gson().fromJson(jsonData.toString(), JsonObject.class).getAt("Data");
JsonParser parser = new JsonParser();
for (int i = 0; i < items.size(); i++) {
JsonElement value = items.get(i).get("name");
JsonElement batters = parser.parse(items.get(i).get("batters").toString());
JsonArray batter = batters.getAsJsonArray("batter");
JsonArray topping = items.get(i).get("topping");
println("batter - " + batter);
println("topping - " + topping);
println("\n\n\n");
}
]]></script>
<export include-original-data="true"></export>
</config>
Assignments
1. Practice JSON Manipulations
Create simple BP with single Machine Task.
Load JSON from external file: https://pub_demo.s3.amazonaws.com/trainings/bakery.json
How to read file from HTTP
<var-def name="jsonData">
<http url="${json_url}"/>
</var-def>
Get JSON with <http> webharvest tag into Java variable and parse it with com.google.gson.Gson library (import com.google.gson.Gson)
- check online documentation how to use Gson library
Out of parsed data calculate:
- Count of available toppings for ‘Cakes'
- List 'Old Fashioned' available batter types
- Calculate sum of ppu’s for all donuts
Provide 3 calculations in export data of your MT
How to export data (simple way)
<export include-original-data="true">
<single-column name="topping_length" value="${toppingLength}"/>
<single-column name="batter_type" value="${batterType}"/>
<single-column name="ppu" value="${ppu}"/>
</export>
2. Use Groovy for parsing XML responses
- On input you have file with 30 companies
- Study Google Places API - https://developers.google.com/places/web-service/search
- Register and obtain your googleapikey
- Use Google Places API to verify: Address, Phone, Company Name. Also have API verification link in result data.
- When calling Google API expect XML as response type. Parse responses with Groovy.