KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > calendar > BigWeblogCalendarModel


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.core.tags.calendar;
20
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.model.RollerFactory;
29 import org.apache.roller.model.WeblogManager;
30 import org.apache.roller.pojos.WeblogEntryData;
31 import org.apache.roller.ui.core.RollerContext;
32 import org.apache.roller.ui.rendering.util.WeblogPageRequest;
33 import org.apache.roller.util.DateUtil;
34 import org.apache.roller.util.URLUtilities;
35
36
37 /**
38  * Model for big calendar that displays titles for each day.
39  */

40 public class BigWeblogCalendarModel extends WeblogCalendarModel {
41     
42     private static Log mLogger = LogFactory.getLog(BigWeblogCalendarModel.class);
43     
44     protected static final SimpleDateFormat JavaDoc mStarDateFormat =
45             DateUtil.get8charDateFormat();
46     
47     protected static final SimpleDateFormat JavaDoc mSingleDayFormat =
48             new SimpleDateFormat JavaDoc("dd");
49     
50     
51     public BigWeblogCalendarModel(WeblogPageRequest pRequest, String JavaDoc cat) {
52         super(pRequest, cat);
53     }
54     
55     
56     protected void loadWeblogEntries(Date JavaDoc startDate, Date JavaDoc endDate, String JavaDoc catName) {
57         try {
58             WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
59             monthMap = mgr.getWeblogEntryObjectMap(
60                     weblog, // website
61
startDate, // startDate
62
endDate, // endDate
63
catName, // cat
64
WeblogEntryData.PUBLISHED, // status
65
locale,
66                     0, -1);
67         } catch (RollerException e) {
68             mLogger.error(e);
69             monthMap = new HashMap JavaDoc();
70         }
71     }
72     
73     
74     public String JavaDoc getContent(Date JavaDoc day) {
75         String JavaDoc content = null;
76         try {
77             RollerContext rctx = RollerContext.getRollerContext();
78             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
79             
80             // get the 8 char YYYYMMDD datestring for day, returns null
81
// if no weblog entry on that day
82
String JavaDoc dateString = null;
83             List JavaDoc entries = (List JavaDoc)monthMap.get(day);
84             if ( entries != null ) {
85                 dateString = mStarDateFormat.format(
86                         ((WeblogEntryData)entries.get(0)).getPubTime());
87                 
88                 // append 8 char date string on end of selfurl
89
String JavaDoc dayUrl = URLUtilities.getWeblogCollectionURL(weblog, locale, cat, dateString, -1, false);
90                               
91                 sb.append("<div class=\"hCalendarDayTitleBig\">");
92                 sb.append("<a HREF=\"");
93                 sb.append( dayUrl );
94                 sb.append("\">");
95                 sb.append( mSingleDayFormat.format( day ) );
96                 sb.append("</a></div>");
97                 
98                 for ( int i=0; i<entries.size(); i++ ) {
99                     sb.append("<div class=\"bCalendarDayContentBig\">");
100                     sb.append("<a HREF=\"");
101                     sb.append(((WeblogEntryData)entries.get(i)).getPermalink());
102                     sb.append("\">");
103                     
104                     String JavaDoc title = ((WeblogEntryData)entries.get(i)).getTitle().trim();
105                     if ( title.length()==0 ) {
106                         title = ((WeblogEntryData)entries.get(i)).getAnchor();
107                     }
108                     if ( title.length() > 20 ) {
109                         title = title.substring(0,20)+"...";
110                     }
111                     
112                     sb.append( title );
113                     sb.append("</a></div>");
114                 }
115                 
116             } else {
117                 sb.append("<div class=\"hCalendarDayTitleBig\">");
118                 sb.append( mSingleDayFormat.format( day ) );
119                 sb.append("</div>");
120                 sb.append("<div class=\"bCalendarDayContentBig\"/>");
121             }
122             content = sb.toString();
123         } catch (Exception JavaDoc e) {
124             mLogger.error("ERROR: creating URL", e);
125         }
126         return content;
127     }
128     
129     /**
130      * Create URL for use on view-weblog page
131      * @param day Day for URL or null if no entries on that day
132      * @param nextPrevMonthURL True to create next/prev month URL
133      * @param alwaysURL Always return a URL, never return null
134      * @return URL for day, or null if no weblog entry on that day
135      */

136     public String JavaDoc computeUrl(Date JavaDoc day, boolean nextPrevMonthURL, boolean alwaysURL) {
137         String JavaDoc url = null;
138         // get the 8 char YYYYMMDD datestring for day, returns null
139
// if no weblog entry on that day
140
String JavaDoc dateString = null;
141         List JavaDoc entries = (List JavaDoc)monthMap.get( day );
142         if ( entries != null && day != null ) {
143             WeblogEntryData entry = (WeblogEntryData)entries.get(0);
144             dateString = mStarDateFormat.format(entry.getPubTime());
145         }
146         if (dateString == null && !alwaysURL) return null;
147         else if (dateString == null && !nextPrevMonthURL) {
148             dateString = DateUtil.format8chars(day);
149         } else if (dateString == null && nextPrevMonthURL) {
150             dateString = DateUtil.format6chars(day);
151         }
152         try {
153             if (nextPrevMonthURL && pageLink != null) {
154                 // next/prev month URLs point to current page
155
url = URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, cat, dateString, -1, false);
156             } else {
157                 // all other URLs point back to main weblog page
158
url = URLUtilities.getWeblogCollectionURL(weblog, locale, cat, dateString, -1, false);
159             }
160         } catch (Exception JavaDoc e) {
161             mLogger.error("ERROR: creating URL",e);
162         }
163         return url;
164     }
165 }
166
Popular Tags