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

import java.util.*;
import com.silverwrist.util.image.ImageNormalizerException;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.venice.VeniceNamespaces;
import com.silverwrist.venice.content.*;
import com.silverwrist.venice.except.*;

class VeniceExceptionTranslator implements ExceptionTranslator
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  VeniceExceptionTranslator()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ExceptionTranslator
   *--------------------------------------------------------------------------------
   */

  public Object translateException(Request r, Exception e)
  {
    // get the title for this exception
    String title = null;
    if (e instanceof AuthenticationException)
      title = "xtitle.auth";
    else if (e instanceof DatabaseException)
      title = "xtitle.dbase";
    else if (e instanceof DialogException)
      title = "xtitle.dlg";
    else if (e instanceof DynamoSecurityException)
      title = "xtitle.secure";
    else if (e instanceof MailException)
      title = "xtitle.mail";
    else if (e instanceof ScriptingException)
      title = "xtitle.script";
    else if (e instanceof StyleSheetException)
      title = "xtitle.style";
    else if (e instanceof ValidationException)
      title = "xtitle.valid";
    else if (e instanceof ImageNormalizerException)
      title = "xtitle.image";

    // get the error locations
    String onerr = (String)(PropertyUtils.getPropertyNoErr(r,VeniceNamespaces.REQUEST_INFO_NAMESPACE,
							   "on.error"));
    String onerr_type = (String)(PropertyUtils.getPropertyNoErr(r,VeniceNamespaces.REQUEST_INFO_NAMESPACE,
								"on.error.type"));
    String prefix = "";
    if (title!=null)
    { // look up actual title in resource bundle
      ResourceBundle b = ResourceBundle.getBundle("com.silverwrist.venice.app.VeniceAppMessages",
						  Locale.getDefault());
      title = b.getString(title);

    } // end if
    else
      prefix = e.getClass().getName() + ": ";

    if (onerr!=null)
      return new ErrorBox(title,prefix + e.getMessage(),onerr_type,onerr);
    else
      return new ErrorBox(title,prefix + e.getMessage());

  } // end translateException

} // end class VeniceExceptionTranslator
