Merge script task with an authentificated user session.

Merge script task with an authentificated user session.

book

Article ID: KB0073980

calendar_today

Updated On:

Products

TIBCO EBX

Description

When using the built-in dataspace merge script task, all the merged updates are shown as done by a "System user".

Indeed, in the context of a script task, updates are done in a system session.

In order to modify this behavior, we will have to replace the built-in merge script task with a custom one using the API.

This article will give an example of such a ScriptTaskBean implementation that performs a merge in the context of a user session.
 

The class has two input parameters:

  • userId, which has to contain the user id (login) of the user you want the updates to be made by in the history
  • childDataspaceName, that must contain the name of the child dataspace to merge


These parameters can be data context variables that are previously filled in the workflow.

For the userId variable, you would maybe need to add an output variable to a built-in service.

The following article explains how to achieve this:

Extending a built-in service to add input/output parameters

The output parameter can be set in the handleWorkItemCompletion method of a UserTask extension.

https://docs.tibco.com/pub/ebx/5.9.8/doc/html/en/Java_API/com/orchestranetworks/workflow/UserTask.html#handleWorkItemCompletion-com.orchestranetworks.workflow.UserTaskWorkItemCompletionContext-

https://docs.tibco.com/pub/ebx/5.9.8/doc/html/en/Java_API/com/orchestranetworks/workflow/DataContext.html#setVariableString-java.lang.String-java.lang.String-

Issue/Introduction

Merge script task with an authentificated user session.

Resolution

Custom directory and session creation

In our script task, we will create a session related to a specific user. 

For this we will use the createSessionFromArray method of Repository class.

In order to this method, you have to create your own directory and implement authenticateUserFromArray(Object[] args). It will return a UserReference depending on a given Object[] argument. This authentification method can only be called from custom code.

To create your custom directory, you have to create a DirectoryFactory.

You can extend the default directory and just implement authenticateUserFromArray(Object[] args).

Then, you will have to specify your custom directory in ebx.properties:

https://docs.tibco.com/pub/ebx/5.9.8/doc/html/en/installation/properties.html#userDirectory

The DirectoryFactory has to be in the same folder as ebx.jar.

 

This is our implementation of authenticateUserFromArray:

myCustomDirectory.java
public UserReference authenticateUserFromArray(final Object[] args)
    {
        if (args != null && args[0] != null && args[0instanceof UserReference)
        {
            final UserReference user = (UserReference) args[0];
            return user;
        }
        return super.authenticateUserFromArray(args);

 

MergeScriptTaskWithUserSession.java
Repository repository = context.getRepository();                   
Session createdSession = null;     
 
 
// User session creation.
 
UserReference user = Profile.forUser("this.userId");
 
Object[] args = new Object[]{
        user,
    };
 
         
createdSession = repository.createSessionFromArray(args);

 

Programmatic service and merge

Once our user session is created, we will create a programmatic service and execute a merge procedure that will be executed in the context of this user session. 

MergeScriptTaskWithUserSession.java
AdaptationHome childDataspace = repository.lookupHome(HomeKey.forBranchName(childDataspaceName));
AdaptationHome parentDataspace = childDataspace.getParentBranch();
         
ProgrammaticService.createForSession(createdSession, parentDataspace).execute(new Procedure() {
     
    @Override
    public void execute(ProcedureContext context) throws Exception {
         
        context.doMergeToParent(childDataspace);
    }
});

 

Script task declaration and setting

module.xml
<bean className="com.orchestranetworks.support.module.MergeScriptTaskWithUserSession">
            <documentation xml:lang="en-US">
                <label>Merge script with user session.</label>
            </documentation>
            <properties>
                <property name="userId" input="true">
                    <documentation xml:lang="en-US">
                        <label>User login</label>
                        <description>
                            The user login.
                        </description>
                    </documentation>
                </property>
                <property name="childDataspaceName" input="true">
                    <documentation xml:lang="en-US">
                        <label>Child dataspace name</label>
                        <description>
                            The child dataspace name.
                        </description>
                    </documentation>
                </property>
            </properties>
        </bean>

User-added image

Additional Information

https://docs.tibco.com/pub/ebx/5.9.7/doc/html/en/Java_API/com/orchestranetworks/workflow/ScriptTaskBean.html

Attachments

Merge script task with an authentificated user session. get_app