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.List;
023: import java.util.Map;
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.RollerException;
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.WebsiteData;
032: import org.apache.roller.pojos.wrapper.TemplateWrapper;
033: import org.apache.roller.pojos.wrapper.WeblogCategoryDataWrapper;
034: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
035: import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
036: import org.apache.roller.ui.rendering.pagers.WeblogEntriesDayPager;
037: import org.apache.roller.ui.rendering.pagers.WeblogEntriesLatestPager;
038: import org.apache.roller.ui.rendering.pagers.WeblogEntriesMonthPager;
039: import org.apache.roller.ui.rendering.pagers.WeblogEntriesPager;
040: import org.apache.roller.ui.rendering.pagers.WeblogEntriesPermalinkPager;
041: import org.apache.roller.ui.rendering.util.WeblogEntryCommentForm;
042: import org.apache.roller.ui.rendering.util.WeblogPageRequest;
043: import org.apache.roller.ui.rendering.util.WeblogRequest;
044:
045: /**
046: * Model which provides information needed to render a weblog page.
047: */
048: public class PageModel implements Model {
049:
050: private static Log log = LogFactory.getLog(PageModel.class);
051:
052: private WeblogPageRequest pageRequest = null;
053: private WeblogEntryCommentForm commentForm = null;
054: private Map requestParameters = null;
055: private WebsiteData weblog = null;
056:
057: /**
058: * Creates an un-initialized new instance, Roller calls init() to complete
059: * construction.
060: */
061: public PageModel() {
062: }
063:
064: /**
065: * Template context name to be used for model.
066: */
067: public String getModelName() {
068: return "model";
069: }
070:
071: /**
072: * Init page model based on request.
073: */
074: public void init(Map initData) throws RollerException {
075:
076: // we expect the init data to contain a weblogRequest object
077: WeblogRequest weblogRequest = (WeblogRequest) initData
078: .get("weblogRequest");
079: if (weblogRequest == null) {
080: throw new RollerException(
081: "expected weblogRequest from init data");
082: }
083:
084: // PageModel only works on page requests, so cast weblogRequest
085: // into a WeblogPageRequest and if it fails then throw exception
086: if (weblogRequest instanceof WeblogPageRequest) {
087: this .pageRequest = (WeblogPageRequest) weblogRequest;
088: } else {
089: throw new RollerException(
090: "weblogRequest is not a WeblogPageRequest."
091: + " PageModel only supports page requests.");
092: }
093:
094: // see if there is a comment form
095: this .commentForm = (WeblogEntryCommentForm) initData
096: .get("commentForm");
097:
098: // custom request parameters
099: this .requestParameters = (Map) initData
100: .get("requestParameters");
101:
102: // extract weblog object
103: weblog = pageRequest.getWeblog();
104: }
105:
106: /**
107: * Get the weblog locale used to render this page, null if no locale.
108: */
109: public String getLocale() {
110: return pageRequest.getLocale();
111: }
112:
113: /**
114: * Get weblog being displayed.
115: */
116: public WebsiteDataWrapper getWeblog() {
117: return WebsiteDataWrapper.wrap(weblog);
118: }
119:
120: /**
121: * Is this page considered a permalink?
122: */
123: public boolean isPermalink() {
124: return (pageRequest.getWeblogAnchor() != null);
125: }
126:
127: /**
128: * Is this page showing search results?
129: */
130: public boolean isSearchResults() {
131: // the search results model will extend this class and override this
132: return false;
133: }
134:
135: /**
136: * Get weblog entry being displayed or null if none specified by request.
137: */
138: public WeblogEntryDataWrapper getWeblogEntry() {
139: if (pageRequest.getWeblogEntry() != null) {
140: return WeblogEntryDataWrapper.wrap(pageRequest
141: .getWeblogEntry());
142: }
143: return null;
144: }
145:
146: /**
147: * Get weblog entry being displayed or null if none specified by request.
148: */
149: public TemplateWrapper getWeblogPage() {
150: if (pageRequest.getWeblogPageName() != null) {
151: return TemplateWrapper.wrap(pageRequest.getWeblogPage());
152: } else {
153: try {
154: return TemplateWrapper.wrap(weblog.getDefaultPage());
155: } catch (RollerException ex) {
156: log.error("Error getting default page", ex);
157: }
158: }
159: return null;
160: }
161:
162: /**
163: * Get weblog category specified by request, or null if the category path
164: * found in the request does not exist in the current weblog.
165: */
166: public WeblogCategoryDataWrapper getWeblogCategory() {
167: if (pageRequest.getWeblogCategory() != null) {
168: return WeblogCategoryDataWrapper.wrap(pageRequest
169: .getWeblogCategory());
170: }
171: return null;
172: }
173:
174: /**
175: * A map of entries representing this page. The collection is grouped by
176: * days of entries. Each value is a list of entry objects keyed by the
177: * date they were published.
178: * @param catArgument Category restriction (null or "nil" for no restriction)
179: */
180: public WeblogEntriesPager getWeblogEntriesPager(String catArgument) {
181:
182: // category specified by argument wins over request parameter
183: String cat = pageRequest.getWeblogCategoryName();
184: if (catArgument != null && !StringUtils.isEmpty(catArgument)
185: && !"nil".equals(catArgument)) {
186: cat = catArgument;
187: }
188:
189: String dateString = pageRequest.getWeblogDate();
190:
191: // determine which mode to use
192: if (pageRequest.getWeblogAnchor() != null) {
193: return new WeblogEntriesPermalinkPager(weblog, pageRequest
194: .getLocale(), pageRequest.getWeblogPageName(),
195: pageRequest.getWeblogAnchor(), pageRequest
196: .getWeblogDate(), cat, pageRequest
197: .getTags(), pageRequest.getPageNum());
198: } else if (dateString != null && dateString.length() == 8) {
199: return new WeblogEntriesDayPager(weblog, pageRequest
200: .getLocale(), pageRequest.getWeblogPageName(),
201: pageRequest.getWeblogAnchor(), pageRequest
202: .getWeblogDate(), cat, pageRequest
203: .getTags(), pageRequest.getPageNum());
204: } else if (dateString != null && dateString.length() == 6) {
205: return new WeblogEntriesMonthPager(weblog, pageRequest
206: .getLocale(), pageRequest.getWeblogPageName(),
207: pageRequest.getWeblogAnchor(), pageRequest
208: .getWeblogDate(), cat, pageRequest
209: .getTags(), pageRequest.getPageNum());
210:
211: } else {
212: return new WeblogEntriesLatestPager(weblog, pageRequest
213: .getLocale(), pageRequest.getWeblogPageName(),
214: pageRequest.getWeblogAnchor(), pageRequest
215: .getWeblogDate(), cat, pageRequest
216: .getTags(), pageRequest.getPageNum());
217: }
218: }
219:
220: /**
221: * A map of entries representing this page. The collection is grouped by
222: * days of entries. Each value is a list of entry objects keyed by the
223: * date they were published.
224: */
225: public WeblogEntriesPager getWeblogEntriesPager() {
226: return getWeblogEntriesPager(null);
227: }
228:
229: /**
230: * Get comment form to be displayed, may contain preview data.
231: *
232: * @return Comment form object
233: */
234: public WeblogEntryCommentForm getCommentForm() {
235:
236: if (commentForm == null) {
237: commentForm = new WeblogEntryCommentForm();
238: }
239: return commentForm;
240: }
241:
242: /**
243: * Get request parameter by name.
244: */
245: public String getRequestParameter(String paramName) {
246: String[] values = (String[]) requestParameters.get(paramName);
247: if (values != null && values.length > 0) {
248: return values[0];
249: }
250: return null;
251: }
252:
253: /**
254: * Returns the list of tags specified in the request /tags/foo+bar
255: * @return
256: */
257: public List getTags() {
258: return pageRequest.getTags();
259: }
260: }
|