KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.PageContext JavaDoc;
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.wrapper.WebsiteDataWrapper;
27 import org.apache.roller.ui.core.tags.calendar.BigWeblogCalendarModel;
28 import org.apache.roller.ui.core.tags.calendar.CalendarTag;
29 import org.apache.roller.ui.core.tags.calendar.WeblogCalendarModel;
30 import org.apache.roller.ui.rendering.util.WeblogPageRequest;
31 import org.apache.roller.ui.rendering.util.WeblogRequest;
32
33
34 /**
35  * Model which provides functionality for displaying weblog calendar.
36  *
37  * Implemented by calling hybrid JSP tag.
38  */

39 public class CalendarModel implements Model {
40     
41     private static Log log = LogFactory.getLog(CalendarModel.class);
42     
43     private PageContext JavaDoc pageContext = null;
44     private WeblogPageRequest pageRequest = null;
45     
46     
47     /** Template context name to be used for model */
48     public String JavaDoc getModelName() {
49         return "calendarModel";
50     }
51     
52     
53     /** Init page model based on request */
54     public void init(Map JavaDoc initData) throws RollerException {
55         
56         // extract page context
57
this.pageContext = (PageContext JavaDoc) initData.get("pageContext");
58         
59         // we expect the init data to contain a weblogRequest object
60
WeblogRequest weblogRequest = (WeblogRequest) initData.get("weblogRequest");
61         if(weblogRequest == null) {
62             throw new RollerException("expected weblogRequest from init data");
63         }
64         
65         // CalendarModel only works on page requests, so cast weblogRequest
66
// into a WeblogPageRequest and if it fails then throw exception
67
if(weblogRequest instanceof WeblogPageRequest) {
68             this.pageRequest = (WeblogPageRequest) weblogRequest;
69         } else {
70             throw new RollerException("weblogRequest is not a WeblogPageRequest."+
71                     " CalendarModel only supports page requests.");
72         }
73     }
74     
75     
76     public String JavaDoc showWeblogEntryCalendar(WebsiteDataWrapper websiteWrapper, String JavaDoc catArgument) {
77         return showWeblogEntryCalendar(websiteWrapper, catArgument, false);
78     }
79     
80     
81     public String JavaDoc showWeblogEntryCalendarBig(WebsiteDataWrapper websiteWrapper, String JavaDoc catArgument) {
82         return showWeblogEntryCalendar(websiteWrapper, catArgument, true);
83     }
84     
85     
86     private String JavaDoc showWeblogEntryCalendar(WebsiteDataWrapper websiteWrapper, String JavaDoc catArgument, boolean big) {
87         
88         if ("nil".equals(catArgument)) catArgument = null;
89         String JavaDoc ret = null;
90         try {
91             org.apache.roller.ui.core.tags.calendar.CalendarModel model = null;
92             if (big) {
93                 model = new BigWeblogCalendarModel(pageRequest, catArgument);
94             } else {
95                 model = new WeblogCalendarModel(pageRequest, catArgument);
96             }
97             
98             // save model in JSP page context so CalendarTag can find it
99
pageContext.setAttribute("calendarModel", model);
100             
101             CalendarTag calTag = new CalendarTag();
102             calTag.setPageContext(pageContext);
103             calTag.setName("calendar");
104             calTag.setModel("calendarModel");
105             calTag.setLocale(pageRequest.getLocaleInstance());
106             if (big) {
107                 calTag.setClassSuffix("Big");
108             }
109             ret = calTag.emit();
110         } catch (Exception JavaDoc e) {
111             log.error("ERROR: initializing calendar tag",e);
112         }
113         return ret;
114     }
115     
116 }
117
Popular Tags