/*
 * $Id: NewsListAction.java,v 1.4 2006/10/04 03:15:01 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: NewsListAction.java,v $
 * Revision 1.4  2006/10/04 03:15:01  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.3  2005/02/14 14:01:45  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.2  2004/09/16 13:10:54  marbobo
 * + Auf VKCRequestProcessor umgestellt
 *
 * Revision 1.1  2004/07/26 01:34:51  marbobo
 * + Initiale Version
 *
 */
package vkc.administration.action;

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.administration.form.NewsListForm;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCNewsSerializer;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.exception.DBException;
import vkc.view.WebShare;

/**
 * Klasse zum Anzeigen der Systemnews
 * 
 * @author marbobo
 */
public class NewsListAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/administration/newsList.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 Exception {
		if ( VKCSysParameterSerializer.retrieveIntValue( "NEWS", "EnableNews", 0 ) == 0 ) {
            request.setAttribute( WebShare.INFO_KEY, "vkc.info.access_denied" );
            
            return INFO_FORWARD;
		}

		NewsListForm form = (NewsListForm)f;
		VKCUser user = form.getUser();

		if ( !user.hasAdminPrivileges() ) {
			request.setAttribute( WebShare.INFO_KEY, "vkc.info.access_denied" );

			return INFO_FORWARD;
		}

		if ( request.getParameter( "action.delete.news" ) != null ) {
			return handleDeleteNewsAction( form, request );
		} else if ( request.getParameter( "action.edit.news" ) != null ) {
			return handleEditNewsAction( form, request );
		}

		return handleInitializeAction( form, request );
	}
	
	private ActionForward handleDeleteNewsAction( NewsListForm form, HttpServletRequest request ) throws DBException {
		for ( int i = 0; i < form.getSelectedIds().length; i++ ) {
		    VKCNewsSerializer.deleteNews( form.getSelectedIds()[ i ].intValue() );
		}
		
		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleEditNewsAction( NewsListForm form, HttpServletRequest request ) {
		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleInitializeAction( NewsListForm form, HttpServletRequest request ) {
	    return DEFAULT_FORWARD;
	}
}