0
votes

I have seen in adobe API documentation QueryBuilder Service which can be called by URLs for many query options, this returns JSON based responses. Very well. I wan to use this in java API. I have found some examples and tried in eclipse but some thing i dont know that how to get the Service "sling" as in given code below: OR in other words how to make a QueryBuilder Java object.

Any specific JAR i needed or i need to install Apache SLING on my PC i am not sure any one who can share this will be helpful as i am new to CQ5 and has no idea.

The example code is as below:

        Repository repository = JcrUtils.getRepository(SERVER);
        SimpleCredentials credentials = new SimpleCredentials(USERNAME, PASSWORD.toCharArray());
        Session session = repository.login(credentials);
        System.out.println("got session: " + session);

        /*HERE SLING IS THE PROBLEM HOW TO GET SLING */  
        QueryBuilder qbuilder = sling.getService(QueryBuilder.class);
        String fulltextSearchTerm = "Geometrixx";

        // create query description as hash map (simplest way, same as form
        // post)
        Map<String, String> map = new HashMap<String, String>();

        // create query description as hash map (simplest way, same as form
        // post)
        map.put("path", "/content");
        map.put("type", "cq:Page");
        map.put("group.p.or", "true"); // combine this group with OR
        map.put("group.1_fulltext", fulltextSearchTerm);
        map.put("group.1_fulltext.relPath", "jcr:content");
        map.put("group.2_fulltext", fulltextSearchTerm);
        map.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");

        // can be done in map or with Query methods
        map.put("p.offset", "0"); // same as query.setStart(0) below
        map.put("p.limit", "20"); // same as query.setHitsPerPage(20) below

        Query query = qbuilder.createQuery(PredicateGroup.create(map), session);
        query.setStart(0);
        query.setHitsPerPage(20);

        SearchResult result = query.getResult();
2

2 Answers

1
votes

If you're working in a Java class rather than a JSP, you can use @Reference annotation to do a look-up of a given service — this will find a matching service registered by OSGi and return an implementation of it to you.

From the Felix SCR documentation:

The @Reference annotation defines references to other services made available to the component by the Service Component Runtime.

Your code then becomes simply:

@Reference
QueryBuilder qbuilder;
1
votes

This can be achieved using adobe cq osgi magic...Add a private field in your class to hold the ComponentContext.

private ComponentContext context;

Implement the activate method:

protected void activate(ComponentContext context) {
    this.context = context;
}

Then you can use this context to get the queryBuilder:

ServiceReference queryBuilderReference = context.getBundleContext().getServiceReference(QueryBuilder.class.getName());
QueryBuilder queryBuilder = (QueryBuilder) context.getBundleContext().getService(queryBuilderReference);