File Source: PlanetEntriesPager.java
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.weblogger.ui.rendering.pagers;
20
21 import java.util.ArrayList;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.Iterator;
25 import java.util.List;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.planet.business.PlanetFactory;
29 import org.apache.roller.planet.business.PlanetManager;
30 import org.apache.roller.planet.pojos.Planet;
31 import org.apache.roller.planet.pojos.SubscriptionEntry;
32 import org.apache.roller.planet.pojos.PlanetGroup;
33 import org.apache.roller.planet.pojos.Subscription;
34 import org.apache.roller.weblogger.business.URLStrategy;
35
36
37 /**
38 * Paging through a collection of planet entries.
39 */
40 public class PlanetEntriesPager extends AbstractPager {
41
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.pagers.PlanetEntriesPager__static_init
*
* Postconditions:
* init'ed(log)
*/
42 private static Log log = LogFactory.getLog(PlanetEntriesPager.class);
43
44 private String feedURL = null;
45 private String groupHandle = null;
46 private String locale = null;
47 private int sinceDays = -1;
48 private int length = 0;
49
50 // the collection for the pager
51 private List entries = null;
52
53 // are there more items?
54 private boolean more = false;
55
56
57 public PlanetEntriesPager(
58 URLStrategy strat,
59 String feedURL,
60 String groupHandle,
61 String baseUrl,
62 String locale,
63 int sinceDays,
64 int page,
65 int length) {
66
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.pagers.PlanetEntriesPager(URLStrategy, String, String, String, String, int, int, int)
*
* Preconditions:
* (soft) length <= 232-2
* (soft) log != null
* (soft) sinceDays <= 231
*
* Postconditions:
* this.entries == &new ArrayList(getItems#2)
* this.feedURL == feedURL
* init'ed(this.feedURL)
* this.groupHandle == groupHandle
* init'ed(this.groupHandle)
* this.length == length
* (soft) this.length <= 232-2
* this.locale == locale
* init'ed(this.locale)
* init'ed(this.more)
* ...
*/
67 super(strat, 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 getItems() {
81
/*
P/P * Method: List getItems()
*
* Preconditions:
* init'ed(this.entries)
* (soft) log != null
* (soft) init'ed(this.feedURL)
* (soft) init'ed(this.groupHandle)
* (soft) this.length <= 232-2
* (soft) this.length*this.page in -231..232-1
* (soft) init'ed(this.page)
* (soft) this.sinceDays <= 231
*
* Presumptions:
* java.util.Calendar:getInstance(...)@88 != null
* org.apache.roller.planet.business.Planet:getPlanetManager(...)@96 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@96 != null
* org.apache.roller.planet.business.PlanetManager:getEntries(...)@102 != null
* org.apache.roller.planet.business.PlanetManager:getEntries(...)@105 != null
* ...
*
* Postconditions:
* return_value == One-of{old this.entries, &new ArrayList(getItems#2)}
* return_value != null
* this.entries == return_value
* possibly_updated(this.more)
* new ArrayList(getItems#2) num objects <= 1
*
* Test Vectors:
* this.entries: Inverse{null}, Addr_Set{null}
* this.feedURL: Addr_Set{null}, Inverse{null}
* this.groupHandle: Addr_Set{null}, Inverse{null}
* this.sinceDays: {-231..0}, {1..231}
*/
82 if (entries == null) {
83 // calculate offset
84 int offset = getPage() * length;
85
86 Date startDate = null;
87 if(sinceDays > 0) {
88 Calendar cal = Calendar.getInstance();
89 cal.setTime(new Date());
90 cal.add(Calendar.DATE, -1 * sinceDays);
91 startDate = cal.getTime();
92 }
93
94 List results = new ArrayList();
95 try {
96 PlanetManager planetManager = PlanetFactory.getPlanet().getPlanetManager();
97 Planet planet = planetManager.getPlanet("default");
98
99 List entries = null;
100 if (feedURL != null) {
101 Subscription sub = planetManager.getSubscription(feedURL);
102 entries = planetManager.getEntries(sub, offset, length+1);
103 } else if (groupHandle != null) {
104 PlanetGroup group = planetManager.getGroup(planet, groupHandle);
105 entries = planetManager.getEntries(group, startDate, null, offset, length+1);
106 } else {
107 PlanetGroup group = planetManager.getGroup(planet, "all");
108 entries = planetManager.getEntries(group, startDate, null, offset, length+1);
109 }
110
111 // wrap 'em
112 int count = 0;
113 for (Iterator it = entries.iterator(); it.hasNext();) {
114 SubscriptionEntry entry = (SubscriptionEntry) it.next();
115 // TODO needs pojo wrapping from planet
+ 116 if (count++ < length) {
117 results.add(entry);
118 } else {
119 more = true;
120 }
121 }
122
123 } catch (Exception e) {
124 log.error("ERROR: get aggregation", e);
125 }
126
127 entries = results;
128 }
129
130 return entries;
131 }
132
133
134 public boolean hasMoreItems() {
/*
P/P * Method: bool hasMoreItems()
*
* Preconditions:
* init'ed(this.more)
*
* Postconditions:
* return_value == this.more
* init'ed(return_value)
*/
135 return more;
136 }
137 }
138
139
SofCheck Inspector Build Version : 2.18479
| PlanetEntriesPager.java |
2009-Jan-02 14:25:18 |
| PlanetEntriesPager.class |
2009-Sep-04 03:12:44 |