Hi,
I am fairly new to ADOBE DAY CQ.. I want to extract properties of a particular node...
I am using the following code....
Node aName= session.getNode(absPath);
String propertyName= aName.getPRoperty("Name").getValue().getString();
I am aware that there is an abstract class named ABSTRACTSESSION which extends the OBJECT class and implements SESSION interface...
and getNode is a method defined in the class ABSTRACTSESSION...
What i want to know is "is there any class which extends the ABSTRACTSESSION??? Where can i find material to read about the session management in cq?
Regards and Thanks in advance,
Heidi.
In my opinion, it is generally more appropriate to use the Resource representation of your node. You can use the adaptTo() method to convert it to a ValueMap and call .get("property name", Clazz.class) to retrieve properties. Don't go all the way down to the JCR API unless you need to.
The JavaDocs should tell you if there are any classes that extend AbstractSession. Since it sounds like an abstract class, I would imagine there are classes that extend it.
Hi Ryan,
Could you explain me how and what is a resource reprsentation of a node... I am using the following code to extract the properties of a node in a widget which i am using in a page......
String productName = properties.get("name", "Product Name ");
I am trying to extract the properties of a node (which i have defined in a page) in another page...... I believe from what i saw in the net that i need to create an instance of the repository and then extract the properties of the node from that repository....
I used the following code:
"
//Create a connection to the Day CQ repository running on local host
line 23: Repository repository = JcrUtils.getRepository("http://localhost:4503/crx/server");
//Create a Session
try {
//Create a node that represents the root node
Node root = session.getRootNode();
// Store content
Node adobe = root.addNode("adobe");
Node day = adobe.addNode("day");
day.setProperty("message", "DAY CQ is part of the Adobe Digital Marketing Suite!");
// Retrieve content
Node node = root.getNode("adobe/day");
System.out.println(node.getPath());
System.out.println(node.getProperty("message").getString());"
I tried this code and i am getting the following error
Could you help me out..... If there is any other way to access the properties of the node in a widget in another page could you please tell me....
Regards & Thanks,
Heidi...
Hi Heidi,
As Ryan replied You can use ResourceResolver to get the Node resource and using adapTo() method convert this resource to Node.
Below code snippets will help you to get the node properties:
Resource res = resourceResolver.getResource("Node Path like /content/myweb/en/home");
Node node = res.adaptTo(Node.class);
PropertyIterator pIterator = node.getProperties();
while (pIterator.hasNext()){
Property property = pIterator.nextProperty();
Value value = property.getValue();
String strValue = value.getString();
}
You shouldn't have to use the Node API at all. It's way lower-level than you need. Use the resource resolver to do something like this...
Resource resource = resourceResolver.getResouce("path/to/node");
Then you can adapt that resource to a ValueMap to pull properties off of it, like this...
String propertyValue = resource.adaptTo(ValueMap.class).get("propertyName", String.class);
Adapting to a Node or retrieving a Node is not needed. A resource is a more abstracted entity, and it's easier to work with.
Heidi,
You need to import only javax.jcr.* package. All required class used in given code are available in javax.jcr package.
If you wanted to access a particular property of a node, you can use similar to this:
String propValue = node.getProperty("jcr:title").getValue().getString();
I have done this way only, you can try Ryan approach also.
Thanks
Brijesh.
Hi,
Thank you Ryan and Brijesh....
I am trying the following code :
<%@ page import="java.util.*,java.lang.*,
com.day.text.Text,
com.day.cq.wcm.api.PageFilter,
com.day.cq.wcm.api.Page,
com.day.cq.search.eval.EvaluationContext,
javax.jcr.*,
org.apache.sling.api.resource.*,
com.day.cq.dam.api.*,
com.day.cq.security.*,
com.day.cq.collab.wiki.api.dom.*,
com.day.cq.contentbus.*,
com.day.cq.commons.jcr.*;"%>
<%! int count=0;
String strProductPath;
String productTitle;
%>
<%
Page navRootPage = currentPage;
if (navRootPage != null)
{
Iterator<Page> children = navRootPage.listChildren(new PageFilter(request));
count++;%>
<div id='parent_div'>
<ul style="text-align:left;"><%while (children.hasNext())
{
if(count>4)
{
count=1;
}
Page child = children.next();
switch(count){
case 1:%>
<div class='child_div'>
<li style="text-align:left; margin-left:140px; display: inline; list-style-type: none;">
<a href="<%= child.getPath() %>.html"><%=child.getTitle() %></a>
<%strProductPath =child.getPath()+"/jcr:content/par/productassignment";
Resource res = resourceResolver.getResource("strProductPath");
LINE 46: Node nodes = res.adaptTo(Node.class);
productTitle = nodes.getProperty("name").getValue().getString();
%> <%=productTitle%>
I am getting the following error message:
org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/trainings/components/productp/productp.jsp at line 46......
Can you please help me out?
Regards & Thanks,
Heidi...
Heidi albert wrote:
<%strProductPath =child.getPath()+"/jcr:content/par/productassignment";
Resource res = resourceResolver.getResource("strProductPath");
LINE 46: Node nodes = res.adaptTo(Node.class);
productTitle = nodes.getProperty("name").getValue().getString();
%> <%=productTitle%>
I am getting the following error message:
org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/trainings/components/productp/productp.jsp at line 46......
Can you please help me out?
Regards & Thanks,
Heidi...
What is the root exception? My guess is that it is a NullPointerException because 'res' is null.
Hi Heidi,
I didn't say res was null. I said that you should provide the root exception and that my guess was that res was null.
That said, if you literally have the code:
Resource res = resourceResolver.getResource("strProductPath");
then res will naturally be null because "strProductPath" isn't a path that exists. You probably mean
Resource res = resourceResolver.getResource(strProductPath);
Regards,
Justin
Hi Justin,
Thank you for the reply..
i corrected the error... I have changed it to strProductPath without quotes...
But is there any error possibility other than the one you pointed out to me ....
I am still getting an error in the line
Node nodes = res.adaptTo(Node.class);
I am trying to access a node and extract its properties...
But i am getting a message like this
org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/trainings/components/productp/productp.jsp at line 46
Could you please help me...
Regards,
Heidi
Hii Justin,
I found out the problem in my code... When i gave the path till the jcr:content like
strProductPath =child.getPath()+"/jcr:content";
it is showing me the properties of the node i ws trying to access the properties...
But inside this jcr:content there is a node named as "par" and another component inside "par" called "productassignment", when i am trying to access the node named "par" it is giving me the above mentioned errors in the previous mails i sent.....
like
strProductPath =child.getPath()+"/jcr:content/par";
it is showing me the following error:
org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/trainings/components/productp/productp.jsp at line 46
Could you please tell me how to access the component named "productassignment" which is inside "par"......
Thanks in advance...
Regards,
Heidi
North America
Europe, Middle East and Africa
Asia Pacific