/*
 * $Id: DisplayDocumentLinkTag.java,v 1.11 2005/07/14 12:50:41 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: DisplayDocumentLinkTag.java,v $
 * Revision 1.11  2005/07/14 12:50:41  korowa
 * +3 neue dateitypen sind in Datenbank reingekommen(odt,fh11,fh8)
 * +neue Eigenschaft(editable) von Dokumenttype, die Editierbarkeit eines Dokumentes in VKC anzeigt
 *
 * Revision 1.10  2005/01/20 15:25:52  marbobo
 * + Anpassungen an den Methoden getRootURL() und getContextName()
 *
 * Revision 1.9  2004/08/27 16:33:06  koppy
 * Zusammenbau der Doc-URL nach VKCDocument ausgelagert: getDisplayURI()
 *
 * Revision 1.8  2004/07/20 08:56:56  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.view.tag;

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

import org.apache.struts.util.ResponseUtils;

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

public class DisplayDocumentLinkTag extends org.apache.struts.taglib.html.TextTag {
    protected String name   = "document";
    protected String target = "";
    protected boolean editable = false;
    
    public int doStartTag() throws JspException {
		try {
	        HttpSession session = pageContext.getSession();
	        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
	        
			VKCUser user = ServletUtils.getUser( request );

			if ( user == null ) {
		        return SKIP_BODY;
	        }
	        
	        VKCDocument document = (VKCDocument)pageContext.findAttribute( name );
	        
	        if ( document == null ) {
	            throw new JspTagException( "document attribute with name '" + name + "' not found in any scope" );
	        }
	        
	        StringBuffer sb = new StringBuffer();
	        
	        sb.append( "<a href=\"" );
	        sb.append( request.getContextPath() );
	        sb.append( document.getDisplayURI() );
	        if (editable) {
	            sb.append( ".lnk" );
	        } else {
	        	    sb.append( ";jsessionid=" );
	        	    sb.append( session.getId() );
	        }

	        sb.append( "\"" );

	        if ( target.length() > 0 ) {
	            sb.append( " target=\"" );
	            sb.append( target );
	            sb.append( "\"" );
	        }
	
	        sb.append( prepareEventHandlers() );
	        sb.append( prepareStyles() );
	        sb.append( ">" );
	
	        // Print this field to our output writer
	        ResponseUtils.write( pageContext, sb.toString() );
	
	        // Continue processing this page
	        return EVAL_BODY_INCLUDE;
		} catch ( Exception e ) {
			throw new JspException( e );
		}
    }

    public int doEndTag() throws JspException {
        ResponseUtils.write( pageContext, "</a>" );
        
        return EVAL_PAGE;
    }

    public void release() {
        name = "document";
        target = "";
        editable = false;
        super.release();
    }

    public String getName() {
        return name;
    }

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

    public String getTarget() {
        return target;
    }

    public void setTarget( String string ) {
        target = string;
    }
    public boolean isEditable() {
        return editable;
    }
    public void setEditable(boolean editable) {
        this.editable = editable;
    }
}