1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.dbforms;
25
26 import java.util.Hashtable;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpSession;
31
32 import org.apache.log4j.Category;
33
34 import com.pow2.util.ServletUtil;
35
36
37 /***
38 * Utility class for DBForms applications.
39 *
40 * @author Luca Fossato
41 */
42 public class DbFormsUtil
43 {
44 /*** Log4j category */
45 private static Category cat = Category.getInstance(DbFormsUtil.class);
46
47
48 /***
49 * Build an hashTable object used to position the DbForms recordset
50 * to the record whose key match the hashTable tableId value
51 * <br>
52 * See <code>dbform::gotoHt</code> attribute for further details.
53 *
54 * @param request the HttpRequest object
55 * @param tableId the table single key field name
56 * @param id the request attribute/parameter name to use as the table key
57 * @return the hashTable object used to position the DbForm recordset
58 * to the record whose key match the hashTable tableId value
59 */
60 public static Hashtable getHt(HttpServletRequest request, String tableId, String id)
61 {
62 Hashtable ht = null;
63 String idVal = null;
64
65
66 if ((idVal = ServletUtil.getRequestAttribute(request, id)) != null)
67 {
68 ht = new Hashtable();
69 ht.put(tableId, idVal);
70 cat.debug("::getHt - [" + id + "] parameter value = [" + idVal + "]");
71 }
72
73 return ht;
74 }
75
76
77 /***
78 * Build an hashTable object used to position the DbForms recordset
79 * to the record whose key match the hashTable tableId value
80 * <br>
81 * See <code>dbform::gotoHt</code> attribute for further details.
82 *
83 * @param session the HttpSession object
84 * @param tableId the table single key field name
85 * @param id the request attribute/parameter name to use as the table key
86 * @return the hashTable object used to position the DbForm recordset
87 * to the record whose key match the hashTable tableId value
88 */
89 public static Hashtable getHt(HttpSession session, String tableId, String id)
90 {
91 Hashtable ht = null;
92 String idVal = null;
93
94
95 if ((idVal = (String)session.getAttribute(id)) != null)
96 {
97 ht = new Hashtable();
98 ht.put(tableId, idVal);
99 cat.debug("::getHt - [" + id + "] parameter value = [" + idVal + "]");
100 }
101
102 return ht;
103 }
104
105
106 /***
107 * Get the value of the field contained into the input map object.
108 *
109 * @param map the map containing the field object
110 * @param field the field to check
111 * @param defaultValue the default value to return if the map or its field are null
112 * @return the value of the field contained into the input map, or defaultValue
113 * if the input map or its field are null
114 */
115 public static String getValue(Map map, String field, String defaultValue)
116 {
117 String val = getValue(map, field);
118
119 if (val == null)
120 {
121 val = defaultValue;
122 }
123
124 return val;
125 }
126
127
128 /***
129 * Get the value of the field contained into the input mapS.
130 *
131 * @param map the map containing the field object
132 * @param field the field to check
133 * @return the value of the field contained into the input map
134 */
135 public static String getValue(Map map, String field)
136 {
137 String val = null;
138
139 if ((map != null) && map.containsKey(field))
140 {
141 val = (String)map.get(field);
142 }
143
144 return val;
145 }
146 }