/**
 * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 * 
 * Initial Developers: Aizaz Ahmed
 *                     Vivek Lakshmanan
 *                     Andrew Overholt
 *                     Matthew Wringe
 *
 */
package olstore.util;

import javax.servlet.http.HttpServletRequest;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * This class contains many helper functions for use through jsps.
 */
public class WebUtils {
    
    public static final String ITEMURL = "/olstore/views/ItemView.jsp?itemPK=";
    private static DecimalFormat format = new DecimalFormat ( "###,###,###.00" );
    
    /**
     * Use this method when checking strings returned from the
     * request, as these may be null or empty
     */
    public static boolean isEmpty ( String str ) {
        if ( str == null || str.equals("") ) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * Use this method to get a value from the request, it first checks
     * if it is present as an attribute, this gets priority. If not, then
     * it then checks to see if it is present as a parameter. This method
     * will return null if the value is null or empty
     */
    public static String getValue ( String name, HttpServletRequest request ) {
        String value = null;
        value = (String) request.getAttribute ( name );
        if ( ! isEmpty(value) ) {
            return value;
        }
        value = (String) request.getParameter ( name );
        if ( isEmpty(value) ) {
            return null;
        }
        return value;
    }
    
    public static String getItemURL ( String itemPK ) {
        return ITEMURL + itemPK ;
    }
    
    public static String getItemURL ( Integer itemPK ) {
        return getItemURL ( itemPK.toString() );
    }
    
    public static String getItemInterestURL ( Integer itemPK ) {
        return getItemURL ( itemPK ) + "&interest=true";
    }
    
    public static String getItemPurchaseURL ( Integer itemPK ) {
        return getItemURL ( itemPK ) + "&purchase=true";
    }
    
    public static String getItemEditURL ( String itemPK ) {
        return "/olstore/admin/createItem.do?itemId="+itemPK ;
    }
    
    public static String getItemEditURL ( Integer itemPK ) {
        return getItemEditURL ( itemPK.toString() ) ;
    }
    
    public static String getTypeViewURL ( String type ) {
        return "/olstore/views/index.jsp?type="+type; 
    }
    
    //    public static float floatFormat(float f){
    //                return (float) (((int) ((f + 0.005f)*100)))/100;
    //    }
    
    //    public static Float floatFormat(Float f){
    //        return new Float ( floatFormat ( f.floatValue() ) );
    //    }
    
    public static String costDisplay (BigDecimal f){
        return format.format ( f.doubleValue() );
    }
}
