Re: Pagination query

From: Laszlo Spoor (lspoor_cayenn..otmail.com)
Date: Wed Mar 02 2005 - 14:56:08 EST

  • Next message: Laszlo Spoor: "[question] How to query a subset of a resultset efficiently"

    Hi Sami,

    Quickly scanning your code. I notice that:

    You set the currentRow as a constant. If you have a pagesize of 15, then
    your currentRow should be something like 0, 15,30, 45, etc.. So, you should
    get it from your request. Helper logic is inside the PagedObjectList.

    Let me take you again trough the steps that are to be followed:
    1. Subclass the PagedObjectList,
    2. Execute your query, where you wrap your results in the subclass mentioned
    at 1.
    3. Place the ArrayList myInstance.subSetList() in your request, as well as
    the properties needed for your pager.
    4. Loop trough it in your Java ServerPage using the <logic:iterate/>-tag

    ad 1.
    Assume you have a SubClass called ComplexList (extends PagedObjectList),
    which only has a single Constructor. It can have two if you want to use this
    PagedObjectList as a ListOfValues.

    public ComplexList (List pList, int pPageSize, int pCurrentRow)
    {
      super(pList, pPageSize, pCurrentRow);
    }

    Let's define the parameters:
    pList: The List Cayenne will return
    pPageSize: How many rows do you want to see on your page?
    pCurrentRow: Where are we at this moment in the resultset?

    ad. 2
    [...]
      SelectQuery qry = new SelectQuery (Complex.class);
      qry.setPageSize(pPageSize.intValue());
      List lst = mContext.performQuery(qry);

      // This is the subclassed bean.
      ComplexList cpxList = new ComplexList (lst, pPageSize.intValue(),
    pCurrentRow.intValue());
    [..]

    This example shows that you place the lst List inside the ComplexList. You
    also pass the pPageSize (e.g. a rowset of 15) and the currentRow. The
    currentRow is the pointer that keeps track of your position in the entire
    resultset.

    ad. 3
      // Place the resultset in your request.. This is the object you use in
    your <logic:iterate>-tag.
      request.setAttribute("cpxList", cpxList.getSubSetList());

      // Populate pager
      // Previous ResultSet
      Boolean hasPrev = new Boolean(cpxList.hasPreviousSet());
      request.setAttribute("hasPrevious", hasPrev);

      HashMap m1 = new HashMap();
      m1.put("curRow", new Integer(cpxList.getPreviousSet()));
      request.setAttribute("prevSet", m1);

      // Next ResultSet
      Boolean hasNext = new Boolean(cpxList.hasNextSet());
      request.setAttribute("hasNext", hasNext);

      HashMap m2 = new HashMap();
      m2.put("curRow", new Integer(cpxList.getNextSet()));
      request.setAttribute("nextSet", m2);

      // Remember the current row.
      request.setAttribute("curRow", curRow == null ? "0" : curRow);

    Please note that getPreviousSet() returns the previous resultset from a
    persective of your currentRow and getNextSet() returns the next record set.
    For example: Assume your pagesize is 15 and you are at position 45 of 78 in
    your resultset. The method getPreviousSet() returns 30 and getNextSet()
    returns 45. PagedObjectList takes note of the length of your resultset,
    meaning that it will not return a currentRow like 90. The Booleans are set
    so you can determine in your jsp if you should display a next/previous
    url/button/image or not. Now we are ready to go to the jsp.

    ad. 4
    First we will take a look on how to populate the pager. I have created a
    reusable 'tile' (see: Jakarta Tiles for more information), so I pass an url
    as a parameter. In our example this could be /populateCpxList.do. Further
    you see I use the Booleans to find out whether I should display a link or
    just some text. The url for the nextSet is:
    http://myserver.somewhere.com/myapp/populateCpxList.do?curRow=15 (assuming
    that this is the first time you enter the page). The url for the previousSet
    is... Just plain text (not an url), because there is not a previous
    resultset. The <bean:message>-tags reads the value belonging to that key
    from a resource bundle.

    <%-- Retrieve the url needed for paging from the Tiles template --%>
    <tiles:useAttribute name="url" classname="java.lang.String"/>

    <%-- Previous --%>
    <logic:equal name="hasPrevious" value="true">
      <html:link page="<%=url%>"
                 name="prevSet">
         <bean:message key="def.pager.previous"/>
      </html:link>
    </logic:equal>

    <logic:equal name="hasPrevious" value="false">
      <bean:message key="def.pager.previous"/>
    </logic:equal>

    <%-- Next --%>
    <logic:equal name="hasNext" value="true">
      <html:link page="<%=url%>"
                 name="nextSet">
        <bean:message key="def.pager.next"/>
      </html:link>
    </logic:equal>

    <logic:equal name="hasNext" value="false">
      <bean:message key="def.pager.next"/>
    </logic:equal>

    How to loop trough your ComplexList JavaBean:

    <logic:iterate id="cpx" name="cpxList">
      <bean:write name="cpx" property="code"/><br>
    </logic:iterate>

    This is the only tag needed for looping trough the PagedObjectList. The
    'name' attribute points to the name of the PagedObjectList you placed in
    your request. The 'id' attribute is the identifier that points to a single
    instance in the list, a Complex. This piece of code will Display all the
    'code' attributes of a Complex, for example: 212, 220, 234, 70 (stuff
    derived from the database).

    I hope this walktrough will solve your problems.... Regards, Laszlo

    >From: "Sami Mohammed" <SMOHAMME..ndependenthealth.com>
    >Reply-To: cayenne-use..bjectstyle.org
    >To: <cayenne-use..bjectstyle.org>
    >Subject: Re: Pagination query
    >Date: Tue, 01 Mar 2005 19:57:40 -0500
    >
    >Hi Joshua,
    >
    >I didnt get anything with those two attributes.
    >
    >thanks
    >sami
    >
    >CONFIDENTIALITY NOTICE. This e-mail and attachments, if any, may contain
    >confidential information which is privileged and protected from disclosure
    >by Federal and State
    >confidentiality laws rules and regulations. This e-mail and attachments,
    >if any, are intended for the designated addressee only. If you are not the
    >designated addressee, you
    >are hereby notified that any disclosure, copying, or distribution of this
    >e-mail and its attachments, if any, may be unlawful and may subject you to
    >legal consequences. If you
    >have received this e-mail and attachments in error, please contact
    >Independent Health immediately at (716) 631-3001 and delete the e-mail and
    >its attachments from your
    >computer. Thank you for your attention
    >
    > >>> joshua.t.pyl..mail.com 03/01/05 06:11PM >>>
    >Sorry Sami but I don't use the logic:iterate tag. I also use struts
    >but I use my own custom tag to render the results of my queries.
    >
    >Here is a link to the Struts documentation that mentions a length and
    >offest attribute...
    >
    >http://struts.apache.org/userGuide/struts-logic.html#iterate
    >
    >My advise is you experiment with those two attributes and see what you get.
    >
    >On Tue, 01 Mar 2005 16:16:48 -0500, Sami Mohammed
    ><SMOHAMME..ndependenthealth.com> wrote:
    > > Hi joshua,
    > >
    > > Thank you.
    > >
    > > I am using struts iterate tag to display.
    > > see the code BELOW:
    > >
    > > <logic:iterate id="display" name="DisplayResult">
    > > <bean:write name="display" property="name"/>
    > > </logic:iterate>
    > >
    > > In action class i Stored the Display object into the request object like
    >this
    > >
    > > request.setAttribute("DisplayResult", cwViewBean);
    > >
    > > anything i have to do with iterate tag to limit the rows, since i
    >already limit the pageSize to 25 in action class.
    > >
    > > if you have any idea pls let me know.
    > >
    > > thanks
    > > sami
    > >
    > > CONFIDENTIALITY NOTICE. This e-mail and attachments, if any, may
    >contain confidential information which is privileged and protected from
    >disclosure by Federal and State
    > > confidentiality laws rules and regulations. This e-mail and
    >attachments, if any, are intended for the designated addressee only. If
    >you are not the designated addressee, you
    > > are hereby notified that any disclosure, copying, or distribution of
    >this e-mail and its attachments, if any, may be unlawful and may subject
    >you to legal consequences. If you
    > > have received this e-mail and attachments in error, please contact
    >Independent Health immediately at (716) 631-3001 and delete the e-mail and
    >its attachments from your
    > > computer. Thank you for your attention
    > >
    > > >>> joshua.t.pyl..mail.com 03/01/05 03:33PM >>>
    > > It might help Sami if you did not comment out the following 2 lines...
    > >
    > > // adding paging functionality
    > > //cwTableParamMap.put(Constants.CW_REQ_PARAM_PAGE_SIZE, new
    >Integer(mPageSize));
    > > //cwTableParamMap.put(Constants.CW_REQ_PARAM_CURRENT_ROW, new
    > > Integer(mCurrentRow));
    > > // adding paging functionality
    > >
    > > I don't know this crosswalk thing but you seem to have commented out
    > > the controls that would limit your paging. The way cayenne does its
    > > paging is that it will only query the data on a per page basis but if
    > > your code loops through all of the data it will retrieve all of the
    > > data.
    > >
    > > It appears that your cntrols to limit the looping of your result list
    > > is not in place.
    > >
    > > Once again I do not know crosswalk so take my answer with that grain of
    >salt.
    > >
    > > On Tue, 1 Mar 2005 13:47:20 -0500 (EST), Andrus Adamchik
    > > <andru..bjectstyle.org> wrote:
    > > > Sami,
    > > >
    > > > I understand that you are stuck at the moment. But please don't repost
    > > > your question over and over again. Somebody (maybe myself) will look
    >at
    > > > it, just give it some time. That's the nature of free support - people
    >are
    > > > willing to give their time to community, but you shouldn't push too
    >hard.
    > > >
    > > > Andrus
    > > >
    > > >
    > > > > HI
    > > > >
    > > > > any body help me .
    > > > > page reterives all the rows from the table.
    > > > > see code below
    > > > >
    > > > >
    >SelectQuery
    > > > > cwDataQuery
    > > > > = new
    > > > > SelectQuery(cwClass);
    > > > >
    >cwDataQuery.addOrderings(cwObject.getViewOrderings());
    > > > > //cwDataQuery.setFetchLimit(100);
    > > > > cwDataQuery.setPageSize(25); //
    >HERE I AM SETTING PAGE SIZE =25
    > > > > BUT STILL ITS RETRIVING ALL THE ROWS FROM THE TABLE WHATS THE
    > > > > PROBLEM
    > > > >
    > > > >
    > > > > // Perform the query
    > > > > List resultList =
    >context.performQuery(cwDataQuery);
    > > > > THANKS
    > > > > SAMI
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >
    > >
    > > --
    > > Joshua T. Pyle
    > > Go has always existed.
    > >
    > >
    >
    >
    >--
    >Joshua T. Pyle
    >Go has always existed.

    _________________________________________________________________
    Express yourself instantly with MSN Messenger! Download today - it's FREE!
    http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



    This archive was generated by hypermail 2.0.0 : Wed Mar 02 2005 - 14:57:51 EST