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.WeblogManager;
031: import org.apache.roller.pojos.CommentData;
032: import org.apache.roller.pojos.Template;
033: import org.apache.roller.pojos.WebsiteData;
034: import org.apache.roller.pojos.wrapper.CommentDataWrapper;
035:
036: /**
037: * Paging through a collection of comments.
038: */
039: public class CommentsPager extends AbstractPager {
040:
041: private static Log log = LogFactory.getLog(CommentsPager.class);
042:
043: private String locale = null;
044: private int sinceDays = -1;
045: private int length = 0;
046:
047: // the collection for the pager
048: private List comments = null;
049:
050: // are there more items?
051: private boolean more = false;
052:
053: public CommentsPager(String baseUrl, String locale, int sinceDays,
054: int page, int length) {
055:
056: super (baseUrl, page);
057:
058: this .locale = locale;
059: this .sinceDays = sinceDays;
060: this .length = length;
061:
062: // initialize the collection
063: getItems();
064: }
065:
066: public List getItems() {
067:
068: if (comments == null) {
069: // calculate offset
070: int offset = getPage() * length;
071:
072: List results = new ArrayList();
073: Calendar cal = Calendar.getInstance();
074: cal.setTime(new Date());
075: cal.add(Calendar.DATE, -1 * sinceDays);
076: Date startDate = cal.getTime();
077: try {
078: Roller roller = RollerFactory.getRoller();
079: WeblogManager wmgr = roller.getWeblogManager();
080: List entries = wmgr.getComments(null, null, null,
081: startDate, new Date(), null, Boolean.TRUE,
082: Boolean.FALSE, true, offset, length + 1);
083:
084: // check if there are more results for paging
085: if (entries.size() > length) {
086: more = true;
087: entries.remove(entries.size() - 1);
088: }
089:
090: // wrap the results
091: for (Iterator it = entries.iterator(); it.hasNext();) {
092: CommentData comment = (CommentData) it.next();
093: results.add(CommentDataWrapper.wrap(comment));
094: }
095:
096: } catch (Exception e) {
097: log.error("ERROR: fetching comment list", e);
098: }
099:
100: comments = results;
101: }
102:
103: return comments;
104: }
105:
106: public boolean hasMoreItems() {
107: return more;
108: }
109:
110: }
|