blob: 636373ece9a0773dfe5446a400b786a1a23a5530 [file] [log] [blame]
////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
////
:documentationPath: /plugins/actions/
:language: en_US
:page-alternativeEditUrl: https://github.com/apache/incubator-hop/edit/master/plugins/actions/eval/src/main/doc/eval.adoc
= JavaScript
== Description
Use the JavaScript action to calculate a boolean expression. The result can be used to determine which action will be executed next. You can use functions, procedure calls, ANDs, ampersands, ORs, EQUALs, etc. The Javascript workflow action evaluates and returns a true or false.
== Options
[width="90%", options="header"]
|===
|Option|Description
|Workflow action name|The name of the workflow action. *Note*: This name has to be unique in a single workflow. A workflow action can be placed several times on the canvas, however it will be the same workflow action.
|JavaScript|The JavaScript field.
|===
== Evaluation
The result of a JavaScript action is either true or false. In other words, it needs to end with a boolean expression.
Here are a few possible evaluations to end your script with:
[source,javascript]
lines_input > 100
or
[source,javascript]
true
or
[source,javascript]
parent_workflow.getVariable("INPUT_DIRECTORY").equals("/tmp");
The following variables are available for the expression:
[width="90%", options="header"]
|===
|Variable|Description
|```errors```|Number of errors in the previous workflow action (long).
|```lines_input```|Number of rows read from database or file (long).
|```lines_output```|Number of rows written to database or file (long).
|```lines_updated```|Number of rows updated in a database table (long).
|```lines_read```|number of rows read from a previous pipeline transform (long).
|```lines_written```|Number of rows written to a next pipeline transform (long).
|```files_retrieved```|Number of files retrieved from an FTP server (long).
|```exit_status```|The exit status of a shell script (integer).
|```nr```|The workflow action number of the previous workflow action (long); increments at every next workflow action.
|```is_windows```|use if Hop runs on Windows (boolean).
|```parent_workflow```|The parent workflow of the current workflow action.
|```__action__```|The current workflow action.
|===
== Variables
Here is how you can evaluate the content of a variable string:
[source,javascript]
parent_workflow.getVariable("NR_OF_ROWS") == 1000000;
Since we have access to the parent_workflow object, we can also set variables in the parent workflow this way:
[source,javascript]
parent_workflow.setVariable("NR_OF_ROWS", "1000000");
For example you can do something like the following to manipulate variables within this workflow action:
[source,javascript]
----
useDate = parent_workflow.getVariable("use_date").equals("1");
if (useDate == 0) {
date = new java.util.Date();
date.setDate(date.getDate()-1); //Go back 1 full day
dateFormat = new java.text.SimpleDateFormat("yyyyMMdd");
newDateStr = dateFormat.format(date);
parent_workflow.setVariable("start_date", newDateStr);
}
true;
----
== Previous result
When a workflow action finishes, the result of the execution will be a Result object exposed as "previous_result" to the JavaScript engine:
[width="90%", options="header"]
|===
|Expression|Alternative|Data type|Meaning
|```previous_result.getResult()```||boolean|true if the previous workflow action was executed successfully, false if there was some error.
|```previous_result.getExitStatus()```|exit_status|int|exit status of previous shell script workflow action
|```previous_result.getActionNr()```|nr|int|The action number is increased every time a workflow action is executed.
|```previous_result.getNrErrors()```|errors|long|the number of errors, also available as variable "errors".
|```previous_result.getNrLinesInput()```|lines_input|long|The number of rows read from a file or database.
|```previous_result.getNrLinesOutput()```|lines_output|long|The number of rows written to a file or database.
|```previous_result.getNrLinesRead()```|lines_read|long|The number of rows read from previous transforms.
|```previous_result.getNrLinesUpdated()```|lines_updated|long|The number of rows updated in a file or database.
|```previous_result.getNrLinesWritten()```|lines_written|long|The number of rows written to next transform.
|```previous_result.getNrLinesDeleted()```|lines_deleted|long|The number of deleted rows.
|```previous_result.getNrLinesRejected()```|lines_rejected|long|The number of rows rejected and passed to another transform via error handling.
|```previous_result.getRows()```||List<RowMetaAndData>|The result rows, see also below.
|```previous_result.isStopped()```||boolean|Flag to signal if the previous previous workflow action stopped or not.
|```previous_result.getResultFilesList()```||List<ResultFile>|The list of all the files used in the previous workflow action (or actions).
|```previous_result.getNrFilesRetrieved()```|files_retrieved|int|The number of files retrieved from FTP, SFTP, etc.
|```previous_result.getLogText()```||String|The log text of the execution of the previous workflow action and its children.
|```previous_result.getLogChannelId()```||String|The ID of the log channel of the previous workflow action. You can use this to look up information on the execution lineage in the log channel log table.
|===
=== Rows
The "rows" variable we expose to JavaScript helps you evaluate the result rows you passed to the next workflow action using the "Copy rows to result" transform.
Here is an example script on how to use this array:
[source,javascript]
----
var firstRow = rows[0];
firstRow.getString("name", "").equals("Foo")
----
This script will follow the green workflow hop is the expression evaluates to true. This happens if field "name" contains String "Foo".