/*
 * 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 com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.db.PostLinkDecoder;
import com.silverwrist.venice.servlets.format.*;

public class PostShortcut extends VeniceServlet
{
  /*--------------------------------------------------------------------------------
   * Overrides from class HttpServlet
   *--------------------------------------------------------------------------------
   */

  public String getServletInfo()
  {
    String rc = "PostShortcut servlet - Decodes a post link and redirects to it\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 raw_link = request.getPathInfo().substring(1);
    PostLinkDecoder decoder;

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

    } // end try
    catch (ValidationException e)
    { // the post link decoder failed
      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
      return new ErrorBox("Invalid Post Link","Invalid post link \"" + raw_link
			  + "\": ambiguous post link (no community)",null);

    CommunityContext comm;
    try
    { // get the community represented by that alias
      comm = user.getCommunityContext(decoder.getCommunity());

    } // end try
    catch (DataException e)
    { // can't find the community - we're screwed
      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
      throw new RedirectResult("sig/" + decoder.getCommunity());

    ConferenceContext conf;
    try
    { // get the conference represented by that alias
      conf = comm.getConferenceContext(decoder.getConference());

    } // end try
    catch (DataException e)
    { // can't find the conference - we're screwed
      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...
      return new ErrorBox("Access Error",ae.getMessage(),null);

    } // end catch

    // compute an elementary "locator"
    String locator = "sig=" + comm.getCommunityID() + "&conf=" + conf.getConfID();

    if (decoder.getTopic()==-1) // just a conference link - go to the top-level display
      throw new RedirectResult("confdisp?" + locator);

    TopicContext topic;
    try
    { // get the topic number specified within that topic
      topic = conf.getTopic(decoder.getTopic());

    } // end try
    catch (DataException e)
    { // we can't find the topic - we're screwed
      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...
      return new ErrorBox("Access Error",ae.getMessage(),null);

    } // end catch

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

    if (decoder.getFirstPost()==-1) // we're just referencing the topic
      throw new RedirectResult("confdisp?" + locator + "&rnm=1");

    // we're referencing a post range within the topic
    throw new RedirectResult("confdisp?" + locator + "&p1=" + decoder.getFirstPost() + "&p2="
			     + decoder.getLastPost());

  } // end doVeniceGet

} // end class PostShortcut
