KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > pagers > AbstractWeblogEntriesPager


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.pagers;
20
21 import java.text.ParsePosition JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.TreeMap JavaDoc;
30 import org.apache.commons.collections.comparators.ReverseComparator;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.roller.model.Roller;
35 import org.apache.roller.model.RollerFactory;
36 import org.apache.roller.model.WeblogManager;
37 import org.apache.roller.pojos.WeblogCategoryData;
38 import org.apache.roller.pojos.WeblogEntryData;
39 import org.apache.roller.pojos.WeblogTemplate;
40 import org.apache.roller.pojos.WebsiteData;
41 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
42 import org.apache.roller.util.DateUtil;
43 import org.apache.roller.util.MessageUtilities;
44 import org.apache.roller.util.URLUtilities;
45
46
47 /**
48  *
49  */

50 public abstract class AbstractWeblogEntriesPager implements WeblogEntriesPager {
51     
52     private static Log log = LogFactory.getLog(AbstractWeblogEntriesPager.class);
53     
54     WebsiteData weblog = null;
55     String JavaDoc locale = null;
56     String JavaDoc pageLink = null;
57     String JavaDoc entryAnchor = null;
58     String JavaDoc dateString = null;
59     String JavaDoc catPath = null;
60     int offset = 0;
61     int page = 0;
62     int length = 0;
63     
64     
65     public AbstractWeblogEntriesPager(
66             WebsiteData weblog,
67             String JavaDoc locale,
68             String JavaDoc pageLink,
69             String JavaDoc entryAnchor,
70             String JavaDoc dateString,
71             String JavaDoc catPath,
72             int page) {
73         
74         this.weblog = weblog;
75         this.locale = locale;
76         this.pageLink = pageLink;
77         this.entryAnchor = entryAnchor;
78         this.dateString = dateString;
79         this.catPath = catPath;
80         
81         // make sure offset, length, and page are valid
82
length = weblog.getEntryDisplayCount();
83         if(page > 0) {
84             this.page = page;
85         }
86         this.offset = length * page;
87     }
88     
89     
90     public boolean hasMoreEntries() {
91         return false;
92     }
93     
94     
95     public String JavaDoc getHomeLink() {
96         return createURL(0, 0, weblog, locale, pageLink, entryAnchor, dateString, catPath);
97     }
98     
99     
100     public String JavaDoc getHomeName() {
101         return MessageUtilities.getString("weblogEntriesPager.latest.home");
102     }
103     
104     
105     public String JavaDoc getNextLink() {
106         if (hasMoreEntries()) {
107             return createURL(page, 1, weblog, locale, pageLink, entryAnchor, dateString, catPath);
108         }
109         return null;
110     }
111     
112     
113     public String JavaDoc getNextName() {
114         if (hasMoreEntries()) {
115             return MessageUtilities.getString("weblogEntriesPager.latest.next");
116         }
117         return null;
118     }
119     
120     
121     public String JavaDoc getPrevLink() {
122         if (page > 0) {
123             return createURL(page, -1, weblog, locale, pageLink, entryAnchor, dateString, catPath);
124         }
125         return null;
126     }
127     
128     
129     public String JavaDoc getPrevName() {
130         if (page > 0) {
131             return MessageUtilities.getString("weblogEntriesPager.latest.prev");
132         }
133         return null;
134     }
135     
136     
137     public String JavaDoc getNextCollectionLink() {
138         return null;
139     }
140     
141     
142     public String JavaDoc getNextCollectionName() {
143         return null;
144     }
145     
146     
147     public String JavaDoc getPrevCollectionLink() {
148         return null;
149     }
150     
151     
152     public String JavaDoc getPrevCollectionName() {
153         return null;
154     }
155     
156     
157     /**
158      * Parse data as either 6-char or 8-char format.
159      */

160     protected Date JavaDoc parseDate(String JavaDoc dateString) {
161         Date JavaDoc ret = null;
162         SimpleDateFormat JavaDoc char8DateFormat = DateUtil.get8charDateFormat();
163         SimpleDateFormat JavaDoc char6DateFormat = DateUtil.get6charDateFormat();
164         if ( dateString!=null
165                 && dateString.length()==8
166                 && StringUtils.isNumeric(dateString) ) {
167             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
168             ret = char8DateFormat.parse( dateString, pos );
169             
170             // make sure the requested date is not in the future
171
Date JavaDoc today = getToday();
172             if (ret.after(today)) ret = today;
173         }
174         if ( dateString!=null
175                 && dateString.length()==6
176                 && StringUtils.isNumeric(dateString) ) {
177             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
178             ret = char6DateFormat.parse( dateString, pos );
179             
180             // make sure the requested date is not in the future
181
Date JavaDoc today = getToday();
182             if (ret.after(today)) ret = today;
183         }
184         return ret;
185     }
186     
187     
188     /**
189      * Return today based on current blog's timezone/locale.
190      */

191     protected Date JavaDoc getToday() {
192         Calendar JavaDoc todayCal = Calendar.getInstance();
193         todayCal = Calendar.getInstance(
194                 weblog.getTimeZoneInstance(), weblog.getLocaleInstance());
195         todayCal.setTime(new Date JavaDoc());
196         return todayCal.getTime();
197     }
198     
199     
200     /**
201      * Create URL that encodes pager state using most appropriate forms of URL.
202      * @param pageAdd To be added to page number, or 0 for no page number
203      */

204     protected String JavaDoc createURL(
205             int page,
206             int pageAdd,
207             WebsiteData website,
208             String JavaDoc locale,
209             String JavaDoc pageLink,
210             String JavaDoc entryAnchor,
211             String JavaDoc dateString,
212             String JavaDoc catPath) {
213         
214         int pageNum = page + pageAdd;
215         
216         if (pageLink != null) {
217             return URLUtilities.getWeblogPageURL(website, locale, pageLink, entryAnchor, catPath, dateString, pageNum, false);
218         } else if (entryAnchor != null) {
219             return URLUtilities.getWeblogEntryURL(website, locale, entryAnchor, true);
220         }
221         
222         return URLUtilities.getWeblogCollectionURL(website, locale, catPath, dateString, pageNum, false);
223     }
224     
225 }
226
Popular Tags