/*
 * $Id: ChangeCategoryCreatorAction.java,v 1.3 2006/10/04 03:14:59 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008
 *           (c) Universität Paderborn 2008
 * All Rights Reserved.
 *
 * $Log: ChangeCategoryCreatorAction.java,v $
 * Revision 1.3  2006/10/04 03:14:59  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.2  2005/09/13 09:51:45  jonyp
 * cleaning the code
 *
 * Revision 1.1  2005/08/17 12:58:07  jonyp
 * *** empty log message ***
 *
 */

package vkc.administration.action;

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

import org.apache.struts.Globals;
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.ChangeCategoryCreatorForm;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.view.WebShare;

public final class ChangeCategoryCreatorAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/administration/changeCategoryCreator.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 {
	    ChangeCategoryCreatorForm form = (ChangeCategoryCreatorForm)f;
    	VKCUser user = form.getUser();		

		if ( !user.hasAdminPrivileges() && !user.isDedicatedUsersAdmin() ) { 
            request.setAttribute( WebShare.INFO_KEY, "vkc.info.access_denied" );
            return INFO_FORWARD;
        }

		if ( request.getParameter( "action.change.publisher" ) != null ) {
			return handleChangeCategoryCreatorAction( form, request );
		}

		if ( request.getParameter( "action.cancel" ) != null ) {
			return handleCancelAction( form, request );
		}

		// No Action
		return DEFAULT_FORWARD;
	}

	private ActionForward handleChangeCategoryCreatorAction( ChangeCategoryCreatorForm form, HttpServletRequest request ) throws Exception {
	    
	    VKCCategorySerializer.changeCreatorId(form.getCategoryId(), form.getOldPublisherId(), form.getSelectedNewPublisherId(), form.getUser().getUserId());

        request.setAttribute(Globals.MESSAGE_KEY, "vkc.administration.changecreator.succeeded");
	    return DEFAULT_FORWARD;
	}
	
	private ActionForward handleCancelAction( ChangeCategoryCreatorForm form, HttpServletRequest request ) {
    	
    	return new ActionForward("/editCategory.do?categoryId=" + form.getCategoryId());
    }
}
