/*
 * 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@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-02 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 env;
  private int cid;
  private int topicid;
  private short topicnum;
  private int creator_uid;
  private int total_msgs;
  private boolean frozen;
  private boolean archived;
  private java.util.Date createdate;
  private java.util.Date lastupdate;
  private String name;
  private String postlink;

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

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

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

      // query the topics table for information
      StringBuffer sql =
          new StringBuffer("SELECT confid, num, creator_uid, top_message, frozen, archived, createdate, "
			   + "lastupdate, name FROM topics WHERE topicid = ");
      sql.append(topicid).append(";");
      ResultSet rs = stmt.executeQuery(sql.toString());
      if (!(rs.next()))
	throw new DataException("Topic with ID " + topicid + " not found");

      // fill internal attributes with data
      this.env = env;
      this.cid = cid;
      this.topicid = topicid;
      int confid = rs.getInt(1);
      this.topicnum = rs.getShort(2);
      this.creator_uid = rs.getInt(3);
      this.total_msgs = rs.getInt(4) + 1;
      this.frozen = rs.getBoolean(5);
      this.archived = rs.getBoolean(6);
      this.createdate = SQLUtil.getFullDateTime(rs,7);
      this.lastupdate = SQLUtil.getFullDateTime(rs,8);
      this.name = rs.getString(9);

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

      // retrieve a conference alias
      rs = stmt.executeQuery("SELECT alias FROM confalias WHERE confid = " + confid + " LIMIT 1;");
      if (!(rs.next()))
	throw new DataException("Conference with ID " + confid + " not found");
      postlink += (rs.getString(1) + "." + 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(stmt);
      env.releaseConnection(conn);

    } // end finally

  } // end constructor

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

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

  public int getTopicID()
  {
    return topicid;

  } // end getTopicID

  public short getTopicNumber()
  {
    return topicnum;

  } // end getTopicNumber

  public String getName()
  {
    return name;

  } // end getName

  public int getUnreadMessages()
  {
    return 0;

  } // end getUnreadMessages

  public int getTotalMessages()
  {
    return total_msgs;

  } // end getTotalMessages

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

  } // end getLastUpdateDate

  public int getCreatorUID()
  {
    return creator_uid;

  } // end getCreatorUID

  public boolean isFrozen()
  {
    return frozen;

  } // end isFrozen

  public boolean isArchived()
  {
    return archived;

  } // end isArchived

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

  } // end egtCreatedDate

  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 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 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 postlink;

  } // end getPostLink

} // end PublishedMessageTopicImpl
