KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > repository > AbstractQuerySession


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webservice.repository;
18
19 import org.alfresco.util.GUID;
20
21 /**
22  * Abstract implementation of a QuerySession providing support
23  * for automatic id generation and provides support for
24  * paging through query results.
25  *
26  * @author gavinc
27  */

28 public abstract class AbstractQuerySession implements QuerySession
29 {
30    protected int batchSize;
31    protected int position = 0;
32    
33    private String JavaDoc id;
34    
35    /**
36     * Common constructor that initialises the session's id and batch size
37     *
38     * @param batchSize The batch size this session will use
39     */

40    public AbstractQuerySession(int batchSize)
41    {
42       this.id = GUID.generate();
43       this.batchSize = batchSize;
44    }
45    
46    /**
47     * @see org.alfresco.repo.webservice.repository.QuerySession#getId()
48     */

49    public String JavaDoc getId()
50    {
51       return this.id;
52    }
53  
54    /**
55     * Calculates the index of the last row to retrieve.
56     *
57     * @param totalRowCount The total number of rows in the results
58     * @return The index of the last row to return
59     */

60    protected int calculateLastRowIndex(int totalRowCount)
61    {
62       int lastRowIndex = totalRowCount;
63       
64       // set the last row index if there are more results available
65
// than the batch size
66
if ((this.batchSize != -1) && ((this.position + this.batchSize) < totalRowCount))
67       {
68          lastRowIndex = this.position + this.batchSize;
69       }
70       
71       return lastRowIndex;
72    }
73    
74    /**
75     * Calculates the value of the next position.
76     * If the end of the result set is reached the position is set to -1
77     *
78     * @param totalRowCount The total number of rows in the results
79     * @param queryResult The QueryResult object being returned to the client,
80     * if there are no more results the id is removed from the QueryResult instance
81     */

82    protected void updatePosition(int totalRowCount, QueryResult queryResult)
83    {
84       this.position += this.batchSize;
85       if (this.position >= totalRowCount)
86       {
87          // signify that there are no more results
88
this.position = -1;
89          queryResult.setQuerySession(null);
90       }
91    }
92 }
93
Popular Tags