/*
 * $Id: DisplayUrlForm.java,v 1.3 2009/01/14 15:11:09 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: DisplayUrlForm.java,v $
 * Revision 1.3  2009/01/14 15:11:09  korowa
 * Anpassung der Funktionen zum Anzeigen der Kategorie- und Dokumentlinks. Ab jetzt werden die Links für die 	Guest-Users nur angezeigt, wen auch die entsprechende Rechte(lesen) für "Jeder" auf "true" gesetzt werden.
 *
 * Revision 1.2  2005/09/01 08:32:02  korowa
 * Es werden mehrere URL angeboten, welche direkten Zugriff auf Dateien
 *  und automatisches Anmelden als Gast erlauben.
 *
 * Revision 1.1  2005/04/26 17:52:39  marbobo
 * + Initiale Version
 *
 */

package vkc.view.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCAclEntry;
import vkc.db.entities.VKCAclType;
import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCAclSerializer;
import vkc.db.serializer.VKCAclTypeSerializer;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.security.VKCAcl;
import vkc.view.WebShare;

public final class DisplayUrlForm extends BasicActionForm {
	public static final String CATEGORY_LINK = "categoryLink";
	public static final String DOCUMENT_LINK = "documentLink";

	private Integer objectId = null;
	
	private String linkType = null;
	
	private VKCCategory category = null;

	private VKCDocument document = null;
	
	/**
	 * Reset der Eingabefelder auf Defaultwerte
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public void reset( ActionMapping mapping, HttpServletRequest request ) {
	    super.reset(mapping, request);
	}

    public String getLinkType() {
        return linkType;
    }

    public void setLinkType(String linkType) {
        this.linkType = linkType;
    }

    public Integer getObjectId() {
        return objectId;
    }

    public void setObjectId(Integer objectId) {
        this.objectId = objectId;
    }
    
    public boolean isCategoryLink() {
        return CATEGORY_LINK.equals(getLinkType());
    }

    public boolean isDocumentLink() {
        return DOCUMENT_LINK.equals(getLinkType());
    }

    public VKCCategory getCategory() throws DBException {
        if (category == null) {
            category = VKCCategorySerializer.retrieveCategory(getObjectId());
        }
        
        return category;
    }

    public VKCDocument getDocument() throws DBException {
        if (document == null) {
            document = VKCDocumentSerializer.retrieveDocument(getObjectId());
        }
        
        return document;
    }
    
    public String getCategoryUrl() throws DBException {
        if (getCategory() != null) {
            return WebShare.getRootURL() + WebShare.getContextPath() + getCategory().getCategorySysLink();
        }
        
        return "";
    }

    public String getDocumentUrl() throws DBException {
        if (getDocument() != null) {
            return WebShare.getRootURL() + WebShare.getContextPath() + getDocument().getDocumentSysLink();
        }
        
        return "";
    }

    public String getDocumentOpenUrl() throws DBException {
        if (getDocument() != null) {
            return WebShare.getRootURL() + WebShare.getContextPath() + getDocument().getDocumentOpenSysLink();
        }
        
        return "";
    }
    public String getCategoryGuestUrl() throws DBException {
        if (getCategory() != null&&getCategory().hasPermission(VKCUser.createGuestUser(), 'r')) {
            return WebShare.getRootURL() + WebShare.getContextPath() + getCategory().getCategorySysLink() + "&loginType=" + SysLinkForm.LOGINTYPE_GUEST_USER;
        }
        
        return null;
    }

    public String getDocumentGuestUrl() throws DBException {
        if (getDocument() != null&&getDocument().getCategory().hasPermission(VKCUser.createGuestUser(), 'r')) {
            return WebShare.getRootURL() + WebShare.getContextPath() + getDocument().getDocumentSysLink() + "&loginType=" + SysLinkForm.LOGINTYPE_GUEST_USER;
        }
        
        return null;
    }

    public String getDocumentOpenGuestUrl() throws DBException {
        if (getDocument() != null&&getDocument().getCategory().hasPermission(VKCUser.createGuestUser(), 'r')) {
            return WebShare.getRootURL() + WebShare.getContextPath() + getDocument().getDocumentOpenSysLink() + "&loginType=" + SysLinkForm.LOGINTYPE_GUEST_USER;
        }
        
        return null;
    }
}