Expand my Community achievements bar.

AEM 6.5 Workflow Launcher Conditions to set dc:title property to filename or another property value

Avatar

Level 2

Hi Community,

I am working with AEM 6.5

I have written a custom workflow process step to add metadata properties under the asset metadata node

for Ex: add property title to metadata node with value filename

 

when I Start the Workflow Model on an asset having metadata property dc:title the workflow process is successfully updating the dc:title property with the filename

 

But when I trigger the Workflow Launcher (attached the screenshot) with the same Workflow Model it is unable to override the property dc:title

 

Is there any way or any condition to override the dc:title property while using Workflow Launcher

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

12 Replies

Avatar

Level 4

Hi,

could you share the workflow step code? how are you getting the metadata node?

Hi @VictorToledo_ 
please find the requested code

 

public class AAWKNDCustomAssetMetadataProcess implements WorkflowProcess {

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
            throws WorkflowException {
        try {
            WorkflowData workflowData = workItem.getWorkflowData();

            // Check if the payload is an asset (JCR_PATH)
            if ("JCR_PATH".equals(workflowData.getPayloadType())) {
                String assetPath = workflowData.getPayload().toString();
                String metadataPath = getMetadataPath(assetPath);

                if (StringUtils.isNotEmpty(metadataPath)) {
                    LOGGER.info("Asset Path: {}", assetPath);
                    LOGGER.info("Metadata Node Path: {}", metadataPath);

                    // Get the filename without extension
                    String filename = getProcessedTitle(assetPath);

                    // Add the filename as properties to the metadata node
                    addFilenameToMetadata(metadataPath, workflowSession.adaptTo(Session.class), filename);
                } else {
                }
            }
        } catch (Exception e) {
            throw new WorkflowException("Error processing asset metadata", e);
        }
    }

    private void addFilenameToMetadata(String metadataPath, Session session, String filename) {
        try {
            Node metadataNode = session.getNode(metadataPath);

            // Add the filename as the new dc:title property
            metadataNode.setProperty("dc:title", filename);
            LOGGER.info("DC Title property value is: {}", filename);
            
            // Save the changes to the session
            session.save();
        }
    }

    private String getMetadataPath(String assetPath) {
        try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(getServiceUserMap())) {
            Resource assetResource = resourceResolver.getResource(assetPath);
            if (assetResource != null) {
                Resource metadataResource = assetResource.getChild("jcr:content/metadata");
                if (metadataResource != null) {
                    return metadataResource.getPath();
                }
            }
        } catch (Exception e) {
        }
        return null;
    }

    private String getProcessedTitle(String assetPath) {
        // Extract the filename from the assetPath and remove the extension
        return assetPath.substring(assetPath.lastIndexOf('/') + 1);
    }

    private Map<String, Object> getServiceUserMap() {
        Map<String, Object> serviceUserMap = new HashMap<>();
        // Set the service user mapping according to your AEM setup
        serviceUserMap.put(ResourceResolverFactory.SUBSERVICE, "my-subservice");
        return serviceUserMap;
    }
}

Hi @nviswanathareddy 

looks good to me. I was doing some tests with your code and launcher and it is working fine on my local.

(if the launcher is executing the workflow you can debug it and see where is the issue.)

Make sure that your resource resolver has the right permissions for the launchers path and dam path.

VictorToledo__0-1691155677252.png

 

for me understanding you are able to execute the workflow using the launcher. But it is not working as you expected. Please add more details where you have the issue

 

 

Hi @VictorToledo_ 

As you can see in the attached images, when I debug the code I am able to get the results as expected
I tried adding jcr:title and filenameTitle properties, these are added to the node as expected, but I'm having trouble with only dc:title it is not getting overridden, it is taking the existing metadata property itself

also as soon I upload and refresh the node I can see the result for dc:title also after its fully uploaded it gets back to its original property

 

- when the image is in a processing state:

Screenshot 2023-08-05 073957.png

 

- when the image is processed:

Screenshot 2023-08-05 074038.png

 

Note: If I upload any asset that doesn't have any metadata property dc:title it works fine

Screenshot 2023-08-05 075630.png

 

 

do we have any condition or any step in the workflow that fulfills my requirement?

Hi @VictorToledo_ 
please find the code below

 

package com.adobe.aem.guides.core.workflows;

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 org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Node;
import javax.jcr.Session;
import java.util.HashMap;
import java.util.Map;

