/*
 * $Id: ClusterCategoryAction.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: ClusterCategoryAction.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/07/13 14:50:19  marbobo
 * + Bugfix
 *
 * 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 java.util.HashMap;

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.Clustern;
import vkc.db.entities.VKCCategory;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.procoba.project.ProjectViewForm;
import vkc.security.PermissionManager;
import vkc.util.ClusterUtils;
import vkc.view.PageId;
import vkc.view.WebShare;
import vkc.view.form.ClusterCategoryForm;

public final class ClusterCategoryAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/admin/ClusterCategory.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 {
            ClusterCategoryForm form = (ClusterCategoryForm) 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()
               || !PermissionManager.hasPermission(form.getCategory().getAcl(), form.getUser(), 'C')
               || !PermissionManager.hasPermission(form.getCategory().getAcl(), form.getUser(), 'd')) {
                request.setAttribute(WebShare.INFO_KEY, "category.create.denied");
                
                return INFO_FORWARD;
            }

        	if (request.getParameter("action.cluster") != null) {
				return handleClusterAction(form, request);
            } else if (request.getParameter("action.cancel") != null) {
				return handleCancelAction(form, request);
            } else {
                return handleInitializeAction(form, request);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

	private ActionForward handleInitializeAction(ClusterCategoryForm form, HttpServletRequest request) {
	    return DEFAULT_FORWARD;
	}
	
	private ActionForward handleClusterAction(ClusterCategoryForm form, HttpServletRequest request) {
	    try {
		    request.getSession().setAttribute(Clustern.CLUSTER_MAP_NAME, ClusterUtils.clusterCategory(form.getCategory(), form.getClusterAlgoMode(), form.getClusterMode(), form.isTermIntersection()));
        } catch (Exception e) {
		    request.getSession().setAttribute(Clustern.CLUSTER_MAP_NAME, new HashMap());
        }
	    
	    String forwardURL = "/templates/${template.name}/common/ClusterMainView.jsp?";
	    forwardURL += "categoryId=" + form.getCategoryId();
	    forwardURL += "&sourcePageId=" + form.getSourcePageId();
	    
	    if (form.getTargetId() != null) {
		    forwardURL += "&targetId=" + form.getTargetId();
	    }
	    
	    if (form.getSubTargetId() != null) {
		    forwardURL += "&subTargetId=" + form.getSubTargetId();
	    }
        
        return new ActionForward(forwardURL);
	}
	
	private ActionForward handleCancelAction(ClusterCategoryForm form, HttpServletRequest request) {
		String forwardURL = "";

		if (form.getSourcePageId().equals(PageId.PROJECT_VIEW)) {
			forwardURL = "/projectView.do?viewMode=" + ProjectViewForm.VIEW_MODE_FOLDER + "&projectId=" + form.getTargetId() + "&subfolderId=" + form.getSubTargetId();
			
			return new ActionForward(forwardURL, true);
		} else if (form.getSourcePageId().equals(PageId.FUNCTIONAL_VIEW)) {
			forwardURL = "/functionalView.do?functionalViewId=" + form.getTargetId() + "&subfolderId=" + form.getSubTargetId();

			return new ActionForward(forwardURL, true);
		} else if (form.getSourcePageId().equals(PageId.CATEGORY_VIEW)) {
			if (form.getTargetId() != null) {
				forwardURL = "/showCategory.do?categoryId=" + form.getTargetId();
			} else {
				forwardURL = "/showCategory.do?categoryId=" + form.getCategoryId();
			}

			return new ActionForward(forwardURL, true);
		} else {
			return new ActionForward("/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID, true);
		}
	}	
}