/*
 * $Id: ClusterTreeViewAction.java,v 1.3 2006/04/29 18:39:44 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ClusterTreeViewAction.java,v $
 * Revision 1.3  2006/04/29 18:39:44  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:34  aram
 * + Initiale Version
 *
 */
package vkc.util.treeview;

import java.util.HashMap;

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.clusterer.Clustern;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.util.ServletUtils;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

/**
 * This class is used for building a tree and afterwards forwarding to a
 * TreeView JSP
 */
public final class ClusterTreeViewAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/common/clusterTreeView.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 {
		ClusterTreeViewForm form = (ClusterTreeViewForm) f;
		 
		TreeView tree = (TreeView)request.getSession().getAttribute(TreeView.CLUSTER_TREE_KEY); 

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

		if (tree == null || form.getAction().equals("create")) {
			return handleCreateAction(form, request);
		} else if (form.refresh()) {
		    return handleRefreshAction(form, request);
		} else if (request.getParameter("action.toggle.treeitem") != null && form.getSelectedId() != null) {
		    return handleToggleTreeItemAction(form, request);
		}

		return DEFAULT_FORWARD;
	}

    private ActionForward handleCreateAction(ClusterTreeViewForm form, HttpServletRequest request) {
		VKCUser user = ServletUtils.getUser(request);
	    HashMap cluster = (HashMap)request.getSession().getAttribute(Clustern.CLUSTER_MAP_NAME); 
		TreeView tree = (TreeView)request.getSession().getAttribute(TreeView.CLUSTER_TREE_KEY); 
		tree = ClusterTreeViewManager.getClusterTreeView(cluster);
	    tree.setUser(user);
	    tree.setDrawCheckBox(true);
		request.getSession().setAttribute(TreeView.CLUSTER_TREE_KEY, tree);

		return DEFAULT_FORWARD;
    }

    private ActionForward handleRefreshAction(ClusterTreeViewForm form, HttpServletRequest request) {
	    HashMap categories = (HashMap)request.getSession().getAttribute(Clustern.CLUSTER_MAP_NAME);
        
	    if (categories != null) {
    	    ClusteringItem c = (ClusteringItem)categories.get(form.getCategoryId());
    	    
    	    if (c != null) {
        	    TreeViewItem item = form.getTreeItem(form.getCategoryId().toString());
                
        	    if (item != null) {
	                String linkPath = WebShare.getContextPath() + "/clusterEditCategoryView.do?categoryId=" + c.getClusterID();
	                
	                if (!StringUtils.isEmpty(c.getName())) {
		                linkPath += "&name=" + VKCStringUtils.base64Encode(c.getName());
	                } else {
		                linkPath += "&name=" + VKCStringUtils.base64Encode("");
	                }

	                if (!StringUtils.isEmpty(c.getDescription())) {
		                linkPath += "&description=" + VKCStringUtils.base64Encode(c.getDescription());
	                } else {
		                linkPath += "&description=" + VKCStringUtils.base64Encode("");
	                }

	                linkPath += "&action.decode.parameter=true";
	                
	                if (c.isPlaneHierarchy()) {
		                linkPath += "&planeHierarchy=true";
	                }

	                item.setText(c.getRecursivDocCounts() + (StringUtils.isEmpty(c.getName()) ? "" : " - " + c.getName()));
	                item.setLink(linkPath);
        	    }
    	    }
        }
        
        form.setCategoryId(null);
        form.setSelectedId(null);
        form.setAction("");

        return DEFAULT_FORWARD;
    }

    private ActionForward handleToggleTreeItemAction(ClusterTreeViewForm form, HttpServletRequest request) {
        TreeViewItem item = form.getTreeItem(form.getSelectedId().toString());
        item.setChecked(!item.isChecked());
        form.setSelectedId(null);

        return DEFAULT_FORWARD;
    }
}