Task Template

Last modified 02 Jul 2024 14:22 +02:00

MidPoint offers an easy way of starting a task that is similar to a pre-existing one. The pre-existing task is called a template, although it is technically an ordinary task: with an exception that it is not meant to be executed directly.

An example

Task that modifies a given user

Git

<!--

  ~ Copyright (c) 2010-2021 Evolveum

  ~

  ~ Licensed 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.

  -->



<task xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"

      xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"

      xmlns:s="http://midpoint.evolveum.com/xml/ns/public/model/scripting-3"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      oid="78a39955-743b-480f-86c0-9dbeafdbaba6">

    <name>Change description task template</name>

    <ownerRef oid="00000000-0000-0000-0000-000000000002" type="UserType"/>

    <executionState>waiting</executionState> <!-- this is task template -->

    <cleanupAfterCompletion>PT10M</cleanupAfterCompletion> <!-- delete 10 minutes after completion -->

    <schedule>

        <recurrence>single</recurrence>

    </schedule>

    <activity>

        <work>

            <iterativeScripting>

                <objects>

                    <type>UserType</type>

                </objects>

                <scriptExecutionRequest>

                    <s:action>

                        <s:type>execute-script</s:type>

                        <s:parameter>

                            <s:name>script</s:name>

                            <c:value xsi:type="c:ScriptExpressionEvaluatorType">

                                <c:code>

                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.*



                                    log.info('Modifying user {}', input)

                                    def deltas = midpoint.deltaFor(UserType.class)

                                        .item(UserType.F_DESCRIPTION)

                                        .replace('hello ' + basic.stringify(input.name))

                                        .asObjectDeltas(input.oid)

                                    log.info('Change:\n{}', deltas.iterator().next().debugDump())

                                    midpoint.executeChanges(deltas, null)

                                </c:code>

                            </c:value>

                        </s:parameter>

                    </s:action>

                </scriptExecutionRequest>

            </iterativeScripting>

        </work>

    </activity>

</task>

Note that executionState of this task is set to waiting, so that the task template itself will not be executed.

Finally, note the cleanupAfterCompletion: it says that tasks created from this template will be automatically deleted 10 minutes after their completion (i.e. switching into the CLOSED state).

Creation of a task from a template

Creation of a templated task is facilitated by two methods among midPoint functions:

midpoint.submitTaskFromTemplate(String templateTaskOid, ActivityCustomization customization)

The former method expects a list of prism items to be inserted into an extension of newly created task. The latter method is less general but easier to use: it expects a map of extension item name and its (real) value.

So, in our case the usage would look like this:

var taskOid = modelInteractionService.submitTaskFromTemplate(
        TASK_TEMPLATE_SCRIPTING_NO_PROFILE.oid,
        ActivityCustomization.forOids(List.of(USER_JOE.oid)),
        task, result);
midpoint.submitTaskFromTemplate('79dacdf7-2bc9-4eef-9c63-08d53d2392c3', ActivityCustomization.forOids(List.of('3490c9ae-8839-40e5-8ba5-6ceb235d9af2')));

Task Cleanup

This method is likely to produce a lot of tasks that are relatively short-lived. There is a cleanup task in midPoint that deletes old closed tasks. However, the default setting may cause tasks to stay around for too long.

Therefore, there is now a cleanupAfterCompletion task property that can be used for finer control over the cleanup. This property can be set explicitly to force task cleanup after a specific interval.

Was this page helpful?
YES NO
Thanks for your feedback