Working with resources

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.

Getting ready

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 9Essential Plugins.

How to do it...

This recipe contains multiple parts, each dealing with different aspects of resources.

Adding resources manually

Let's start by adding a resource to Orchestrator manually:

  1. Switch Orchestrator to Design mode.
  2. Click on Resources (the white page with a blue symbol on it).
  3. Create a new folder where you can store your resources by right-clicking the root in the tree and selecting New folder.
  4. Right-click on the new folder and select Import resources.
  5. Select a file such as an image or text file from your local folder and click on Open. The new resource is now available under the folder you created.
  6. Click on the resource and browse through the tabs that are presented.
  7. Please note that Description automatically contains the location from where you imported it. Also, if the file is a picture or text file, you can view it in the Viewer tab.

You can also update (re-upload a file) and download (save to a file) resources in Orchestrator.

Using resources in workflows

To add a resource to a workflow, we need to add it as an attribute. This is shown as follows:

  1. Create a new workflow and add a new attribute.
  2. Rename the attribute textFile.
  3. Change the type of the attribute to ResourceElement.
  4. Now click on Value. You can now search the existing resources and select one.
  5. Add a scriptable task to the schema and edit it.
  6. Bind the 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); 
    
  7. Save and run the workflow.

In the logs, we output all the possible properties of resourceElement. The most important one is its content.

Creating a new resource element

Instead of manually uploading a resource element, you can dynamically add new resource elements to Orchestrator:

  1. Create a new workflow and add the following variables:

    Name

    Type

    Section

    Use

    name

    String

    IN

    This is the name of the resource

    resourceFolder

    ResourceElementCategory

    IN

    This is where the resource should be stored

    textContent

    String

    IN

    This is the plain text that the resource should contain

  2. Add a scripting task to the schema and bind all variables.
  3. Add the following script:
          //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); 
    
  4. Save and run the workflow.

Create a resource by uploading a file

Instead of using a mime element, you can upload a resource from a file.

  1. Create a new workflow and add the following variables:

    Name

    Type

    Section

    Use

    localfile

    path

    IN

    File that you want to make a resource.

  2. Add a scripting task to the schema and bind the variable.
  3. Add the following script:
          //get filename of path 
          temp=localfile.split("/") 
          fileName=temp[temp.length-1]; 
          //upload resource 
          attachment=Server.createResourceElement("vRO Example", fileName,localfile); 
    
  4. Save and run the workflow.

After the workflow has finished, check the resource folder vRO Example and its content.

Updating a resource

We can also update existing resources. To do this, perform the following steps:

  1. Create a new workflow and add the following variables:

    Name

    Type

    Section

    Use

    resource

    ResourceElement

    IN

    This is the resource element that should be updated

    newTextContent

    String

    IN

    This is the new text content

  2. Add a scripting task to the schema and bind all variables.
  3. Add the following script:
          //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"; 
    
  4. Save and run the workflow.

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.

How it works...

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.

There's more...

You can do more with resources. Some examples follow.

Accessing resources directly

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); 
         } 
   } 
} 

Deleting a resource

You can delete a resource by using:

Server.removeResourceElement(attachment); 

See also

The following are some example workflows:

  • 08.01.1 Use Resources
  • 08.01.2 Create Resources (mime)
  • 08.01.3 Create Resource (file)
  • 08.01.4 Access Resources directly
  • 08.01.5 get resource from name
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset