AEM 6.5.12.0 - Start Bulk Workflow on Assets using Workflow Packages

Goal

Similar to Sites pages, add a Start Bulk Workflow action bar button in Assets Console to start a workflow on multiple assets as a package so there is only one inbox notification for all assets in the package (eg. when used with a Request for Activation workflow)

Demo | Package Install | Github


Button in UI


Start Workflow


Inbox Notification


In Replication


Solution

1) Create a maven project for the extension eaem-assets-start-bulk-wf

mvn -B org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate -D archetypeGroupId=com.adobe.aem 
-D archetypeArtifactId=aem-project-archetype -D archetypeVersion=36 -D aemVersion="6.5.10"
-D appTitle="Experience AEM Assets Start Bulk WF" -D appId="eaem-assets-start-bulk-wf" -D groupId="apps.experienceaem.assets"
-D frontendModule=none -D includeExamples=n -D includeDispatcherConfig=n


2) Create the Start Bulk Workflow button /apps/eaem-assets-start-bulk-wf/components/content/start-bulk-wf-but

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
granite:rel="cq-damadmin-admin-actions-eaem-start-bulk-wf-activator"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/collection/action"
icon="chevronDoubleRight"
target=".cq-damadmin-admin-childpages"
text="Start Bulk Workflow"
variant="actionBar">
</jcr:root>


3) Create the page for selecting workflow model /apps/eaem-assets-start-bulk-wf/components/content/start-bulk-workflow

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:Page">
<jcr:content
jcr:mixinTypes="[sling:VanityPath]"
jcr:primaryType="nt:unstructured"
jcr:title="Start Bulk Workflow"
sling:resourceType="granite/ui/components/coral/foundation/page">
<head jcr:primaryType="nt:unstructured">
<favicon
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/page/favicon"/>
<viewport
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/admin/page/viewport"/>
<clientlibs
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/includeclientlibs"
categories="[coralui3,granite.ui.coral.foundation,eaem.bulkwf]"/>
</head>
<body
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/page/body">
<items jcr:primaryType="nt:unstructured">
<content
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form"
action="/var/workflow/instances.package"
foundationForm="{Boolean}true"
maximized="{Boolean}true"
method="post"
novalidate="{Boolean}true"
style="vertical">
<successresponse
jcr:primaryType="nt:unstructured"
jcr:title="Success"
sling:resourceType="granite/ui/components/coral/foundation/form/responses/openprompt"
open="/libs/cq/workflow/admin/console/content/instances.html"
redirect="/assets.html"
text="Workflow started..."/>
<items jcr:primaryType="nt:unstructured">
<wizard
jcr:primaryType="nt:unstructured"
jcr:title="Start Workflow"
sling:resourceType="granite/ui/components/coral/foundation/wizard">
<items jcr:primaryType="nt:unstructured">
<area
jcr:primaryType="nt:unstructured"
jcr:title="Start Workflow..."
sling:resourceType="granite/ui/components/coral/foundation/container"
maximized="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<columns
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
margin="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<charset
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/hidden"
name="_charset_"
value="utf-8"/>
<model
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
emptyText="Select one"
fieldLabel="Workflow model"
ignoreData="{Boolean}true"
name="workflowModel"
required="{Boolean}true">
<datasource
jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/coral/common/admin/timeline/events/workflow/datasources/models"/>
</model>
<workflowtitle
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Workflow title"
ignoreData="{Boolean}true"
name="workflowTitle"
required="{Boolean}true"/>
<keeppackage
granite:rel="cq-sites-startbulkworkflows-keeppackage"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
checked="{Boolean}false"
disabled="{Boolean}true"
fieldDescription="If checked, the workflow package will have a unique name."
text="Keep workflow package"
value="true"/>
<packagetitle
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
disabled="{Boolean}true"
fieldLabel="Package title"
ignoreData="{Boolean}true"
name="packageTitle"
required="{Boolean}true"/>
<packagesPath
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/hidden"
name="workflowPackagesPath"
value="/var/workflow/packages${empty tenant.id? &quot;&quot; : &quot;/&quot;}${tenant.id}"/>
</items>
</column>
</items>
</columns>
</items>
<parentConfig jcr:primaryType="nt:unstructured">
<prev
granite:class="foundation-wizard-control"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/anchorbutton"
href="/aem/start.html"
text="Cancel">
<granite:data
jcr:primaryType="nt:unstructured"
foundation-wizard-control-action="cancel"/>
</prev>
<next
granite:class="foundation-wizard-control"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/button"
text="Start"
type="submit"
variant="primary">
<granite:data
jcr:primaryType="nt:unstructured"
foundation-wizard-control-action="next"/>
</next>
</parentConfig>
</area>
</items>
</wizard>
</items>
</content>
</items>
</body>
</jcr:content>
</jcr:root>


