/*
 * $Id: StrutsUtils.java,v 1.4 2007/02/14 13:08:23 jw Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: StrutsUtils.java,v $
 * Revision 1.4  2007/02/14 13:08:23  jw
 * In method addInfoMessage() messages stored into request instead of session.
 *
 * Revision 1.3  2004/07/20 08:46:28  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.util;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import javax.servlet.http.HttpServletRequest;

public final class StrutsUtils {

	private StrutsUtils() {
	}

	/**
	 * Method adds a new struts ActionMessage to the ActionMessages
	 * in a request.
	 */
	public final static void addInfoMessage( HttpServletRequest request, String property, String key, Object[] options )
	{
		ActionMessages messages = (ActionMessages) request.getAttribute( Globals.MESSAGE_KEY );
		if ( messages == null ) {
			messages = new ActionMessages(  );
			request.setAttribute( Globals.MESSAGE_KEY, messages );
		}

		ActionMessage msg = ( options!=null && options.length>0 ) ? 
                new ActionMessage( key, options ) : new ActionMessage( key );
                
		messages.add( property, msg );
	}

	/**
	 * Method adds a new struts ActionMessage to the ActionMessages
	 * in a request.
	 */
	public final static void addInfoMessage( HttpServletRequest request, String key, Object[] options )
	{
		ActionMessages messages = (ActionMessages)request.getAttribute( Globals.MESSAGE_KEY );

		if ( messages == null ) {
			messages = new ActionMessages(  );
			request.setAttribute( Globals.MESSAGE_KEY, messages );
		}

		ActionMessage msg = ( options!=null && options.length>0 ) ?
			new ActionMessage( key, options ) : new ActionMessage( key );

		messages.add( "info", msg );
	}

	/**
	 * Method adds a new struts ActionMessage to the ActionMessages
	 * in a request.
	 */
	public final static void addInfoMessage( HttpServletRequest request, String key, Object option )
	{
		if ( option==null ) {
			addInfoMessage( request, key, null );
		} else {
			addInfoMessage( request, key, new Object[] { option } );
		}
	}

	/**
	 * Method adds a new struts ActionMessage to the ActionMessages
	 * in a request.
	 */
	public final static void addInfoMessage( HttpServletRequest request, String key ) {
		addInfoMessage( request, key, null );
	}

	/**
	 * Method adds a new struts ActionError to the ActionErrors
	 * in a request.
	 */
	public final static void addErrorMessage( HttpServletRequest request, String key, Object[] options )
	{
		ActionErrors errors = (ActionErrors) request.getAttribute( Globals.ERROR_KEY );

		if ( errors == null ) {
			errors = new ActionErrors(  );
			request.setAttribute( Globals.ERROR_KEY, errors );
		}

		ActionError err = ( options!=null && options.length>0 ) ?
			new ActionError( key, options ) :
			new ActionError( key );
		errors.add( "error", err );
	}

	/**
	 * Method adds a new struts ActionError to the ActionErrors
	 * in a request.
	 */
	public final static void addErrorMessage( HttpServletRequest request, String key )
	{
		addErrorMessage( request, key, null );
	}

	/**
	 * Method adds a new struts ActionError to the ActionErrors
	 * in a request.
	 */
	public final static void addErrorMessage( HttpServletRequest request, String key, Object option )
	{
		if ( option==null ) {
			addErrorMessage( request, key, null );
		} else {
			addErrorMessage( request, key, new Object[] { option } );
		}
	}
}
