KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.model.Roller;
31 import org.apache.roller.model.RollerFactory;
32 import org.apache.roller.model.UserManager;
33 import org.apache.roller.pojos.WebsiteData;
34 import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
35
36
37 /**
38  * Paging through a collection of weblogs.
39  */

40 public class WeblogsPager extends AbstractPager {
41     
42     private static Log log = LogFactory.getLog(WeblogsPager.class);
43     
44     private String JavaDoc letter = null;
45     private String JavaDoc locale = null;
46     private int sinceDays = -1;
47     private int length = 0;
48     
49     // collection for the pager
50
private List JavaDoc weblogs;
51     
52     // are there more items?
53
private boolean more = false;
54     
55     
56     public WeblogsPager(
57             String JavaDoc baseUrl,
58             String JavaDoc locale,
59             int sinceDays,
60             int page,
61             int length) {
62         
63         super(baseUrl, page);
64         
65         this.locale = locale;
66         this.sinceDays = sinceDays;
67         this.length = length;
68         
69         // initialize the collection
70
getItems();
71     }
72     
73     
74     public WeblogsPager(
75             String JavaDoc baseUrl,
76             String JavaDoc letter,
77             String JavaDoc locale,
78             int sinceDays,
79             int page,
80             int length) {
81         
82         super(baseUrl, page);
83         
84         this.letter = letter;
85         this.locale = locale;
86         this.sinceDays = sinceDays;
87         this.length = length;
88         
89         // initialize the collection
90
getItems();
91     }
92     
93     
94     public String JavaDoc getNextLink() {
95         // need to add letter param if it exists
96
if(letter != null) {
97             int page = getPage() + 1;
98             if(hasMoreItems()) {
99                 Map JavaDoc params = new HashMap JavaDoc();
100                 params.put("page", ""+page);
101                 params.put("letter", letter);
102                 return createURL(getUrl(), params);
103             }
104             return null;
105         } else {
106             return super.getNextLink();
107         }
108     }
109     
110     
111     public String JavaDoc getPrevLink() {
112         // need to add letter param if it exists
113
if(letter != null) {
114             int page = getPage() - 1;
115             if (page >= 0) {
116                 Map JavaDoc params = new HashMap JavaDoc();
117                 params.put("page", ""+page);
118                 params.put("letter", letter);
119                 return createURL(getUrl(), params);
120             }
121             return null;
122         } else {
123             return super.getPrevLink();
124         }
125     }
126     
127     
128     public List JavaDoc getItems() {
129         
130         if (weblogs == null) {
131             // calculate offset
132
int offset = getPage() * length;
133             
134             List JavaDoc results = new ArrayList JavaDoc();
135             Date JavaDoc startDate = null;
136             if (sinceDays != -1) {
137                 Calendar JavaDoc cal = Calendar.getInstance();
138                 cal.setTime(new Date JavaDoc());
139                 cal.add(Calendar.DATE, -1 * sinceDays);
140                 startDate = cal.getTime();
141             }
142             try {
143                 Roller roller = RollerFactory.getRoller();
144                 UserManager umgr = roller.getUserManager();
145                 List JavaDoc weblogs = null;
146                 if (letter == null) {
147                     weblogs = umgr.getWebsites(null, Boolean.TRUE, Boolean.TRUE, startDate, null, offset, length + 1);
148                 } else {
149                     weblogs = umgr.getWeblogsByLetter(letter.charAt(0), offset, length + 1);
150                 }
151                 
152                 // check if there are more results for paging
153
if(weblogs.size() > length) {
154                     more = true;
155                     weblogs.remove(weblogs.size() - 1);
156                 }
157                 
158                 // wrap the results
159
for (Iterator JavaDoc it = weblogs.iterator(); it.hasNext();) {
160                     WebsiteData website = (WebsiteData) it.next();
161                     results.add(WebsiteDataWrapper.wrap(website));
162                 }
163                 
164             } catch (Exception JavaDoc e) {
165                 log.error("ERROR: fetching weblog list", e);
166             }
167             
168             weblogs = results;
169         }
170         
171         return weblogs;
172     }
173     
174     
175     public boolean hasMoreItems() {
176         return more;
177     }
178     
179 }
180
Popular Tags