/*
Copyright (C) 2000-2010  Ministere de la culture et de la communication (France), AJLSM
See LICENCE file
*/
package fr.gouv.culture.oai.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.avalon.framework.logger.Logger;

import fr.gouv.culture.sdx.exception.SDXException;
import fr.gouv.culture.sdx.utils.Utilities;

public class OAIUtilities {
	
	/**
	 * Checks string validity.
	 * <p>Checks string validity, ie the String must not be null or empty. 
	 * </p>
	 * @param The String to checks 
	 * @return	<code>false</code> if the String is null or empty, 
	 * <code>true</code> if not.
	 */
	public static boolean checkString(String s){
		if(s==null || "".equals(s) || "".equals(s.trim()))
			return false;
		else return true;
	}
    
    /**Verifies the super.getLog() and logs a message
     *
     * @param logger    The super.getLog()
     * @param s         The message
     */
    public static void logInfo(Logger logger, String s) {
        if (logger != null && Utilities.checkString(s)){
            logger.info(s);
        }
    }
    public static void logInfo(Logger logger, int code, String[] arguments) {
    	if (null != logger && logger.isInfoEnabled()) {
        	logger.info( new SDXException(code, arguments).getMessage() );
		}
    }

    /**Verifies the super.getLog() and logs a message
     *
     * @param logger    The super.getLog()
     * @param s         The message (maybe null)
     */
    public static void logDebug(Logger logger, String s) {
    	logDebug(logger, s, null);
    }
    /**Verifies the super.getLog() and logs a message
    *
    * @param logger    The super.getLog()
    * @param s         The message (maybe null)
    * @param e         The exception (maybe null)
    */
    public static void logDebug(Logger logger, String s, Exception e) {
        if (logger != null) {
            if (Utilities.checkString(s)) {
                if (e != null) {
                    logger.debug(s, e);
                } else
                    logger.debug(s);
            } else if (e != null && (e.getMessage() != null & e.fillInStackTrace() != null)) {
                logger.debug(e.getMessage(), e.fillInStackTrace());
            }
        }
    }
    public static void logDebug(Logger logger, int code, String[] arguments) {
    	if (null != logger && logger.isDebugEnabled()) {
        	logger.debug( new SDXException(code, arguments).getMessage() );
		}
    }

    /**Verifies the super.getLog() and logs a message
     *
     * @param logger    The super.getLog()
     * @param s         The message (maybe null)
     */
    public static void logError(Logger logger, String s) {
    	logError(logger, s, null);
    }

    /**Verifies the super.getLog() and logs a message
    *
    * @param logger    The super.getLog()
    * @param s         The message (maybe null)
    * @param e         The exception (maybe null)
    */
    public static void logError(Logger logger, String s, Exception e) {
        if (logger != null) {
            if (Utilities.checkString(s)) {
                if (e != null) {
                    logger.error(s, e);
                } else
                    logger.error(s);
            } else if (e != null && (e.getMessage() != null & e.fillInStackTrace() != null)) {
                logger.error(e.getMessage(), e.fillInStackTrace());
            }
        }
    }
    public static void logError(Logger logger, int code, String[] arguments) {
    	if (null != logger && logger.isErrorEnabled()) {
        	logger.error( new SDXException(code, arguments).getMessage() );
		}
    }

    /**Verifies the super.getLog() and logs a message
     *
     * @param logger    The super.getLog()
     * @param s         The message (maybe null)
     */
    public static void logWarn(Logger logger, String s) {
    	logWarn(logger, s, null);
    }
   
    /**Verifies the super.getLog() and logs a message
    *
    * @param logger    The super.getLog()
    * @param s         The message (maybe null)
    * @param e         The exception (maybe null)
    */
    public static void logWarn(Logger logger, String s, Exception e) {
        if (logger != null) {
            if (Utilities.checkString(s)) {
                if (e != null) {
                    logger.warn(s, e);
                } else
                    logger.warn(s);
            } else if (e != null)
                logger.warn(e.getMessage(), e);

        }
    }
    public static void logWarn(Logger logger, int code, String[] arguments) {
    	if (null != logger && logger.isWarnEnabled()) {
        	logger.warn( new SDXException(code, arguments).getMessage() );
		}
    }

    /**Verifies the super.getLog() and logs an exception
     *
     * @param logger    The super.getLog()
     * @param e         The exception (maybe null)
     */
    public static void logException(Logger logger, Exception e) {
        if (logger != null && e != null && (e.getMessage() != null && e.fillInStackTrace() != null))
            logger.error(e.getMessage(), e.fillInStackTrace());
    }

    public static class Date {
        public static String formatDate(java.util.Date date) {
            // return formatDate(date, "yyyy/MM/dd HH:mm:ss");
            return formatUtcISO8601Date(date);
        }

        public static String formatUtcISO8601Date(java.util.Date date) {
            return formatDate(date, "yyyy-MM-dd'T'HH:mm:ss") + "Z";
        }

        public static java.util.Date getUtcIso8601Date() {
            GregorianCalendar cal = new GregorianCalendar();
            java.util.Date theDate = null;
            int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
            // Convert to UTC
            theDate = new java.util.Date(cal.getTime().getTime() - offset);
            return theDate;
        }

        /** Formats a date according to the specified format.
         *
         * @param date The date to format.
         * @param format The desired format.
         * @return The formated date.
         */
        public static String formatDate(java.util.Date date, String format) {
            //TODO : next method would return null for an invalid date ?! -pb
            if (date == null || format == null) return "";

            SimpleDateFormat df = new SimpleDateFormat(format);
            try {
                return df.format(date);
            } catch (Exception e) {
                return "";
            }
        }
    }

    //TODO:why is this happening
    public static String normalizeHttpRequestParameterName(String origName) {
        String prefix = "amp;";
        if (origName == null || !origName.startsWith(prefix))
            return origName;
        else
            return origName.substring(prefix.length());

    }


}
