This is an internal documentation. There is a good chance you’re looking for something else. See Disclaimer.

Persist Tasks

Note

If you are not working on, or interacting with, legacy code, you should probably not use our persist tasks. Use the default functional interfaces with Java Streams and Optionals for similarly chained and functional logic.

Persist tasks are pieces of code that may accept some argument, return some value and which can be ran in different modes, for instance in a transaction, in some business unit or as a privileged user. They are created by implementing the PersistTask interface and can be run with the CommandExecutor or the CommandService.

Combining persist tasks

Persist tasks can be chained. The most common variant, passing the return value of the first as the argument for the second, can be achieved by using the PersistTask#compose method. When simply executing tasks one after the other, without passing values, PersistTask#andThenDo can be used. These and other methods are summarized in the following table.

Method

Summary

Closest Java Equivalent

PersistTask#compose

Pass return value from first task to the second

Optional#map

PersistTask#compose2

Pass return value from first task to the second, which then returns an iterable

Stream#flatMap

PersistTask#andThenDo

Execute first task, then execute second task

Imperatively calling pure functions

PersistTask#orWhen

Check passed predicate, if true, execute second task with initial argument, otherwise return value from first task

Optional#filter followed by Optional#orElse

PersistTask#orWhenNot

Check passed predicate, if false, execute second task with initial argument, otherwise return value from first task

Optional#filter followed by Optional#orElse

PersistTask#composeWhen

Check passed predicate, if true, compose tasks, otherwise return value from first task

Optional#filter followed by Optional#map

PersistTask#ifElse

Check passed predicate, if true, compose with first passed task, otherwise compose with second passed task

if-else branch

PersistTaskIterable

A specialized interface PersistTaskIterable always returns an iterable and provides typed combinators to make iterables easier to work with. An easy way to get a PersistTaskIterable is to use PersistTask#expand, which then accepts an iterable of inputs and returns an iterable, being generated by applying the original task to each input element.

OptionalTask

A specialized interface OptionalTask always accepts an optional and stops execution if the result of the task would be null. It then returns null itself.

PartitionedTask

A specialized implementation PartitionedTask allows running some bit of code on an iterable argument, creating a new context (and transaction, if relevant) for each batch of a given size.

Helper classes and default tasks

There exist several default tasks that deal with many common use cases, especially when dealing with entities.

Execution modes

The mode, that a task is executed in, is configured by different flags which then get read by the executing code. These can bet set with the different Mode#with methods. You will usually not need to define your own flags, but can use the following default flags and helper methods:

Name

Summary

Code

No Flags

Method to get a mode with no flags set

Mode.create()

Transaction

Method to get a mode which runs a task in a transaction

Mode.createTransactional()

Privileged

Flag to run a task privileged

with(SecModes.PRIVILEGED)

Business unit

Flag to run a task in a business unit

with(TaskModes.BUSINESS_UNIT, BusinessUnit)

Business unit (by id)

Flag to run a task in a business unit by passing its unique id

with(TaskModes.BUSINESS_UNIT_ID, "bu")