/*
 * 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 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.servlets.format.*;

public class UserDisplay extends VeniceServlet
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

  /*--------------------------------------------------------------------------------
   * Overrides from class HttpServlet
   *--------------------------------------------------------------------------------
   */

  public String getServletInfo()
  {
    String rc = "UserDisplay servlet - Displays Venice user profiles\n"
              + "Part of the Venice Web Communities System\n";
    return rc;

  } // end getServletInfo

  /*--------------------------------------------------------------------------------
   * Overrides from class VeniceServlet
   *--------------------------------------------------------------------------------
   */

  protected VeniceContent doVeniceGet(HttpServletRequest request, VeniceEngine engine,
				      UserContext user, RenderData rdat)
    throws ServletException, IOException, VeniceServletResult
  {
    String uname = request.getPathInfo().substring(1);  // the username we're looking at

    try
    { // load the profile corresponding to that username and display it
      UserProfile prof = user.getProfile(uname);
      changeMenuTop(request);
      setMyLocation(request,"user" + request.getPathInfo());
      return new UserProfileData(engine,prof);

    } // end try
    catch (DataException de)
    { // unable to get the user name
      return new ErrorBox("Database Error","Database error finding user: " + de.getMessage(),"top");

    } // end catch

  } // end doVeniceGet

  protected VeniceContent doVenicePost(HttpServletRequest request, VeniceEngine engine,
				       UserContext user, RenderData rdat)
    throws ServletException, IOException, VeniceServletResult
  {
    String uname = request.getPathInfo().substring(1);  // the username we're looking at
    UserProfile prof;
    String on_error;

    if (logger.isDebugEnabled())
      logger.debug("Posting to profile: " + uname);

    try
    { // load the profile corresponding to that username and display it
      prof = user.getProfile(uname);
      on_error = "user/" + prof.getUserName();

    } // end try
    catch (DataException de)
    { // unable to get the user name
      logger.error("error retrieving user profile: " + de.getMessage(),de);
      return new ErrorBox("Database Error","Database error finding user: " + de.getMessage(),"top");

    } // end catch

    String cmd = getStandardCommandParam(request);

    if (cmd.equals("E"))
    { // send a quick email message - let's do it!
      if (logger.isDebugEnabled())
	logger.debug("sending quick email message");

      try
      { // send a quick email message...
	prof.sendQuickEmail(request.getParameter("subj"),request.getParameter("pb"));

      } // end try
      catch (AccessError ae)
      { // throw an access error box
	logger.error("access error sending email: " + ae.getMessage(),ae);
	return new ErrorBox("Access Error",ae.getMessage(),on_error);

      } // end catch
      catch (DataException de)
      { // database error
	logger.error("database error sending email: " + de.getMessage(),de);
	return new ErrorBox("Database Error","Database error sending message: " + de.getMessage(),on_error);

      } // end catch
      catch (EmailException ee)
      { // error sending the actual email
	logger.error("email exception: " + ee.getMessage(),ee);
	return new ErrorBox("E-Mail Error","Error sending e-mail: " + ee.getMessage(),on_error);

      } // end catch

    } // end if

    if (logger.isDebugEnabled())
      logger.debug("redisplaying profile window");
    changeMenuTop(request);
    setMyLocation(request,"user" + request.getPathInfo());
    return new UserProfileData(engine,prof);

  } // end doVenicePost

} // end class UserDisplay
