Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Servlet for fetching data from one node and using that data elsewhere

Avatar

Level 5

Say I have multiple pages under /content/practice/articles. Now in /content/practice/articles/firstpage/jcr:content there is a property named myImage. I want to read and fetch that value from myImage property which is a path basically.

 

Now I want to navigate to /content/practice/articles/firstpage/jcr:content/root/container/detailed_page and replace the properties (mainImage & smallImage) value with the data fetched from myImage property.

 

Can someone help me with the servlet code that I need to write in order to fetch the same?

 

Note : There are 500 pages under /content/practice/articles and I would have to do the above for all of them.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
sample code 
- @component(service = Servlet.class, property = {
        Constants.SERVICE_DESCRIPTION + "= Sample servlet to copy property value",
        "sling.servlet.methods=" + HttpConstants.METHOD_POST,
        "sling.servlet.paths=" + "/bin/copy-property"
})
public class CopyPropertyServlet extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(CopyPropertyServlet.class);

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ResourceResolver resourceResolver = request.getResourceResolver();
        Session session = resourceResolver.adaptTo(Session.class);

        // Get the source property value
        Node sourceNode = session.getNode("/content/practice/articles/jcr:content");
        Property sourceProperty = sourceNode.getProperty("myImage");
        String sourcePropertyValue = sourceProperty.getString();

        // Get the destination property and set the value
        Node destinationNode = session.getNode("/content/practice/articles/jcr:content/root/container/main_page");
        destinationNode.setProperty("mainImage", sourcePropertyValue);

        // Save the changes
        session.save();

        response.getWriter().write("Property value copied successfully");
    }
}

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor
sample code 
- @component(service = Servlet.class, property = {
        Constants.SERVICE_DESCRIPTION + "= Sample servlet to copy property value",
        "sling.servlet.methods=" + HttpConstants.METHOD_POST,
        "sling.servlet.paths=" + "/bin/copy-property"
})
public class CopyPropertyServlet extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(CopyPropertyServlet.class);

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ResourceResolver resourceResolver = request.getResourceResolver();
        Session session = resourceResolver.adaptTo(Session.class);

        // Get the source property value
        Node sourceNode = session.getNode("/content/practice/articles/jcr:content");
        Property sourceProperty = sourceNode.getProperty("myImage");
        String sourcePropertyValue = sourceProperty.getString();

        // Get the destination property and set the value
        Node destinationNode = session.getNode("/content/practice/articles/jcr:content/root/container/main_page");
        destinationNode.setProperty("mainImage", sourcePropertyValue);

        // Save the changes
        session.save();

        response.getWriter().write("Property value copied successfully");
    }
}

Avatar

Level 5

Hi @Nitin_laad , need some more help. Can you please surround this with try/catch block? And in my scenario there are 500 pages under /content/practice/articles. So can you please format the code accordingly?

Like get all nodes under that path and then maybe fetch that nod get the property value and then replace the existing value with this recent one?