001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.util;
027:
028: import javax.servlet.http.HttpServletRequest;
029:
030: import java.math.BigDecimal;
031: import java.text.DecimalFormat;
032:
033: /**
034: * This class contains many helper functions for use through jsps.
035: */
036: public class WebUtils {
037:
038: public static final String ITEMURL = "/olstore/views/ItemView.jsp?itemPK=";
039: private static DecimalFormat format = new DecimalFormat(
040: "###,###,###.00");
041:
042: /**
043: * Use this method when checking strings returned from the
044: * request, as these may be null or empty
045: */
046: public static boolean isEmpty(String str) {
047: if (str == null || str.equals("")) {
048: return true;
049: } else {
050: return false;
051: }
052: }
053:
054: /**
055: * Use this method to get a value from the request, it first checks
056: * if it is present as an attribute, this gets priority. If not, then
057: * it then checks to see if it is present as a parameter. This method
058: * will return null if the value is null or empty
059: */
060: public static String getValue(String name,
061: HttpServletRequest request) {
062: String value = null;
063: value = (String) request.getAttribute(name);
064: if (!isEmpty(value)) {
065: return value;
066: }
067: value = (String) request.getParameter(name);
068: if (isEmpty(value)) {
069: return null;
070: }
071: return value;
072: }
073:
074: public static String getItemURL(String itemPK) {
075: return ITEMURL + itemPK;
076: }
077:
078: public static String getItemURL(Integer itemPK) {
079: return getItemURL(itemPK.toString());
080: }
081:
082: public static String getItemInterestURL(Integer itemPK) {
083: return getItemURL(itemPK) + "&interest=true";
084: }
085:
086: public static String getItemPurchaseURL(Integer itemPK) {
087: return getItemURL(itemPK) + "&purchase=true";
088: }
089:
090: public static String getItemEditURL(String itemPK) {
091: return "/olstore/admin/createItem.do?itemId=" + itemPK;
092: }
093:
094: public static String getItemEditURL(Integer itemPK) {
095: return getItemEditURL(itemPK.toString());
096: }
097:
098: public static String getTypeViewURL(String type) {
099: return "/olstore/views/index.jsp?type=" + type;
100: }
101:
102: // public static float floatFormat(float f){
103: // return (float) (((int) ((f + 0.005f)*100)))/100;
104: // }
105:
106: // public static Float floatFormat(Float f){
107: // return new Float ( floatFormat ( f.floatValue() ) );
108: // }
109:
110: public static String costDisplay(BigDecimal f) {
111: return format.format(f.doubleValue());
112: }
113: }
|