Expand my Community achievements bar.

SOLVED

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

Avatar

Level 1

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   

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

@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

Avatar

Community Advisor

@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

Avatar

Community Advisor

@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.