/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server;

import org.bodington.pool.*;

/**
 * An exception that indicates any kind of abonormal processing with
 * BuildingServer and associated classes.
 * 
 * @author Jon Maber
 * @version 1
 */

public class BuildingServerException extends java.lang.Exception
	{

	private String friendly_message;

	/**
	 * Instantiates an exception.
	 * 
	 * @param s The message to display for this exception.
	 */
	public BuildingServerException( String s )
		{
		super( s );
		friendly_message = getMessage();
		}

	/**
	 * Instantiates an exception.
	 * @param s The message that caused this exception.
	 * @param friendly The message to display to the user.
	 */
	public BuildingServerException( String s, String friendly )
		{
		super( s );
		friendly_message = friendly;
		}

	public BuildingServerException( ObjectPoolException opex )
		{
		switch ( opex.getReason() )
			{
			case ObjectPoolException.REASON_TIMEOUT:
				friendly_message = 
					"The page request you made required a database operation but " +
					"the database is currently very busy.  Please try again later. ";
				break;
				
			case ObjectPoolException.REASON_MAX_OBJECTS:
			case ObjectPoolException.REASON_MAX_OBJECTS_AND_REQUESTS:
			case ObjectPoolException.REASON_MAX_REQUESTS:
				friendly_message = 
					"The page request you made required a database operation but " +
					"you already have a number of outstanding database operations " +
					"in progress.  Your page request was cancelled to prevent your " +
					"activity from reducing the performance of the service for other " +
					"users. " +
					"In future, when you click on a link or button please wait for " +
					"the page to be delivered. ";
				break;
				
			default:
				friendly_message = 
					"The page request you made required a database operation but " +
					"there was a technical problem obtaining one.  Please try again later. ";
				break;
			}
		
		}

	public String getMessage()
		{
		if ( super.getMessage() != null )
			return super.getMessage();
		
		return friendly_message;
		}
		
	public String friendlyMessage()
		{
		if ( friendly_message!= null )
			return friendly_message;
		return getMessage();
		}
	} 

