Tuesday, June 11, 2013

DYNAMIC_SETTINGS in Talend Open Studio Custom Components

DYNAMIC_SETTINGS in Talend Open Studio Custom Components

The "Dynamic settings" tab is present on all Talend Open Studio components, but few components actually use it.  That's because most components can be parameterized through text boxes in which a user can insert a context variable statement (ex, "context.RUN_ALL").  However, this technique does not permit the configuration of checkbox values and any components that offer a setting on the Component View ought to be fully configurable.

tContextLoad is a Talend Open Studio component that uses Dynamic Settings.  The checkbox "Print operations" will specify that each key/value pair be printed out as the context is loaded.  This is a troubleshooting function that you might turn off in a production environment to reduce the clutter of a log file.  If a problem arises, say caused by a system change, retaining the ability to turn it on without changing code is indispensable.

Dynamic Settings

Within Talend Open Studio, the setting "Print operations" can be toggled during development.  However, once the job is exported, the last setting (checked or unchecked) will be saved.  Without Dynamic Settings, this setting will remain until a new job is deployed.

The Print Operations Parameter
Dynamic Settings gives you the ability to associate a parameter -- such as "Print operations" -- with an expression.  Generally, the expression will be a context variable like "context.RUN_ALL".  This is the same type of expression that you might put in a text box like a filename, but you need Dynamic Settings because there's nowhere in a checkbox to put a line of code.

Select the Dynamic Settings tab and press the green plus icon.  In the Code box, you can put a constant or a line of code.  In this case, 'false' is used as a default value.

Dynamic Settings Tab of tContextLoad
Development

To use Dynamic Settings in a custom component, define a PARAMETER element in the component's XML file.  The PARAMETER can be placed in the PARAMETERS or ADVANCED_PARAMETERS sections.  The parameter should have the DYNAMIC_SETTINGS attribute set to "true". A default value is provided in the DEFAULT subelement.


This is a parameter definition from a soon-to-be-realeased (Dec 2012) version of tScriptRules.


  false


Working with the Dynamic Setting in the Talend Javajet code is a little different than the usual 'getValue()' call you might make on a text box.  You'll need to check if you're operating context mode and if you are not,  you'll need to treat the variable as a String.  Otherwise, make the getValue() call.

Boolean runAll_<%= cid %> = false;

<%
  if( node.getElementParameter("RUN_ALL").isContextMode() ) {
%>
   runAll_<%= cid %> = <%= ElementParameterParser.getValue(node, "__RUN_ALL__") %>;
<%
   } else {
    if ( ElementParameterParser.getValue(node, "__RUN_ALL__").equals("true") ) {
%>
     runAll_<%= cid %> = true; // not from context
<%
   }
}
%>


Note the RUN_ALL used in the isContextMode() call and the __RUN_ALL__ used in the getValue() calls.

The result of this block of JET code is a variable 'runAll_<%= cid %>' that I use later in the program to initialize an object.

Usage

This job shows a simple usage of tScriptRules.  There is a context variable 'RUN_ALL' that is defined as a Boolean with a default value of 'false'.

A tScriptRules Job with a Context Variable
This is the Dynamic Settings tab of the tScriptRules_1 component.  The plus icon was pressed and the "Run all rules" parameter was displayed with its default value "false".  I selected the Code text box and entered "context.RUN_ALL".

Dynamic Settings of a tScriptRules Component
This will affect the "Advanced settings" tab by disabling and highlighting the parameter.  Prior to the Dynamic Settings modification, this checkbox could be selected and deselected.  If you want this control returned while you work on the job, return to the Dynamic Settings tab and remove the Run all rules parameter using the red X icon.

Parameter Referenced on Dynamic Settings is Disabled
The context variable is the mechanism by which a command line argument is passed to the Talend job.  See this post for more information about command line arguments.

When you write a custom component for Talend Open Studio, you ought to make all available parameters configurable from the command line.  That's because you may not know the exact use case for a component user.  Give them the most flexibility with a little extra effort on the Javajet side

No comments: