/*
 * $Id: LoginLicenseForm.java,v 1.5 2007/10/29 08:31:42 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: LoginLicenseForm.java,v $
 * Revision 1.5  2007/10/29 08:31:42  marbobo
 * *** empty log message ***
 *
 * Revision 1.4  2007/10/25 15:02:36  korowa
 * *** empty log message ***
 *
 * Revision 1.3  2007/08/08 12:14:47  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 */
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.VKCLicense;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCLicenseSerializer;
import vkc.exception.DBException;
import vkc.view.WebShare;

public class LoginLicenseForm extends ActionForm {
    VKCLicense license = null;

    VKCUser user = null;

    private String agent = "";
    private String linkType = null;
    private String objectId = null;
    private String loginType = null;

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        license = null;
        user = (VKCUser) request.getSession().getAttribute(WebShare.TEMP_USER_KEY);

        agent = request.getHeader("user-agent").toLowerCase();
        linkType = null;
        objectId = null;
        loginType = 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 VKCLicense getLicense() throws DBException {
        if (license == null) {
            license = VKCLicenseSerializer.retrieveActiveLicense();
        }
        
        return license;
    }

    public VKCUser getUser() {
        return user;
    }

    public void setUser(VKCUser user) {
        this.user = user;
    }

    public String getLinkType() {
        return linkType;
    }

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

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public String getLoginType() {
        return loginType;
    }

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

    public boolean isSysLink() {
        return getLinkType() != null && getObjectId() != null;
    }

    public String getSysLink() {
        return "/sysLink.do?linkType=" + getLinkType() + "&objectId=" + getObjectId() + (getLoginType() != null ? "&loginType=" + getLoginType() : "");
    }
    
    public String getLicenseText() {
        try {
            if (getUser() == null || getLicense() == null) {
                return "";
            }
            
            return getLicense().getLicense(getUser().getLanguage().getLanguageId());
        } catch (DBException e) {
            return "";
        }
    }
	
	public boolean isTinyMCECompatible() {
		boolean isMSIE = agent.indexOf("msie") != -1;
		//boolean isMSIE5 = isMSIE && (agent.indexOf("msie 5") != -1);
		//boolean isMSIE5_0 = isMSIE && (agent.indexOf("msie 5.0") != -1);
		boolean isGecko = agent.indexOf("gecko") != -1;
		//boolean isSafari = agent.indexOf("safari") != -1;
		boolean isOpera = agent.indexOf("opera") != -1;
		//boolean isMac = agent.indexOf("mac") != -1;
		//boolean isNS7 = agent.indexOf("netscape/7") != -1;
		//boolean isNS71 = agent.indexOf("netscape/7.1") != -1;

		// Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those
		if (isOpera) {
			isMSIE = true;
			isGecko = false;
			//isSafari =  false;
		}
		
		if ((isMSIE && !isOpera) || isGecko) {
		    return true;
		}

		return false;
	}
}