/*
 * $Id: TreeViewTag.java,v 1.11 2006/10/04 03:15:09 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TreeViewTag.java,v $
 * Revision 1.11  2006/10/04 03:15:09  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.10  2005/06/23 01:04:37  aram
 * + Anpassungen bzgl. Cluster-Moduls
 *
 * Revision 1.9  2004/11/10 15:02:53  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.8  2004/09/03 15:18:53  marbobo
 * + Bugfix2
 *
 * Revision 1.7  2004/09/03 12:46:55  marbobo
 * + Bugfix
 *
 * Revision 1.6  2003/11/11 02:37:19  koppy
 * + linkPath-Attribut
 *
 * Revision 1.5  2003/08/03 15:34:19  bigmac
 * *** empty log message ***
 *
 * Revision 1.4  2003/05/13 12:53:07  koppy
 * *** empty log message ***
 *
 * Revision 1.3  2003/05/01 22:46:06  koppy
 * <No Comment Entered>
 *
 * Revision 1.2  2003/04/30 01:10:05  koppy
 * + showDocs-Attribute
 *
 * Revision 1.1  2003/04/29 11:08:51  koppy
 * Initiale Version
 *
 */
package vkc.view.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import vkc.db.entities.VKCUser;
import vkc.misc.VKCLogManager;
import vkc.util.ServletUtils;
import vkc.util.treeview.TreeView;

/**
 * Tag for displaying a tree which
 * is built in <code>vkc.util.treeview.TreeViewAction</code>
 *
 * @see vkc.util.treeview.TreeViewAction
 * @author koppy
 */
public class TreeViewTag extends TagSupport {
	public static final String defaultImageDir = "/gfx/tree";
	public static final String defaultTarget = "_blank";

	/**
	 * directory of the images used for drawing the tree, e.g. "/gfx-tree"
	 */
	private String imageDir = TreeViewTag.defaultImageDir;
	
	/**
	 * target frame for the Links in the tree, e.g. "_blank"
	 */
	private String target = TreeViewTag.defaultTarget;

	private String path = "/treeview.do";
	private String showRoot = "true";	
	private String linkPath = null;
	private String checkPermission = "true";

	private String name = TreeView.TREE_KEY;

	/**
	 * Constructor
	 */
	public TreeViewTag() {
		super();
	}

	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	public int doEndTag() throws javax.servlet.jsp.JspTagException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		VKCUser user = ServletUtils.getUser(request);
		String contextPath = request.getContextPath();

		TreeView tree = (TreeView) request.getSession().getAttribute(this.name);

		if (tree == null) {
			throw new JspTagException("TreeView bean not found in session scope: KEY=" + name);
		}
		
		imageDir = ServletUtils.replacePathVars(imageDir, user);
		path = ServletUtils.replacePathVars(path, user);

		tree.setImageDir(contextPath + this.imageDir);
		tree.setPath(contextPath + this.path);
		tree.setShowRoot("true".equalsIgnoreCase(this.showRoot.trim()));
		tree.setCheckPermission("true".equalsIgnoreCase(this.checkPermission.trim()));
		tree.setTarget(this.target);
		
		if (this.linkPath != null && this.linkPath.trim().length() > 0) {
		    if (this.linkPath.startsWith("javascript:")) {
		        tree.setLinkPath(this.linkPath);
		    } else {
		        tree.setLinkPath(contextPath + this.linkPath);
		    }
		}
		
		try {
			tree.draw(request, pageContext.getOut());
		} catch (IOException e) {
			VKCLogManager.errorLog(this, "", e.getMessage(),e);
			throw new JspTagException("IOException occured while drawing the tree!");
		}

		return EVAL_PAGE;
	}

	/**
	 * Releases allocated resources
	 */
	public void release() {
		super.release();

		this.imageDir = TreeViewTag.defaultImageDir;
		this.target = TreeViewTag.defaultTarget;

	}
	
	public String getImageDir() {
		return imageDir;
	}

	public String getPath() {
		return path;
	}

	public String getShowRoot() {
		return showRoot;
	}

	public String getTarget() {
		return target;
	}

	public void setImageDir(String string) {
		imageDir = string;
	}

	public void setPath(String string) {
		path = string;
	}

	public void setShowRoot(String string) {
		showRoot = string;
	}

	public void setTarget(String string) {
		target = string;
	}

	public String getName() {
		return name;
	}

	public void setName(String string) {
		name = string;
	}

	public String getLinkPath() {
		return linkPath;
	}

	public void setLinkPath(String string) {
		linkPath = string;
	}

    public String getCheckPermission() {
        return checkPermission;
    }

    public void setCheckPermission(String checkPermission) {
        this.checkPermission = checkPermission;
    }
}