/*
 * $Id: ClusterEditCategoryViewAction.java,v 1.4 2006/10/04 03:14:35 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ClusterEditCategoryViewAction.java,v $
 * Revision 1.4  2006/10/04 03:14:35  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.3  2006/04/29 18:39:45  marbobo
 * + Refactoriengs und eine Ausgaben vereinheitlicht
 *
 * Revision 1.2  2005/06/29 21:15:49  marbobo
 * + Clustern ist nun aktivierbar/deaktivierbar
 *
 * Revision 1.1  2005/06/23 00:58:28  aram
 * + Initiale Version
 *
 */
package vkc.view.action;

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.clusterer.ClusteringItem;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;
import vkc.view.form.ClusterEditCategoryViewForm;

public final class ClusterEditCategoryViewAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/admin/ClusterEditCategoryView.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 {
            ClusterEditCategoryViewForm form = (ClusterEditCategoryViewForm) f;

    		if (VKCSysParameterSerializer.retrieveIntValue("CATEGORYVIEW", "EnableClusterCategory", 0) == 0) {
    			request.setAttribute(WebShare.INFO_KEY, "vkc.info.action_not_allowed");
                
    			return INFO_FORWARD;
    		}

            if (form.getUser().isGuestUser()) {
                request.setAttribute(WebShare.INFO_KEY, "category.create.denied");
                
                return INFO_FORWARD;
            }

            if (request.getParameter("action.apply") != null) {
				return handleApplyAction(form, request);
            } else if (request.getParameter("action.decode.parameter") != null) {
				return handleDecodeParameterAction(form, request);
            }
            
            return handleInitializeAction(form, request);
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    private ActionForward handleDecodeParameterAction(ClusterEditCategoryViewForm form, HttpServletRequest request) {
        if (!StringUtils.isEmpty(form.getName())) {
            form.setName(VKCStringUtils.base64Decode(form.getName()));
        }
        
        if (!StringUtils.isEmpty(form.getDescription())) {
            form.setDescription(VKCStringUtils.base64Decode(form.getDescription()));
        }
        
        return DEFAULT_FORWARD;
    }

    private ActionForward handleApplyAction(ClusterEditCategoryViewForm form, HttpServletRequest request) {
        ClusteringItem c = form.getCategory();
        c.setName(form.getName());
        c.setDescription(form.getDescription());
        c.setPlaneHierarchy(form.isPlaneHierarchy());
        form.setRefreshTreeView(true);
        
        return DEFAULT_FORWARD;
    }

    private ActionForward handleInitializeAction(ClusterEditCategoryViewForm form, HttpServletRequest request) {
	    return DEFAULT_FORWARD;
	}
}