You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Ronald Appelfelder <ro...@web.de> on 2018/02/25 09:24:46 UTC

How to use the JackrabitQueryResult method getTotalSize?

Hi folks!

I created the following servlet and have trouble getting the "getTotalSize"
method to work (environment: Jackrabbit 2.14.0, Java 7).

///////////////////////////////////////////////////////////////////////////////

import java.io.IOException;
import java.io.PrintWriter;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.jackrabbit.api.query.JackrabbitQueryResult;

public class QueryResultTotalSize extends HttpServlet {

  protected void doPost(HttpServletRequest request,
                        HttpServletResponse response)
                 throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    String queryStatement = request.getParameter("queryStatement");
    String queryLanguage = request.getParameter("queryLanguage");

    try {
      ServletContext jackrabbitCtx =
        this.getServletContext().getContext("/jackrabbit");

      Repository repository =
        (Repository)jackrabbitCtx.getAttribute(Repository.class.getName());

      Session session = repository.login(
        new SimpleCredentials("admin", "admin".toCharArray()), "default"
      );

      QueryManager queryManager = session.getWorkspace().getQueryManager();
      Query query = queryManager.createQuery(queryStatement, queryLanguage);
      query.setLimit(1);
      QueryResult result = query.execute();

      // variant 1: compile time error (method getTotalSize not found)
      // int totalSize = result.getTotalSize();

      // variant 2: runtime error (ClassCastException)
      // int totalSize = ((JackrabbitQueryResult)result).getTotalSize();

      out.println(totalSize);
      session.logout();
    }
    catch ( Exception e ) {
      e.printStackTrace();
    }
  }
}

///////////////////////////////////////////////////////////////////////////////

With variant 1 I get a compiler error:

  cannot find symbol
    symbol:   method getTotalSize()
    location: variable result of type javax.jcr.query.QueryResult

With variant 2 I get a runtime error:

  java.lang.ClassCastException: \
    org.apache.jackrabbit.core.query.lucene.SingleColumnQueryResult \
    cannot be cast to org.apache.jackrabbit.api.query.JackrabbitQueryResult

I have also tried other casting variants, but all my attempts result in a
ClassCastException.

Is there anyone out there who can enlighten me?

Thanks.

	Ronald Appelfelder


Re: How to use the JackrabitQueryResult method getTotalSize?

Posted by Ronald Appelfelder <ro...@web.de>.
Julian Reschke schrieb am 26.02.2018 um 10:13:

> On 2018-02-25 10:24, Ronald Appelfelder wrote:
>> ...
>> With variant 1 I get a compiler error:
>>
>>    cannot find symbol
>>      symbol:   method getTotalSize()
>>      location: variable result of type javax.jcr.query.QueryResult
>>
>> With variant 2 I get a runtime error:
>>
>>    java.lang.ClassCastException: \
>>      org.apache.jackrabbit.core.query.lucene.SingleColumnQueryResult \
>>      cannot be cast to org.apache.jackrabbit.api.query.JackrabbitQueryResult
>>
>> I have also tried other casting variants, but all my attempts result in a
>> ClassCastException.
>>
>> Is there anyone out there who can enlighten me?
>> ...

> Well, SingleColumnQueryResult doesn't implement JackrabbitQueryResult - not sure
> why or whether it should.

Of course. How could I have miss that?

I was hoping that using JackrabbitQueryResult would be the fastest way to get
both, the total number of results (for pagination) and the dataset defined by
limit and offset with a single query.

> In general, you should not rely on extensions, so do something like
> 
>   if(x instanceof JackrabbitQueryResult) {
>      y = ((JackrabbitQueryResult)x).getTotalSize();
>   } else {
>      // do something else
>   }

Yes.

Thanks a lot.

	Ronald Appelfelder


Re: How to use the JackrabitQueryResult method getTotalSize?

Posted by Julian Reschke <ju...@gmx.de>.
On 2018-02-25 10:24, Ronald Appelfelder wrote:
> ...
> With variant 1 I get a compiler error:
> 
>    cannot find symbol
>      symbol:   method getTotalSize()
>      location: variable result of type javax.jcr.query.QueryResult
> 
> With variant 2 I get a runtime error:
> 
>    java.lang.ClassCastException: \
>      org.apache.jackrabbit.core.query.lucene.SingleColumnQueryResult \
>      cannot be cast to org.apache.jackrabbit.api.query.JackrabbitQueryResult
> 
> I have also tried other casting variants, but all my attempts result in a
> ClassCastException.
> 
> Is there anyone out there who can enlighten me?
> ...

Well, SingleColumnQueryResult doesn't implement JackrabbitQueryResult - 
not sure why or whether it should.

In general, you should not rely on extensions, so do something like

   if(x instanceof JackrabbitQueryResult) {
      y = ((JackrabbitQueryResult)x).getTotalSize();
   } else {
      // do something else
   }

Best regards, Julian