KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.RollerException;
26 import org.apache.roller.pojos.WebsiteData;
27 import org.apache.roller.pojos.wrapper.TemplateWrapper;
28 import org.apache.roller.pojos.wrapper.WeblogCategoryDataWrapper;
29 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
30 import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
31 import org.apache.roller.ui.rendering.pagers.WeblogEntriesDayPager;
32 import org.apache.roller.ui.rendering.pagers.WeblogEntriesLatestPager;
33 import org.apache.roller.ui.rendering.pagers.WeblogEntriesMonthPager;
34 import org.apache.roller.ui.rendering.pagers.WeblogEntriesPager;
35 import org.apache.roller.ui.rendering.pagers.WeblogEntriesPermalinkPager;
36 import org.apache.roller.ui.rendering.util.WeblogEntryCommentForm;
37 import org.apache.roller.ui.rendering.util.WeblogPageRequest;
38 import org.apache.roller.ui.rendering.util.WeblogRequest;
39
40
41 /**
42  * Model which provides information needed to render a weblog page.
43  */

44 public class PageModel implements Model {
45     
46     private static Log log = LogFactory.getLog(PageModel.class);
47     
48     private WeblogPageRequest pageRequest = null;
49     private WeblogEntryCommentForm commentForm = null;
50     private Map JavaDoc requestParameters = null;
51     private WebsiteData weblog = null;
52     
53     
54     /**
55      * Creates an un-initialized new instance, Roller calls init() to complete
56      * construction.
57      */

58     public PageModel() {}
59     
60     
61     /**
62      * Template context name to be used for model.
63      */

64     public String JavaDoc getModelName() {
65         return "model";
66     }
67     
68     
69     /**
70      * Init page model based on request.
71      */

72     public void init(Map JavaDoc initData) throws RollerException {
73         
74         // we expect the init data to contain a weblogRequest object
75
WeblogRequest weblogRequest = (WeblogRequest) initData.get("weblogRequest");
76         if(weblogRequest == null) {
77             throw new RollerException("expected weblogRequest from init data");
78         }
79         
80         // PageModel only works on page requests, so cast weblogRequest
81
// into a WeblogPageRequest and if it fails then throw exception
82
if(weblogRequest instanceof WeblogPageRequest) {
83             this.pageRequest = (WeblogPageRequest) weblogRequest;
84         } else {
85             throw new RollerException("weblogRequest is not a WeblogPageRequest."+
86                     " PageModel only supports page requests.");
87         }
88         
89         // see if there is a comment form
90
this.commentForm = (WeblogEntryCommentForm) initData.get("commentForm");
91         
92         // custom request parameters
93
this.requestParameters = (Map JavaDoc)initData.get("requestParameters");
94         
95         // extract weblog object
96
weblog = pageRequest.getWeblog();
97     }
98     
99     
100     /**
101      * Get the weblog locale used to render this page, null if no locale.
102      */

103     public String JavaDoc getLocale() {
104         return pageRequest.getLocale();
105     }
106     
107     
108     /**
109      * Get weblog being displayed.
110      */

111     public WebsiteDataWrapper getWeblog() {
112         return WebsiteDataWrapper.wrap(weblog);
113     }
114     
115     
116     /**
117      * Is this page considered a permalink?
118      */

119     public boolean isPermalink() {
120         return (pageRequest.getWeblogAnchor() != null);
121     }
122     
123     
124     /**
125      * Is this page showing search results?
126      */

127     public boolean isSearchResults() {
128         // the search results model will extend this class and override this
129
return false;
130     }
131     
132     
133     /**
134      * Get weblog entry being displayed or null if none specified by request.
135      */

136     public WeblogEntryDataWrapper getWeblogEntry() {
137         if(pageRequest.getWeblogEntry() != null) {
138             return WeblogEntryDataWrapper.wrap(pageRequest.getWeblogEntry());
139         }
140         return null;
141     }
142     
143     
144     /**
145      * Get weblog entry being displayed or null if none specified by request.
146      */

147     public TemplateWrapper getWeblogPage() {
148         if(pageRequest.getWeblogPageName() != null) {
149             return TemplateWrapper.wrap(pageRequest.getWeblogPage());
150         } else {
151             try {
152                 return TemplateWrapper.wrap(weblog.getDefaultPage());
153             } catch (RollerException ex) {
154                 log.error("Error getting default page", ex);
155             }
156         }
157         return null;
158     }
159     
160     
161     /**
162      * Get weblog category specified by request, or null if the category path
163      * found in the request does not exist in the current weblog.
164      */

165     public WeblogCategoryDataWrapper getWeblogCategory() {
166         if(pageRequest.getWeblogCategory() != null) {
167             return WeblogCategoryDataWrapper.wrap(pageRequest.getWeblogCategory());
168         }
169         return null;
170     }
171     
172     
173     /**
174      * A map of entries representing this page. The collection is grouped by
175      * days of entries. Each value is a list of entry objects keyed by the
176      * date they were published.
177      * @param catArgument Category restriction (null or "nil" for no restriction)
178      */

179     public WeblogEntriesPager getWeblogEntriesPager(String JavaDoc catArgument) {
180         
181         // category specified by argument wins over request parameter
182
String JavaDoc cat = pageRequest.getWeblogCategoryName();
183         if(catArgument != null && !StringUtils.isEmpty(catArgument) &&
184                 !"nil".equals(catArgument)) {
185             cat = catArgument;
186         }
187         
188         String JavaDoc dateString = pageRequest.getWeblogDate();
189         
190         // determine which mode to use
191
if (pageRequest.getWeblogAnchor() != null) {
192             return new WeblogEntriesPermalinkPager(
193                     weblog,
194                     pageRequest.getLocale(),
195                     pageRequest.getWeblogPageName(),
196                     pageRequest.getWeblogAnchor(),
197                     pageRequest.getWeblogDate(),
198                     cat,
199                     pageRequest.getPageNum());
200         } else if (dateString != null && dateString.length() == 8) {
201             return new WeblogEntriesDayPager(
202                     weblog,
203                     pageRequest.getLocale(),
204                     pageRequest.getWeblogPageName(),
205                     pageRequest.getWeblogAnchor(),
206                     pageRequest.getWeblogDate(),
207                     cat,
208                     pageRequest.getPageNum());
209         } else if (dateString != null && dateString.length() == 6) {
210             return new WeblogEntriesMonthPager(
211                     weblog,
212                     pageRequest.getLocale(),
213                     pageRequest.getWeblogPageName(),
214                     pageRequest.getWeblogAnchor(),
215                     pageRequest.getWeblogDate(),
216                     cat,
217                     pageRequest.getPageNum());
218         } else {
219             return new WeblogEntriesLatestPager(
220                     weblog,
221                     pageRequest.getLocale(),
222                     pageRequest.getWeblogPageName(),
223                     pageRequest.getWeblogAnchor(),
224                     pageRequest.getWeblogDate(),
225                     cat,
226                     pageRequest.getPageNum());
227         }
228     }
229     
230     
231     /**
232      * A map of entries representing this page. The collection is grouped by
233      * days of entries. Each value is a list of entry objects keyed by the
234      * date they were published.
235      */

236     public WeblogEntriesPager getWeblogEntriesPager() {
237         return getWeblogEntriesPager(null);
238     }
239         
240     
241     /**
242      * Get comment form to be displayed, may contain preview data.
243      *
244      * @return Comment form object
245      */

246     public WeblogEntryCommentForm getCommentForm() {
247         
248         if(commentForm == null) {
249             commentForm = new WeblogEntryCommentForm();
250         }
251         return commentForm;
252     }
253     
254     /**
255      * Get request parameter by name.
256      */

257     public String JavaDoc getRequestParameter(String JavaDoc paramName) {
258         String JavaDoc[] values = (String JavaDoc[])requestParameters.get(paramName);
259         if (values != null && values.length > 0) {
260             return values[0];
261         }
262         return null;
263     }
264 }
265
Popular Tags