Expand my Community achievements bar.

SOLVED

this code should map the photoshop:status after import the metadata and after trigger the workflow it should add the value of the photoshop:status from meta data as a comment but it always go to "cant match the metadata" whats wrong

Avatar

Level 3


package com.adobe.aem.guides.wknd.core.servlets;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;

@Component(service = WorkflowProcess.class, property = { "process.label = Import Image Comments from Payload step 2" })
public class ServletWorkWithAemMetaData implements WorkflowProcess {
    private static final Logger log = LoggerFactory.getLogger(ExecuteWorkFlow.class);
 
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        String commentDescription = metaDataMap.get("photoshop:State", String.class);
        String assetPaths = metaDataMap.get("assetPath",String.class);
   
        // Split the assetPaths string into individual paths
        String[] paths = assetPaths.split(",");
   
        for (String path : paths) {
            try(ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class)) {
                Resource assetResource = resourceResolver.getResource(path.trim());
   
                if(assetResource != null) {
                    Session jcrSession = resourceResolver.adaptTo(Session.class);
                    Node assetNode = assetResource.adaptTo(Node.class);
   
                    Node collectionNode;
                    if (assetNode.hasNode("jcr:content/comments")) {
                        collectionNode = assetNode.getNode("jcr:content/comments");
                    } else {
                        collectionNode = assetNode.addNode("jcr:content/comments");
                        collectionNode.setPrimaryType("nt:unstructured");
                        collectionNode.addMixin("mix:lastModified");
                        collectionNode.setProperty("sling:resourceType", "granite/comments/component/collection");
                    }
   
                    Node commentNode = collectionNode.addNode("Comment3");// Ensure this is unique or generate a unique ID
                    commentNode.setPrimaryType("nt:unstructured");
                    commentNode.addMixin("mix:lastModified");
                    commentNode.setProperty("sling:resourceType", "granite/comments/components/comment");
                    if (commentDescription != null && !commentDescription.isEmpty()) {
                        commentNode.setProperty("jcr:description", commentDescription);
                    } else {
                        commentNode.setProperty("jcr:description", "cant match the metadata");
                    }
   
                    jcrSession.save();
                }
            } catch(RepositoryException e) {
                log.error("Error while adding comment from workflow", e);
            }
        }
    }
   


}


1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Based on my understanding from your previous questions, you import metadata using the Asset console. Afterward, you manually initiate the workflow by choosing an asset as the payload. In this scenario, the "photoshop:State" is not accessible directly in the workflow metadata; instead, you need to retrieve it from the asset metadata. 

final String path = workItem.getWorkflowData().getPayload().toString();

try(ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class)) {
  Resource assetResource = resourceResolver.getResource(path);

  if(assetResource != null) {
    Asset asset = assetResource.adaptTo(Asset.class);
    String commentDescription = asset.getMetadataValue("photoshop:State");

    Session jcrSession = resourceResolver.adaptTo(Session.class);
    Node assetNode = assetResource.adaptTo(Node.class);

    Node collectionNode;
    if (assetNode.hasNode("jcr:content/comments")) {
      collectionNode = assetNode.getNode ("jcr:content/comments");
    } else {
      collectionNode = assetNode.addNode("jcr:content/comments");
      collectionNode.setPrimaryType ("nt:unstructured");
      collectionNode.addMixin("mix:lastModified");
      collectionNode.setProperty ("sling:resourceType",
         "granite/comments/component/collection");
    }

    Node commentNode = collectionNode.addNode ("Comment3");
    commentNode.setPrimaryType("nt:unstructured");
    commentNode.addMixin( "mix:lastModified");
    commentNode.setProperty("sling:resourceType", 
        "granite/comments/components/comment");
 
    commentDescription = commentDescription != null && !commentDescription.isEmpty()
        ? commentDescription : "cant match the metadata";
    commentNode.setProperty("jcr:description", commentDescription);

    jcrSession.save();
  }
} catch(RepositoryException e) {
  e.printStackTrace ();
}

 

 

View solution in original post

5 Replies

Avatar

Community Advisor

It looks like the issue is because the "commentDescription" is null, so the issue is in this line, are you sure the "metaDataMap" object should contain the property "photoshop:state" attribute? 

String commentDescription = metaDataMap.get("photoshop:State", String.class);

Can you try this?

String commentDescription= workItem.getWorkflowData().getMetaDataMap().get("photoshop:State", String.class);

 



Esteban Bustamante

Avatar

Level 3

no still not working
and yeah by default it has  photoshop:state you can check this

Avatar

Employee Advisor

Hi,

 

The code attempts to import comments from asset metadata into AEM assets via a workflow. If it always falls back to "can't match the metadata," here are some steps:

  1. Check Metadata Key: Ensure the metadata key "photoshop:State" matches exactly, considering case sensitivity.

  2. Logging: Add logging to print the values of commentDescription and assetPaths. This helps debug what's retrieved from metadata.

  3. Metadata Structure: Confirm the metadata structure in assets matches expectations, especially the presence of "photoshop:State."

  4. Fallback Value: Ensure the fallback value ("cant match the metadata") doesn't inadvertently overwrite actual metadata.

  5. Asset Paths: Verify that the assetPath in metadata is correctly formatted and contains the paths of the assets you want to update.

  6. Permissions: Check that the workflow user has the necessary permissions to read metadata and modify assets.

Avatar

Level 3

is there away to debug the workflow to check if the map really working 

Avatar

Correct answer by
Community Advisor

Based on my understanding from your previous questions, you import metadata using the Asset console. Afterward, you manually initiate the workflow by choosing an asset as the payload. In this scenario, the "photoshop:State" is not accessible directly in the workflow metadata; instead, you need to retrieve it from the asset metadata. 

final String path = workItem.getWorkflowData().getPayload().toString();

try(ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class)) {
  Resource assetResource = resourceResolver.getResource(path);

  if(assetResource != null) {
    Asset asset = assetResource.adaptTo(Asset.class);
    String commentDescription = asset.getMetadataValue("photoshop:State");

    Session jcrSession = resourceResolver.adaptTo(Session.class);
    Node assetNode = assetResource.adaptTo(Node.class);

    Node collectionNode;
    if (assetNode.hasNode("jcr:content/comments")) {
      collectionNode = assetNode.getNode ("jcr:content/comments");
    } else {
      collectionNode = assetNode.addNode("jcr:content/comments");
      collectionNode.setPrimaryType ("nt:unstructured");
      collectionNode.addMixin("mix:lastModified");
      collectionNode.setProperty ("sling:resourceType",
         "granite/comments/component/collection");
    }

    Node commentNode = collectionNode.addNode ("Comment3");
    commentNode.setPrimaryType("nt:unstructured");
    commentNode.addMixin( "mix:lastModified");
    commentNode.setProperty("sling:resourceType", 
        "granite/comments/components/comment");
 
    commentDescription = commentDescription != null && !commentDescription.isEmpty()
        ? commentDescription : "cant match the metadata";
    commentNode.setProperty("jcr:description", commentDescription);

    jcrSession.save();
  }
} catch(RepositoryException e) {
  e.printStackTrace ();
}