简体   繁体   中英

How to delete items from bpm_package at the beginning of an Activiti Workflow in Alfresco?

I've been working in a solution to delete from bpm_package every node that doesn't have the 'cm:versionable' aspect. I need it to be done at the beginning of the start task, so user can't start the workflow with a document that doesn't have the aspect attached.

I don't want to do this verification after the user clicks the start workflow button.

I've tried with this piece of code, but it's not doing what i need. It should work, shouldn't it?

<startEvent id="start" name="Início" activiti:initiator="initiatorUserName"
                activiti:formKey="workflowdocumentrequest:start">
    <extensionElements>
        <activiti:executionListener event="start" class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener">
            <activiti:field name="script">
                <activiti:string>
                    <![CDATA[
                            for (var i = 0; i < bpm_package.children.length; i++)
                            {
                                if(!bpm_package.children[i].hasAspect("cm:versionable")){
                                    bpm_package.removeNode(bpm_package.children[i]);
                                }
                            }
                        ]]>
                </activiti:string>
            </activiti:field>
        </activiti:executionListener>
    </extensionElements>
</startEvent>

Actually, the code I posted is not supposed to be executed on form creation as I thought. This event is fired once user clicks the "Start Workflow" button.

So, I changed te code to show users an error message with the items that could not be attached to the workflow instead of deleting them and starting it without giving them any option or response.

The new code looks like this one below:

    <startEvent id="start" name="Início" activiti:initiator="initiatorUserName"
                activiti:formKey="workflowdocumentrequest:start">
        <extensionElements>
            <activiti:executionListener class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener"
                                        event="start">
                <activiti:field name="script">
                    <activiti:string><![CDATA[
                        var count = 0;
                        var items = "";

                        for (var i = 0; i < bpm_package.children.length; i++) {
                            var child = bpm_package.children[i];

                            if (!child.hasAspect("cm:versionable")) {
                                items += child.properties['cm:name'] + "\n";
                                count++;
                            }
                        }

                        if (count > 0){
                            var message = "\n\nThe following item(s) cannot be attached to the workflow:\n";
                            throw new Error(message + items + "\n");
                        }

                    ]]></activiti:string>
                </activiti:field>
            </activiti:executionListener>
        </extensionElements>
    </startEvent>

Hope it helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM