Add an attachment with a workflow

You can use workflows to add attachments to a community domain or asset in your Collibra Platform.

Example workflow

Take the user task script tasks from this example workflow and integrate the upload functionality into your own wrokflow. The user task asks for a file to be uploaded. The script task retrieves the required information from that file and uses the AddAttachmentRequest builder of the Java Core API to attach it to the current resource.

The user task form has a fileUpload field with the ID file that is marked as required.

The Groovy script task performs the following operations:

  • Sets a fileName and a fileStream variable that are required by the add attachment request builder:
    UUID fileUUID = string2Uuid(file)
    FileInfo fileInfo = fileApi.getFileInfo(fileUUID)
    String fileName = fileInfo.getName()
    
    InputStream fileStream = fileApi.getFileAsStream(fileUUID)

    The first three lines are meant to illustrate the various Java classes that are being used to get the name of the uploaded file. You can use String fileName = fileApi.getFileInfo(string2Uuid(file)).getName() instead.

  • Builds the add attachment request:
    AddAttachmentRequest attachmentRequest = AddAttachmentRequest.builder()
        .baseResourceId(item.getId())
        .baseResourceType(item.getType())
        .fileStream(fileStream)
        .fileName(fileName)
        .build()

    See the Introduction to builders tutorial for details about the builder method.

    The item variable refers to the current resource you are attaching the file to: a community, domain or asset. You use it to access the WorkflowBusinessItem bean of the Workflow Java API.

  • Attaches the uploaded file to the current resource:
    attachmentApi.addAttachment(attachmentRequest)

Here is the complete script:

import com.collibra.dgc.core.api.dto.instance.attachment.AddAttachmentRequest
import com.collibra.dgc.core.api.component.instance.AttachmentApi
import com.collibra.dgc.core.api.component.file.FileApi
import com.collibra.dgc.core.api.model.file.FileInfo

UUID fileUUID = string2Uuid(file)
FileInfo fileInfo = fileApi.getFileInfo(fileUUID)
String fileName = fileInfo.getName()

InputStream fileStream = fileApi.getFileAsStream(fileUUID)

AddAttachmentRequest attachmentRequest = AddAttachmentRequest.builder()
    .baseResourceId(item.getId())
    .baseResourceType(item.getType())
    .fileStream(fileStream)
    .fileName(fileName)
    .build()

attachmentApi.addAttachment(attachmentRequest)