如何在工作流程中添加额外的附件?

How to add additional attachments in the workflow?

在我的文档管理过程中,经常需要提供一些额外的文档(例如评论列表、差异列表、一些屏幕截图等)。

这些附加文件可以方便地添加到 Activiti 表单中。我希望能够在业务流程的启动阶段和修改阶段添加文档。

为此,我在workflow-model.xml(相关部分)中添加了一个带有关联的方面:

...
<type name="mswf:activitiRevise">
    ...
    <mandatory-aspects>
        ...
        <aspect>mswf:attachments</aspect>
    </mandatory-aspects>
</type>        
...
<aspect name="mswf:attachments">
    <associations>
        <association name="mswf:package">
            <source>
                <mandatory>false</mandatory>
                <many>true</many>
            </source>
            <target>
                <class>cm:content</class>
                <mandatory>false</mandatory>
                <many>true</many>
            </target>
        </association>
    </associations>      
</aspect>   
...
etc

share-config-custom.xml中我有以下(相关部分):

...
<config evaluator="task-type" condition="bpm:startTask">
    <forms>
        <form id="workflow-details">
            <field-visibility>
                ...
                <show id="mswf:package" />
            </field-visibility>
            <appearance>
                ...
                <set id="attachments" appearance="title" label-id="Additional docs" />
                <field id="mswf:package" set="attachments" />
            </appearance>
        </form>
        <form>
            <field-visibility>
                <show id="mswf:package" />
                ...
            </field-visibility>
            <appearance>
                <set id="attachments" appearance="title" label-id="Additional docs" />
                <field id="mswf:package" set="attachments" />
                ...
            </appearance>
        </form>
    </forms>
</config>
...
etc

现在我有一个额外的字段,我可以在其中选择适当的文档。

它有效 - 我添加了一些文档并且可以在文档管理过程的所有阶段看到它们。

当我尝试更改一组最初选择的文件时出现问题。例如,当我尝试添加一个新的时。如果我添加一个新的(或删除)- 更改不会保存,并且在下一个任务中,我会看到在开始时选择的相同文档。

为了控制这种行为,我开发了 WebScript,我试图在其中管理属性。我在 getAddedItems() 方法中从 Share 调用 WebScript:

/**
* Returns items that have been added to the current value
*
* @method getAddedItems
* @return {array}
*/
getAddedItems: function ObjectFinder_getAddedItems() {
    var addedItems = [],
    currentItems = Alfresco.util.arrayToObject(this.options.currentValue.split(","));

    for (var item in this.selectedItems) {
        if (this.selectedItems.hasOwnProperty(item)) {
            if (!(item in currentItems)) {
                addedItems.push(item);                     
            }
        }
    }

    ...

    // here the call to the WebScript

    return addedItems;
},

我的 Java 支持的 WebScript 的一部分:

...
public class WorkflowAttachmentsManipulator extends DeclarativeWebScript  {
    private static final String WORKFLOW_MODEL_URI = "...";
    private WorkflowService workflowService;

    @Override
    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
        Map<String, Object> model = new HashMap<>();

        String taskId = req.getParameter("taskId");
        String addedItem = req.getParameter("addedItem");

        WorkflowTask workflowTask = workflowService.getTaskById(taskId);
        Map<QName, Serializable> taskProperties = workflowTask.getProperties();

