KickJava   Java API By Example, From Geeks To Geeks.

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


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.MessageFormat JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.TreeMap JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.RollerException;
29 import org.apache.roller.model.Roller;
30 import org.apache.roller.model.RollerFactory;
31 import org.apache.roller.model.WeblogManager;
32 import org.apache.roller.pojos.WeblogEntryData;
33 import org.apache.roller.pojos.WebsiteData;
34 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
35 import org.apache.roller.util.MessageUtilities;
36 import org.apache.roller.util.Utilities;
37
38
39 /**
40  *
41  */

42 public class WeblogEntriesPermalinkPager extends AbstractWeblogEntriesPager {
43     
44     private static Log log = LogFactory.getLog(WeblogEntriesPermalinkPager.class);
45     
46     WeblogEntryData currEntry = null;
47     WeblogEntryData nextEntry = null;
48     WeblogEntryData prevEntry = null;
49     
50     // collection for the pager
51
private Map JavaDoc entries = null;
52     
53     
54     public WeblogEntriesPermalinkPager(
55             WebsiteData weblog,
56             String JavaDoc locale,
57             String JavaDoc pageLink,
58             String JavaDoc entryAnchor,
59             String JavaDoc dateString,
60             String JavaDoc catPath,
61             int page) {
62         
63         super(weblog, locale, pageLink, entryAnchor, dateString, catPath, page);
64         
65         getEntries();
66     }
67     
68     
69     public Map JavaDoc getEntries() {
70         if (entries == null) try {
71             Roller roller = RollerFactory.getRoller();
72             WeblogManager wmgr = roller.getWeblogManager();
73             currEntry = wmgr.getWeblogEntryByAnchor(weblog, entryAnchor);
74             if (currEntry != null && currEntry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
75                 entries = new TreeMap JavaDoc();
76                 entries.put(new Date JavaDoc(currEntry.getPubTime().getTime()),
77                         Collections.singletonList(WeblogEntryDataWrapper.wrap(currEntry)));
78             }
79         } catch (Exception JavaDoc e) {
80             log.error("ERROR: fetching entry");
81         }
82         
83         return entries;
84     }
85     
86     
87     public String JavaDoc getHomeLink() {
88         return createURL(0, 0, weblog, locale, pageLink, null, dateString, catPath);
89     }
90     
91     
92     public String JavaDoc getHomeName() {
93         return MessageUtilities.getString("weblogEntriesPager.single.home");
94     }
95     
96     
97     public String JavaDoc getNextLink() {
98         if (getNextEntry() != null) {
99             return createURL(0, 0, weblog, locale, pageLink, nextEntry.getAnchor(), dateString, catPath);
100         }
101         return null;
102     }
103     
104     
105     public String JavaDoc getNextName() {
106         if (getNextEntry() != null) {
107             String JavaDoc title = Utilities.truncateNicely(getNextEntry().getTitle(), 15, 20, "...");
108             return MessageUtilities.getString("weblogEntriesPager.single.next", new Object JavaDoc[] {title});
109         }
110         return null;
111     }
112     
113     
114     public String JavaDoc getPrevLink() {
115         if (getPrevEntry() != null) {
116             return createURL(0, 0, weblog, locale, pageLink, prevEntry.getAnchor(), dateString, catPath);
117         }
118         return null;
119     }
120     
121     
122     public String JavaDoc getPrevName() {
123         if (getPrevEntry() != null) {
124             String JavaDoc title = Utilities.truncateNicely(getPrevEntry().getTitle(), 15, 20, "...");
125             return MessageUtilities.getString("weblogEntriesPager.single.prev", new Object JavaDoc[] {title});
126         }
127         return null;
128     }
129     
130     
131     private WeblogEntryData getNextEntry() {
132         if (nextEntry == null) try {
133             Roller roller = RollerFactory.getRoller();
134             WeblogManager wmgr = roller.getWeblogManager();
135             nextEntry = wmgr.getNextEntry(currEntry, null, locale);
136             // make sure that entry is published and not to future
137
if (nextEntry != null && nextEntry.getPubTime().after(new Date JavaDoc())
138             && nextEntry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
139                 nextEntry = null;
140             }
141         } catch (RollerException e) {
142             log.error("ERROR: getting next entry", e);
143         }
144         return nextEntry;
145     }
146     
147     
148     private WeblogEntryData getPrevEntry() {
149         if (prevEntry == null) try {
150             Roller roller = RollerFactory.getRoller();
151             WeblogManager wmgr = roller.getWeblogManager();
152             prevEntry = wmgr.getPreviousEntry(currEntry, null, locale);
153             // make sure that entry is published and not to future
154
if (prevEntry != null && prevEntry.getPubTime().after(new Date JavaDoc())
155             && prevEntry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
156                 prevEntry = null;
157             }
158         } catch (RollerException e) {
159             log.error("ERROR: getting prev entry", e);
160         }
161         return prevEntry;
162     }
163     
164 }
165
Popular Tags