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

import org.apache.log4j.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.db.PostLinkDecoder;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.*;
import com.silverwrist.venice.ui.servlet.BaseServlet;

public class PostShortcutServlet extends BaseServlet implements LinkTypes
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private static final Redirect makeNewRedirect(String s)
  {
    if (logger.isDebugEnabled())
      logger.debug("Post Redirect decoded - going to: " + s);
    return new Redirect(s,SERVLET);

  } // end makeNewRedirect

  /*--------------------------------------------------------------------------------
   * Implementations from class BaseServlet
   *--------------------------------------------------------------------------------
   */

  public Object process(RequestInput req)
  {
    String raw_link = req.getPathInfo().substring(1);
    PostLinkDecoder decoder;

    if (logger.isDebugEnabled())
      logger.debug("decoding post link: " + raw_link);

    try
    { // attempt to decode the path link information
      decoder = new PostLinkDecoder(raw_link);

    } // end try
    catch (ValidationException e)
    { // the post link decoder failed
      logger.error("Post link decode failed!",e);
      return new ErrorBox("Invalid Post Link","Invalid post link \"" + raw_link + "\": " + e.getMessage(),
			  null);

    } // end catch

    if (decoder.getCommunity()==null)
    { // it must include the community
      logger.error("No community on post link!");
      return new ErrorBox("Invalid Post Link","Invalid post link \"" + raw_link
			  + "\": ambiguous post link (no community)",null);

    } // end if

    CommunityContext comm;
    String tmpstr = decoder.getCommunity();
    try
    { // get the community represented by that alias
      if (logger.isDebugEnabled())
	logger.debug("decoding community: " + tmpstr);
      comm = req.getUser().getCommunityContext(tmpstr);

    } // end try
    catch (DataException e)
    { // can't find the community - we're screwed
      logger.error("unable to decode community \"" + tmpstr + "\"",e);
      return new ErrorBox("Invalid Post Link","Invalid post link \"" + raw_link + "\": cannot find community: "
			  + e.getMessage(),null);
      
    } // end catch

    if (decoder.getConference()==null) // it's a community link only - redirect to the default page
      return makeNewRedirect("community/" + decoder.getCommunity());

    ConferenceContext conf;
    tmpstr = decoder.getConference();
    try
    { // get the conference represented by that alias
      if (logger.isDebugEnabled())
	logger.debug("decoding conference: " + tmpstr);
      conf = comm.getConferenceContext(tmpstr);

    } // end try
    catch (DataException e)
    { // can't find the conference - we're screwed
      logger.error("unable to decode conference \"" + tmpstr + "\"",e);
      return new ErrorBox("Invalid Post Link","Invalid post link \"" + raw_link
			  + "\": cannot find conference: " + e.getMessage(),null);
      
    } // end catch
    catch (AccessError ae)
    { // we can't get to the conference...
      logger.error("AccessError getting conference \"" + tmpstr + "\"",ae);
      if (req.getUser().isLoggedIn())
	return new ErrorBox("Access Error",ae.getMessage(),null);
      else
	return new LogInOrCreate();

    } // end catch

    // compute the locator
    String locator = "cc=" + comm.getCommunityID() + "&conf=" + conf.getConfID();

    if (decoder.getTopic()==-1) // just a conference link - go to the top-level display
      return makeNewRedirect("conf/topics.js.vs?" + locator);

    TopicContext topic;
    short tmpshort = decoder.getTopic();
    try
    { // get the topic number specified within that topic
      if (logger.isDebugEnabled())
	logger.debug("decoding conference: " + tmpshort);
      topic = conf.getTopic(tmpshort);

    } // end try
    catch (DataException e)
    { // we can't find the topic - we're screwed
      logger.error("unable to decode topic \"" + tmpshort + "\"",e);
      return new ErrorBox("Invalid Post Link","Invalid post link \"" + raw_link + "\": cannot find topic: "
			  + e.getMessage(),null);
      
    } // end catch
    catch (AccessError ae)
    { // we can't get to the topic...
      logger.error("AccessError getting topic \"" + tmpshort + "\"",ae);
      if (req.getUser().isLoggedIn())
	return new ErrorBox("Access Error",ae.getMessage(),null);
      else
	return new LogInOrCreate();

    } // end catch

    // add the topic to our locator
    locator += "&top=" + decoder.getTopic();

    if (decoder.getFirstPost()==-1) // we're just referencing the topic
      return makeNewRedirect("conf/posts.js.vs?" + locator + "&rnm=1");

    // we're referencing a post range within the topic
    int tmpint = decoder.getFirstPost();
    int tmpint2 = decoder.getLastPost();
    if (logger.isDebugEnabled())
      logger.debug("decoding post range: " + tmpint + " through " + tmpint2);
    return makeNewRedirect("conf/posts.js.vs?" + locator + "&p1=" + tmpint + "&p2=" + tmpint2);

  } // end process

} // end class PostShortcutServlet
