/*
 * $Id: ExplorerTreeViewAction.java,v 1.4 2006/10/02 14:39:13 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ExplorerTreeViewAction.java,v $
 * Revision 1.4  2006/10/02 14:39:13  marbobo
 * + Eigenes Schlüssel für den Explorer in den Suchmasken eingefügt
 *
 * Revision 1.3  2005/02/15 09:26:52  bigmac
 * Refactorings
 *
 * Revision 1.2  2004/11/02 21:42:31  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.1  2004/09/03 00:39:26  marbobo
 * + Überarbeitet
 *
 * Revision 1.2  2004/06/15 20:21:33  marbobo
 * Refactoring
 *
 * Revision 1.1  2004/02/18 15:22:53  marbobo
 * Initiale Version
 *
 */
package vkc.util.treeview;

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.db.entities.VKCUser;
import vkc.util.ObjectCloner;
import vkc.util.ServletUtils;

/**
 * This class is used for building a tree and afterwards forwarding to a
 * TreeView JSP
 *
 * @author marbobo
 */
public final class ExplorerTreeViewAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/common/explorerTreeView.jsp");

	public ActionForward execute(ActionMapping mapping,
	        					 ActionForm f,
	        					 HttpServletRequest request,
	        					 HttpServletResponse response)
	throws Exception {
		VKCUser user = ServletUtils.getUser(request);
		
		ExplorerTreeViewForm form = (ExplorerTreeViewForm) f;
		TreeViewManager tm = TreeViewManager.getInstance();
		 
		// TreeView aktualisieren
		if (tm.updateRequested() || form.refresh()) {
			tm.buildGlobalTree();
		}
						
		TreeView tree = (TreeView)request.getSession().getAttribute(TreeView.SEARCH_TREE_KEY); 

		if (tree == null || !tree.isUpToDate()) {
			tree = (TreeView)ObjectCloner.deepCopy(tm.getGlobalTreeView());			
			tree.setUser(user);	
			tree.clearTreeViewItems();										
			request.getSession().setAttribute(TreeView.SEARCH_TREE_KEY, tree);
		}				

		return DEFAULT_FORWARD;
	}
}