Skip to main content

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.

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.


Comments

  1. 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.
    Asset Management Software
    Asset Management Software India
    Asset Management Software Chennai
    Asset Management System
    Asset Tracking Software

    ReplyDelete
  2. Really very great information for that post, am amazed and then more new information are get after refer that post. I like that post.
    Asset Management Software
    Fixed Asset Management Software
    Asset Tracking Software
    Asset Management System
    Asset Management Software Dubai

    ReplyDelete

Post a Comment

Popular posts from this blog

Groovy Console Integration and executing scripts in AEM

Steps to install Groovy in AEM. Below Github have the steps to install the latest Groovy console https://github.com/icfnext/aem-groovy-console You can also navigate to the releases section and download the required package. https://github.com/icfnext/aem-groovy-console/releases I have used 12.0.0 version for this demo. Download the Groovy Package ( Direct link to the package  ) Upload the package to AEM via Package manager  http://localhost:4502/crx/packmgr/index.jsp Once the package is installed, you should be able to access the groovy console  http://localhost:4502/etc/groovyconsole.html Note: From 13.0.0 release, the paths were changed from /etc/ to /apps/ to access Groovy Console Sample Groovy Scripts: Printing a simple String println 'Hello World'; Click on Run Script Displays Hello World in the Console Get a Page Object final def page = getPage('/content/aem-flash') println 'page path is: ' + page.path; Re

AEM Project Maven Build - Easy setup and steps to run commands via command line

We generally run maven commands every day to deploy the bundles or any other changes in the project to AEM server. There are a lot of options to sync them automatically like using Brackets or AEM Server plugin configurations in IDE's etc.. These are very faster for deployments and changes to be reflected in AEM server In case if you would like to do a complete build, we can run the same from Eclipse by using the run commands or via command prompt using the complete maven command but IDE's generally occupy lot of memory and might slow down sometimes. I generally use Command prompt for the builds. I generally navigate to the repo folder and type the maven command " mvn clean install -PautoInstallPackage,adobe-public " Note: adobe-public can be ignored if you already set the repo in the maven settings I work on different projects and repositories every day and navigating to different folders and typing the same command (You can always copy and paste the comman