KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > util > WebUtils


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 package olstore.util;
28
29 import javax.servlet.http.HttpServletRequest;
30
31 import java.math.BigDecimal;
32 import java.text.DecimalFormat;
33
34 /**
35  * This class contains many helper functions for use through jsps.
36  */

37 public class WebUtils {
38     
39     public static final String ITEMURL = "/olstore/views/ItemView.jsp?itemPK=";
40     private static DecimalFormat format = new DecimalFormat ( "###,###,###.00" );
41     
42     /**
43      * Use this method when checking strings returned from the
44      * request, as these may be null or empty
45      */

46     public static boolean isEmpty ( String str ) {
47         if ( str == null || str.equals("") ) {
48             return true;
49         } else {
50             return false;
51         }
52     }
53     
54     /**
55      * Use this method to get a value from the request, it first checks
56      * if it is present as an attribute, this gets priority. If not, then
57      * it then checks to see if it is present as a parameter. This method
58      * will return null if the value is null or empty
59      */

60     public static String getValue ( String name, HttpServletRequest request ) {
61         String value = null;
62         value = (String) request.getAttribute ( name );
63         if ( ! isEmpty(value) ) {
64             return value;
65         }
66         value = (String) request.getParameter ( name );
67         if ( isEmpty(value) ) {
68             return null;
69         }
70         return value;
71     }
72     
73     public static String getItemURL ( String itemPK ) {
74         return ITEMURL + itemPK ;
75     }
76     
77     public static String getItemURL ( Integer itemPK ) {
78         return getItemURL ( itemPK.toString() );
79     }
80     
81     public static String getItemInterestURL ( Integer itemPK ) {
82         return getItemURL ( itemPK ) + "&interest=true";
83     }
84     
85     public static String getItemPurchaseURL ( Integer itemPK ) {
86         return getItemURL ( itemPK ) + "&purchase=true";
87     }
88     
89     public static String getItemEditURL ( String itemPK ) {
90         return "/olstore/admin/createItem.do?itemId="+itemPK ;
91     }
92     
93     public static String getItemEditURL ( Integer itemPK ) {
94         return getItemEditURL ( itemPK.toString() ) ;
95     }
96     
97     public static String getTypeViewURL ( String type ) {
98         return "/olstore/views/index.jsp?type="+type;
99     }
100     
101     // public static float floatFormat(float f){
102
// return (float) (((int) ((f + 0.005f)*100)))/100;
103
// }
104

105     // public static Float floatFormat(Float f){
106
// return new Float ( floatFormat ( f.floatValue() ) );
107
// }
108

109     public static String costDisplay (BigDecimal f){
110         return format.format ( f.doubleValue() );
111     }
112 }
113
Popular Tags