@Component(service = WorkflowProcess.class, property = {
        Constants.SERVICE_DESCRIPTION + "=Custom Workflow Process Step for WKND Project to update/add filename property",
        Constants.SERVICE_VENDOR + "=Adobe Systems",
        "process.label" + "=AA WKND Asset Metadata Filename Property Update Process"
})
public class AAWKNDCustomAssetMetadataProcess implements WorkflowProcess {

    private static final Logger LOGGER = LoggerFactory.getLogger(AAWKNDCustomAssetMetadataProcess.class);

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
            throws WorkflowException {
        LOGGER.info("Custom Workflow Process to Add filename Property for Asset Metadata in WKND Project");
        try {
            WorkflowData workflowData = workItem.getWorkflowData();

            // Check if the payload is an asset (JCR_PATH)
            if ("JCR_PATH".equals(workflowData.getPayloadType())) {
                String assetPath = workflowData.getPayload().toString();
                String metadataPath = getMetadataPath(assetPath);

                if (StringUtils.isNotEmpty(metadataPath)) {
                    LOGGER.info("Asset Path: {}", assetPath);
                    LOGGER.info("Metadata Node Path: {}", metadataPath);

                    // Get the filename without extension
                    String filename = getProcessedTitle(assetPath);

                    // Add the filename as properties to the metadata node
                    addFilenameToMetadata(metadataPath, workflowSession.adaptTo(Session.class), filename);
                } else {
                    LOGGER.warn("Metadata not found for Asset Path: {}", assetPath);
                }
            }
        } catch (Exception e) {
            LOGGER.error("Error processing asset metadata", e);
            throw new WorkflowException("Error processing asset metadata", e);
        }
    }

    private void addFilenameToMetadata(String metadataPath, Session session, String filename) {
        try {
            Node metadataNode = session.getNode(metadataPath);

            // Add the filename as the new dc:title property
            metadataNode.setProperty("dc:title", filename);
            LOGGER.info("DC Title property value is: {}", filename);

            // Set the filename as the jcr:title property of the metadata node
            metadataNode.setProperty("jcr:title", filename);
            LOGGER.info("JCR Title property value is: {}", filename);

            // Set the filename as the jcr:title property of the metadata node
            metadataNode.setProperty("filenameTitle", filename);
            LOGGER.info("JCR Title property value is: {}", filename);


            // Save the changes to the session
            session.save();
            LOGGER.info("Properties updated in metadata node: {}", metadataPath);
        } catch (Exception e) {
            LOGGER.error("Error updating dc:title property in metadata node: {}", metadataPath, e);
        }
    }

    private String getMetadataPath(String assetPath) {
        try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(getServiceUserMap())) {
            Resource assetResource = resourceResolver.getResource(assetPath);
            if (assetResource != null) {
                Resource metadataResource = assetResource.getChild("jcr:content/metadata");
                if (metadataResource != null) {
                    return metadataResource.getPath();
                }
            }
        } catch (Exception e) {
            LOGGER.error("Error getting metadata path for asset: {}", assetPath, e);
        }
        return null;
    }

    private String getProcessedTitle(String assetPath) {
        // Extract the filename from the assetPath and remove the extension
        return assetPath.substring(assetPath.lastIndexOf('/') + 1);
    }

    private Map<String, Object> getServiceUserMap() {
        Map<String, Object> serviceUserMap = new HashMap<>();
        // Set the service user mapping according to your AEM setup
        serviceUserMap.put(ResourceResolverFactory.SUBSERVICE, "my-subservice");
        return serviceUserMap;
    }
}

Avatar

Community Advisor

@nviswanathareddy :  I think, Your idea is to trigger the workflow whenever there is an asset upload under /content/dam/wknd right.

In this situation i think, your workflow is not getting triggered itself.

 

Can you change the path as below (as the below path states that  upload anything under the wknd folder you workflow gets triggered.)

/content/dam/wknd(/.*/)

 

Let me know if still didnt work.

 

Thanks 

Siva

Thanks,
Siva

Hi @SivakumarKanoori 

Thanks for your response, your understanding was right

I am trying to update dc:title property on an asset, when I trigger the workflow model it was able to update the dc:title successfully
But when I use the same workflow model with a launcher my dc:title property was unable to update, it is taking the title of the asset as the dc:title only, if an asset doesn't have the title the workflow launcher can successfully update the property

Avatar

Community Advisor

@nviswanathareddy : As stated in the previous comment, try to edit the launcher and change the path to /content/dam/wknd(/.*/)

 

Thanks,
Siva

@SivakumarKanoori 
not working with your update, the  workflow model has no effect on the asset when I use /content/dam/wknd(/.*/)