/*
 * 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 com.silverwrist.venice.db.SQLUtil;

class TopicMessageFoundHelper
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Connection conn;
  private HashMap comm_aliases = new HashMap();
  private HashMap conf_aliases = new HashMap();
  private HashMap user_names = new HashMap();

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

  TopicMessageFoundHelper(Connection conn)
  {
    this.conn = conn;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  final String getCommunityAlias(int cid) throws SQLException
  {
    Integer key = new Integer(cid);
    String rc = (String)(comm_aliases.get(key));
    if (rc==null)
    { // create an SQL statement to find the alias
      Statement stmt = null;

      try
      { // use the statement
	stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("SELECT alias FROM sigs WHERE sigid = " + cid + ";");
	if (rs.next())
	  rc = rs.getString(1);
	else
	  rc = "(unknown)";

      } // end try
      finally
      { // shut down the statement to conserve resources
	SQLUtil.shutdown(stmt);

      } // end finally

      comm_aliases.put(key,rc);

    } // end if

    return rc;

  } // end getCommunityAlias

  final String getConferenceAlias(int confid) throws SQLException
  {
    Integer key = new Integer(confid);
    String rc = (String)(conf_aliases.get(key));
    if (rc==null)
    { // create an SQL statement to find the alias
      Statement stmt = null;

      try
      { // use the SQL statement
	stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("SELECT alias FROM confalias WHERE confid = " + confid + " LIMIT 1;");
	if (rs.next())
	  rc = rs.getString(1);
	else
	  rc = "(unknown)";

      } // end try
      finally
      { // shut down the statement to conserve resources
	SQLUtil.shutdown(stmt);

      } // end finally

      conf_aliases.put(key,rc);

    } // end if

    return rc;

  } // end getConferenceAlias

  final String getUserName(int uid) throws SQLException
  {
    Integer key = new Integer(uid);
    String rc = (String)(user_names.get(key));
    if (rc==null)
    { // create an SQL statement to find the username
      Statement stmt = null;

      try
      { // use the statement
	stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("SELECT username FROM users WHERE uid = " + uid + ";");
	if (rs.next())
	  rc = rs.getString(1);
	else
	  rc = "(unknown)";

      } // end try
      finally
      { // shut down the statement to save resources
	SQLUtil.shutdown(stmt);

      } // end finally

      user_names.put(key,rc);
      
    } // end if

    return rc;

  } // end getUserName

} // end TopicMessageFoundHelper
