In this recipe, we will work with resources. We will see how we can integrate files with Orchestrator and use them in workflows and for other purposes, such as storing configuration information.
We need a functional Orchestrator. We also need a text file in a directory that Orchestrator can access. To create such a text file, you could use the example workflow 09.02.1 Write a File
. Also see the recipe File operations in Chapter 9, Essential Plugins.
This recipe contains multiple parts, each dealing with different aspects of resources.
Let's start by adding a resource to Orchestrator manually:
You can also update (re-upload a file) and download (save to a file) resources in Orchestrator.
To add a resource to a workflow, we need to add it as an attribute. This is shown as follows:
textFile
.textFile
attribute as an input parameter and add the following script:System.log("Name :"+textFile.name); System.log("MimeType :"+textFile.mimeType); System.log("Resource Category :"+textFile.getResourceElementCategory().name); System.log("Description :"+textFile.description); System.log("Version :"+textFile.version); System.log("Size :"+textFile.contentSize); //get the content as MimeAttachment Attachment = textFile.getContentAsMimeAttachment(); // get string content from MimeAttachment content = attachment.content; System.log("Content: "+content);
In the logs, we output all the possible properties of resourceElement
. The most important one is its content.
Instead of manually uploading a resource element, you can dynamically add new resource elements to Orchestrator:
Name |
Type |
Section |
Use |
|
String |
IN |
This is the name of the resource |
|
ResourceElementCategory |
IN |
This is where the resource should be stored |
|
String |
IN |
This is the plain text that the resource should contain |
//initialize a mime attachment object var attachment = new MimeAttachment(); //fill it attachment.name = name; attachment.mimeType = "text/plain"; attachment.content = textContent; //create the resource element from the Mime attachment Server.createResourceElement(resourceFolder,name,attachment);
Instead of using a mime element, you can upload a resource from a file.
Name |
Type |
Section |
Use |
|
path |
IN |
File that you want to make a resource. |
//get filename of path temp=localfile.split("/") fileName=temp[temp.length-1]; //upload resource attachment=Server.createResourceElement("vRO Example", fileName,localfile);
After the workflow has finished, check the resource folder vRO Example
and its content.
We can also update existing resources. To do this, perform the following steps:
Name |
Type |
Section |
Use |
|
ResourceElement |
IN |
This is the resource element that should be updated |
|
String |
IN |
This is the new text content |
//prepare for update var attachment = new MimeAttachment() ; //set new content attachment.content = newTextContent; //use old settings attachment.name = resource.name; attachment.mimeType = resource.mimeType; //update resource.setContentFromMimeAttachment(attachment); //overwrite the description resource.description="Updated";
When you run the workflow, select a resource to update, preferably one you have created before. After the workflow has finished, check the resource and its content.
Resource elements have many uses. First of all, you can use a small picture as an icon for Orchestrator workflows. Secondly, you can use them to send e-mail attachments (see the recipe Working with mails in Chapter 9, Essential Plugins). There is also a method of storing information as a resource element. Orchestrator uses this method to store the configuration for the SOAP, REST, and multi-node plug-ins. The stored information is the configuration for each SOAP, REST, or Orchestrator server. Go have a look!
Last but not least, you can use the resource elements to store templates. For example, you can store a text template for an e-mail you want to send to customers. You can write variables in the text such as "Dear {name}...
" and use the string method replace
to substitute the variables before sending the e-mail, for example ResourceContent.replace("{name}",sendToName);
. Have a look at the workflows stored under Daniels Toolbox/CoolMail
Any kind of file can be used as a resource element, but typically one uses XML, plain text, or pictures as resource elements.
You can do more with resources. Some examples follow.
You don't need to actually add a resource as an attribute to a workflow to access it: you can access them directly.
You can get all existing resource element categories with Server.getAllResourceElementCategories()
. To get resource elements from a given category, use ResourceElementCategory.resourceElements
. Use ResourceElementCategory.subCategories
to get all sub categories from a given category.
The following little script lets you find a resource by its name. It loops through all resource folders and all resource elements:
allresource=Server.getAllResourceElementCategories(); for each (category in allresource) { for each (resource in category.resourceElements){ if (resource.name == name ){ System.log("Found: " + name); } } }