1 23 24 package org.dbforms.servlets.reports; 25 26 import org.apache.commons.beanutils.PropertyUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 31 import org.dbforms.util.Util; 32 import org.dbforms.util.external.FileUtil; 33 34 import java.io.BufferedReader ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.File ; 37 import java.io.FileReader ; 38 import java.io.OutputStream ; 39 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletException ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import javax.servlet.http.HttpSession ; 45 46 75 public abstract class LineReportServletAbstract extends ReportServletAbstract { 76 private static Log logCat = LogFactory 77 .getLog(LineReportServletAbstract.class); 78 79 protected abstract String getMimeType(); 80 81 protected abstract String getFileExtension(); 82 83 protected abstract void writeData(Object [] data) throws Exception ; 84 protected abstract void openStream(OutputStream out) throws Exception ; 85 protected abstract void closeStream(OutputStream out) throws Exception ; 86 87 private static final String REPORTMIMETYPEPARAM = "reportMimeType"; 88 89 private String mimeType = getMimeType(); 90 91 private int rownum = 0; 92 93 public void init() throws ServletException { 94 super.init(); 95 String value = getServletConfig().getInitParameter(REPORTMIMETYPEPARAM); 96 if (!Util.isNull(value)) { 97 mimeType = value; 98 } 99 } 100 101 protected String getReportFileExtension() { 102 return ".xr"; 103 } 104 105 protected void writeHeader(String [] header) throws Exception { 106 writeData(header); 107 } 108 109 private Object getFieldValue(HttpServletRequest request, 110 JRDataSourceAbstract dataSource, String search) { 111 Object o = null; 112 search = search.replaceAll("__", "."); 113 114 if (search.startsWith("internal.")) { 115 search = search.substring(search.indexOf("internal.")); 116 logCat.debug("Trying to find data for internal value: " + search); 117 o = getInternalValue(search); 118 } else if (search.startsWith("request.")) { 119 search = search.substring(search.indexOf("request.")); 120 logCat.debug("Trying to find data for page context value: " 121 + search); 122 o = getRequestValue(request, search); 123 } else if (search.startsWith("session.")) { 124 search = search.substring(search.indexOf("session.")); 125 logCat.debug("Trying to find data for session value: " + search); 126 o = getSessionValue(request.getSession(), search); 127 } else { 128 logCat.debug("Trying to find data for field named: " + search); 129 o = dataSource.getFieldValue(search); 130 } 131 return o; 132 133 } 134 135 private Object getInternalValue(String search) { 136 Object obj = null; 137 try { 138 logCat.debug("Trying to find data for internal var : " + search); 139 if (search.equalsIgnoreCase("rownum")) { 140 return new Integer (rownum); 141 } 142 } catch (Exception e) { 143 logCat.error("getInternalValue: " + e); 144 } 145 146 return obj; 147 } 148 149 private Object getRequestValue(HttpServletRequest request, String search) { 150 Object obj = null; 151 int pos; 152 try { 153 pos = search.indexOf("."); 154 if (pos == -1) { 155 obj = request.getAttribute(search); 157 } else { 158 String search_bean = search.substring(0, pos); 161 search = search.substring(pos + 1); 162 Object bean = request.getAttribute(search_bean); 163 if (bean != null) { 164 logCat.debug("calling PropertyUtils.getProperty " + search_bean 165 + " " + search); 166 obj = PropertyUtils.getProperty(bean, search); 167 } 168 } 169 } catch (Exception e) { 170 logCat.error("getPageContextValue: " + e); 171 } 172 173 return obj; 174 } 175 176 private Object getSessionValue(HttpSession session, String search) { 177 Object obj = null; 178 int pos; 179 try { 180 pos = search.indexOf("."); 181 if (pos == -1) { 182 obj = session.getAttribute(search); 184 } else { 185 String search_bean = search.substring(0, pos); 187 search = search.substring(pos + 1); 188 Object bean = session.getAttribute(search_bean); 189 if (bean != null) { 190 logCat.debug("calling PropertyUtils.getProperty " + search_bean 191 + " " + search); 192 obj = PropertyUtils.getProperty(bean, search); 193 } 194 } 195 } catch (Exception e) { 196 logCat.error("getSessionValue: " + e); 197 } 198 199 return obj; 200 } 201 202 private ReportWriter fillReport(HttpServletRequest request, 203 String [] header, String [] fields, JRDataSourceAbstract dataSource) 204 throws Exception { 205 ReportWriter res = new ReportWriter(); 206 res.mimeType = mimeType; 207 res.data = new ByteArrayOutputStream (); 208 res.fileName = getFileExtension(); 209 openStream(res.data); 210 writeHeader(header); 211 Object [] data = new Object [fields.length]; 213 while (dataSource.next()) { 214 rownum++; 215 for (int i = 0; i < fields.length; i++) { 216 data[i] = getFieldValue(request, dataSource, fields[i]); 217 } 218 writeData(data); 219 } 220 closeStream(res.data); 221 return res; 222 } 223 224 241 protected ReportWriter processReport(String reportFileFullName, 242 JRDataSourceAbstract dataSource, ServletContext context, 243 HttpServletRequest request, HttpServletResponse response) { 244 245 try { 246 File f = new File (reportFileFullName + getReportFileExtension()); 247 BufferedReader in = new BufferedReader (new FileReader (f)); 248 String line1 = in.readLine(); 249 String line2 = in.readLine(); 250 String fields = null; 251 String headers = null; 252 if (Util.isNull(line2)) { 253 fields = line1; 254 } else { 255 headers = line1; 256 fields = line2; 257 } 258 if (Util.isNull(fields)) { 259 logCat.error("no fields found"); 260 return null; 261 } 262 String [] reportFields = fields.split(","); 263 String [] headerFields; 264 if (headers != null) { 265 headerFields = headers.split(","); 266 } else { 267 headerFields = new String [] {}; 268 } 269 if (reportFields.length != headerFields.length) { 270 logCat.error("reportFields.length != headerFields.length"); 271 headerFields = reportFields; 272 } 273 if (reportFields.length == 0) { 274 logCat.error("no fields found"); 275 return null; 276 } 277 278 ReportWriter res = fillReport(request, headerFields, reportFields, 279 dataSource); 280 if (res != null) { 281 StringBuffer buf = new StringBuffer (); 282 buf.append(FileUtil.filename(reportFileFullName)); 283 buf.append(res.fileName); 284 res.fileName = buf.toString(); 285 } 286 return res; 287 } catch (Exception e) { 288 logCat.error("read report file", e); 289 handleException(request, response, e); 290 return null; 291 } 292 } 293 294 295 } 296 | Popular Tags |