/*
 * $Id: BasicActionForm.java,v 1.13 2008/07/23 07:03:51 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: BasicActionForm.java,v $
 * Revision 1.13  2008/07/23 07:03:51  marbobo
 * + Erkennung der Browser-Agenten optimiert
 *
 * Revision 1.12  2008/07/22 12:11:07  korowa
 * TinyMCE auf neueste Version gebracht. Ab jetzt werden zusätzlich Opera(ab 9.5) und Safari (ab 3.*) unterstützt. Neue Funktion "Fullscreen" hinzugefügt.
 *
 * Revision 1.11  2006/05/24 17:56:17  marbobo
 * + Toolbar des Html-Editors kann optional angezeigt oder verborgen werden
 *
 * Revision 1.10  2006/05/16 02:04:09  marbobo
 * + isTinyMCECompatible() hinzugefügt
 *
 * Revision 1.9  2004/07/20 08:53:51  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.view.form;

import java.text.ParseException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import vkc.db.entities.VKCUser;
import vkc.util.DateUtils;
import vkc.util.ServletUtils;
import vkc.util.StringUtils;
import vkc.view.InputFieldConstraints;

public class BasicActionForm extends ActionForm {
    private boolean          showTinyMCEToolbar = false;

    private Locale           locale             = null;

    private MessageResources resources          = null;

    private String           agent              = "";

    private VKCUser          user               = null;

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        super.reset(mapping, request);
        showTinyMCEToolbar = false;

        locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);
        resources = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
        agent = request.getHeader("user-agent").toLowerCase();
        user = ServletUtils.getUser(request);
    }

    /**
     * Method returns true if the string argument is null or its trimmed
     * <p>
     */
    public static final boolean isEmpty(String s) {
        if (s == null) {
            return true;
        }

        return StringUtils.normalize(s).length() == 0;
    }

    /**
     * Method returns true if the argument is a parsable String representation
     * of a date value
     * <p>
     * @param s - String representation of a date value
     */
    public static final boolean isParsableDate(String d) {
        return DateUtils.parseDate(d) != null;
    }

    /**
     * Method returns true if the argument is a parsable String representation
     * of a double value
     * <p>
     * @param s - String representation of an double value
     */
    public static final boolean isParsableDouble(String s) {
        try {
            StringUtils.parseDouble(s, Locale.GERMANY);

            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * Method returns true if the argument is a parsable String representation
     * of a double value and its value is greater zero
     * <p>
     * @param s - String representation of a double value
     */
    public static final boolean isPositiveDouble(String s) {
        try {
            return StringUtils.parseDouble(s, Locale.GERMANY) > 0;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * Method returns true if the argument is a parsable String representation
     * of a integer number
     * <p>
     * @param s - String representation of a integer number
     */
    public static final boolean isParsableInteger(String s) {
        try {
            Integer.parseInt(s);

            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * Method returns true if the argument is a parsable String representation
     * of a integer number and its value is greater zero
     * <p>
     * @param s - String representation of a integer number
     */
    public static final boolean isPositiveInteger(String s) {
        try {
            return Integer.parseInt(s) > 0;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * Method returns true if the length of the string argument conforms
     * to the denoted field length constraint
     * <p>
     */
    public static final boolean isLengthValid(String value, String fieldConstraint) {
        return InputFieldConstraints.isLengthValid(value, fieldConstraint);
    }

    public MessageResources getResources() {
        return resources;
    }

    public Locale getLocale() {
        return locale;
    }

    public VKCUser getUser() {
        return user;
    }

    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;

        boolean isOperaCorrectVersion = false;
        boolean isSafariCorrectVersion = false;

        String version = "";
        Double operaVersion = null;
        Integer safariVersion = null;

        //opera wird ab version 9.5 unterstützt
        if (isOpera) {
            if (agent.indexOf("opera/") != -1) {
                version = agent.substring(agent.indexOf("opera/") + "opera/".length());
                version = version.substring(0, version.indexOf(" "));

                try {
                    operaVersion = Double.valueOf(version);
                } catch (Exception e) {
                    operaVersion = new Double(9.0);
                }

                isOperaCorrectVersion = operaVersion.doubleValue() < 9.5 ? false : true;
            }

            // Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those
            isMSIE = false;
            isGecko = false;
            isSafari = false;
        }

        // safari wird ab version 3 unterstützt
        if (isSafari) {
            if (agent.indexOf("version/") != -1) {
                version = agent.substring(agent.indexOf("version/") + "version/".length());
                version = version.substring(0, version.indexOf("."));

                try {
                    safariVersion = Integer.valueOf(version);
                } catch (Exception e) {
                    safariVersion = new Integer(2);
                }

                isSafariCorrectVersion = safariVersion.intValue() < 3 ? false : true;
            }

            isMSIE = false;
            isGecko = false;
            isSafari = false;
        }

        if (isMSIE || (isOpera && isOperaCorrectVersion) || isGecko || (isSafari && isSafariCorrectVersion)) {
            return true;
        }

        return false;
    }

    public boolean isShowTinyMCEToolbar() {
        return showTinyMCEToolbar;
    }

    public void setShowTinyMCEToolbar(boolean showTinyMCEToolbar) {
        this.showTinyMCEToolbar = showTinyMCEToolbar;
    }
}