/*
 * 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 <http://www.mozilla.org/MPL/>.
 * 
 * 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 <erbo@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import java.sql.*;
import java.util.*;
import org.apache.log4j.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.core.internals.EnvEngine;
import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.htmlcheck.HTMLChecker;

class PublishedMessageTopicImpl implements TopicContext
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(PublishedMessageTopicImpl.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private EnvEngine m_env;
  private int m_cid;
  private int m_topicid;
  private short m_topicnum;
  private int m_creator_uid;
  private int m_total_msgs;
  private boolean m_frozen;
  private boolean m_archived;
  private boolean m_sticky;
  private java.util.Date m_createdate;
  private java.util.Date m_lastupdate;
  private String m_name;
  private String m_postlink;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  PublishedMessageTopicImpl(EnvEngine env, int cid, int topicid) throws DataException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try
    { // get a database connection
      conn = env.getConnection();

      // query the topics table for information
      stmt = conn.prepareStatement("SELECT confid, num, creator_uid, top_message, frozen, archived, sticky, "
				   + "createdate, lastupdate, name FROM topics WHERE topicid = ?;");
      stmt.setInt(1,topicid);
      rs = stmt.executeQuery();
      if (!(rs.next()))
	throw new DataException("Topic with ID " + topicid + " not found");

      // fill internal attributes with data
      m_env = env;
      m_cid = cid;
      m_topicid = topicid;
      int confid = rs.getInt(1);
      m_topicnum = rs.getShort(2);
      m_creator_uid = rs.getInt(3);
      m_total_msgs = rs.getInt(4) + 1;
      m_frozen = rs.getBoolean(5);
      m_archived = rs.getBoolean(6);
      m_sticky = rs.getBoolean(7);
      m_createdate = SQLUtil.getFullDateTime(rs,8);
      m_lastupdate = SQLUtil.getFullDateTime(rs,9);
      m_name = rs.getString(10);
      rs.close();
      stmt.close();

      // retrieve the community alias
      stmt = conn.prepareStatement("SELECT alias FROM sigs WHERE sigid = ?;");
      stmt.setInt(1,cid);
      rs = stmt.executeQuery();
      if (!(rs.next()))
	throw new DataException("Community with ID " + cid + " not found");
      m_postlink = rs.getString(1) + "!";
      rs.close();
      stmt.close();

      // retrieve a conference alias
      stmt = conn.prepareStatement("SELECT alias FROM confalias WHERE confid = ? LIMIT 1;");
      stmt.setInt(1,confid);
      rs = stmt.executeQuery();
      if (!(rs.next()))
	throw new DataException("Conference with ID " + confid + " not found");
      m_postlink += (rs.getString(1) + "." + m_topicnum);

    } // end try
    catch (SQLException e)
    { // turn this into a data exception
      logger.error("unable to get topic data",e);
      throw new DataException("Error getting topic data: " + e.getMessage(),e);

    } // end catch
    finally
    { // release the connection
      SQLUtil.shutdown(rs);
      SQLUtil.shutdown(stmt);
      SQLUtil.shutdown(conn);

    } // end finally

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface TopicContext
   *--------------------------------------------------------------------------------
   */

  public void refresh()
  { // do nothing
  } // end refresh

  public int getTopicID()
  {
    return m_topicid;

  } // end getTopicID

  public short getTopicNumber()
  {
    return m_topicnum;

  } // end getTopicNumber

  public String getName()
  {
    return m_name;

  } // end getName

  public int getUnreadMessages()
  {
    return 0;

  } // end getUnreadMessages

  public int getTotalMessages()
  {
    return m_total_msgs;

  } // end getTotalMessages

  public java.util.Date getLastUpdateDate()
  {
    return m_lastupdate;

  } // end getLastUpdateDate

  public int getCreatorUID()
  {
    return m_creator_uid;

  } // end getCreatorUID

  public boolean isFrozen()
  {
    return m_frozen;

  } // end isFrozen

  public boolean isArchived()
  {
    return m_archived;

  } // end isArchived

  public boolean isSticky()
  {
    return m_sticky;

  } // end isSticky

  public java.util.Date getCreatedDate()
  {
    return m_createdate;

  } // end getCreatedDate

  public boolean isHidden()
  {
    return false;

  } // end isHidden

  public boolean isDeleted()
  {
    return false;

  } // end isDeleted

  public boolean canFreeze()
  {
    return false;

  } // end canFreeze

  public boolean canArchive()
  {
    return false;

  } // end canArchive

  public boolean canStick()
  {
    return false;

  } // end canStick

  public void setFrozen(boolean flag) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end setFrozen

  public void setArchived(boolean flag) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end setArchived

  public void setSticky(boolean flag) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end setSticky

  public void setHidden(boolean flag)
  { // do nothing
  } // end setHidden

  public int getFirstUnreadMessage()
  {
    return -1;

  } // end getFirstUnreadMessage

  public void setUnreadMessages(int count)
  { // do nothing
  } // end setUnreadMessages

  public void fixSeen()
  { // do nothing
  } // end fixSeen

  public List getMessages(int low, int high) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getMessages

  public TopicMessageContext getMessage(int number) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getMessage

  public TopicMessageContext postNewMessage(long parent, String pseud, String text, int mode)
      throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end postNewMessage

  public TopicMessageContext postNewMessage(long parent, String pseud, String text) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end postNewMessage

  public HTMLChecker getPreviewChecker()
  {
    return null;

  } // end getPreviewChecker

  public boolean canDelete()
  {
    return false;

  } // end canDelete

  public void delete() throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end delete

  public void deleteTopic() throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end deleteTopic

  public List getActivePosters(int skip, int limit) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getActivePosters

  public List getActivePosters(int limit) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getActivePosters

  public List getActivePosters() throws DataException, AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getActivePosters

  public List getActiveReaders(int skip, int limit) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getActiveReaders

  public List getActiveReaders(int limit) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getActiveReaders

  public List getActiveReaders() throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getActiveReaders

  public boolean isBozo(int other_uid)
  {
    return false;

  } // end isBozo

  public void setBozo(int other_uid, boolean bozo)
  { // do nothing
  } // end setBozo

  public boolean canSetBozo(int other_uid)
  {
    return false;

  } // end canSetBozo

  public List getBozos()
  {
    return Collections.EMPTY_LIST;

  } // end getBozos

  public boolean isSubscribed()
  {
    return false;

  } // end isSubscribed

  public void setSubscribed(boolean flag)
  { // do nothing
  } // end setSubscribed

  public void sendInvitation(String address, String personal_message) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end sendInvitation

  public boolean canSendInvitation()
  {
    return false;

  } // end canSendInvitation

  public void sendMailToParticipants(boolean posters, int day_limit, String subject, String text)
      throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end sendMailToParticipants

  public List searchPosts(String search_terms, int offset, int count)
      throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end searchPosts

  public int getSearchPostCount(String search_terms) throws AccessError
  {
    throw new AccessError("cannot perform this function from a read-only topic view");

  } // end getSearchPostCount

  public ConferenceContext getEnclosingConference()
  {
    throw new InternalStateError("function not implemented");

  } // end getEnclosingConference

  public String getPostLink()
  {
    return m_postlink;

  } // end getPostLink

} // end PublishedMessageTopicImpl
