KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > containers > QueryNextBatchesTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.containers;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.jsp.taglib.*;
14
15 import org.mmbase.bridge.*;
16 import org.mmbase.bridge.util.Queries;
17 import org.mmbase.storage.search.SearchQuery;
18
19 import java.util.*;
20 import javax.servlet.jsp.JspTagException JavaDoc;
21 //import org.mmbase.util.logging.*;
22

23
24 /**
25  * A way to make paging-mechanisms. Considers 'offset' and 'maxnumber' of surrounding query.
26  *
27  * @author Michiel Meeuwissen
28  * @since MMBase-1.7
29  * @version $Id: QueryNextBatchesTag.java,v 1.7 2006/04/11 22:58:36 michiel Exp $
30  */

31 public class QueryNextBatchesTag extends StringListTag implements QueryContainerReferrer {
32     //private static final Logger log = Logging.getLoggerInstance(QueryNextBatchesTag.class);
33

34
35     protected Attribute container = Attribute.NULL;
36     protected Attribute indexOffsetOffset = Attribute.NULL;
37     protected Attribute maxtotal = Attribute.NULL;
38
39     protected int indexOffSet = 0;
40
41     public void setIndexoffset(String JavaDoc i ) throws JspTagException JavaDoc {
42         indexOffsetOffset = getAttribute(i);
43     }
44
45     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
46         container = getAttribute(c);
47     }
48
49     public void setMaxtotal(String JavaDoc m) throws JspTagException JavaDoc {
50         maxtotal = getAttribute(m);
51     }
52
53     public int getIndexOffset() {
54         return indexOffSet;
55     }
56
57
58
59     protected List getList() throws JspTagException JavaDoc {
60         QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String JavaDoc) container.getValue(this));
61         Query query = c.getQuery();
62         int offset = query.getOffset();
63         int maxNumber = query.getMaxNumber();
64         if (maxNumber == SearchQuery.DEFAULT_MAX_NUMBER) {
65             throw new JspTagException JavaDoc("No max-number set. Cannot batch results (use mm:maxnumber first)");
66         }
67         if (offset % maxNumber != 0) { // be paranoid, perhaps not necessary, but guarantees less queries in case of url-hacking (if 'offset' is used on url)
68
throw new JspTagException JavaDoc("Offset (" + offset + ") is not a multipible of max-number (" + maxNumber + "): Cannot batch results.");
69         }
70
71         int totalSize = Queries.count(query);
72
73         indexOffSet = offset / maxNumber + 1 + indexOffsetOffset.getInt(this, 0);
74         List resultList = new ArrayList();
75
76         int maxTotalSize = maxtotal.getInt(this, -1);
77
78         int maxSize; // the size of this list.
79

80         if (maxTotalSize > 0) {
81             maxSize = (maxTotalSize - 1) / 2; // half for both, 1 for current
82
int numberOfPreviousBatches = offset / maxNumber;
83             int availableForPrevious = maxTotalSize / 2; // == maxSize in QueryPreviousBatches
84
if (numberOfPreviousBatches < availableForPrevious) { // previousbatches did not use all
85
maxSize += (availableForPrevious - numberOfPreviousBatches);
86             }
87
88             int max = getMaxNumber();
89             if (max > 0 && maxSize > max) maxSize = max;
90
91         } else {
92             maxSize = getMaxNumber();
93         }
94
95
96         while (offset + maxNumber < totalSize) {
97             offset += maxNumber;
98             resultList.add(new Integer JavaDoc(offset));
99             if (maxSize > 0 && resultList.size() == maxSize) break;
100         }
101         return resultList;
102     }
103
104 }
105
Popular Tags