/*
 * $Id: ShowCategoryFavoritesAction.java,v 1.10 2005/02/14 14:01:50 bigmac Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ShowCategoryFavoritesAction.java,v $
 * Revision 1.10  2005/02/14 14:01:50  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.9  2004/11/10 15:02:49  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.8  2004/11/02 21:42:28  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.7  2004/08/20 21:51:57  marbobo
 * + Löschen per Auswahl ( geändert )
 *
 * Revision 1.6  2004/08/19 12:16:57  marbobo
 * +Umgeschrieben und Bugfix
 *
 * Revision 1.5  2004/08/11 11:28:04  marbobo
 * - Unnötige Variablen entfernt
 *
 * Revision 1.4  2004/08/09 16:08:42  jonyp
 * changes due change of category favorites class
 *
 * Revision 1.3  2004/05/07 10:16:56  jonyp
 * added use of private "handle...Action" functions to answer to diffrent requests
 *
 * Revision 1.2  2004/05/06 15:39:30  jonyp
 * *** empty log message ***
 *
 * Revision 1.1  2004/05/06 15:09:53  jonyp
 * first strike
 *
 * Revision 1.1  2004/05/05 15:02:43  vchris
 * Initiale Version
 *
 * 
 */
package vkc.profile.action;

import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.db.serializer.VKCCategoryFavoritesSerializer;
import vkc.view.WebShare;
import vkc.misc.VKCLogManager;
import vkc.profile.form.ShowCategoryFavoritesForm;

public class ShowCategoryFavoritesAction extends Action {	
    private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/userProfile/showCategoryFavorites.jsp" );

    private ActionForward INFO_FORWARD = 
		new ActionForward( "/templates/${template.name}/common/InfoPage.jsp" );

	public ActionForward execute( ActionMapping mapping,
								  ActionForm f,
								  HttpServletRequest request,
								  HttpServletResponse response )
	throws ServletException {
		try {
   		    ShowCategoryFavoritesForm form = (ShowCategoryFavoritesForm)f;

			if ( form.getUser().isGuestUser() ) {
				request.setAttribute( WebShare.INFO_KEY, "vkc.info.access_denied" );
				
				return INFO_FORWARD;
			}

			if ( request.getParameter( "action.delete.selected-favorites" ) != null ) {
				return handleDeleteSelectedAction( form, request );		
			} 

			return handleInitializeAction( form, request );
		} catch( Exception ex ) {
		    VKCLogManager.errorLog(this,"", ex.getMessage(),ex);
			throw new ServletException( ex );
		}
	}
	
	private ActionForward handleInitializeAction( ShowCategoryFavoritesForm form, HttpServletRequest request ) throws Exception {
		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleDeleteSelectedAction( ShowCategoryFavoritesForm form, HttpServletRequest request ) throws Exception {
		Integer[] ids = form.getSelectedIds();

		if ( ids.length > 0 ) {
		    VKCCategoryFavoritesSerializer.deleteCategoryFavorites( form.getUser().getUserId(), new ArrayList( Arrays.asList( ids ) ) );
		} 

		return DEFAULT_FORWARD;
	}
}