        ...
        taskProperties.replace(
            QName.createQName(WORKFLOW_MODEL_URI, "package"), oldValue, addedItem);
        workflowService.updateTask(taskId, taskProperties, null, null);

...

我正在尝试用一些任意的 replace(...) 方法 returns true 替换选定的文件。在 alfrescotomcat-stdout.2017-09-06.log 中,我还看到 属性 已被替换:

调用WebScript之前(包中有两个文件):

key: {WORKFLOW_MODEL_URI_HERE}package
value: [workspace://SpacesStore/7f980005-2a1b-49a5-a8ff-ce9dff31a98a, 
        workspace://SpacesStore/30d9122f-4467-451b-aeab-ca8b164f7769]

调用WebScript后(包中的一个文件):

key: {WORKFLOW_MODEL_URI_HERE}package
value: workspace://SpacesStore/1a0b110f-1e09-4ca2-b367-fe25e4964a4e

在当前任务中更新表单后,我看到了我的新文件。

但修改/审阅后该值未保存(丢失),在下一个任务中我看到了相同的文件。比方说,当前用户的任务 ID 是 activiti4587,然后它变成等于 activiti4647...

我在BPMN图中添加了一些调试代码,发现调用WebScript后mswf_package的内容并没有改变。

在'Submit'中,主要配置:

for(var i = 0; i < mswf_package.size(); i++) {  
    logger.log(mswf_package.get(i).nodeRef);
}

输出:

DEBUG [repo.jscript.ScriptLogger] [http-apr-8080-exec-9] workspace://SpacesStore/7f980005-2a1b-49a5-a8ff-ce9dff31a98a
DEBUG [repo.jscript.ScriptLogger] [http-apr-8080-exec-9] workspace://SpacesStore/30d9122f-4467-451b-aeab-ca8b164f7769

在 'Review Task' 中,createcomplete 事件的侦听器:

for(var i = 0; i < mswf_package.size(); i++) {  
    logger.log(mswf_package.get(i).nodeRef);
}

输出:

DEBUG [repo.jscript.ScriptLogger] [http-apr-8080-exec-9] workspace://SpacesStore/7f980005-2a1b-49a5-a8ff-ce9dff31a98a
DEBUG [repo.jscript.ScriptLogger] [http-apr-8080-exec-9] workspace://SpacesStore/30d9122f-4467-451b-aeab-ca8b164f7769

如何在工作流中添加额外的附件?可能吗?

一组带有NodeRefs的字符串可以传递给下面的WebScript,例如:

public class WorkflowAttachmentsManipulator extends DeclarativeWebScript  {
    private static final String WORKFLOW_MODEL_URI = "...";
    private WorkflowService workflowService;

    @Override
    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
        Map<String, Object> model = new HashMap<>();

        String taskId = req.getParameter("taskId");
        String addedItems = req.getParameter("addedItems");

        String oldValue = "";

        WorkflowTask workflowTask = workflowService.getTaskById(taskId);
        Map<QName, Serializable> taskProperties = workflowTask.getProperties();

        Iterator taskIterator = taskProperties.entrySet().iterator();
        while(taskIterator.hasNext()) {
            Map.Entry taskPair = (Map.Entry)taskIterator.next();
            Object key = taskPair.getKey();
            if(key != null && 
                key.toString().equalsIgnoreCase("{" + WORKFLOW_MODEL_URI + "}package")) {

                if(taskPair.getValue() != null) {
                    oldValue = taskPair.getValue().toString();
                    if(!oldValue.equals("[]")) {
                        oldValue = oldValue.replaceAll("[\[\]]", "");
                        addedItems = "[" + oldValue + "," + addedItems + "]";
                    } else {

                        if(addedItems.indexOf(",") > 0) {
                            addedItems = "[" + addedItems + "]";    
                        }
                    }
                }

                taskProperties.replace(
                    QName.createQName(WORKFLOW_MODEL_URI, "package"), 
                    oldValue, 
                    addedItems);

                workflowService.updateTask(workflowTask.getId(), 
                    taskProperties, null, null);

                break;
            }
        }
        ...
    }

    public WorkflowService getWorkflowService() {
        return workflowService;
    }

    public void setWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }
}

此代码覆盖特定任务的附件。

其他文件需要与文档管理过程中涉及的文件区分开来。可以这样做,例如,如下:

/**
* Returns items that have been added to the current value
*
* @method getAddedItems
* @return {array}
*/
getAddedItems: function ObjectFinder_getAddedItems() {
    var addedItems = [],
    currentItems = Alfresco.util.arrayToObject(this.options.currentValue.split(","));

    var attachments = [];

    for (var item in this.selectedItems) {
        if (this.selectedItems.hasOwnProperty(item)) {          
            if (!(item in currentItems)) {
                // modified for differentiation
                if (this.options.displayMode == "items") {
                    attachments.push(item);
                } else {
                    addedItems.push(item);                     
                }
            }
        }
    }

    ...

    // call to the WebScript with attachments

    // modified for merge
    return addedItems.concat(attachments);
},

为了在流程变量中保存覆盖的附件,有必要定义 complete 事件的侦听器。

此外,可以通过使用此技术从一个任务到另一个任务(有更改)通过链 "pass" 文件:

complete 事件的侦听器:

public class TaskCompleteListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        DelegateExecution execution = delegateTask.getExecution();      
        execution.setVariable("mswf_package", delegateTask.getVariable("mswf_package"));
    }
}

create 事件的侦听器:

public class TaskCreateListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) { 
        DelegateExecution execution = delegateTask.getExecution();
        delegateTask.setVariable("mswf_package", execution.getVariable("mswf_package"));
    }
}

这解决了我的问题。