/*
 * $Id: SysLinkForm.java,v 1.12 2006/10/04 03:15:17 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: SysLinkForm.java,v $
 * Revision 1.12  2006/10/04 03:15:17  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.11  2006/02/27 16:15:56  marbobo
 * + Systemsichten hinzugefügt
 * + SysLinks angepasst
 * + Benutzer können falls gewünscht Dokumentqueries anlegen
 *
 * Revision 1.10  2005/09/01 13:11:52  marbobo
 * + Einige Anpassung zur SysLink
 *
 * Revision 1.9  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.8  2004/12/08 14:42:44  marbobo
 * + Bei Dokumentlinks wird zur aktuellsten Version verwiesen
 *
 * Revision 1.7  2004/06/27 17:39:28  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.6  2004/04/01 20:49:08  marbobo
 * + getVirtowebSysLink()
 *
 * Revision 1.5  2004/03/07 13:45:45  bigmac
 * Anpassungen an VKCDocumentTypeSerializer, VKCDocumentClassSerializer, VKCDocumentStatusSerializer
 *
 * Revision 1.4  2004/03/02 23:06:31  bigmac
 * Anpassungen an VKCDocumentSerializer und VKCCategorySerializer
 *
 * Revision 1.3  2004/02/18 15:05:33  marbobo
 * /sysLink.do anpassung
 *
 * Revision 1.2  2004/02/18 14:50:55  marbobo
 * /sysLink.do anpassung
 *
 * Revision 1.1  2004/02/18 01:17:21  marbobo
 * Initiale Version
 *
 */

package vkc.view.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.util.StringUtils;

/**
 * @author marbobo
 */
public final class SysLinkForm extends ActionForm {
	public static final String CATEGORY_LINK = "categoryLink";
	public static final String DOCUMENT_LINK = "documentLink";
	public static final String PROCOBA_FUNCTIONAL_LINK = "procobaFunctionalLink";
	public static final String PROCOBA_PROJECT_LINK = "procobaProjectLink";
	public static final String DOCUMENT_OPEN_LINK = "documentOpenLink";

	public static final String LOGINTYPE_GUEST_USER = "guestUserType" ;
	public static final String LOGINTYPE_NORMAL_USER = "normalUserType" ;

	private Integer objectId = null;
	
	private String linkType = null;
	private String loginType = LOGINTYPE_NORMAL_USER;

