File Source: PageModel.java
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.weblogger.ui.rendering.model;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.WebloggerException;
28 import org.apache.roller.weblogger.business.URLStrategy;
29 import org.apache.roller.weblogger.business.WebloggerFactory;
30 import org.apache.roller.weblogger.pojos.Weblog;
31 import org.apache.roller.weblogger.pojos.wrapper.ThemeTemplateWrapper;
32 import org.apache.roller.weblogger.pojos.wrapper.WeblogCategoryWrapper;
33 import org.apache.roller.weblogger.pojos.wrapper.WeblogEntryWrapper;
34 import org.apache.roller.weblogger.pojos.wrapper.WeblogWrapper;
35 import org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesDayPager;
36 import org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesLatestPager;
37 import org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesMonthPager;
38 import org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesPager;
39 import org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesPermalinkPager;
40 import org.apache.roller.weblogger.ui.rendering.util.WeblogEntryCommentForm;
41 import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
42 import org.apache.roller.weblogger.ui.rendering.util.WeblogRequest;
43
44
45 /**
46 * Model which provides information needed to render a weblog page.
47 */
48 public class PageModel implements Model {
49
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.model.PageModel__static_init
*
* Postconditions:
* init'ed(log)
*/
50 private static Log log = LogFactory.getLog(PageModel.class);
51
52 private WeblogPageRequest pageRequest = null;
53 private URLStrategy urlStrategy = null;
54 private WeblogEntryCommentForm commentForm = null;
55 private Map requestParameters = null;
56 private Weblog weblog = null;
57
58
59 /**
60 *
61 * Creates an un-initialized new instance, Weblogger calls init() to complete
62 * construction.
63 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.model.PageModel()
*
* Postconditions:
* this.commentForm == null
* this.pageRequest == null
* this.requestParameters == null
* this.urlStrategy == null
* this.weblog == null
*/
64 public PageModel() {}
65
66
67 /**
68 * Template context name to be used for model.
69 */
70 public String getModelName() {
/*
P/P * Method: String getModelName()
*
* Postconditions:
* return_value == &"model"
*/
71 return "model";
72 }
73
74
75 /**
76 * Init page model based on request.
77 */
78 public void init(Map initData) throws WebloggerException {
79
80 // we expect the init data to contain a weblogRequest object
/*
P/P * Method: void init(Map)
*
* Preconditions:
* initData != null
*
* Presumptions:
* java.util.Map:get(...)@81 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@104 != null
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:instanceof(...)@88 == 1
*
* Postconditions:
* init'ed(this.commentForm)
* (soft) this.pageRequest != null
* init'ed(this.requestParameters)
* init'ed(this.urlStrategy)
* init'ed(this.weblog)
*
* Test Vectors:
* java.util.Map:get(...)@102: Inverse{null}, Addr_Set{null}
*/
81 WeblogRequest weblogRequest = (WeblogRequest) initData.get("parsedRequest");
82 if(weblogRequest == null) {
83 throw new WebloggerException("expected weblogRequest from init data");
84 }
85
86 // PageModel only works on page requests, so cast weblogRequest
87 // into a WeblogPageRequest and if it fails then throw exception
88 if(weblogRequest instanceof WeblogPageRequest) {
89 this.pageRequest = (WeblogPageRequest) weblogRequest;
90 } else {
91 throw new WebloggerException("weblogRequest is not a WeblogPageRequest."+
92 " PageModel only supports page requests.");
93 }
94
95 // see if there is a comment form
96 this.commentForm = (WeblogEntryCommentForm) initData.get("commentForm");
97
98 // custom request parameters
99 this.requestParameters = (Map)initData.get("requestParameters");
100
101 // look for url strategy
102 urlStrategy = (URLStrategy) initData.get("urlStrategy");
103 if(urlStrategy == null) {
104 urlStrategy = WebloggerFactory.getWeblogger().getUrlStrategy();
105 }
106
107 // extract weblog object
108 weblog = pageRequest.getWeblog();
109 }
110
111
112 /**
113 * Get the weblog locale used to render this page, null if no locale.
114 */
115 public String getLocale() {
/*
P/P * Method: String getLocale()
*
* Preconditions:
* this.pageRequest != null
*
* Postconditions:
* init'ed(return_value)
*/
116 return pageRequest.getLocale();
117 }
118
119
120 /**
121 * Get weblog being displayed.
122 */
123 public WeblogWrapper getWeblog() {
/*
P/P * Method: WeblogWrapper getWeblog()
*
* Preconditions:
* init'ed(this.urlStrategy)
* this.weblog != null
*
* Postconditions:
* return_value == &new WeblogWrapper(wrap#1)
* new WeblogWrapper(wrap#1) num objects == 1
* new WeblogWrapper(wrap#1).pojo == this.weblog
* new WeblogWrapper(wrap#1).pojo != null
* new WeblogWrapper(wrap#1).urlStrategy == this.urlStrategy
* init'ed(new WeblogWrapper(wrap#1).urlStrategy)
*/
124 return WeblogWrapper.wrap(weblog, urlStrategy);
125 }
126
127
128 /**
129 * Is this page considered a permalink?
130 */
131 public boolean isPermalink() {
/*
P/P * Method: bool isPermalink()
*
* Preconditions:
* this.pageRequest != null
*
* Postconditions:
* init'ed(return_value)
*/
132 return (pageRequest.getWeblogAnchor() != null);
133 }
134
135
136 /**
137 * Is this page showing search results?
138 */
139 public boolean isSearchResults() {
140 // the search results model will extend this class and override this
/*
P/P * Method: bool isSearchResults()
*
* Postconditions:
* return_value == 0
*/
141 return false;
142 }
143
144
145 /**
146 * Get weblog entry being displayed or null if none specified by request.
147 */
148 public WeblogEntryWrapper getWeblogEntry() {
/*
P/P * Method: WeblogEntryWrapper getWeblogEntry()
*
* Preconditions:
* this.pageRequest != null
*
* Postconditions:
* return_value == One-of{&new WeblogEntryWrapper(wrap#1), null}
* return_value in Addr_Set{null,&new WeblogEntryWrapper(wrap#1)}
* new WeblogEntryWrapper(wrap#1) num objects <= 1
* new WeblogEntryWrapper(wrap#1).pojo != null
* new WeblogEntryWrapper(wrap#1).urlStrategy == this.urlStrategy
* init'ed(new WeblogEntryWrapper(wrap#1).urlStrategy)
*
* Test Vectors:
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblogEntry(...)@149: Addr_Set{null}, Inverse{null}
*/
149 if(pageRequest.getWeblogEntry() != null) {
150 return WeblogEntryWrapper.wrap(pageRequest.getWeblogEntry(), urlStrategy);
151 }
152 return null;
153 }
154
155
156 /**
157 * Get weblog entry being displayed or null if none specified by request.
158 */
159 public ThemeTemplateWrapper getWeblogPage() {
/*
P/P * Method: ThemeTemplateWrapper getWeblogPage()
*
* Preconditions:
* this.pageRequest != null
* (soft) log != null
* (soft) this.weblog != null
*
* Presumptions:
* org.apache.roller.weblogger.pojos.Weblog:getTheme(...)@164 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblogPageName(...)@160: Addr_Set{null}, Inverse{null}
*/
160 if(pageRequest.getWeblogPageName() != null) {
161 return ThemeTemplateWrapper.wrap(pageRequest.getWeblogPage());
162 } else {
163 try {
164 return ThemeTemplateWrapper.wrap(weblog.getTheme().getDefaultTemplate());
165 } catch (WebloggerException ex) {
166 log.error("Error getting default page", ex);
167 }
168 }
169 return null;
170 }
171
172
173 /**
174 * Get weblog category specified by request, or null if the category path
175 * found in the request does not exist in the current weblog.
176 */
177 public WeblogCategoryWrapper getWeblogCategory() {
/*
P/P * Method: WeblogCategoryWrapper getWeblogCategory()
*
* Preconditions:
* this.pageRequest != null
*
* Postconditions:
* return_value == One-of{&new WeblogCategoryWrapper(wrap#1), null}
* return_value in Addr_Set{null,&new WeblogCategoryWrapper(wrap#1)}
* new WeblogCategoryWrapper(wrap#1) num objects <= 1
* new WeblogCategoryWrapper(wrap#1).pojo != null
* new WeblogCategoryWrapper(wrap#1).urlStrategy == this.urlStrategy
* init'ed(new WeblogCategoryWrapper(wrap#1).urlStrategy)
*
* Test Vectors:
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblogCategory(...)@178: Addr_Set{null}, Inverse{null}
*/
178 if(pageRequest.getWeblogCategory() != null) {
179 return WeblogCategoryWrapper.wrap(pageRequest.getWeblogCategory(), urlStrategy);
180 }
181 return null;
182 }
183
184
185 /**
186 * Returns the list of tags specified in the request /tags/foo+bar
187 */
188 public List getTags() {
/*
P/P * Method: List getTags()
*
* Preconditions:
* this.pageRequest != null
*
* Postconditions:
* init'ed(return_value)
*/
189 return pageRequest.getTags();
190 }
191
192
193 /**
194 * A map of entries representing this page. The collection is grouped by
195 * days of entries. Each value is a list of entry objects keyed by the
196 * date they were published.
197 */
198 public WeblogEntriesPager getWeblogEntriesPager() {
/*
P/P * Method: WeblogEntriesPager getWeblogEntriesPager()
*
* Preconditions:
* init'ed(this.urlStrategy)
* (soft) init'ed(this.pager)
*
* Postconditions:
* init'ed(return_value)
* new ArrayList(AbstractWeblogEntriesPager#1) num objects <= 1
* new I18nMessages(getMessages#2) num objects <= 1
* possibly_updated(new I18nMessages(getMessages#2).bundle)
* possibly_updated(new I18nMessages(getMessages#2).locale)
* new Locale(AbstractWeblogEntriesPager#2) num objects == 0
* init'ed(new Locale(AbstractWeblogEntriesPager#2)._tainted)
* new Locale(AbstractWeblogEntriesPager#3) num objects == 0
* init'ed(new Locale(AbstractWeblogEntriesPager#3)._tainted)
* new TreeMap(getEntries#1) num objects <= 1
* ...
*/
199 return getWeblogEntriesPager(null);
200 }
201
202
203 /**
204 * A map of entries representing this page - with entries restricted by category.
205 * The collection is grouped by days of entries.
206 * Each value is a list of entry objects keyed by the date they were published.
207 * @param catArgument Category restriction (null or "nil" for no restriction)
208 */
209 public WeblogEntriesPager getWeblogEntriesPager(String catArgument) {
/*
P/P * Method: WeblogEntriesPager getWeblogEntriesPager(String)
*
* Postconditions:
* init'ed(return_value)
*/
210 return getWeblogEntriesPager(catArgument, null);
211 }
212
213
214 /**
215 * A map of entries representing this page - with entries restricted by tag.
216 * The collection is grouped by days of entries.
217 * Each value is a list of entry objects keyed by the date they were published.
218 * @param tagArgument tag restriction (null or "nil" for no restriction)
219 */
220 public WeblogEntriesPager getWeblogEntriesPagerByTag(String tagArgument) {
/*
P/P * Method: WeblogEntriesPager getWeblogEntriesPagerByTag(String)
*
* Postconditions:
* init'ed(return_value)
*/
221 return getWeblogEntriesPager(null, tagArgument);
222 }
223
224
225 private WeblogEntriesPager getWeblogEntriesPager(String catArgument, String tagArgument) {
226
227 // category specified by argument wins over request parameter
/*
P/P * Method: WeblogEntriesPager getWeblogEntriesPager(String, String)
*
* Preconditions:
* org/apache/roller/weblogger/util/I18nMessages.messagesMap != null
* this.pageRequest != null
* init'ed(this.urlStrategy)
* this.weblog != null
* (soft) org/apache/roller/weblogger/ui/rendering/pagers/WeblogEntriesDayPager.log != null
* (soft) org/apache/roller/weblogger/ui/rendering/pagers/WeblogEntriesLatestPager.log != null
* (soft) org/apache/roller/weblogger/ui/rendering/pagers/WeblogEntriesMonthPager.log != null
*
* Presumptions:
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getLocale(...)@243 == null
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getLocale(...)@254 == null
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getLocale(...)@265 == null
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getLocale(...)@277 == null
*
* Postconditions:
* return_value in Addr_Set{&new WeblogEntriesDayPager(getWeblogEntriesPager#3),&new WeblogEntriesMonthPager(getWeblogEntriesPager#4),&new WeblogEntriesLatestPager(getWeblogEntriesPager#5),&new WeblogEntriesPermalinkPager(getWeblogEntriesPager#2)}
* new ArrayList(AbstractWeblogEntriesPager#1) num objects == 1
* new ArrayList(getWeblogEntriesPager#1) num objects <= 1
* new I18nMessages(getMessages#2) num objects <= 1
* possibly_updated(new I18nMessages(getMessages#2).bundle)
* possibly_updated(new I18nMessages(getMessages#2).locale)
* new Locale(AbstractWeblogEntriesPager#2) num objects == 0
* new Locale(AbstractWeblogEntriesPager#2)._tainted == 0
* new Locale(AbstractWeblogEntriesPager#3) num objects == 0
* new Locale(AbstractWeblogEntriesPager#3)._tainted == 0
* ...
*
* Test Vectors:
* catArgument: Addr_Set{null}, Inverse{null}
* tagArgument: Addr_Set{null}, Inverse{null}
* java.lang.String:equals(...)@229: {1}, {0}
* java.lang.String:equals(...)@234: {1}, {0}
* java.lang.String:length(...)@253: {0..7, 9..232-1}, {8}
* java.lang.String:length(...)@264: {0..5, 7..232-1}, {6}
* org.apache.commons.lang.StringUtils:isEmpty(...)@229: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@234: {1}, {0}
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblogAnchor(...)@242: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblogDate(...)@239: Addr_Set{null}, Inverse{null}
*/
228 String cat = pageRequest.getWeblogCategoryName();
229 if (catArgument != null && !StringUtils.isEmpty(catArgument) && !"nil".equals(catArgument)) {
230 cat = catArgument;
231 }
232
233 List tags = pageRequest.getTags();
234 if (tagArgument != null && !StringUtils.isEmpty(tagArgument) && !"nil".equals(tagArgument)) {
235 tags = new ArrayList();
236 tags.add(tagArgument);
237 }
238
239 String dateString = pageRequest.getWeblogDate();
240
241 // determine which mode to use
242 if (pageRequest.getWeblogAnchor() != null) {
243 return new WeblogEntriesPermalinkPager(
244 urlStrategy,
245 weblog,
246 pageRequest.getLocale(),
247 pageRequest.getWeblogPageName(),
248 pageRequest.getWeblogAnchor(),
249 pageRequest.getWeblogDate(),
250 cat,
251 tags,
252 pageRequest.getPageNum());
253 } else if (dateString != null && dateString.length() == 8) {
254 return new WeblogEntriesDayPager(
255 urlStrategy,
256 weblog,
257 pageRequest.getLocale(),
258 pageRequest.getWeblogPageName(),
259 pageRequest.getWeblogAnchor(),
260 pageRequest.getWeblogDate(),
261 cat,
262 tags,
263 pageRequest.getPageNum());
264 } else if (dateString != null && dateString.length() == 6) {
265 return new WeblogEntriesMonthPager(
266 urlStrategy,
267 weblog,
268 pageRequest.getLocale(),
269 pageRequest.getWeblogPageName(),
270 pageRequest.getWeblogAnchor(),
271 pageRequest.getWeblogDate(),
272 cat,
273 tags,
274 pageRequest.getPageNum());
275
276 } else {
277 return new WeblogEntriesLatestPager(
278 urlStrategy,
279 weblog,
280 pageRequest.getLocale(),
281 pageRequest.getWeblogPageName(),
282 pageRequest.getWeblogAnchor(),
283 pageRequest.getWeblogDate(),
284 cat,
285 tags,
286 pageRequest.getPageNum());
287 }
288 }
289
290
291 /**
292 * Get comment form to be displayed, may contain preview data.
293 *
294 * @return Comment form object
295 */
296 public WeblogEntryCommentForm getCommentForm() {
297
/*
P/P * Method: WeblogEntryCommentForm getCommentForm()
*
* Preconditions:
* init'ed(this.commentForm)
*
* Postconditions:
* return_value == One-of{old this.commentForm, &new WeblogEntryCommentForm(getCommentForm#1)}
* return_value != null
* this.commentForm == return_value
* new WeblogEntryCommentForm(getCommentForm#1) num objects <= 1
*
* Test Vectors:
* this.commentForm: Inverse{null}, Addr_Set{null}
*/
298 if(commentForm == null) {
299 commentForm = new WeblogEntryCommentForm();
300 }
301 return commentForm;
302 }
303
304 /**
305 * Get request parameter by name.
306 */
307 public String getRequestParameter(String paramName) {
/*
P/P * Method: String getRequestParameter(String)
*
* Preconditions:
* init'ed(this.requestParameters)
*
* Postconditions:
* return_value == null
*
* Test Vectors:
* this.requestParameters: Addr_Set{null}, Inverse{null}
* java.util.Map:get(...)@309: Addr_Set{null}, Inverse{null}
* values.length@309: {0}, {1..+Inf}
*/
308 if (requestParameters != null) {
309 String[] values = (String[])requestParameters.get(paramName);
310 if (values != null && values.length > 0) {
311 return values[0];
312 }
313 }
314 return null;
315 }
316
317 }
SofCheck Inspector Build Version : 2.18479
| PageModel.java |
2009-Jan-02 14:24:46 |
| PageModel.class |
2009-Sep-04 03:12:44 |