cancel
Showing results for 
Search instead for 
Did you mean: 

Pass attachments to sub process (call activity)

cijujoseph
Champ on-the-rise
Champ on-the-rise
Hi,

I've a main process from where I'll be calling a sub-process using call activity. In the main process I've a user task form with an upload(attachment) field. I would like to show the attachment from the main process in a display value field on a user task form in the sub-process. How can I pass the related content reference to a sub-process in activiti app?

Regards,
Ciju
3 REPLIES 3

cijujoseph
Champ on-the-rise
Champ on-the-rise
I'm using version 1.2.2. Using the following task listener I managed to do it. Not sure if this is the best approach or not.

<java>
@Component("copyAttachmentTaskListener")
public class CopyAttachmentTaskListener implements TaskListener {

/**
  * This task listener copies the "upload" field's attachments in parent
  * process instance to a new attachment field "upload1" in the subprocess
  * task.
  */
private static final long serialVersionUID = 1L;

private static final Logger log = LoggerFactory
   .getLogger(CopyAttachmentTaskListener.class);

@Autowired
private UserService userService;

@Autowired
private RelatedContentService relatedContentService;

@Autowired
private RelatedContentRepository relatedContentRepository;

@Override
public void notify(DelegateTask delegateTask) {
  List<RelatedContent> relatedContent = new ArrayList<RelatedContent>();
  Page<RelatedContent> page = null;

  while ((page == null) || (page.hasNext())) {
   page = relatedContentRepository.findAllByProcessInstanceIdAndField(
     (String) delegateTask.getExecution().getVariable(
       "superProcessInstanceId"), "upload", null);
   relatedContent.addAll(page.getContent());
  }
  for (RelatedContent rc : relatedContent) {
   // Create a new related content object
   RelatedContent newRelatedContentObject = relatedContentService
     .createRelatedContent(userService.getUser(new Long(
       delegateTask.getAssignee())), rc.getName(), rc
       .getSource(), rc.getSourceId(), delegateTask
       .getId(), delegateTask.getProcessInstanceId(),
       "upload1", rc.getMimeType(), null, 0L);
   // Now copy all the existing values from original attachment
   newRelatedContentObject.setContentStoreId(rc.getContentStoreId());
   newRelatedContentObject
     .setContentAvailable(rc.isContentAvailable());  
   newRelatedContentObject.setContentSize(rc.getContentSize());
   newRelatedContentObject.setRelatedContent(rc.isRelatedContent());
   newRelatedContentObject.setField("upload1");
   // Save
   relatedContentRepository.saveAndFlush(newRelatedContentObject);
  }
}
}
</java>

Hello,

Its grate example to know how call activiti works. 

In the above example you are passing parent process uploaded content to child process but I want in reverse way like from child process uploaded content to parent  process. How can I achieve this?

Thanks

Hi Ciju,

I am trying to fetch the user task form attachments in task listener in the same process, using the activiti-6.0 (not the enterprise version)  and using the activit-ap-logic-6.0.

I kept the task listener in com.activiti.extension.bean package and the below code returns the null.

 page = relatedContentRepository.findAllByProcessInstanceIdAndField(
delegateTask.getProcessInstanceId().toString(), "selectbpmnfiles", null);

the "selectbpmnfiles" is the attachment field id in user task.

 

here is the complete code tried with different ways but none of them worked.

package com.activiti.extension.bean;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.activiti.app.domain.runtime.RelatedContent;
import org.activiti.app.repository.runtime.RelatedContentRepository;
import org.activiti.app.service.runtime.RelatedContentService;
import org.activiti.app.service.runtime.RelatedContentStreamProvider;
import org.activiti.content.storage.api.ContentObject;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.Execution;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;


