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.model;
020:
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.RollerException;
029: import org.apache.roller.config.RollerRuntimeConfig;
030: import org.apache.roller.business.Roller;
031: import org.apache.roller.business.RollerFactory;
032: import org.apache.roller.business.WeblogManager;
033: import org.apache.roller.pojos.CommentData;
034: import org.apache.roller.pojos.WeblogEntryData;
035: import org.apache.roller.pojos.WebsiteData;
036: import org.apache.roller.pojos.wrapper.CommentDataWrapper;
037: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
038: import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
039: import org.apache.roller.ui.rendering.util.WeblogFeedRequest;
040: import org.apache.roller.ui.rendering.util.WeblogRequest;
041:
042: /**
043: * Model which provides information needed to render a feed.
044: */
045: public class FeedModel implements Model {
046:
047: private static Log log = LogFactory.getLog(FeedModel.class);
048:
049: private WeblogFeedRequest feedRequest = null;
050: private WebsiteData weblog = null;
051:
052: public void init(Map initData) throws RollerException {
053:
054: // we expect the init data to contain a weblogRequest object
055: WeblogRequest weblogRequest = (WeblogRequest) initData
056: .get("weblogRequest");
057: if (weblogRequest == null) {
058: throw new RollerException(
059: "expected weblogRequest from init data");
060: }
061:
062: // PageModel only works on page requests, so cast weblogRequest
063: // into a WeblogPageRequest and if it fails then throw exception
064: if (weblogRequest instanceof WeblogFeedRequest) {
065: this .feedRequest = (WeblogFeedRequest) weblogRequest;
066: } else {
067: throw new RollerException(
068: "weblogRequest is not a WeblogFeedRequest."
069: + " FeedModel only supports feed requests.");
070: }
071:
072: // extract weblog object
073: weblog = feedRequest.getWeblog();
074: }
075:
076: /** Template context name to be used for model */
077: public String getModelName() {
078: return "model";
079: }
080:
081: /**
082: * Get the weblog locale used to render this page, null if no locale.
083: */
084: public String getLocale() {
085: return feedRequest.getLocale();
086: }
087:
088: /**
089: * Get weblog being displayed.
090: */
091: public WebsiteDataWrapper getWeblog() {
092: return WebsiteDataWrapper.wrap(weblog);
093: }
094:
095: /**
096: * Get category path or name specified by request.
097: */
098: public boolean getExcerpts() {
099: return feedRequest.isExcerpts();
100: }
101:
102: /**
103: * Get category path or name specified by request.
104: */
105: public String getCategoryPath() {
106: return feedRequest.getWeblogCategoryName();
107: }
108:
109: /**
110: * Gets most recent entries limited by: weblog and category specified in
111: * request plus the weblog.entryDisplayCount.
112: */
113: public List getWeblogEntries() {
114:
115: // all feeds get the site-wide default # of entries
116: int entryCount = RollerRuntimeConfig
117: .getIntProperty("site.newsfeeds.defaultEntries");
118:
119: List results = new ArrayList();
120: try {
121: Roller roller = RollerFactory.getRoller();
122: WeblogManager wmgr = roller.getWeblogManager();
123: List entries = wmgr.getWeblogEntries(weblog, null, null,
124: new Date(), feedRequest.getWeblogCategoryName(),
125: feedRequest.getTags(), WeblogEntryData.PUBLISHED,
126: "pubTime", feedRequest.getLocale(), 0, entryCount);
127: for (Iterator it = entries.iterator(); it.hasNext();) {
128: WeblogEntryData entry = (WeblogEntryData) it.next();
129: results.add(WeblogEntryDataWrapper.wrap(entry));
130: }
131: } catch (Exception e) {
132: log.error("ERROR: fetching weblog list", e);
133: }
134: return results;
135: }
136:
137: /**
138: * Gets most recent comments limited by: weblog specified in request and
139: * the weblog.entryDisplayCount.
140: */
141: public List getComments() {
142:
143: // all feeds get the site-wide default # of entries
144: int entryCount = RollerRuntimeConfig
145: .getIntProperty("site.newsfeeds.defaultEntries");
146:
147: List recentComments = new ArrayList();
148: try {
149: WeblogManager wmgr = RollerFactory.getRoller()
150: .getWeblogManager();
151: List recent = wmgr.getComments(RollerRuntimeConfig
152: .isSiteWideWeblog(weblog.getHandle()) ? null
153: : weblog, null, // weblog entry
154: null, // search String
155: null, // startDate
156: new Date(), // endDate
157: null, // pending
158: Boolean.TRUE, // approved only
159: Boolean.FALSE, // no spam
160: true, // we want reverse chrono order
161: 0, // offset
162: entryCount); // length
163:
164: // wrap pojos
165: recentComments = new ArrayList(recent.size());
166: Iterator it = recent.iterator();
167: while (it.hasNext()) {
168: recentComments.add(CommentDataWrapper
169: .wrap((CommentData) it.next()));
170: }
171: } catch (RollerException e) {
172: log.error("ERROR: getting comments", e);
173: }
174: return recentComments;
175: }
176:
177: /**
178: * Returns the list of tags specified in the request /?tags=foo+bar
179: * @return
180: */
181: public List getTags() {
182: return feedRequest.getTags();
183: }
184:
185: }
|