/*
 * $Id: ShowRecentNewsForm.java,v 1.5 2006/05/16 16:13:44 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ShowRecentNewsForm.java,v $
 * Revision 1.5  2006/05/16 16:13:44  marbobo
 * + isTinyMCECompatible() hinzugefügt
 *
 * Revision 1.4  2005/09/01 13:11:52  marbobo
 * + Einige Anpassung zur SysLink
 *
 * Revision 1.3  2004/07/23 14:55:13  jonyp
 * changed method signature retrieveNews(int) to retrieveNews()
 *
 * Revision 1.2  2004/07/22 22:11:26  marbobo
 * + Notwendige Methoden implementiert
 *
 * Revision 1.1  2004/07/22 13:18:22  marbobo
 * + Initiale Version
 *
 */

package vkc.news.form;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

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.VKCNews;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCNewsSerializer;
import vkc.exception.DBException;
import vkc.view.WebShare;


/**
 * Form bean für die aktuellen News Seite
 * @author marbobo
 */

public final class ShowRecentNewsForm extends ActionForm {
    private String agent = "";
    private String linkType = null;
    private String objectId = null;
    private String loginType = null;

    private VKCUser user = 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) {
        agent = request.getHeader("user-agent").toLowerCase();
   		linkType = null;
   		objectId = null;
   	    loginType = null;
   		
   		user = (VKCUser) request.getSession().getAttribute(WebShare.TEMP_USER_KEY);
    }

    /**
     * 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 String getObjectId() {
		return objectId;
	}
	
	public List getRecentNews() throws DBException {
	    List news = VKCNewsSerializer.retrieveNews();
	    List recentNews = new ArrayList();

	    if (getUser().getMostRecentNews() != null) {
	        for (int i = 0; i < news.size(); i++) {
	            VKCNews n = (VKCNews)news.get(i);
	            
	            if (n.getValidFrom() != null && n.getValidUntil() != null) {
	                Timestamp now = new Timestamp(System.currentTimeMillis());
	                boolean b1 = now.after(n.getValidFrom());
	                boolean b2 = now.before(n.getValidUntil());
	                
	                if (b1 && b2 && getUser().getMostRecentNews().before(n.getValidFrom())) {
	                    recentNews.add(n);
	                }
	            } else if (n.getValidFrom() == null && n.getValidUntil() != null) {
	                Timestamp now = new Timestamp(System.currentTimeMillis());
	                boolean b1 = now.after(n.getCreated());
	                boolean b2 = now.before(n.getValidUntil());
	                
	                if (b1 && b2 && getUser().getMostRecentNews().before(n.getCreated())) {
	                    recentNews.add(n);
	                }
	            } else if (n.getValidFrom() != null) {
	                Timestamp now = new Timestamp(System.currentTimeMillis());
	                boolean b1 = now.after(n.getValidFrom());
	                
	                if (b1 && getUser().getMostRecentNews().before(n.getValidFrom())) {
	                    recentNews.add(n);
	                }
	            } else if (getUser().getMostRecentNews().before(n.getCreated())) {
                    recentNews.add(n);
	            }
	        }
	    } else {
	        for (int i = 0; i < news.size(); i++) {
	            VKCNews n = (VKCNews)news.get(i);
	            
	            if (n.getValidFrom() != null && n.getValidUntil() != null) {
	                Timestamp now = new Timestamp(System.currentTimeMillis());
	                boolean b1 = now.after(n.getValidFrom());
	                boolean b2 = now.before(n.getValidUntil());
	                
	                if (b1 && b2) {
	                    recentNews.add(n);
	                }
	            } else if (n.getValidFrom() == null && n.getValidUntil() != null) {
	                Timestamp now = new Timestamp(System.currentTimeMillis());
	                boolean b1 = now.after(n.getCreated());
	                boolean b2 = now.before(n.getValidUntil());
	                
	                if (b1 && b2) {
	                    recentNews.add(n);
	                }
	            } else if (n.getValidFrom() != null) {
	                Timestamp now = new Timestamp(System.currentTimeMillis());
	                boolean b1 = now.after(n.getValidFrom());
	                
	                if (b1) {
	                    recentNews.add(n);
	                }
	            } else {
                    recentNews.add(n);
	            }
	        }
	    }

        return recentNews;
	}

	public String getSysLink() {
		return "/sysLink.do?linkType=" + getLinkType() + "&objectId=" + getObjectId() + (getLoginType() != null ? "&loginType=" + getLoginType() : "");
	}

    public VKCUser getUser() {
        return user;
    }

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

	public void setObjectId(String string) {
		objectId = string;
	}

    public void setUser(VKCUser u) {
        user = u;
    }

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

	public String getLoginType() {
		return loginType;
	}

	public void setLoginType(String loginType) {
		this.loginType = loginType;
	}
	
	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;
	}
}