FR [ 2581145 ] Jasper: Provide parameters info

* better exception handling
* improve JavaDoc
* use org.compiere.util.DB instead of DBUtils and deprecate DBUtils
* Fix NPE on getReportFile() when reportFile is null
This commit is contained in:
teo_sarca 2009-02-09 11:45:54 +00:00
parent 71ee570659
commit 9ffa44de88
2 changed files with 111 additions and 71 deletions

View File

@ -21,6 +21,7 @@ import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -32,7 +33,6 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -40,6 +40,7 @@ import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.PropertyResourceBundle; import java.util.PropertyResourceBundle;
import java.util.logging.Level;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
@ -53,11 +54,13 @@ import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.engine.util.JRLoader;
import org.adempiere.exceptions.DBException;
import org.compiere.db.CConnection; import org.compiere.db.CConnection;
import org.compiere.interfaces.MD5; import org.compiere.interfaces.MD5;
import org.compiere.model.MAttachment; import org.compiere.model.MAttachment;
import org.compiere.model.MAttachmentEntry; import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MProcess; import org.compiere.model.MProcess;
import org.compiere.model.X_AD_PInstance_Para;
import org.compiere.process.ClientProcess; import org.compiere.process.ClientProcess;
import org.compiere.process.ProcessCall; import org.compiere.process.ProcessCall;
import org.compiere.process.ProcessInfo; import org.compiere.process.ProcessInfo;
@ -68,7 +71,7 @@ import org.compiere.util.Env;
import org.compiere.util.Ini; import org.compiere.util.Ini;
import org.compiere.util.Language; import org.compiere.util.Language;
import org.compiere.util.Trx; import org.compiere.util.Trx;
import org.compiere.utils.DBUtils; import org.compiere.util.Util;
import org.compiere.utils.DigestOfFile; import org.compiere.utils.DigestOfFile;
/** /**
@ -80,9 +83,12 @@ import org.compiere.utils.DigestOfFile;
* @author Ashley Ramdass * @author Ashley Ramdass
* @author victor.perez@e-evolution.com * @author victor.perez@e-evolution.com
* @see FR 1906632 http://sourceforge.net/tracker/?func=detail&atid=879335&aid=1906632&group_id=176962 * @see FR 1906632 http://sourceforge.net/tracker/?func=detail&atid=879335&aid=1906632&group_id=176962
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>FR [ 2581145 ] Jasper: Provide parameters info
*/ */
public class ReportStarter implements ProcessCall, ClientProcess { public class ReportStarter implements ProcessCall, ClientProcess
//logger {
/** Logger */
private static CLogger log = CLogger.getCLogger(ReportStarter.class); private static CLogger log = CLogger.getCLogger(ReportStarter.class);
private static File REPORT_HOME = null; private static File REPORT_HOME = null;
@ -348,13 +354,13 @@ public class ReportStarter implements ProcessCall, ClientProcess {
* It should only return false, if the function could not be performed * It should only return false, if the function could not be performed
* as this causes the process to abort. * as this causes the process to abort.
* @author rlemeill * @author rlemeill
* @param ctx context * @param ctx context
* @param pi Compiere standard process info * @param pi standard process info
* @param trx * @param trx
* @return true if success * @return true if success
*/ */
public boolean startProcess(Properties ctx, ProcessInfo pi, Trx trx) { public boolean startProcess(Properties ctx, ProcessInfo pi, Trx trx)
{
processInfo = pi; processInfo = pi;
String Name=pi.getTitle(); String Name=pi.getTitle();
int AD_PInstance_ID=pi.getAD_PInstance_ID(); int AD_PInstance_ID=pi.getAD_PInstance_ID();
@ -364,15 +370,16 @@ public class ReportStarter implements ProcessCall, ClientProcess {
if (trx != null) { if (trx != null) {
trxName = trx.getTrxName(); trxName = trx.getTrxName();
} }
ReportData reportData = getReportData( pi, trxName); ReportData reportData = getReportData(pi, trxName);
if (reportData==null) { if (reportData == null) {
reportResult( AD_PInstance_ID, "Can not find report data", trxName); reportResult(AD_PInstance_ID, "Can not find report data", trxName);
return false; return false;
} }
String reportPath = reportData.getReportFilePath(); String reportPath = reportData.getReportFilePath();
if ((reportPath==null)||(reportPath.length()==0)) { if (Util.isEmpty(reportPath, true))
reportResult( AD_PInstance_ID, "Can not find report", trxName); {
reportResult(AD_PInstance_ID, "Can not find report", trxName);
return false; return false;
} }
@ -381,19 +388,18 @@ public class ReportStarter implements ProcessCall, ClientProcess {
String fileExtension = ""; String fileExtension = "";
HashMap<String, Object> params = new HashMap<String, Object>(); HashMap<String, Object> params = new HashMap<String, Object>();
addProcessParameters( AD_PInstance_ID, params, trxName); addProcessParameters(AD_PInstance_ID, params, trxName);
addProcessInfoParameters(params, pi.getParameter()); addProcessInfoParameters(params, pi.getParameter());
reportFile = getReportFile(reportPath, (String)params.get("ReportType")); reportFile = getReportFile(reportPath, (String)params.get("ReportType"));
if (reportFile == null || reportFile.exists() == false) if (reportFile == null || reportFile.exists() == false)
{ {
log.severe("No report file found for given type, falling back to " + reportPath); log.severe("No report file found for given type, falling back to " + reportPath);
reportFile = getReportFile(reportPath); reportFile = getReportFile(reportPath);
} }
if (reportFile == null || reportFile.exists() == false) { if (reportFile == null || reportFile.exists() == false)
{
String tmp = "Can not find report file at path - " + reportPath; String tmp = "Can not find report file at path - " + reportPath;
log.severe(tmp); log.severe(tmp);
reportResult(AD_PInstance_ID, tmp, trxName); reportResult(AD_PInstance_ID, tmp, trxName);
@ -658,7 +664,8 @@ public class ReportStarter implements ProcessCall, ClientProcess {
* @param reportPath * @param reportPath
* @return the abstract file corresponding to report * @return the abstract file corresponding to report
*/ */
protected File getReportFile(String reportPath) { protected File getReportFile(String reportPath)
{
File reportFile = null; File reportFile = null;
// Reports deployment on web server Thanks to Alin Vaida // Reports deployment on web server Thanks to Alin Vaida
@ -688,7 +695,10 @@ public class ReportStarter implements ProcessCall, ClientProcess {
} }
// Set org.compiere.report.path because it is used in reports which refer to subreports // Set org.compiere.report.path because it is used in reports which refer to subreports
System.setProperty("org.compiere.report.path", reportFile.getParentFile().getAbsolutePath()); if (reportFile != null)
{
System.setProperty("org.compiere.report.path", reportFile.getParentFile().getAbsolutePath());
}
return reportFile; return reportFile;
} }
@ -779,25 +789,17 @@ public class ReportStarter implements ProcessCall, ClientProcess {
} }
/** /**
* Update AD_PInstance result and error message
* @author rlemeill * @author rlemeill
* @param AD_PInstance_ID * @param AD_PInstance_ID
* @param errMsg * @param errMsg error message or null if there is no error
*/ */
protected void reportResult( int AD_PInstance_ID, String errMsg, String trxName) { protected void reportResult(int AD_PInstance_ID, String errMsg, String trxName)
int result = (errMsg==null)?1:0; {
errMsg = (errMsg==null)?"":errMsg; int result = (errMsg == null ? 1 : 0);
String sql = "UPDATE AD_PInstance SET result="+result+", errormsg='"+errMsg+"' "+ String sql = "UPDATE AD_PInstance SET Result=?, ErrorMsg=?"
"WHERE AD_PInstance_ID="+AD_PInstance_ID; +" WHERE AD_PInstance_ID="+AD_PInstance_ID;
Statement pstmt = null; DB.executeUpdateEx(sql, new Object[]{result, errMsg}, trxName);
try {
pstmt = DB.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, trxName);
pstmt.executeUpdate(sql);
pstmt.close();
} catch (SQLException e) {
log.severe(sql+e.getMessage());
} finally {
DBUtils.close( pstmt);
}
} }
/** /**
@ -835,30 +837,38 @@ public class ReportStarter implements ProcessCall, ClientProcess {
return new JasperData( jasperReport, reportDir, jasperName, jasperFile); return new JasperData( jasperReport, reportDir, jasperName, jasperFile);
} }
/**
protected void addProcessParameters( int AD_PInstance_ID, Map<String, Object> params, String trxName) { * Load Process Parameters into given params map
log.info(""); * @param AD_PInstance_ID
String sql = "SELECT ParameterName, "+ * @param params
"P_String, "+ * @param trxName
"P_String_To, "+ */
"P_Number, "+ private static void addProcessParameters(int AD_PInstance_ID, Map<String, Object> params, String trxName)
"P_Number_To, "+ {
"P_Date, "+ final String sql = "SELECT "
"P_Date_To "+ +" "+X_AD_PInstance_Para.COLUMNNAME_ParameterName
"FROM AD_PInstance_Para "+ +","+X_AD_PInstance_Para.COLUMNNAME_P_String
"WHERE AD_PInstance_ID=?"; +","+X_AD_PInstance_Para.COLUMNNAME_P_String_To
+","+X_AD_PInstance_Para.COLUMNNAME_P_Number
+","+X_AD_PInstance_Para.COLUMNNAME_P_Number_To
+","+X_AD_PInstance_Para.COLUMNNAME_P_Date
+","+X_AD_PInstance_Para.COLUMNNAME_P_Date_To
+","+X_AD_PInstance_Para.COLUMNNAME_Info
+","+X_AD_PInstance_Para.COLUMNNAME_Info_To
+" FROM "+X_AD_PInstance_Para.Table_Name
+" WHERE "+X_AD_PInstance_Para.COLUMNNAME_AD_PInstance_ID+"=?";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try
{
pstmt = DB.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, trxName); pstmt = DB.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, trxName);
pstmt.setInt(1, AD_PInstance_ID); pstmt.setInt(1, AD_PInstance_ID);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next())
{
String name = rs.getString(1); String name = rs.getString(1);
String pStr = rs.getString(2); String pStr = rs.getString(2);
String pStrTo = rs.getString(3); String pStrTo = rs.getString(3);
//Double pNum = new Double( rs.getDouble(4));
//Double pNumTo = new Double( rs.getDouble(5));
BigDecimal pNum = rs.getBigDecimal(4); BigDecimal pNum = rs.getBigDecimal(4);
BigDecimal pNumTo = rs.getBigDecimal(5); BigDecimal pNumTo = rs.getBigDecimal(5);
@ -879,23 +889,35 @@ public class ReportStarter implements ProcessCall, ClientProcess {
params.put( name, pDate); params.put( name, pDate);
} }
} else if (pNum != null) { } else if (pNum != null) {
if (rs.getBigDecimal(5)!=null) { if (pNumTo!=null) {
params.put( name+"1", pNum); params.put( name+"1", pNum);
params.put( name+"2", pNumTo); params.put( name+"2", pNumTo);
} else { } else {
params.put( name, pNum); params.put( name, pNum);
} }
} }
//
// Add parameter info - teo_sarca FR [ 2581145 ]
String info = rs.getString(8);
String infoTo = rs.getString(9);
params.put(name+"_Info1", (info != null ? info : ""));
params.put(name+"_Info2", (infoTo != null ? infoTo : ""));
} }
} catch (SQLException e) { }
log.severe("Execption; sql = "+sql+"; e.getMessage() = " +e.getMessage()); catch (SQLException e)
} finally { {
DBUtils.close( rs); // log.severe("Execption; sql = "+sql+"; e.getMessage() = " +e.getMessage());
DBUtils.close( pstmt); throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
} }
private void addProcessInfoParameters(Map<String, Object> params, ProcessInfoParameter[] para) { private void addProcessInfoParameters(Map<String, Object> params, ProcessInfoParameter[] para)
{
if (para != null) { if (para != null) {
for (int i = 0; i < para.length; i++) { for (int i = 0; i < para.length; i++) {
if (para[i].getParameter_To() == null) { if (para[i].getParameter_To() == null) {
@ -981,7 +1003,8 @@ public class ReportStarter implements ProcessCall, ClientProcess {
* @param jasperFile * @param jasperFile
* @return compiled JasperReport * @return compiled JasperReport
*/ */
private JasperReport compileReport( File reportFile, File jasperFile) { private JasperReport compileReport( File reportFile, File jasperFile)
{
JWScorrectClassPath(); JWScorrectClassPath();
JasperReport compiledJasperReport = null; JasperReport compiledJasperReport = null;
try { try {
@ -989,7 +1012,7 @@ public class ReportStarter implements ProcessCall, ClientProcess {
jasperFile.setLastModified( reportFile.lastModified()); //Synchronize Dates jasperFile.setLastModified( reportFile.lastModified()); //Synchronize Dates
compiledJasperReport = (JasperReport)JRLoader.loadObject(jasperFile); compiledJasperReport = (JasperReport)JRLoader.loadObject(jasperFile);
} catch (JRException e) { } catch (JRException e) {
log.severe("JRException; e.getMessage()= "+ e.getMessage()); log.log(Level.SEVERE, "Error", e);
} }
return compiledJasperReport; return compiledJasperReport;
} }
@ -997,9 +1020,10 @@ public class ReportStarter implements ProcessCall, ClientProcess {
/** /**
* @author rlemeill * @author rlemeill
* @param ProcessInfo * @param ProcessInfo
* @return ReportData * @return ReportData or null if no data found
*/ */
public ReportData getReportData( ProcessInfo pi, String trxName) { public ReportData getReportData (ProcessInfo pi, String trxName)
{
log.info(""); log.info("");
String sql = "SELECT pr.JasperReport, pr.IsDirectPrint " String sql = "SELECT pr.JasperReport, pr.IsDirectPrint "
+ "FROM AD_Process pr, AD_PInstance pi " + "FROM AD_Process pr, AD_PInstance pi "
@ -1007,7 +1031,8 @@ public class ReportStarter implements ProcessCall, ClientProcess {
+ " AND pi.AD_PInstance_ID=?"; + " AND pi.AD_PInstance_ID=?";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try
{
pstmt = DB.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, trxName); pstmt = DB.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, trxName);
pstmt.setInt(1, pi.getAD_PInstance_ID()); pstmt.setInt(1, pi.getAD_PInstance_ID());
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
@ -1026,12 +1051,17 @@ public class ReportStarter implements ProcessCall, ClientProcess {
} }
return new ReportData( path, directPrint); return new ReportData( path, directPrint);
} catch (SQLException e) { }
log.severe("sql = "+sql+"; e.getMessage() = "+ e.getMessage()); catch (SQLException e)
return null; {
} finally { throw new DBException(e, sql);
DBUtils.close( rs); // log.severe("sql = "+sql+"; e.getMessage() = "+ e.getMessage());
DBUtils.close( pstmt); // return null;
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
} }
@ -1071,8 +1101,14 @@ public class ReportStarter implements ProcessCall, ClientProcess {
} }
} }
class JasperData { public static class JasperData
private JasperReport jasperReport; implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 4375195020654531202L;
private JasperReport jasperReport;
private File reportDir; private File reportDir;
private String jasperName; private String jasperName;
private File jasperFile; private File jasperFile;

View File

@ -14,6 +14,10 @@ import java.sql.Types;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
/**
*
* @deprecated please use {@link DB} class instead
*/
public class DBUtils { public class DBUtils {
static { static {
try { try {