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.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.business.Roller;
031: import org.apache.roller.business.RollerFactory;
032: import org.apache.roller.business.UserManager;
033: import org.apache.roller.pojos.WebsiteData;
034: import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
035:
036: /**
037: * Paging through a collection of weblogs.
038: */
039: public class WeblogsPager extends AbstractPager {
040:
041: private static Log log = LogFactory.getLog(WeblogsPager.class);
042:
043: private String letter = null;
044: private String locale = null;
045: private int sinceDays = -1;
046: private int length = 0;
047:
048: // collection for the pager
049: private List weblogs;
050:
051: // are there more items?
052: private boolean more = false;
053:
054: public WeblogsPager(String baseUrl, String locale, int sinceDays,
055: int page, int length) {
056:
057: super (baseUrl, page);
058:
059: this .locale = locale;
060: this .sinceDays = sinceDays;
061: this .length = length;
062:
063: // initialize the collection
064: getItems();
065: }
066:
067: public WeblogsPager(String baseUrl, String letter, String locale,
068: int sinceDays, int page, int length) {
069:
070: super (baseUrl, page);
071:
072: this .letter = letter;
073: this .locale = locale;
074: this .sinceDays = sinceDays;
075: this .length = length;
076:
077: // initialize the collection
078: getItems();
079: }
080:
081: public String getNextLink() {
082: // need to add letter param if it exists
083: if (letter != null) {
084: int page = getPage() + 1;
085: if (hasMoreItems()) {
086: Map params = new HashMap();
087: params.put("page", "" + page);
088: params.put("letter", letter);
089: return createURL(getUrl(), params);
090: }
091: return null;
092: } else {
093: return super .getNextLink();
094: }
095: }
096:
097: public String getPrevLink() {
098: // need to add letter param if it exists
099: if (letter != null) {
100: int page = getPage() - 1;
101: if (page >= 0) {
102: Map params = new HashMap();
103: params.put("page", "" + page);
104: params.put("letter", letter);
105: return createURL(getUrl(), params);
106: }
107: return null;
108: } else {
109: return super .getPrevLink();
110: }
111: }
112:
113: public List getItems() {
114:
115: if (weblogs == null) {
116: // calculate offset
117: int offset = getPage() * length;
118:
119: List results = new ArrayList();
120: Date startDate = null;
121: if (sinceDays != -1) {
122: Calendar cal = Calendar.getInstance();
123: cal.setTime(new Date());
124: cal.add(Calendar.DATE, -1 * sinceDays);
125: startDate = cal.getTime();
126: }
127: try {
128: Roller roller = RollerFactory.getRoller();
129: UserManager umgr = roller.getUserManager();
130: List weblogs = null;
131: if (letter == null) {
132: weblogs = umgr.getWebsites(null, Boolean.TRUE,
133: Boolean.TRUE, startDate, null, offset,
134: length + 1);
135: } else {
136: weblogs = umgr.getWeblogsByLetter(letter.charAt(0),
137: offset, length + 1);
138: }
139:
140: // check if there are more results for paging
141: if (weblogs.size() > length) {
142: more = true;
143: weblogs.remove(weblogs.size() - 1);
144: }
145:
146: // wrap the results
147: for (Iterator it = weblogs.iterator(); it.hasNext();) {
148: WebsiteData website = (WebsiteData) it.next();
149: results.add(WebsiteDataWrapper.wrap(website));
150: }
151:
152: } catch (Exception e) {
153: log.error("ERROR: fetching weblog list", e);
154: }
155:
156: weblogs = results;
157: }
158:
159: return weblogs;
160: }
161:
162: public boolean hasMoreItems() {
163: return more;
164: }
165:
166: }
|