/*
 * $Id$
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log$
 * Revision 1.2  2006/10/04 03:15:09  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.1  2006/03/09 01:35:13  marbobo
 * + Initiale Version
 *
 */

package vkc.view.tag;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.logic.IterateTag;
import org.apache.struts.util.RequestUtils;

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;
import vkc.util.VKCStringUtils;

public class LinkTag extends org.apache.struts.taglib.html.LinkTag {
    /**
     * Filter the rendered output for characters that are sensitive in HTML?
     */
    protected boolean filter = true;

    public boolean getFilter() {
        return filter;
    }

    public void setFilter(boolean filter) {
        this.filter = filter;
    }

    /**
    * Return the text specified by the literal value or the message resources
    * key, if any; otherwise return <code>null</code>.
    *
    * @param literal Literal text value or <code>null</code>
    * @param key Message resources key or <code>null</code>
    *
    * @exception JspException if both arguments are non-null
    */
	protected String message(String literal, String key) throws JspException {
	   if (literal != null) {
	       if (key != null) {
	           JspException e = new JspException(messages.getMessage("common.both"));
	           RequestUtils.saveException(pageContext, e);
	           throw e;
	       }

	       if (filter) {
	    	   return VKCStringUtils.filterUnicodeChars(literal);
	       }
	       
	       return literal;
	   } 

	   if (key != null) {
           if (filter) {
    		   return VKCStringUtils.filterUnicodeChars(RequestUtils.message(pageContext, getBundle(), getLocale(), key));
           }
           
		   return RequestUtils.message(pageContext, getBundle(), getLocale(), key);
       } 

       return null;
	}

    /**
	 * Return the complete URL to which this hyperlink will direct the user.
	 * Support for indexed property since Struts 1.1
	 * 
	 * @exception JspException
	 *                if an exception is thrown calculating the value
	 */
	protected String calculateURL() throws JspException {
		// Identify the parameters we will add to the completed URL
		Map params = RequestUtils.computeParameters(pageContext, paramId,
				paramName, paramProperty, paramScope, name, property, scope,
				transaction);

		// if "indexed=true", add "index=x" parameter to query string
		// * @since Struts 1.1
		if (indexed) {
			// look for outer iterate tag
			IterateTag iterateTag = (IterateTag) findAncestorWithClass(this,
					IterateTag.class);

			if (iterateTag == null) {
				// This tag should only be nested in an iterate tag
				// If it's not, throw exception
				JspException e = new JspException(messages
						.getMessage("indexed.noEnclosingIterate"));
				RequestUtils.saveException(pageContext, e);
				throw e;
			}

			// calculate index, and add as a parameter
			if (params == null) {
				params = new HashMap(); // create new HashMap if no other params
			}
			
			if (indexId != null) {
				params.put(indexId, Integer.toString(iterateTag.getIndex()));
			} else {
				params.put("index", Integer.toString(iterateTag.getIndex()));
			}
		}

		VKCUser user = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());
		String url = null;
		forward = ServletUtils.replacePathVars(forward, user);
		href = ServletUtils.replacePathVars(href, user);
		page = ServletUtils.replacePathVars(page, user);
		action = ServletUtils.replacePathVars(action, user);
		
		try {
			url = RequestUtils.computeURL(pageContext, forward, href, page,
					action, params, anchor, false);
		} catch (MalformedURLException e) {
			RequestUtils.saveException(pageContext, e);
			throw new JspException(messages.getMessage("rewrite.url", e
					.toString()));
		}
		
		return url;
	}

    /**
     * Release any acquired resources.
     */
    public void release() {
        super.release();
        
        filter = true;
    }
}