MonkeyWrench
MonkeyWrench

Reputation: 1839

Alfresco Activiti script task listener current scriptnode?

Is there a way to get the ScriptNode that initiated a state in an Activiti workflow in Alfresco? I have a ScriptTask in my workflow, and it has a Alfresco Script Listener set up for the Start event. When the script is called, I'd like the ScriptNode that transitioned into the ScriptTask in the workflow to be passed as a parameter to the function defined as the listener. Is that possible?

Editing for clarity: Here's a screenshot of Eclispe with the Activiti plugin. https://i.sstatic.net/RXTbl.jpg

This workflow gets started by another workflow with webscripts.

var props = new Object();
var dd = new Date();
props[EL_PROP_WORK_UNIT_NAME] = "testNode" + DateISOString( dd );
props[EL_PROP_WORK_UNIT_SOURCE_CODE] = "ROB";
props[EL_PROP_WORK_UNIT_DELIVERY_DATE] = dd;

node = getHome().createNode(name, EL_TYPE_WORK_UNIT, props);

var EL_WORKFLOW = "activiti$The-workflow";
var activeWfs = node.activeWorkflows;
if( activeWfs === null || activeWfs.length === 0 )
{
    var workflowPackage = workflow.createPackage();
    workflowPackage.addNode( node );
    var workflowDef = workflow.getDefinitionByName(EL_WORKFLOW);
    var workflowPath = workflowDef.startWorkflow( workflowPackage, new Object());
}

So the listener calls another javascript method...

function artPDFRename()
{
    logger.log("==============================");
    logger.log("<START> artPDFRename");

    var workflowDef = workflow.getDefinitionByName(EL_WORKFLOW);
    var activeInstance = workflowDef.getActiveInstances();
        // ????
}

The goal is to have this handling be automatic. We're trying to design this with as little of manual intervention as possible, and are not assigning tasks to users to perform. Yes, there's probably another way to rename a PDF file, but I can't seem to figure out from the documentation listed here how to get a pointer to the node I put in the bpm_package object. That's the question.

Or am I so far off base on how we're developing this that it makes no sense?

Upvotes: 0

Views: 2701

Answers (1)

Tahir Malik
Tahir Malik

Reputation: 6643

As an example check the ScriptTaskListener class. Here all the workflow variables are put in a map.

The following code is interesting:

// Add all workflow variables to model Map variables = delegateTask.getExecution().getVariables();

    for (Entry<String, Object> varEntry : variables.entrySet())
    {
        scriptModel.put(varEntry.getKey(), varEntry.getValue());
    }

So with this you could use bpm_package as an object within your script within the workflow script task.

So if you need the node were the workflow has run on, the following code should work (where task is your delegateTask from your notify method of the Listener:

delegateTask.getVariable("bpm_package");
// or like the example above
delegateTask.getExecution().getVariable("bpm_package");

This will be a list so take the first one and that will be your node.

---------update

If you're using the javascript from alfresco then you can directly use the parent object bpm_package.

So in your case it would be best to do the following:

var node = bpm_package.children[0]; //or you could check if the package isn't null
// Then send the node into your
artPDFRename(node); //or you could just add the bpm_package code in your js file

Upvotes: 1

Related Questions