1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.servlets.reports;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.Field;
30
31
32 import org.dbforms.taglib.TextFormatterUtil;
33 import org.dbforms.util.MessageResources;
34 import org.dbforms.util.ParseUtil;
35 import org.dbforms.util.Util;
36
37 import java.io.File;
38 import java.util.Map;
39
40 /***
41 * Helper class send as parameter to JasperReports. So it is not neccesary to
42 * send all the stuff in different parameters
43 */
44 import java.sql.Connection;
45
46 import java.util.Locale;
47
48 import javax.servlet.ServletContext;
49 import javax.servlet.http.HttpServletRequest;
50
51
52
53 /***
54 * DOCUMENT ME!
55 *
56 * @author $author$
57 * @version $Revision: 1.20 $
58 */
59 public class ReportParameter {
60 private static Log logCat = LogFactory.getLog(ReportParameter.class);
61 private Connection connection;
62 private HttpServletRequest request;
63 private Locale locale;
64 private ServletContext context;
65 private String reportPath;
66 private Map attributes;
67
68 /***
69 * Creates a new ReportParameter object.
70 *
71 * @param context DOCUMENT ME!
72 * @param request DOCUMENT ME!
73 * @param connection DOCUMENT ME!
74 * @param reportPath DOCUMENT ME!
75 */
76 public ReportParameter(ServletContext context,
77 HttpServletRequest request,
78 Map attributes,
79 Connection connection,
80 String reportPath) {
81 this.context = context;
82 this.request = request;
83 this.connection = connection;
84 this.reportPath = reportPath;
85 this.attributes = attributes;
86 this.locale = MessageResources.getLocale(request);
87 }
88
89 /***
90 * Returns the connection.
91 *
92 * @return Connection
93 */
94 public Connection getConnection() {
95 return connection;
96 }
97
98
99 /***
100 * DOCUMENT ME!
101 *
102 * @return
103 */
104 public String getContextPath() {
105 return context.getRealPath("") + File.separator;
106 }
107
108 public String getBaseURL() {
109 return Util.getBaseURL(request);
110 }
111
112 /***
113 * Returns a message
114 *
115 * @param msg DOCUMENT ME!
116 *
117 * @return String
118 */
119 public String getMessage(String msg) {
120 return MessageResources.getMessage(request, msg);
121 }
122
123
124 /***
125 * Returns a request parameter
126 *
127 * @param param DOCUMENT ME!
128 *
129 * @return String
130 */
131 public String getParameter(String param) {
132 return ParseUtil.getParameter(request, param);
133 }
134
135 /***
136 * Returns a request parameter
137 *
138 * @param param DOCUMENT ME!
139 *
140 * @return String
141 */
142 public Object getAttribute(String key) {
143 return attributes.get(key);
144 }
145
146 /***
147 * Returns the reportPath.
148 *
149 * @return String
150 */
151 public String getReportPath() {
152 return reportPath;
153 }
154
155
156 /***
157 * Returns a formatted string with the same formatting as used inside
158 * dbforms
159 *
160 * @param obj The object to format
161 * @param pattern to use as pattern for numeric and date fields
162 *
163 * @return The string representation
164 */
165 public String getStringValue(Object obj,
166 String pattern) {
167 try {
168 Field field = new Field();
169 field.setTypeByObject(obj);
170 return TextFormatterUtil.FormatText(field, locale, pattern, obj);
171 } catch (Exception e) {
172 logCat.error(e);
173
174 return e.getMessage();
175 }
176 }
177
178
179 /***
180 * Returns a formatted string with the same formatting as used inside
181 * dbforms
182 *
183 * @param obj The object to format
184 *
185 * @return The string representation
186 */
187 public String getStringValue(Object obj) {
188 return getStringValue(obj, null);
189 }
190 }