KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > model > FeedModel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.model;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.RollerException;
29 import org.apache.roller.config.RollerRuntimeConfig;
30 import org.apache.roller.model.Roller;
31 import org.apache.roller.model.RollerFactory;
32 import org.apache.roller.model.WeblogManager;
33 import org.apache.roller.pojos.CommentData;
34 import org.apache.roller.pojos.WeblogEntryData;
35 import org.apache.roller.pojos.WebsiteData;
36 import org.apache.roller.pojos.wrapper.CommentDataWrapper;
37 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
38 import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
39 import org.apache.roller.ui.rendering.util.WeblogFeedRequest;
40 import org.apache.roller.ui.rendering.util.WeblogRequest;
41
42
43 /**
44  * Model which provides information needed to render a feed.
45  */

46 public class FeedModel implements Model {
47     
48     private static Log log = LogFactory.getLog(FeedModel.class);
49     
50     private WeblogFeedRequest feedRequest = null;
51     private WebsiteData weblog = null;
52     
53     
54     public void init(Map JavaDoc initData) throws RollerException {
55         
56         // we expect the init data to contain a weblogRequest object
57
WeblogRequest weblogRequest = (WeblogRequest) initData.get("weblogRequest");
58         if(weblogRequest == null) {
59             throw new RollerException("expected weblogRequest from init data");
60         }
61         
62         // PageModel only works on page requests, so cast weblogRequest
63
// into a WeblogPageRequest and if it fails then throw exception
64
if(weblogRequest instanceof WeblogFeedRequest) {
65             this.feedRequest = (WeblogFeedRequest) weblogRequest;
66         } else {
67             throw new RollerException("weblogRequest is not a WeblogFeedRequest."+
68                     " FeedModel only supports feed requests.");
69         }
70         
71         // extract weblog object
72
weblog = feedRequest.getWeblog();
73     }
74     
75     
76     /** Template context name to be used for model */
77     public String JavaDoc getModelName() {
78         return "model";
79     }
80     
81     
82     /**
83      * Get the weblog locale used to render this page, null if no locale.
84      */

85     public String JavaDoc getLocale() {
86         return feedRequest.getLocale();
87     }
88     
89     
90     /**
91      * Get weblog being displayed.
92      */

93     public WebsiteDataWrapper getWeblog() {
94         return WebsiteDataWrapper.wrap(weblog);
95     }
96     
97     
98     /**
99      * Get category path or name specified by request.
100      */

101     public boolean getExcerpts() {
102         return feedRequest.isExcerpts();
103     }
104     
105     
106     /**
107      * Get category path or name specified by request.
108      */

109     public String JavaDoc getCategoryPath() {
110         return feedRequest.getWeblogCategoryName();
111     }
112     
113     
114     /**
115      * Gets most recent entries limited by: weblog and category specified in
116      * request plus the weblog.entryDisplayCount.
117      */

118     public List JavaDoc getWeblogEntries() {
119         
120         // all feeds get the site-wide default # of entries
121
int entryCount =
122                 RollerRuntimeConfig.getIntProperty("site.newsfeeds.defaultEntries");
123         
124         List JavaDoc results = new ArrayList JavaDoc();
125         try {
126             Roller roller = RollerFactory.getRoller();
127             WeblogManager wmgr = roller.getWeblogManager();
128             List JavaDoc entries = wmgr.getWeblogEntries(
129                     weblog,
130                     null,
131                     null,
132                     new Date JavaDoc(),
133                     feedRequest.getWeblogCategoryName(),
134                     WeblogEntryData.PUBLISHED,
135                     "pubTime",
136                     feedRequest.getLocale(),
137                     0,
138                     entryCount);
139             for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
140                 WeblogEntryData entry = (WeblogEntryData) it.next();
141                 results.add(WeblogEntryDataWrapper.wrap(entry));
142             }
143         } catch (Exception JavaDoc e) {
144             log.error("ERROR: fetching weblog list", e);
145         }
146         return results;
147     }
148     
149     
150     /**
151      * Gets most recent comments limited by: weblog specified in request and
152      * the weblog.entryDisplayCount.
153      */

154     public List JavaDoc getComments() {
155             
156         // all feeds get the site-wide default # of entries
157
int entryCount =
158                 RollerRuntimeConfig.getIntProperty("site.newsfeeds.defaultEntries");
159         
160         List JavaDoc recentComments = new ArrayList JavaDoc();
161         try {
162             WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
163             List JavaDoc recent = wmgr.getComments(
164                     RollerRuntimeConfig.isSiteWideWeblog(weblog.getHandle()) ? null : weblog,
165                     null, // weblog entry
166
null, // search String
167
null, // startDate
168
new Date JavaDoc(), // endDate
169
null, // pending
170
Boolean.TRUE, // approved only
171
Boolean.FALSE, // no spam
172
true, // we want reverse chrono order
173
0, // offset
174
entryCount); // length
175

176             // wrap pojos
177
recentComments = new ArrayList JavaDoc(recent.size());
178             Iterator JavaDoc it = recent.iterator();
179             while(it.hasNext()) {
180                 recentComments.add(CommentDataWrapper.wrap((CommentData) it.next()));
181             }
182         } catch (RollerException e) {
183             log.error("ERROR: getting comments", e);
184         }
185         return recentComments;
186     }
187     
188 }
189
Popular Tags