RE: Using workflow engine from external web application and JAX RS API modu

Nikolay Patyukov, modified 5 Years ago. New Member Posts: 3 Join Date: 6/17/20 Recent Posts
I'm new to Liferay, so I apologize in advance for my possible ingorance.
I have an external web application that interacts with Liferay server via REST API, and I was able to perform some JSON WS and Open API requests for DL and asset elements.
Right now I'm investigating a workflow and trying to create default 'Single Approver' workflow instance for a given document (file entry) in my custom API application, but I can't quite understand what exactly I have to use. Here is my @POST method

    @POST
    @Path("/startWorkflow")
    @Consumes("application/json")
    @Produces("application/json")
    public FileEntry startWorkflow(StartWorkflowRequest request) throws PortalException {
        FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(request.getDocumentId());
        ServiceContext serviceContext = new ServiceContext();
        serviceContext.setScopeGroupId(fileEntry.getGroupId());
        WorkflowDefinitionLinkLocalServiceUtil.addWorkflowDefinitionLink(request.getUserId(), fileEntry.getCompanyId(), fileEntry.getGroupId(),
                DLFileEntry.class.getName(), fileEntry.getPrimaryKey(), 0, "Single Approver", 1);
        return WorkflowHandlerRegistryUtil.startWorkflowInstance(fileEntry.getCompanyId(), fileEntry.getGroupId(),
                request.getUserId(), DLFileEntry.class.getName(), fileEntry.getPrimaryKey(), fileEntry, serviceContext,
                Collections.emptyMap());
    }

At first there was an error about absent workflow definition link, so I've added it (I'm not sure if it is right). Now it throws
Caused by: com.liferay[code].document.library.kernel.exception.NoSuchFileVersionException: No DLFileVersion exists with the primary key 36111
        at com.liferay.portlet.documentlibrary.service.persistence.impl.DLFileVersionPersistenceImpl.findByPrimaryKey(DLFileVersionPersistenceImpl.java:6543)
        at com.liferay.portlet.documentlibrary.service.persistence.impl.DLFileVersionPersistenceImpl.findByPrimaryKey(DLFileVersionPersistenceImpl.java:6561)
        at com.liferay.portlet.documentlibrary.service.impl.DLFileVersionLocalServiceImpl.getFileVersion(DLFileVersionLocalServiceImpl.java:71)

where '36111' is a fileEntryId from 'request.getDocumentId()'. There is no way I've found to tell workflow engine about file versions or something like that, and I don't know how workflow works with different file versions.
There are other ways to address workflow engine, for example, 'WorkflowInstanceManager' reference inside my component, but there is no way to pass any entity directly, as far as I understand we need to set some related information in 'workflowContext'.It's not clear to me how to use standard workflow via API, I've found no documentation about it and I have a lot of questions that I don't know how to answer:
What are workflow definition/instance links and how to use them?
How 'DLFileVersion' primary key is related to 'DLFileEntry'?
What exact way should I use from kernel library to manipulate workflow instances?
What 'workflowContext' should contain and how to pass it via API?
I'd appreciate some useful links or direct advices on workflow terminology and parameters. Thank you in advance!
thumbnail
Christoph Rabel, modified 5 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
Hmm, maybe you use just the wrong classname?
FileEntry fileEntry = ...
WorkflowDefinitionLinkLocalServiceUtil.addWorkflowDefinitionLink(request.getUserId(), fileEntry.getCompanyId(), fileEntry.getGroupId(),
                DLFileEntry.class.getName(), fileEntry.getPrimaryKey(), 0, "Single Approver", 1);
Nikolay Patyukov, modified 5 Years ago. New Member Posts: 3 Join Date: 6/17/20 Recent Posts
WorkflowHandler is registered only for 'DLFileEntry' class, so I've changed to getting file entry from 'DLFileEntryLocalServiceUtil':
DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(request.getDocumentId());
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(fileEntry.getGroupId());
WorkflowDefinitionLinkLocalServiceUtil.addWorkflowDefinitionLink(request.getUserId(), fileEntry.getCompanyId(), fileEntry.getGroupId(),
        DLFileEntry.class.getName(), fileEntry.getPrimaryKey(), 0, "Single Approver", 1);
return WorkflowHandlerRegistryUtil.startWorkflowInstance(fileEntry.getCompanyId(), fileEntry.getGroupId(),
        request.getUserId(), DLFileEntry.class.getName(), fileEntry.getPrimaryKey(), fileEntry, serviceContext,
        Collections.emptyMap());
Now 'DLFileEntry' is used everywhere. Still getting the same error about 'DLFileVersion'. I've looked up the DB, seems like all file versions there have their fileVersionId = fileEntryId + 1:
​​​​​​​
I don't get why liferay tries to find file version with id of file entry.
Alexey Ulasevich, modified 5 Years ago. New Member Post: 1 Join Date: 6/19/20 Recent Posts
I am not sure that it's possible set workflow for some file. Common way is set workflow for some folder. Then the workflow will be applied for every file uploaded to the folder.
See.: https://sourceforge.net/p/lportal/news/2014/05/how-to-put-your-document-library-folder-under-workflow-control-programmatically/
or https://liferay.dev/blogs/-/blogs/how-to-put-your-document-library-folder-under-workflow-control-programmatically
Nikolay Patyukov, modified 5 Years ago. New Member Posts: 3 Join Date: 6/17/20 Recent Posts
I'm not sure if applying workflow only for folders suits our needs, as we might want to start some workflow processes on individual documents (or even without any documents) manually.
Still wasn't able to figure it out, any help is appreciated.