Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Proble while Persisting CQ data in the Java Content Repository

Avatar

Level 2

Hi All,

While persisting i am getting the below error

Please let me know what is the problem with the code

<h1>Error during include of component '/apps/test/components/page1/templateJCR'</h1><h3>Error Message:</h3><pre>org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.jsp.jasper.JasperException: Unable to compile class for JSP:An error occurred at line: 8 in the jsp file: /apps/test/components/page1/templateJCR/persist.json.jspcom.adobe.cq1.CustomerService cannot be resolved to a type5: String phone = request.getParameter(&quot;phone&quot;);6: String desc = request.getParameter(&quot;desc&quot;);7:8: com.adobe.cq1.CustomerService cs = sling.getService(com.adobe.cq1.CustomerService.class);9:10: int myPK = cs.injectCustData(first, last, phone, desc) ;11:An error occurred at line: 8 in the jsp file: /apps/test/components/page1/templateJCR/persist.json.jspcom.adobe.cq1.CustomerService cannot be resolved to a type5: String phone = request.getParameter(&quot;phone&quot;);6: String desc = request.getParameter(&quot;desc&quot;);7:8: com.adobe.cq1.CustomerService cs = sling.getService(com.adobe.cq1.CustomerService.class);9:10: int myPK = cs.injectCustData(first, last, phone, desc) ;11:</pre><h3>Processing Info:</h3><table style='font-family: monospace'><tr><td>Page</td><td>=</td><td>/content/test1<td></tr><tr><td>Resource Path</td><td>=</td><td>/content/test1/jcr:content<td></tr><tr><td>Cell</td><td>=</td><td>templateJCR<td></tr><tr><td>Cell Search Path</td><td>=</td><td>templateJCR|page<td></tr><tr><td>Component Path</td><td>=</td><td>/apps/test/components/page1/templateJCR<td></tr></table><h3>Sling Request Progress:</h3>

The Service interface

package com.adobe.cq1;

public interface CustomerService {
    public int injectCustData(String firstName, String lastName, String phone, String desc);

}

The service implemetation

package com.adobe.cq1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import javax.jcr.Repository;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.jackrabbit.commons.JcrUtils;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

import javax.jcr.RepositoryException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.jackrabbit.commons.JcrUtils;

import javax.jcr.Session;
import javax.jcr.Node;

//Sling Imports
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;

//This is a component so it can provide or consume services
@Component
// This component provides the service defined through the interface
@Service
public class CustomerServiceImp implements CustomerService {

    /** Default log. */
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    // Inject a Sling ResourceResolverFactory
    @Reference
    private ResourceResolverFactory resolverFactory;

    private Session session;

    // Stores customer data in the Adobe CQ JCR

    
    public int injectCustData(String firstName, String lastName, String phone,
            String desc) {
        int num = 0;
        try {
            System.out.println("<<<<<<<<<<<<<<<Coming here>>>>>>>>>>");
            // Invoke the adaptTo method to create a Session
            ResourceResolver resourceResolver = resolverFactory
                    .getAdministrativeResourceResolver(null);
            session = resourceResolver.adaptTo(Session.class);

            // Create a node that represents the root node
            Node root = session.getRootNode();

            // Get the content node in the JCR
            Node content = root.getNode("content");

            // Determine if the content/customer node exists
            Node customerRoot = null;
            int custRec = doesCustExist(content);

            // -1 means that content/customer does not exist
            if (custRec == -1)
                // content/customer does not exist -- create it
                customerRoot = content.addNode("customer",
                        "sling:OrderedFolder");
            else
                // content/customer does exist -- retrieve it
                customerRoot = content.getNode("customer");

            int custId = custRec + 1; // assign a new id to the customer node

            // Store content from the client JSP in the JCR
            Node custNode = customerRoot.addNode("customer" + firstName
                    + lastName + custId, "nt:unstructured");

            // make sure name of node is unique
            custNode.setProperty("id", custId);
            custNode.setProperty("firstName", firstName);
            custNode.setProperty("lastName", lastName);
            custNode.setProperty("address", phone);
            custNode.setProperty("desc", desc);

            // Save the session changes and log out
            session.save();
            session.logout();
            return custId;
        }

        catch (Exception e) {
            log.error("RepositoryException: " + e);
        }
        return 0;
    }

