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.business.Roller;
029: import org.apache.roller.business.RollerFactory;
030: import org.apache.roller.business.UserManager;
031: import org.apache.roller.business.WeblogManager;
032: import org.apache.roller.pojos.Template;
033: import org.apache.roller.pojos.UserData;
034: import org.apache.roller.pojos.WeblogEntryData;
035: import org.apache.roller.pojos.WebsiteData;
036: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
037:
038: /**
039: * Simple pager for list of weblog entries.
040: */
041: public class WeblogEntriesListPager extends AbstractPager {
042:
043: private static Log log = LogFactory
044: .getLog(WeblogEntriesListPager.class);
045:
046: private String locale = null;
047: private int sinceDays = -1;
048: private int length = 0;
049:
050: private WebsiteData queryWeblog = null;
051: private UserData queryUser = null;
052: private String queryCat = null;
053: private List queryTags = null;
054:
055: // entries for the pager
056: private List entries;
057:
058: // are there more entries?
059: private boolean more = false;
060:
061: public WeblogEntriesListPager(String baseUrl,
062: WebsiteData queryWeblog, UserData queryUser,
063: String queryCat, List queryTags, String locale,
064: int sinceDays, int pageNum, int length) {
065:
066: super (baseUrl, pageNum);
067:
068: // store the data
069: this .queryWeblog = queryWeblog;
070: this .queryUser = queryUser;
071: this .queryCat = queryCat;
072: this .queryTags = queryTags;
073: this .locale = locale;
074: this .sinceDays = sinceDays;
075: this .length = length;
076:
077: // initialize the pager collection
078: getItems();
079: }
080:
081: public List getItems() {
082:
083: if (entries == null) {
084: // calculate offset
085: int offset = getPage() * length;
086:
087: List results = new ArrayList();
088: Calendar cal = Calendar.getInstance();
089: cal.setTime(new Date());
090: cal.add(Calendar.DATE, -1 * sinceDays);
091: Date startDate = cal.getTime();
092: try {
093: Roller roller = RollerFactory.getRoller();
094: WeblogManager wmgr = roller.getWeblogManager();
095: UserManager umgr = roller.getUserManager();
096: List rawEntries = wmgr.getWeblogEntries(queryWeblog,
097: queryUser, startDate, new Date(), queryCat,
098: queryTags, WeblogEntryData.PUBLISHED,
099: "pubTime", locale, offset, length + 1);
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 the results
108: for (Iterator it = rawEntries.iterator(); it.hasNext();) {
109: WeblogEntryData entry = (WeblogEntryData) it.next();
110: results.add(WeblogEntryDataWrapper.wrap(entry));
111: }
112:
113: } catch (Exception e) {
114: log.error("ERROR: fetching weblog entries list", e);
115: }
116:
117: entries = results;
118: }
119:
120: return entries;
121: }
122:
123: public boolean hasMoreItems() {
124: return more;
125: }
126:
127: }
|