Skip to content

Instantly share code, notes, and snippets.

@sreeni-b
Last active August 27, 2022 10:59
Show Gist options
  • Save sreeni-b/6f95860ba7419511dbab0880b8433871 to your computer and use it in GitHub Desktop.
Save sreeni-b/6f95860ba7419511dbab0880b8433871 to your computer and use it in GitHub Desktop.
package com.aemks.core.workflow;
import com.adobe.cq.dam.cfm.ContentFragment;
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.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.core.process.ProcessingProfileApplier;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
/**
* By default, the metadata processing profile does not work for content fragments.
* Hence this custom process is required to apply processing profile on create
* of any Content Fragment.
*/
@Component(property = { "process.label = Content Fragment Metadata Processor" })
public class ContentFragmentMetadataProcessor implements WorkflowProcess {
private ProcessingProfileApplier processingProfileApplier = new ProcessingProfileApplier();
@Override public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {
Session session = workflowSession.adaptTo(Session.class);
WorkflowData data = workItem.getWorkflowData();
String payloadPath = data.getPayload().toString();
if(payloadPath.endsWith("/jcr:content")){
payloadPath=payloadPath.replace("/jcr:content","");
}
ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
if(resourceResolver==null){
throw new WorkflowException("Resource Resolver is null");
}
Resource assetResource = resourceResolver.getResource(payloadPath);
if(assetResource==null){
throw new WorkflowException("Asset resource is null");
}
ContentFragment contentFragment = assetResource.adaptTo(ContentFragment.class);
if(contentFragment==null){
throw new WorkflowException("Asset resource is not a content fragment");
}
Asset asset = DamUtil.resolveToAsset(assetResource);
try {
processingProfileApplier.applyProcessingProfile(session,asset);
} catch (RepositoryException e) {
throw new WorkflowException("Error occurred while applying processing profile to Content Fragment",e);
}
}
}
@r-malav
Copy link

r-malav commented Aug 27, 2022

Hi can share project create command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment