why this code isnot adding the comment to the workflow | Community
Skip to main content
Level 3
September 15, 2023
Solved

why this code isnot adding the comment to the workflow

  • September 15, 2023
  • 2 replies
  • 739 views
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();
        }
    }
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mahedi_Sabuj

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

 

2 replies

Mahedi_Sabuj
Community Advisor
Mahedi_SabujCommunity AdvisorAccepted solution
Community Advisor
September 15, 2023

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

 

Mahedi Sabuj
kautuk_sahni
Community Manager
Community Manager
September 18, 2023

@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