/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.venice.servlets.format; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import com.silverwrist.util.StringUtil; import com.silverwrist.venice.core.*; public class TopicPosts implements JSPRender { /*-------------------------------------------------------------------------------- * Static data members *-------------------------------------------------------------------------------- */ // Attribute name for request attribute protected static final String ATTR_NAME = "com.silverwrist.venice.content.TopicPosts"; /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ private VeniceEngine engine; private SIGContext sig; private ConferenceContext conf; private TopicContext topic; private int first; private int last; private boolean show_advanced; private int unread; private List messages; private TopicVisitOrder visit_order; private String topic_stem; private String cache_locator = null; /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ public TopicPosts(HttpServletRequest request, VeniceEngine engine, SIGContext sig, ConferenceContext conf, TopicContext topic, int first, int last, boolean read_new, boolean show_advanced) throws DataException, AccessError { this.engine = engine; this.sig = sig; this.conf = conf; this.topic = topic; this.first = first; this.last = last; this.show_advanced = show_advanced; this.unread = topic.getUnreadMessages(); if (read_new) topic.setUnreadMessages(0); this.messages = topic.getMessages(first,last); this.visit_order = TopicVisitOrder.retrieve(request.getSession(true),conf.getConfID()); visit_order.visit(topic.getTopicNumber()); List aliases = conf.getAliases(); topic_stem = (String)(aliases.get(0)) + "." + String.valueOf(topic.getTopicNumber()) + "."; } // end constructor /*-------------------------------------------------------------------------------- * External static functions *-------------------------------------------------------------------------------- */ public static TopicPosts retrieve(ServletRequest request) { return (TopicPosts)(request.getAttribute(ATTR_NAME)); } // end retrieve /*-------------------------------------------------------------------------------- * Implementations from interface VeniceContent *-------------------------------------------------------------------------------- */ public String getPageTitle(RenderData rdat) { return topic.getName() + ": " + topic.getTotalMessages() + " Total; " + unread + " New; Last: " + rdat.formatDateForDisplay(topic.getLastUpdateDate()); } // end getPageTitle /*-------------------------------------------------------------------------------- * Implementations from interface JSPRender *-------------------------------------------------------------------------------- */ public void store(ServletRequest request) { request.setAttribute(ATTR_NAME,this); } // end store public String getTargetJSPName() { return "posts.jsp"; } // end getTargetJSPName /*-------------------------------------------------------------------------------- * External operations *-------------------------------------------------------------------------------- */ public static String getPosterName(TopicMessageContext msg) { try { // have to guard agains a DataException here return msg.getCreatorName(); } // end try catch (DataException de) { // just return "unknown" on failure return "(unknown)"; } // end catch } // end getPosterName public static String getMessageBodyText(TopicMessageContext msg) { try { // have to guard against a DataException here return msg.getBodyText(); } // end try catch (DataException de) { // just return an error message return "(Unable to retrieve message data: " + StringUtil.encodeHTML(de.getMessage()) + ")"; } // end catch } // end getMessageBodyText public int getSIGID() { return sig.getSIGID(); } // end getSIGID public int getConfID() { return conf.getConfID(); } // end getConfID public int getTopicNumber() { return topic.getTopicNumber(); } // end getTopicNumber public int getNextTopicNumber() { return visit_order.getNext(); } // end getNextTopicNumber public String getConfLocator() { StringBuffer buf = new StringBuffer("sig="); buf.append(sig.getSIGID()).append("&conf=").append(conf.getConfID()); return buf.toString(); } // end getConfLocator public String getLocator() { if (cache_locator==null) { // build up the standard locator StringBuffer buf = new StringBuffer("sig="); buf.append(sig.getSIGID()).append("&conf=").append(conf.getConfID()).append("&top="); buf.append(topic.getTopicNumber()); cache_locator = buf.toString(); } // end if return cache_locator; } // end getLocator public String getNextLocator() { StringBuffer buf = new StringBuffer("sig="); buf.append(sig.getSIGID()).append("&conf=").append(conf.getConfID()).append("&top="); buf.append(visit_order.getNext()); return buf.toString(); } // end getNextLocator public String getRestoreLocator() { StringBuffer buf = new StringBuffer("rtop="); buf.append(topic.getTopicNumber()).append("&rct=").append(unread); return buf.toString(); } // end getRestoreLocator public String getIdentifyingData() { StringBuffer buf = new StringBuffer("Posts "); buf.append(first).append(" through ").append(last).append(" in topic #").append(topic.getTopicID()); return buf.toString(); } // end getIdentifyingData public String getTopicName() { return topic.getName(); } // end getTopicName public int getTotalMessages() { return topic.getTotalMessages(); } // end getTotalMessages public int getNewMessages() { return unread; } // end getNewMessages public Date getLastUpdate() { return topic.getLastUpdateDate(); } // end getLastUpdate public boolean isTopicHidden() { return topic.isHidden(); } // end isTopicHidden public boolean canDoNextTopic() { return visit_order.isNext(); } // end canDoNextTopic public boolean canFreezeTopic() { return topic.canFreeze(); } // end canFreezeTopic public boolean isTopicFrozen() { return topic.isFrozen(); } // end isTopicFrozen public boolean canArchiveTopic() { return topic.canArchive(); } // end canArchiveTopic public boolean isTopicArchived() { return topic.isArchived(); } // end isTopicArchived public boolean canDeleteTopic() { return topic.canDelete(); } // end canDeleteTopic public boolean canScrollUp() { return (first>0); } // end canScrollUp public String getScrollUpLocator() { int new_first = first - engine.getNumPostsPerPage(); int new_last = last - 1; if (new_first<0) { // normalize so we start at 0 new_last += (-new_first); new_first = 0; } // end if StringBuffer buf = new StringBuffer("p1="); buf.append(new_first).append("&p2=").append(new_last); return buf.toString(); } // end getScrollUpLocator public boolean canScrollDown() { return ((topic.getTotalMessages() - (1 + last))>=engine.getNumPostsPerPage()); } // end canScrollDown public String getScrollDownLocator() { StringBuffer buf = new StringBuffer("p1="); buf.append(last+1).append("&p2=").append(last+engine.getNumPostsPerPage()); return buf.toString(); } // end getScrollDownLocator public boolean canScrollToEnd() { return ((topic.getTotalMessages() - (1 + last))>0); } // end canScrollToEnd public String getScrollToEndLocator() { int my_last = topic.getTotalMessages(); StringBuffer buf = new StringBuffer("p1="); buf.append(my_last-engine.getNumPostsPerPage()).append("&p2=").append(my_last-1); return buf.toString(); } // end getScrollToEndLocator public Iterator getMessageIterator() { return messages.iterator(); } // end getMessageIterator() public boolean emitBreakLinePoint(int msg) { return (msg==(topic.getTotalMessages()-unread)); } // end emitBreakLinePoint public boolean showAdvanced() { return show_advanced && (last==first); } // end showAdvanced public boolean displayPostBox() { boolean flag1 = conf.canPostToConference(); boolean flag2 = (topic.isFrozen() ? topic.canFreeze() : true); boolean flag3 = (topic.isArchived() ? topic.canArchive() : true); return flag1 && flag2 && flag3; } // end displayPostBox public String getDefaultPseud() { return conf.getDefaultPseud(); } // end getDefaultPseud public String getMessageReference(TopicMessageContext msg) { return topic_stem + String.valueOf(msg.getPostNumber()); } // end getMessageReference public int getNumPostsPerPage() { return engine.getNumPostsPerPage(); } // end getNumPostsPerPage } // end class TopicPosts