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
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);
}
}
}
}