Hi,
Am Montag, den 24.09.2007, 11:34 -0300 schrieb Edgar Poce:
> Another doubt is what's the recommended practice to create a simple
> sling component with write capabilities, how the component should
> access the jcr repository to perform write operations? and how the jcr
> session should be acquired?
The nice thing about Sling is, that you do not have to write the data
yourself by acquiring a session and calling all these JCR methods. You
just take the Content you get from the request, update it using the
Content's setter methods and call the ContentManager.store(Content)
method. That is all.
For example: Consider you have a TextComponent and a TextContent, where
the TextContent has a title and a text field. In your
TextComponent.service() method you might do the following:
if ("POST".equalsIgnoreCase(request.getMethod())) {
TextContext textContent = (TextContent) request.getContent();
textContent.setTitle(request.getParameter("title"));
textContent.setText(request.getParameter("text"));
ContentManager contentManager = (ContentManager)
request.getAttribute(org.apache.sling.core.Constants.ATTR_CONTENT_MANAGER);
contentManager.store(textContent);
contentManager.save();
}
This is more or less the idea. If you look at the
org.apache.sling.core.components.DefaultComponent you will see more
elaborate code showing this intention.
To create content, you would just instantiate your Content object, set
the properties and call the ContentManager.create(String path, Content
content) (currently missing, see SLING-44 [1]) to store the content at
the given path.
Hope this helps.
Regards
Felix
[1] https://issues.apache.org/jira/browse/SLING-44