	/**
	 * 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) {
		if (request.getParameter("linkType") != null && request.getParameter("objectId") != null) {
			String lValue = request.getParameter("linkType");
			String oValue = request.getParameter("objectId");
			
			if (request.getParameter("loginType") != null) {  
			    loginType = request.getParameter("loginType");
			} else {
				loginType = LOGINTYPE_NORMAL_USER;
			}

			if (lValue.equals(CATEGORY_LINK)) {
				linkType = CATEGORY_LINK;
			} else if (lValue.equals(DOCUMENT_LINK)) {
				linkType = DOCUMENT_LINK;
			} else if (lValue.equals(PROCOBA_FUNCTIONAL_LINK)) {
				linkType = PROCOBA_FUNCTIONAL_LINK;
			} else if (lValue.equals(PROCOBA_PROJECT_LINK)) {
				linkType = PROCOBA_PROJECT_LINK;
			} else if (lValue.equals(DOCUMENT_OPEN_LINK)) {
			    linkType = DOCUMENT_OPEN_LINK;
			}

			try {
				objectId = Integer.valueOf(oValue);
			} catch (NumberFormatException e) {
				objectId = null;
			}
		}
	}

	/**
	 * Validierung der Eingabefelder
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
        
		return errors;
	}

	public String getLinkType() {
		return linkType;
	}

	public Integer getObjectId() {
		return objectId;
	}

	public void setLinkType(String string) {
		linkType = string;
	}

	public void setObjectId(Integer integer) {
		objectId = integer;
	}
	
	public boolean isValidSysLink() {
		if (getLinkType() != null && getObjectId() != null) {
			return true;
		} 

		return false;
	}
	
	public VKCCategory getCategory(Integer id) {
		try {
			return VKCCategorySerializer.retrieveCategory(id);
		} catch (DBException e) {
			return null;
		}
	}

	public VKCDocument getDocument(Integer id) {
		try {
			return VKCDocumentSerializer.retrieveDocument(id);
		} catch (DBException e) {
			return null;
		}
	}

	public VKCDocument getTopDocument(Integer id) {
		try {
			return VKCDocumentSerializer.retrieveDocument(VKCDocumentSerializer.getTopVersionDocId(id));
		} catch ( DBException e ) {
			return null;
		}
	}

	public static String getCategorySysLinkNoSlash(Integer id, String loginType) {
		if (id != null) {
		    if (!StringUtils.isEmpty(loginType)) {
		        return "sysLink.do?linkType=" + CATEGORY_LINK + "&objectId=" + id.intValue() + "&loginType=" + loginType;
		    }

		    return "sysLink.do?linkType=" + CATEGORY_LINK + "&objectId=" + id.intValue();
		} 

		return "";
	}

	public static String getCategorySysLink(Integer id, String loginType) {
		if (id != null) {
		    if (!StringUtils.isEmpty(loginType)) {
		        return "/sysLink.do?linkType=" + CATEGORY_LINK + "&objectId=" + id.intValue() + "&loginType=" + loginType;
		    }

		    return "/sysLink.do?linkType=" + CATEGORY_LINK + "&objectId=" + id.intValue();
		} 

		return "";
	}

	public static String getDocumentSysLinkNoSlash(Integer id, String loginType) {
		if (id != null) {
		    if (!StringUtils.isEmpty(loginType)) {
		        return "sysLink.do?linkType=" + DOCUMENT_LINK + "&objectId=" + id.intValue()+ "&loginType=" + loginType;
		    }
			
		    return "sysLink.do?linkType=" + DOCUMENT_LINK + "&objectId=" + id.intValue();
		} 

		return "";
	}

	public static String getDocumentSysLink(Integer id, String loginType) {
		if (id != null) {
		    if (!StringUtils.isEmpty(loginType)) {
		        return "/sysLink.do?linkType=" + DOCUMENT_LINK + "&objectId=" + id.intValue()+ "&loginType=" + loginType;
		    }
			
		    return "/sysLink.do?linkType=" + DOCUMENT_LINK + "&objectId=" + id.intValue();
		} 

		return "";
	}

	public static String getProcobaFunctionalSysLink() {
		return "/sysLink.do?linkType=" + PROCOBA_FUNCTIONAL_LINK + "&objectId=0";
	}
	
	public static String getProcobaProjectSysLink() {
		return "/sysLink.do?linkType=" + PROCOBA_PROJECT_LINK + "&objectId=0";
	}
	
	public static String getDocumentOpenSysLinkNoSlash(Integer id, String loginType){
	    if (id != null) {
		    if (!StringUtils.isEmpty(loginType)) {
		        return "sysLink.do?linkType=" + DOCUMENT_OPEN_LINK + "&objectId=" + id.intValue() + "&loginType=" + loginType;
		    }

		    return "sysLink.do?linkType=" + DOCUMENT_OPEN_LINK + "&objectId=" + id.intValue();
		} 
	    
	    return "";  
	}

	public static String getDocumentOpenSysLink(Integer id, String loginType){
	    if (id != null) {
		    if (!StringUtils.isEmpty(loginType)) {
		        return "/sysLink.do?linkType=" + DOCUMENT_OPEN_LINK + "&objectId=" + id.intValue() + "&loginType=" + loginType;
		    }

		    return "/sysLink.do?linkType=" + DOCUMENT_OPEN_LINK + "&objectId=" + id.intValue();
		} 
	    
	    return "";  
	}

	public String getLoginType() {
        return loginType;
    }
    
	public void setLoginType(String loginType) {
        this.loginType = loginType;
    }
}