KickJava   Java API By Example, From Geeks To Geeks.

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


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.PlanetManager;
29 import org.apache.roller.model.Roller;
30 import org.apache.roller.model.RollerFactory;
31 import org.apache.roller.pojos.PlanetEntryData;
32 import org.apache.roller.pojos.PlanetGroupData;
33 import org.apache.roller.pojos.Template;
34 import org.apache.roller.pojos.WebsiteData;
35 import org.apache.roller.pojos.wrapper.PlanetEntryDataWrapper;
36
37
38 /**
39  * Paging through a collection of planet entries.
40  */

41 public class PlanetEntriesPager extends AbstractPager {
42     
43     private static Log log = LogFactory.getLog(PlanetEntriesPager.class);
44     
45     private String JavaDoc feedURL = null;
46     private String JavaDoc groupHandle = null;
47     private String JavaDoc locale = null;
48     private int sinceDays = -1;
49     private int length = 0;
50     
51     // the collection for the pager
52
private List JavaDoc entries = null;
53     
54     // are there more items?
55
private boolean more = false;
56     
57     
58     public PlanetEntriesPager(
59             String JavaDoc feedURL,
60             String JavaDoc groupHandle,
61             String JavaDoc baseUrl,
62             String JavaDoc locale,
63             int sinceDays,
64             int page,
65             int length) {
66         
67         super(baseUrl, page);
68         
69         this.feedURL = feedURL;
70         this.groupHandle = groupHandle;
71         this.locale = locale;
72         this.sinceDays = sinceDays;
73         this.length = length;
74         
75         // initialize the collection
76
getItems();
77     }
78     
79     
80     public List JavaDoc getItems() {
81         
82         if (entries == null) {
83             // calculate offset
84
int offset = getPage() * length;
85             
86             Calendar JavaDoc cal = Calendar.getInstance();
87             cal.setTime(new Date JavaDoc());
88             cal.add(Calendar.DATE, -1 * sinceDays);
89             Date JavaDoc startDate = cal.getTime();
90             
91             List JavaDoc results = new ArrayList JavaDoc();
92             try {
93                 Roller roller = RollerFactory.getRoller();
94                 PlanetManager planetManager = roller.getPlanetManager();
95                 
96                 List JavaDoc rawEntries = null;
97                 if (feedURL != null) {
98                     rawEntries = planetManager.getFeedEntries(feedURL, offset, length+1);
99                 } else if (groupHandle != null) {
100                     PlanetGroupData group = planetManager.getGroup(groupHandle);
101                     rawEntries = planetManager.getAggregation(group, startDate, null, offset, length+1);
102                 } else {
103                     rawEntries = planetManager.getAggregation(startDate, null, offset, length+1);
104                 }
105                 
106                 // check if there are more results for paging
107
if(rawEntries.size() > length) {
108                     more = true;
109                     rawEntries.remove(rawEntries.size() - 1);
110                 }
111                 
112                 // wrap 'em
113
for (Iterator JavaDoc it = rawEntries.iterator(); it.hasNext();) {
114                     PlanetEntryData entry = (PlanetEntryData) it.next();
115                     results.add(PlanetEntryDataWrapper.wrap(entry));
116                 }
117                 
118             } catch (Exception JavaDoc e) {
119                 log.error("ERROR: get aggregation", e);
120             }
121             
122             entries = results;
123         }
124         
125         return entries;
126     }
127     
128     
129     public boolean hasMoreItems() {
130         return more;
131     }
132     
133 }
134
Popular Tags