Delete DAM Asset using Workflow
This article shows you to create a simple workflow process step and use it for deleting an DAM Asset.
You can ask me why do I need a workflow when I can delete it directly using the delete button :)
The intention is to validate the asset information before deleting the asset or trigger any other workflow for the asset and do manipulations like updating the asset data to an user before the deletion as you will not be able to get that post deletion. you can catch the events in the Event Listeners, but there will be no data by then.
You can ask me why do I need a workflow when I can delete it directly using the delete button :)
The intention is to validate the asset information before deleting the asset or trigger any other workflow for the asset and do manipulations like updating the asset data to an user before the deletion as you will not be able to get that post deletion. you can catch the events in the Event Listeners, but there will be no data by then.
Simple Workflow Process Step
Create a workflow process step by using the below example
package com.flash.aem.core.workflow;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
/**
* The Class AssetValidationProcess.
*
* @author sudheerdvn
*/
@Component(service = WorkflowProcess.class, property = { "process.label=Asset Validation Proces" })
public class AssetValidationProcess implements WorkflowProcess {
/** The log. */
protected final Logger log = LoggerFactory.getLogger(this.getClass());
/** The resolver factory. */
@Reference
private ResourceResolverFactory resolverFactory;
/*
* (non-Javadoc)
*
* @see
* com.adobe.granite.workflow.exec.WorkflowProcess#execute(com.adobe.granite.
* workflow.exec.WorkItem, com.adobe.granite.workflow.WorkflowSession,
* com.adobe.granite.workflow.metadata.MetaDataMap)
*/
public void execute(WorkItem item, WorkflowSession wfsession, MetaDataMap args) throws WorkflowException {
log.info("In Execute");
String payload = item.getWorkflowData().getPayload().toString();
log.debug("payload is {}", payload);
/* Adding a condition to restrict it for only dam assets */
if (payload.startsWith("/content/dam")) {
Map param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver;
Session session = null;
try {
resolver = resolverFactory.getServiceResourceResolver(param);
session = resolver.adaptTo(Session.class);
if (null != session) {
Node assetNode = session.getNode(payload);
if (null != assetNode) {
//TODO: Validate asset references before deleting
log.debug("Deleting asset node {}", assetNode.getPath());
assetNode.remove();
session.save();
}
}
} catch (LoginException e) {
log.error("LoginException occurred while executing the workflow", e);
} catch (RepositoryException e) {
log.error("RepositoryException occurred while deleting the asset", e);
} finally {
if (null != session) {
session.logout();
}
}
}
}
}
Create a new workflow model
- Navigate to the Workflow Console and click on models
Tools --> Workflow --> Models - Click on Create --> New Model
- Enter the model Title and name and save
- Edit the model
- Drag and drop the process step from the sidebar ( Deleted the step 1 as we are not adding any participant as part of this process)
- Edit the process step
- Provide the title and select the Process step "Asset Validation Process" from the process tab
- Save the model
- Sync the model using top right button (Syncing will sync the model from /conf to /var --> this is where the run time workflow model will be present)
Workflow Model will look like below after the above steps
<?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/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 cq:designPath="/libs/settings/wcm/designs/default" cq:lastModified="{Date}2019-09-18T21:43:46.584-07:00" cq:lastModifiedBy="admin" cq:template="/libs/cq/workflow/templates/model" jcr:primaryType="cq:PageContent" jcr:title="Delete Asset" sling:resourceType="cq/workflow/components/pages/model"> <flow jcr:primaryType="nt:unstructured" sling:resourceType="foundation/components/parsys"> <process jcr:created="{Date}2019-09-18T21:35:08.438-07:00" jcr:createdBy="admin" jcr:lastModified="{Date}2019-09-18T21:43:46.573-07:00" jcr:lastModifiedBy="admin" jcr:primaryType="nt:unstructured" jcr:title="Asset Validation Process" sling:resourceType="cq/workflow/components/model/process"> <metaData jcr:primaryType="nt:unstructured" PROCESS="com.flash.aem.core.workflow.AssetValidationProcess" PROCESS_AUTO_ADVANCE="true"/> </process> </flow> </jcr:content> </jcr:root>
Once the above steps are done, you can run the workflow for any asset and for delete the asset only if the required conditions are met.
I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteAsset Management Software
Asset Management Software India
Asset Management Software Chennai
Asset Management System
Asset Tracking Software
Really very great information for that post, am amazed and then more new information are get after refer that post. I like that post.
ReplyDeleteAsset Management Software
Fixed Asset Management Software
Asset Tracking Software
Asset Management System
Asset Management Software Dubai