/*
 * 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.db;

import java.sql.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.util.IDUtils;

public class UserNameRewriter implements Rewriter
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  public static final String URI_PREFIX = "x-userlink:";

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

  private GlobalSite globalsite;         // global site containing utilities

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

  public UserNameRewriter(GlobalSite globalsite)
  {
    this.globalsite = globalsite;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Rewriter
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return null;

  } // end getName

  public MarkupData rewrite(String data, RewriterServices svc)
  {
    if (StringUtil.isStringEmpty(data) || (data.length()>64) || !(IDUtils.isValidVeniceID(data)))
      return null;

    Connection conn = null;
    Statement stmt = null;

    try
    { // get a database connection and create a statement
      conn = globalsite.getConnection(null);
      stmt = conn.createStatement();
      StringBuffer sql = new StringBuffer("SELECT uid FROM users WHERE username = '");
      sql.append(SQLUtil.encodeString(data)).append("';");
      ResultSet rs = stmt.executeQuery(sql.toString());
      if (!(rs.next()))
	return null;  // not found

    } // end try
    catch (SQLException e)
    { // if we hit a database error, just return null...
      return null;

    } // end catch
    finally
    { // make sure and release the connection before we go
      SQLUtil.shutdown(stmt);
      SQLUtil.shutdown(conn);

    } // end finally

    // build the markup data and return it
    StringBuffer open_a = new StringBuffer("<A HREF=\"");
    open_a.append(URI_PREFIX).append(data).append("\"");
    String catenate = svc.getRewriterAttrValue("ANCHORTAIL");
    if (!(StringUtil.isStringEmpty(catenate)))
      open_a.append(' ').append(catenate);
    open_a.append('>');
    return new MarkupData(open_a.toString(),data,"</A>");

  } // end rewrite

} // end class UserNameRewriter
