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.pagers;
020:
021: import java.text.MessageFormat;
022: import java.util.Collections;
023: import java.util.Date;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.TreeMap;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.business.Roller;
031: import org.apache.roller.business.RollerFactory;
032: import org.apache.roller.business.WeblogManager;
033: import org.apache.roller.pojos.WeblogEntryData;
034: import org.apache.roller.pojos.WebsiteData;
035: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
036: import org.apache.roller.util.MessageUtilities;
037: import org.apache.roller.util.Utilities;
038:
039: /**
040: *
041: */
042: public class WeblogEntriesPermalinkPager extends
043: AbstractWeblogEntriesPager {
044:
045: private static Log log = LogFactory
046: .getLog(WeblogEntriesPermalinkPager.class);
047:
048: WeblogEntryData currEntry = null;
049: WeblogEntryData nextEntry = null;
050: WeblogEntryData prevEntry = null;
051:
052: // collection for the pager
053: Map entries = null;
054:
055: public WeblogEntriesPermalinkPager(WebsiteData weblog,
056: String locale, String pageLink, String entryAnchor,
057: String dateString, String catPath, List tags, int page) {
058:
059: super (weblog, locale, pageLink, entryAnchor, dateString,
060: catPath, tags, page);
061:
062: getEntries();
063: }
064:
065: public Map getEntries() {
066: if (entries == null)
067: try {
068: Roller roller = RollerFactory.getRoller();
069: WeblogManager wmgr = roller.getWeblogManager();
070: currEntry = wmgr.getWeblogEntryByAnchor(weblog,
071: entryAnchor);
072: if (currEntry != null
073: && currEntry.getStatus().equals(
074: WeblogEntryData.PUBLISHED)) {
075: entries = new TreeMap();
076: entries.put(new Date(currEntry.getPubTime()
077: .getTime()), Collections
078: .singletonList(WeblogEntryDataWrapper
079: .wrap(currEntry)));
080: }
081: } catch (Exception e) {
082: log.error("ERROR: fetching entry");
083: }
084:
085: return entries;
086: }
087:
088: public String getHomeLink() {
089: return createURL(0, 0, weblog, locale, pageLink, null,
090: dateString, catPath, tags);
091: }
092:
093: public String getHomeName() {
094: return MessageUtilities
095: .getString("weblogEntriesPager.single.home");
096: }
097:
098: public String getNextLink() {
099: if (getNextEntry() != null) {
100: return createURL(0, 0, weblog, locale, pageLink, nextEntry
101: .getAnchor(), dateString, catPath, tags);
102: }
103: return null;
104: }
105:
106: public String getNextName() {
107: if (getNextEntry() != null) {
108: String title = Utilities.truncateNicely(getNextEntry()
109: .getTitle(), 15, 20, "...");
110: return MessageUtilities.getString(
111: "weblogEntriesPager.single.next",
112: new Object[] { title });
113: }
114: return null;
115: }
116:
117: public String getPrevLink() {
118: if (getPrevEntry() != null) {
119: return createURL(0, 0, weblog, locale, pageLink, prevEntry
120: .getAnchor(), dateString, catPath, tags);
121: }
122: return null;
123: }
124:
125: public String getPrevName() {
126: if (getPrevEntry() != null) {
127: String title = Utilities.truncateNicely(getPrevEntry()
128: .getTitle(), 15, 20, "...");
129: return MessageUtilities.getString(
130: "weblogEntriesPager.single.prev",
131: new Object[] { title });
132: }
133: return null;
134: }
135:
136: private WeblogEntryData getNextEntry() {
137: if (nextEntry == null)
138: try {
139: Roller roller = RollerFactory.getRoller();
140: WeblogManager wmgr = roller.getWeblogManager();
141: nextEntry = wmgr.getNextEntry(currEntry, null, locale);
142: // make sure that entry is published and not to future
143: if (nextEntry != null
144: && nextEntry.getPubTime().after(new Date())
145: && nextEntry.getStatus().equals(
146: WeblogEntryData.PUBLISHED)) {
147: nextEntry = null;
148: }
149: } catch (RollerException e) {
150: log.error("ERROR: getting next entry", e);
151: }
152: return nextEntry;
153: }
154:
155: private WeblogEntryData getPrevEntry() {
156: if (prevEntry == null)
157: try {
158: Roller roller = RollerFactory.getRoller();
159: WeblogManager wmgr = roller.getWeblogManager();
160: prevEntry = wmgr.getPreviousEntry(currEntry, null,
161: locale);
162: // make sure that entry is published and not to future
163: if (prevEntry != null
164: && prevEntry.getPubTime().after(new Date())
165: && prevEntry.getStatus().equals(
166: WeblogEntryData.PUBLISHED)) {
167: prevEntry = null;
168: }
169: } catch (RollerException e) {
170: log.error("ERROR: getting prev entry", e);
171: }
172: return prevEntry;
173: }
174:
175: }
|