/*
    PCS - A Framework For Java Web Applications
    Copyright (C) 2002 Patrick Carl, patrick.carl@web.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 
    $Id: ErrorAction.java,v 1.1 2003/01/08 02:31:55 pcs_org Exp $
 
 */

package de.spieleck.pcs.action.base;

import java.io.PrintWriter;
import java.io.StringWriter;

import java.util.Hashtable;

import de.spieleck.pcs.Constants;
import de.spieleck.pcs.action.Action;
import de.spieleck.pcs.action.ActionException;
import de.spieleck.pcs.action.ActionResult;

/**
 * This action is used by the controller servlet for error handling
 * @author  Patrick Carl
 */
public class ErrorAction implements Action {
    
    /** Creates a new instance of ErrorAction */
    public ErrorAction() {
    }
    
    /** 
     * parameters must contain the following:<table><tr><th>key</th><th>value
     * </th></tr>
     * <tr><td>"error.type</td><td>type of error</td></tr>
     * <tr><td>"error.message</td><td>error message (for user)</td></tr>
     * <tr><td>"error.stack</td><td>error stack (for developer)</td></tr></table>
     * All keys and values are Strings.
     * @param parameters a Hashtable for information needed to perform the Action
     * @throws ActionException if an exception occurrs during processing
     * @return an instance of ActionResult as result of the Action
     *
     */
    public ActionResult perform(Hashtable parameters) throws ActionException {
        Exception e = (Exception) parameters.get(
            Constants.CONTROLLER_SESSION_EXCEPTION_PARAM_NAME);
        if(e == null)
            throw new ActionException("The exception is that there is none!");
        String type = e.getClass().getName();
            
        String message = e.getMessage();
        StringBuffer stack = new StringBuffer();
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        
            
        StringBuffer xml = new StringBuffer("<error>" + "<type>" + type
        + "</type>");
        if(message != null)
            xml = xml.append("<message>" + message + "</message>");
        if(stack != null)
            xml = xml.append("<stack>" + stack.toString() + "</stack>");
        xml = xml.append("</error>");
        Hashtable p = new Hashtable();
        return new ActionResult(xml.toString(), p);
    }
}