com.day.cq.commons.jcr.JcrUtil.copy() is it safe method to copy content between components | Community
Skip to main content
Level 2
March 28, 2024
Solved

com.day.cq.commons.jcr.JcrUtil.copy() is it safe method to copy content between components

  • March 28, 2024
  • 3 replies
  • 1359 views

Hello,

I have a two AEM components, and I'm going to use com.day.cq.commons.jcr.JcrUtil.copy() method to copy a set of JCR nodes(with childrens) from one component into another every time with PostConstruct method in Sling Model. I need to copy content from first component into second depending on author choice in the second component. So that the second component will have the same content as first.

So my question is: are there any caveats during copy content JCR nodes between two components using this way? How safe is it?

Thank you   

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 Shashi_Mulugu

@antoname8 can you pls explain about the use case why you want to copy nodes from one component to another component everytime component loads? Maybe community can suggest better way of dealing with it.

 

But just to answer your question, always trying for highest abstraction api in order i.e AEM API > Sling API > JCR API.

 

So based on your need, see if you can find any AEM API, but I could find a sling api which you can use, see sling copy resource and if it fits your requirements. If you can't find anything in aem api or sling api, jcrutil copy method you identified works perfectly fine..

 

https://sling.apache.org/documentation/the-sling-engine/sling-api-crud-support.html

3 replies

Shashi_Mulugu
Community Advisor
Shashi_MuluguCommunity AdvisorAccepted solution
Community Advisor
March 29, 2024

@antoname8 can you pls explain about the use case why you want to copy nodes from one component to another component everytime component loads? Maybe community can suggest better way of dealing with it.

 

But just to answer your question, always trying for highest abstraction api in order i.e AEM API > Sling API > JCR API.

 

So based on your need, see if you can find any AEM API, but I could find a sling api which you can use, see sling copy resource and if it fits your requirements. If you can't find anything in aem api or sling api, jcrutil copy method you identified works perfectly fine..

 

https://sling.apache.org/documentation/the-sling-engine/sling-api-crud-support.html

aanchal-sikka
Community Advisor
Community Advisor
March 31, 2024

@antoname8 

 

Copying the content every time the Sling Model renders does not seem a nice. Its duplication rather than reuse.

 

If the relative paths of these components are constant with respect to the page or each other, we should consider picking the info from component 1 into component 2 and render. That way there is a single source of information.

 

Consider a scenario, where component 1 is updated, component 2 will not be in sync until the page is rendered again. Its confusing.

 

Aanchal Sikka
Anudeep_Garnepudi
Community Advisor
Community Advisor
April 2, 2024

@antoname8 

 

Copying content from Sling Model is considered as write operation, Sling Models are intent to support view i.e should be used only to read the data but not write.

 

You can get the reference/path of first component in second component if author selects option in second component and get first component properties in second component.

antoname8Author
Level 2
April 5, 2024

Thank you for your reply! I agree with you!