4) Create a clientlib /apps/eaem-assets-start-bulk-wf/clientlibs with  categories="[dam.gui.actions.coral,eaem.bulkwf" and dependencies="lodash.compat" for adding script to add the button and start bulk workflow....

(function ($, $document) {
"use strict";

const ASSETS_PAGE = "/assets.html",
FOU_COL_ACT_HIDDEN = "foundation-collection-action-hidden",
START_BULK_WF_PAGE = "/apps/eaem-assets-start-bulk-wf/components/content/start-bulk-workflow.html",
START_WF_BUT_URL = "/apps/eaem-assets-start-bulk-wf/components/content/start-bulk-wf-but.html",
BULK_WF_ACTIVATOR = ".cq-damadmin-admin-actions-eaem-start-bulk-wf-activator",
SHARE_ACTIVATOR = "cq-damadmin-admin-actions-adhocassetshare-activator";

if(isEaemBulkWfPage()){
$document.on("foundation-contentloaded", addSrcPathList);
}else if(isAssetsPage()) {
$document.on("foundation-selections-change", function(){
let $startWfBut = $(BULK_WF_ACTIVATOR);

if(!_.isEmpty($startWfBut)){
$startWfBut.removeClass(FOU_COL_ACT_HIDDEN);
return;
}

$.ajax(START_WF_BUT_URL).done(addActionBarButton);
});
}

function addSrcPathList(){
let $form = $("form");

_.each(queryParameters().paths.split(","),(path) => {
$form.append($("<input type='hidden'/>").attr("name", "srcPathList").attr("value", path));
});
}

function addActionBarButton(html){
const $eActivator = $("." + SHARE_ACTIVATOR);

if ($eActivator.length == 0) {
return;
}

let $startWfBut = $(html).css("margin-left", "20px").insertBefore($eActivator);

$startWfBut.click(openStartWfPage);
}

function openStartWfPage(){
const $items = $(".foundation-selections-item"),
assetPaths = [];

$items.each(function () {
assetPaths.push($(this).data("foundationCollectionItemId"));
});

window.open(START_BULK_WF_PAGE + "?paths=" + assetPaths.join(","), '_blank');
}

function queryParameters() {
var result = {}, param,
params = document.location.search.split(/\?|\&/);

params.forEach( function(it) {
if (_.isEmpty(it)) {
return;
}

param = it.split("=");
result[param[0]] = param[1];
});

return result;
}

function isEaemBulkWfPage(){
return (window.location.pathname.indexOf(START_BULK_WF_PAGE) >= 0);
}

function isAssetsPage() {
return (window.location.pathname.indexOf(ASSETS_PAGE) >= 0);
}
}(jQuery, jQuery(document)));


5) Workflow packages can be created using CURL sending the following request..

http://localhost:4502/var/workflow/instances.package

workflowModel: /var/workflow/models/request_for_activation
workflowTitle: group-activate
workflowPackagesPath: /var/workflow/packages
srcPathList: /content/we-retail/ca/en/about-us
srcPathList: /content/we-retail/ca/en/equipment
:cq_csrf_token: eyJleHAiOjE2NTI5NzkxNjQsImlhd


No comments:

Post a Comment