0

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();
zaxeer
  • 189
  • 2
  • 11

2 Answers2

1

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;
anotherdave
  • 6,656
  • 4
  • 34
  • 65
  • thanks for answer but still QueryBuilder is null at runtime. I have a simple class with main() method. And I have added Flix Annotation Jar in build path. CQ5 server is locally running. – zaxeer Jan 10 '14 at 13:22
  • @zaxeer Are you running this code when deployed into your CQ instance, or as stand-alone class? You won't be able to use `getService` or `@Reference` without being in an OSGi container — it's Felix that's providing the service in each case. – anotherdave Jan 10 '14 at 13:39
  • I am running code as standalone java application, my java application parse the JCR tree, but new requirement was to search in contents. – zaxeer Jan 10 '14 at 13:42
  • Going by the [JavaDoc](http://dev.day.com/docs/en/cq/5-6/javadoc/com/day/cq/search/QueryBuilder.html), the QueryBuilder doesn't expose a public constructor & it mentions "*All examples assume that `queryBuilder` is available as an OSGi SCR reference.*". — You could possibly use reflection to create an instance, though I'd say you're definitely looking at an unsupported scenario if using it outside of CQ. – anotherdave Jan 10 '14 at 13:45
1

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);
Calahad
  • 1,408
  • 14
  • 22