    /*
     * Determines if the content/customer node exists This method returns these
     * values: -1 - if customer does not exist 0 - if content/customer node
     * exists; however, contains no children number - the number of children
     * that the content/customer node contains
     */
    private int doesCustExist(Node content) {
        try {
            int index = 0;
            int childRecs = 0;

            java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content,
                    "customer");
            Iterator it = custNode.iterator();

            // only going to be 1 content/customer node if it exists
            if (it.hasNext()) {
                // Count the number of child nodes in content/customer
                Node customerRoot = content.getNode("customer");
                Iterable itCust = JcrUtils.getChildNodes(customerRoot);
                Iterator childNodeIt = itCust.iterator();

                // Count the number of customer child nodes
                while (childNodeIt.hasNext()) {
                    childRecs++;
                    childNodeIt.next();
                }
                return childRecs;
            } else
                return -1; // content/customer does not exist
        } catch (Exception e) {
            log.error("RepositoryException"+e);
        }
        return 0;
    }

}

 

The persisi.json

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.commons.json.io.*,com.adobe.cq1.*" %><%
String first = request.getParameter("first");
String last = request.getParameter("last");
String phone = request.getParameter("phone");
String desc = request.getParameter("desc");
 
com.adobe.cq1.CustomerService cs = sling.getService(com.adobe.cq1.CustomerService.class);
 
int myPK = cs.injectCustData(first, last, phone, desc) ;
 
//Send the data back to the client
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("pk");
writer.value(myPK);
 
writer.endObject();
%>

1 Accepted Solution

Avatar

Correct answer by
Level 5
"com.adobe.cq1.CustomerService cannot be resolved to a type"

This usually happen if your service is not exported properly. Check your pom.xml to make sure that you are exporting this service. Also using felix console go to your bundle and see if this service is exported and available to use.

 

Yogesh

View solution in original post

4 Replies

Avatar

Correct answer by
Level 5
"com.adobe.cq1.CustomerService cannot be resolved to a type"

This usually happen if your service is not exported properly. Check your pom.xml to make sure that you are exporting this service. Also using felix console go to your bundle and see if this service is exported and available to use.

 

Yogesh

Avatar

Level 2

This service is available in felix console

Avatar

Level 2

please find the pom.xml below

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.day.cq5.test</groupId>
    <artifactId>core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>bundle</packaging>

    <name>core</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-scr-descriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package> com.day.cq5.test.*,com.adobe.support.examples.osgi.service.*</Export-Package>
                        <Import-Package>org.osgi.framework;version="1.3.0"</Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
         
          
        <dependency>
         <groupId>org.apache.felix</groupId>
   
         <artifactId>org.osgi.core</artifactId>
   
         <version>1.4.0</version>
      </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        
         <dependency>
             <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>
             <version>2.0</version>
         </dependency>
    
        <dependency>
            <groupId>org.apache.jackrabbit</groupId>
            <artifactId>jackrabbit-core</artifactId>
            <version>2.4.3</version>
        </dependency>
           
        <dependency>
            <groupId>org.apache.jackrabbit</groupId>
            <artifactId>jackrabbit-jcr-commons</artifactId>
            <version>2.4.3</version>
        </dependency>
    
        <dependency>
            <groupId>com.day.cq.wcm</groupId>
            <artifactId>cq-wcm-api</artifactId>
            <version>5.5.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.day.cq</groupId>
            <artifactId>cq-commons</artifactId>
            <version>5.5.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.api</artifactId>
            <version>2.3.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
</project>

Avatar

Level 2

Thanks Yogesh,

It's working now:)