001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.pagers;
020:
021: import java.util.ArrayList;
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.List;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.planet.business.PlanetManager;
029: import org.apache.roller.business.Roller;
030: import org.apache.roller.business.RollerFactory;
031: import org.apache.roller.planet.pojos.PlanetEntryData;
032: import org.apache.roller.planet.pojos.PlanetGroupData;
033: import org.apache.roller.planet.pojos.wrapper.PlanetEntryDataWrapper;
034:
035: /**
036: * Paging through a collection of planet entries.
037: */
038: public class PlanetEntriesPager extends AbstractPager {
039:
040: private static Log log = LogFactory
041: .getLog(PlanetEntriesPager.class);
042:
043: private String feedURL = null;
044: private String groupHandle = null;
045: private String locale = null;
046: private int sinceDays = -1;
047: private int length = 0;
048:
049: // the collection for the pager
050: private List entries = null;
051:
052: // are there more items?
053: private boolean more = false;
054:
055: public PlanetEntriesPager(String feedURL, String groupHandle,
056: String baseUrl, String locale, int sinceDays, int page,
057: int length) {
058:
059: super (baseUrl, page);
060:
061: this .feedURL = feedURL;
062: this .groupHandle = groupHandle;
063: this .locale = locale;
064: this .sinceDays = sinceDays;
065: this .length = length;
066:
067: // initialize the collection
068: getItems();
069: }
070:
071: public List getItems() {
072:
073: if (entries == null) {
074: // calculate offset
075: int offset = getPage() * length;
076:
077: Calendar cal = Calendar.getInstance();
078: cal.setTime(new Date());
079: cal.add(Calendar.DATE, -1 * sinceDays);
080: Date startDate = cal.getTime();
081:
082: List results = new ArrayList();
083: try {
084: Roller roller = RollerFactory.getRoller();
085: PlanetManager planetManager = roller.getPlanetManager();
086:
087: List rawEntries = null;
088: if (feedURL != null) {
089: rawEntries = planetManager.getFeedEntries(feedURL,
090: offset, length + 1);
091: } else if (groupHandle != null) {
092: PlanetGroupData group = planetManager
093: .getGroup(groupHandle);
094: rawEntries = planetManager.getAggregation(group,
095: startDate, null, offset, length + 1);
096: } else {
097: rawEntries = planetManager.getAggregation(
098: startDate, null, offset, length + 1);
099: }
100:
101: // check if there are more results for paging
102: if (rawEntries.size() > length) {
103: more = true;
104: rawEntries.remove(rawEntries.size() - 1);
105: }
106:
107: // wrap 'em
108: for (Iterator it = rawEntries.iterator(); it.hasNext();) {
109: PlanetEntryData entry = (PlanetEntryData) it.next();
110: results.add(PlanetEntryDataWrapper.wrap(entry));
111: }
112:
113: } catch (Exception e) {
114: log.error("ERROR: get aggregation", e);
115: }
116:
117: entries = results;
118: }
119:
120: return entries;
121: }
122:
123: public boolean hasMoreItems() {
124: return more;
125: }
126:
127: }
|