You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

Action

Flow Elements - Action

Description

The Action element represents an action within the flow. It is one of the viable start node types and upon being called, the given script will be evaluated. When a source of data is linked to this element, it’s value will be made available in the scripting context under the keyword data.

Parameters

Name Description
Prev Accepts another element’s Next socket
Next Connects to another element’s Prev socket
DataSource Accepts another element’s DataSource
DataTarget Connects to another element’s DataSource
Script Given script will be executed as StructrScript with data in it’s context.

Search results for "Action"

doInNewTransaction()

Removes the given function form the current transaction context and allows batching of a given expression, i.e. if the expression contains a long-running function (for example the deletion of all nodes of a given type, see examples below), that function will be instructed to commit its results in batches of batchSize.
Useful in situations where large numbers of nodes are created, modified or deleted.

This function is only available in JavaScript and takes a worker function as its first parameter and an optional error handler function as its second parameter. If the worker function returns true, it is run again. If it returns anything else it is not run again. If an exception occurs in the worker function, the error handler function (if present) is called. If the error handler returns true, the worker function is called again. If the error handler function returns anything else (or an exception occurs) the worker function is not run again.

New in v4.0: The errorHandler function now has a parameter where the error(/exception) is available. Depending on the error type, different methods are available on that object. Syntax errors will yield a PolyglotException (see https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/PolyglotException.html), other error types will yield different exception object. See example 4 for more information.

$.doInNewTransaction(workerFunction [, errorHandlerFunction] )

Connecting flow elements

Now that our Action contains a script, we want it to return its result. For this to happen we have to make sure the flow execution does not stop at the Action element. Drag a connection from the “Next” socket of the Action element to the “Prev” socket of the Return element to create an execution flow, indicated by the green color of the connection. Our function is now capable of returning something after the Action has been dealt with, but at this point in time, it will return an empty result, because we have not yet connected data to the Return element. In the same way the first connection was handled, create a connection between the elements “DataTarget” and “DataSource” sockets to create a data flow. When a node is being evaluated by the engine, it will try to consider connected data flows and make them available within its scripting context in the example of an Action or Return element.

afterCreate

  • Is called after an object has been created (onCreate has already been executed), all checks have passed successfully and the transaction has been committed
  • Multiple afterCreate methods can exist per type - every method prefixed with afterCreate is called after an object has been created successfully (e.g. afterCreate01, afterCreate02)
  • can not roll back the transaction
  • is useful for actions that should only happen if the node is sure to have been created. For example sending a mail: Calling send_html_mail() in onCreate would send the email even if the transaction would be rolled back due to an error.
  • All regular keywords can be used - no special keywords are introduced for this method

Example:

Advanced

From the Advanced properties dialog you can access Structr’s automatic content extraction. The content extraction takes structured documents such as PDF files or images and splits them into structural elements such as pages and sentences, which can be accessed via the Schema types StructuredDocument and StructuredTextNode. You can trigger this content extraction via the Extract document content button (Extract document content).

File Upload

Binary data in form of files can be uploaded to the integrated file system in Structr with a form-data POST request to the upload servlet running on path /structr/upload.

The file to be uploaded has to be put as value for the key file. All other properties defined on the File type can be used to store additional data or link the file directly to an existing data entity in the database.

# example file upload with addional paramter "action" and target directory "parent"
curl --location --request POST 'http://localhost:8082/structr/upload' \
--form 'file=@"/Users/lukas/Pictures/user-example.jpg"' \
--form 'action="ec2947e760bd48b291564ae05a34a3b7"' \
--form 'parent="9aae3f6db3f34a389b84b91e2f4f9761"'

Creating Flow elements

For the sake of this demonstration, a simple flow with an Action and a Return element is created. Both of these can be found under the Action Nodes category of the context menu. The first viable start element added to the flow will automatically be marked as such. A start node or flow entry point is indicated by the green coloring of the node header. The start node can be changed by bringing up the context menu on an element, if it is viable a menu item called “Set as start node” will display.

Action

The Action element represents an action within the flow. It is one of the viable start node types and upon being called, the given script will be evaluated. When a source of data is linked to this element, it’s value will be made available in the scripting context under the keyword data.

Create Connected Nodes

If you select the Create node action for an element that is inside another element with the Create node action, the importer will create a relationship between the two based on the schema configuration. In this example, we select the target type CustomerAddress for the <FullAddress> element inside the <Customer> element, and the import wizard shows the contextual property addresses.

Dynamic Content / Repeater

DOM elements that are connected to the database or trigger database actions are called “active elements”. In the page tree, they are marked with a special colorful icon. Structr provides a range of other active elements: Active input elements, dynamic output elements, action buttons and repeater elements. In the following we make use of other active elements.

REST

REST is an acronym that stands for “Representational State Transfer”. The exchange of messages (the representation) between two parties causes a state transition (e.g. creating an object in the database). The basis for this is the HTTP protocol, which specifies a set of actions with clearly defined meaning. In HTTP, these actions are called methods, of which GET and POST are the most commonly used, because a large part of the functionality of the World Wide Web is based on them. Less well-known are the methods PATCH, PUT and DELETE, which together with GET and POST form a complete set of commands for managing data based on HTTP URLs.

Passive Indexing

Passive indexing is the term for reading a dynamic value from a property (e.g. Function Property or Boolean Property) at application level, and writing it into the database at the end of each transaction, so the value is visible to Cypher. This is important for BooleanProperty, because its getProperty() method returns false instead of null even if there is no actual value in the database. Hence a Cypher query for this property with the value false would not return any results. Structr resolves this by reading all passively indexed properties of an entity, and writing them into the database at the end of a transaction.

Set Properties

The Set property action allows you to import the text content of an element into a property of a schema type. If you select this action, you will see a select box with the properties of the target type you chose for the enclosing element.

Creating Multiple Objects

You can create more than one object of the same type in a single request by sending an array of JSON objects in the request body, as shown in the next example. Doing so has the advantage that all objects are created in a single transaction, so either all objects are created, or none, if an error occurs. It is also much more efficient because the transaction overhead is incurred only once.

Actions Section

From the actions section, the structr file system provides three actions:

Editing scripts within Flow elements

To add functionality to the flow, we will add a small StructrScript snippet to query existing users in the database. Left-click the Script text box of the Action element to bring up a code editor and enter the script as shown below.

Loop

Name Description
Prev Accepts another element’s Next socket
Next Connects to another element’s Prev socket
DataSource Accepts another element’s DataSource. Represents the collection to iterate.
DataTarget Accepts another element’s DataSource. Contains the object of the current iteration.
FirstAction Connects to another element’s Prev socket. Represents the first node to be called for each iteration of the collection.

Buttons

Icon Action
Clone icon Clone element and all its children
Edit Properties icon Open Properties dialog
Delete icon Delete element and all its children
Unlink icon Remove element and all its children
Key icon Open Access Control and Visibility Dialog
Pencil icon Open Edit Content Dialog
Link icon Open Edit Link Dialog