KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > pagers > CommentsPager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.pagers;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.model.Roller;
29 import org.apache.roller.model.RollerFactory;
30 import org.apache.roller.model.WeblogManager;
31 import org.apache.roller.pojos.CommentData;
32 import org.apache.roller.pojos.Template;
33 import org.apache.roller.pojos.WebsiteData;
34 import org.apache.roller.pojos.wrapper.CommentDataWrapper;
35
36
37 /**
38  * Paging through a collection of comments.
39  */

40 public class CommentsPager extends AbstractPager {
41     
42     private static Log log = LogFactory.getLog(CommentsPager.class);
43     
44     private String JavaDoc locale = null;
45     private int sinceDays = -1;
46     private int length = 0;
47     
48     // the collection for the pager
49
private List JavaDoc comments = null;
50     
51     // are there more items?
52
private boolean more = false;
53     
54     
55     public CommentsPager(
56             String JavaDoc baseUrl,
57             String JavaDoc locale,
58             int sinceDays,
59             int page,
60             int length) {
61         
62         super(baseUrl, page);
63         
64         this.locale = locale;
65         this.sinceDays = sinceDays;
66         this.length = length;
67         
68         // initialize the collection
69
getItems();
70     }
71     
72     
73     public List JavaDoc getItems() {
74         
75         if (comments == null) {
76             // calculate offset
77
int offset = getPage() * length;
78             
79             List JavaDoc results = new ArrayList JavaDoc();
80             Calendar JavaDoc cal = Calendar.getInstance();
81             cal.setTime(new Date JavaDoc());
82             cal.add(Calendar.DATE, -1 * sinceDays);
83             Date JavaDoc startDate = cal.getTime();
84             try {
85                 Roller roller = RollerFactory.getRoller();
86                 WeblogManager wmgr = roller.getWeblogManager();
87                 List JavaDoc entries = wmgr.getComments(
88                         null, null, null, startDate, new Date JavaDoc(),
89                         Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, true, offset, length + 1);
90                 
91                 // check if there are more results for paging
92
if(entries.size() > length) {
93                     more = true;
94                     entries.remove(entries.size() - 1);
95                 }
96                 
97                 // wrap the results
98
for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
99                     CommentData comment = (CommentData) it.next();
100                     results.add(CommentDataWrapper.wrap(comment));
101                 }
102                 
103             } catch (Exception JavaDoc e) {
104                 log.error("ERROR: fetching comment list", e);
105             }
106             
107             comments = results;
108         }
109         
110         return comments;
111     }
112     
113     
114     public boolean hasMoreItems() {
115         return more;
116     }
117     
118 }
119
Popular Tags