15 Replies Latest reply: Aug 27, 2012 11:44 PM by Heidi albert RSS

    REGARDING EXTRACTING OF PROPERTIES

    Heidi albert

      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.

        • 1. Re: REGARDING EXTRACTING OF PROPERTIES
          Ryan Lunka Community Member

          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.

          • 2. Re: REGARDING EXTRACTING OF PROPERTIES
            Heidi albert Community Member

            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

            1. javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));

             

            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

             

            • org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/trainings/components/productp/productp.jsp at line 23

             

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

            • 3. Re: REGARDING EXTRACTING OF PROPERTIES
              Brijesh.Yadav

              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();

              }

              • 4. Re: REGARDING EXTRACTING OF PROPERTIES
                Heidi albert Community Member

                Hi brijesh,

                Thanks for the code....

                Bt i am getting error in the line

                 

                Node node =  res.adaptTo(Node.class);

                 

                Apart from including the package "

                com.day.cq.security.resource.*"

                 

                is there any other class i need to include???

                 

                 

                Thanks in advance.

                 

                Heidi

                • 5. Re: REGARDING EXTRACTING OF PROPERTIES
                  Ryan Lunka Community Member

                  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.

                  • 6. Re: REGARDING EXTRACTING OF PROPERTIES
                    Brijesh.Yadav Community Member

                    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.

                    • 7. Re: REGARDING EXTRACTING OF PROPERTIES
                      Heidi albert Community Member

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

                      • 8. Re: REGARDING EXTRACTING OF PROPERTIES
                        justin_at_adobe Adobe Employee

                        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.

                        • 9. Re: REGARDING EXTRACTING OF PROPERTIES
                          Heidi albert Community Member

                          Hi Justin,

                          How can res be null? In the strProductPath, there is a component.. It contains an image, a title and a description.....

                           

                           

                          Could you please help me out..

                           

                          Regards,

                          Heidi

                          • 10. Re: REGARDING EXTRACTING OF PROPERTIES
                            justin_at_adobe Adobe Employee

                            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

                            • 11. Re: REGARDING EXTRACTING OF PROPERTIES
                              Heidi albert Community Member

                              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

                              • 12. Re: REGARDING EXTRACTING OF PROPERTIES
                                justin_at_adobe Adobe Employee

                                Hi Heidi,

                                Again, you need to get the root exception. There should be a stack trace in the log which contains this. That will tell you what the actual problem is, rather than me continuing to guess at a problem on a system I don't have access to.

                                 

                                Regards,

                                Justin

                                • 13. Re: REGARDING EXTRACTING OF PROPERTIES
                                  Heidi albert Community Member

                                  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

                                  • 14. Re: REGARDING EXTRACTING OF PROPERTIES
                                    justin_at_adobe Adobe Employee

                                    Heidi-

                                    As I've said a few times now, you need to provide the root exception. If you want to Private Message me a package which contains both the content and the components, I can try to help you out further. But without the root exception, it is hard to tell what is going on.

                                     

                                    Justin

                                    • 15. Re: REGARDING EXTRACTING OF PROPERTIES
                                      Heidi albert Community Member

                                      Hii Justin,

                                       

                                      Thank you very much for your offer..But i am nt able to private message you as it is against our rules....

                                       

                                      I solved it by checking out the Content Explorer.....

                                       

                                       

                                      Thank you,

                                      Heidi