Expand my Community achievements bar.

SOLVED

why this code isnot adding the comment to the workflow

Avatar

Level 3
package com.adobe.aem.guides.wknd.core.servlets;
import jdk.internal.loader.Resource;
import jdk.internal.org.jline.utils.Log;

 

import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.oak.commons.jmx.ManagementOperation.Status;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;

 

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletPaths;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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;
import org.apache.sling.api.resource.ResourceResolver;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.model.WorkflowModel;

 

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Component(
    service = WorkflowProcess.class,
    property = {
        "process.label=Add Comment to Asset 1"
    }
)
public class ExecuteWorkFlow implements WorkflowProcess {

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        String path = workItem.getWorkflowData().getPayload().toString();

        try {

            ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

            Resource assetResource = resourceResolver.getResource(path);

            if (assetResource != null) {
                Session jcrSession = resourceResolver.adaptTo(Session.class);

                Node assetNode = assetResource.adaptTo(Node.class);

                if (assetNode.hasNode("jcr:content/metadata")) {
                    Node metadataNode = assetNode.getNode("jcr:content/metadata");

                    metadataNode.setProperty("customComment", "Your Comment Here");

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

Avatar

Correct answer by
Community Advisor

Hi @Mohammed-Skouti, You can try below code snippet; it's working fine for me.

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/components/collection");
}

Node commentNode = collectionNode.addNode("customComment");
commentNode.setPrimaryType("nt:unstructured");
commentNode.addMixin( "mix:lastModified");
commentNode.setProperty("sling:resourceType", "granite/comments/components/comment");
commentNode.setProperty("jcr:description", "Your Comment Here");

session.save();

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Mohammed-Skouti, You can try below code snippet; it's working fine for me.

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/components/collection");
}

Node commentNode = collectionNode.addNode("customComment");
commentNode.setPrimaryType("nt:unstructured");
commentNode.addMixin( "mix:lastModified");
commentNode.setProperty("sling:resourceType", "granite/comments/components/comment");
commentNode.setProperty("jcr:description", "Your Comment Here");

session.save();

 

Avatar

Administrator

@Mohammed-Skouti Did you find the suggestion from Mahedi helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni