Workflow to remove renditions other than the original one

In my previous post here, i explained the process to remove the renditions from any given path. But don’t you think this is cumbersome because every time at particular location, if multiple dam assets are submitted, developer/support guy has to check and run the servlet to remove the renditions.

Wouldn’t it nice to create a generic process like a workflow which automatically triggers and remove the rendition as soon as any dam asset is submitted. Today we are going to look as to how we can achieve renditions removal using workflow. I will post some sample code and will detail out the process for people who might not be familiar in writing a workflow process in CQ5.

1) First of all, you will have to create a new workflow model.

2) Use the source code below to create a workflow process step. The label in the code below will help you identify the process step explained below with help of screen shot.

3) If you look closely in the image attached, i dragged a process step component on the demo workflow and the process step label in the code can be seen in the dropdown on the right. If this label is not appearing then there is something wrong with your code.

4) Once you have configured these steps, click on save.

5) You now will have to create a launcher which will bind these workflows with specific file types and on specific path. If you need the functionality on a whole dam then the path that would be given would be /content/dam. You can look at the samples provided in workflow launcher console for rendtions, a similar launcher would be created for this code as well.

workflow cq5

 @Component  
 @Service  
 @Properties({  
      @Property(name = Constants.SERVICE_DESCRIPTION, value = "The logic can be used to remove rendition from all the subnodes below a certain path"),  
      @Property(name = Constants.SERVICE_VENDOR, value = "Sahil Thadhani Test"),  
      @Property(name = "process.label", value = "Rendition Remover") })  
 public class RenditionRemoverWorkflow implements WorkflowProcess{  
       @Reference  
       private ResourceResolverFactory resourceResolverFactory;  
       private ResourceResolver resourceResolver;  
       @Reference  
       private QueryBuilder builder;  
       private Session session;  
       @Override  
        public void execute(WorkItem item, WorkflowSession wfsession, MetaDataMap args)  
             throws WorkflowException {  
                 try {  
                     resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);  
                     WorkflowData workflowData = item.getWorkflowData();  
               String path = workflowData.getPayload().toString();  
               path = path.replace("/jcr:content/renditions", "");  
               session = resourceResolver.adaptTo(Session.class);  
                     Map<String, String> map = new HashMap<String, String>();  
                     map.put("path", path);  
                     map.put("property", "jcr:primaryType");  
                     map.put("property.1_value", "nt:folder");  
                     Query query = builder.createQuery(PredicateGroup.create(map), session);  
               SearchResult result = query.getResult();  
               List<Hit> hits = result.getHits();  
               Resource renditionResource = resourceResolver.resolve(hits.get(0).getPath());       
               LoggerUtil.debugLog(this.getClass(), "renditionRersour path is{}",renditionResource.getPath());  
               Iterator<Resource> reneditionIterator = renditionResource.listChildren();  
               while(reneditionIterator.hasNext()){  
                    Resource specificResource= reneditionIterator.next();  
                    Node renditionNode = specificResource.adaptTo(Node.class);  
                    if(!renditionNode.getName().equals("original")){  
                         renditionNode.remove();  
                    }  
               }  
                     session.save();  
                     session.logout();  
                }catch (Exception e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                }  
       }  
 }  
Advertisements

One thought on “Workflow to remove renditions other than the original one

  1. Hi Sahil,

    Thank you for sharing this. It is very helpful.

    I am trying to implement a logic in such a way that CQ should send out email notification to system administrators , when the number of stale workflows in the system exceeds ‘x’. I am from a support background and new to CQ. Any thoughts/pointers/tips/code would be really helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s