//com.activiti.extension.bean.FileAttachmentTaskListener
@Component
public class FileAttachmentTaskListener implements TaskListener{
@Autowired
private RelatedContentService relatedContentService;
@Autowired
private RelatedContentRepository relatedContentRepository;
@Autowired
private UserDetailsService userService;
@Override
public void notify(DelegateTask delegateTask) {
// TODO Auto-generated method stub
System.out.println("FileAttachmentTaskListener start");
DelegateExecution execution = delegateTask.getExecution();
System.out.println("delegateTask.getProcessInstanceId() >>>>>>"+delegateTask.getProcessInstanceId());
System.out.println("execution.getProcessInstanceId() >>>>>>"+execution.getProcessInstanceId());
List<RelatedContent> relatedContent = new ArrayList<RelatedContent>();
Page<RelatedContent> page = null;

try {

//System.out.println("rcp - findAllByProcessInstanceIdAndField =>"+page.getContent().get(0).getField().toString());
if(relatedContentRepository.findAllByTaskIdAndField(delegateTask.getId().toString(), "selectbpmnfiles", null) != null)
System.out.println("rcp - findAllByTaskIdAndField =>"+relatedContentRepository.findAllByTaskIdAndField(delegateTask.getId().toString(), "selectbpmnfiles", null));
if(relatedContentRepository.findAllByTaskIdAndField(execution.getProcessInstanceId().toString(), "selectbpmnfiles", null) != null)
System.out.println("rcp - findAllByTaskIdAndField =>"+relatedContentRepository.findAllByTaskIdAndField(execution.getProcessInstanceId().toString(), "selectbpmnfiles", null));
if(relatedContentRepository.findAllContentByProcessInstanceId(delegateTask.getProcessInstanceId().toString(), null) != null)
System.out.println("rcp - findAllContentByProcessInstanceId =>"+relatedContentRepository.findAllContentByProcessInstanceId(delegateTask.getProcessInstanceId().toString(), null));
if(relatedContentRepository.findAllContentByProcessInstanceId(execution.getProcessInstanceId(), null) !=null)
System.out.println("rcp - findAllContentByProcessInstanceId =>"+relatedContentRepository.findAllContentByProcessInstanceId(execution.getProcessInstanceId(), null));
if(relatedContentRepository.findAllFieldBasedContentByTaskId(delegateTask.getId(), null) !=null)
System.out.println("rcp - findAllFieldBasedContentByTaskId =>"+relatedContentRepository.findAllFieldBasedContentByTaskId(delegateTask.getId(), null));
if(relatedContentRepository.findAllByProcessInstanceIdAndField(execution.getProcessInstanceId(), "selectbpmnfiles", null)!=null)
page = relatedContentRepository.findAllByProcessInstanceIdAndField(execution.getProcessInstanceId(), "selectbpmnfiles", null);

if(page !=null)
relatedContent.addAll(page.getContent());
} catch (Exception e) {
// TODO: handle exception
System.out.println("rcp - "+e);
}

try {
System.out.println("Try 2==>"+relatedContentService.getFieldContentForProcessInstance(delegateTask.getProcessInstanceId(), "selectbpmnfiles", 1, 0));
if(relatedContentService.getFieldContentForProcessInstance(delegateTask.getProcessInstanceId(), "selectbpmnfiles", 1, 0) !=null) {

List<RelatedContent> contentList = relatedContentService.getFieldContentForProcessInstance(
delegateTask.getProcessInstanceId(), "selectbpmnfiles", 1, 0).getContent();
System.out.println("contentList ==>"+contentList);
if (contentList != null) {
System.out.println("contentList ==>"+contentList.size());
for (RelatedContent relCon : contentList) {
System.out.println("Content file: " + relCon.getName() + ", created: " + relCon.getCreated());
ContentObject co = relatedContentService.getContentStorage().getContentObject(
relCon.getContentStoreId());

InputStream is =co.getContent();

String result = IOUtils.toString(is, StandardCharsets.UTF_8);
System.out.println("GDH:" + result);
// Get the InputStream and do stuff with the file
// co.getContent()
}
}
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("catch 1st exce==>"+e);
}
System.out.println("WHILE loop start");
while ((page == null) || (page.hasNext())) {
page = relatedContentRepository.findAllByProcessInstanceIdAndField(
delegateTask.getProcessInstanceId().toString(), "selectbpmnfiles", null);
System.out.println("Whle page ==>"+page);
if(page !=null)
relatedContent.addAll(page.getContent());

}


System.out.println("FileAttachmentTaskListener end");
}
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Can you help me to fix this issue.

Regards,

Sekhar