temp pentaho parameter added
This commit is contained in:
+449
-359
@@ -18,6 +18,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import javax.sql.rowset.CachedRowSet;
|
import javax.sql.rowset.CachedRowSet;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.StreamingOutput;
|
import javax.ws.rs.core.StreamingOutput;
|
||||||
@@ -25,6 +26,7 @@ import javax.ws.rs.core.StreamingOutput;
|
|||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
|
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
|
||||||
import org.mifosplatform.infrastructure.core.service.FileUtils;
|
import org.mifosplatform.infrastructure.core.service.FileUtils;
|
||||||
|
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
|
||||||
import org.mifosplatform.infrastructure.core.service.ThreadLocalContextUtil;
|
import org.mifosplatform.infrastructure.core.service.ThreadLocalContextUtil;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
|
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.data.ResultsetColumnHeader;
|
import org.mifosplatform.infrastructure.dataqueries.data.ResultsetColumnHeader;
|
||||||
@@ -58,363 +60,451 @@ import com.lowagie.text.pdf.PdfWriter;
|
|||||||
@Service
|
@Service
|
||||||
public class ReadReportingServiceImpl implements ReadReportingService {
|
public class ReadReportingServiceImpl implements ReadReportingService {
|
||||||
|
|
||||||
private final PlatformSecurityContext context;
|
private final DataSource dataSource;
|
||||||
|
private final PlatformSecurityContext context;
|
||||||
private final static Logger logger = LoggerFactory.getLogger(ReadReportingServiceImpl.class);
|
|
||||||
|
private final static Logger logger = LoggerFactory
|
||||||
private Boolean noPentaho = false;
|
.getLogger(ReadReportingServiceImpl.class);
|
||||||
|
|
||||||
@Autowired
|
private Boolean noPentaho = false;
|
||||||
public ReadReportingServiceImpl(final PlatformSecurityContext context) {
|
|
||||||
// kick off pentaho reports server
|
@Autowired
|
||||||
ClassicEngineBoot.getInstance().start();
|
public ReadReportingServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource) {
|
||||||
noPentaho = false;
|
// kick off pentaho reports server
|
||||||
|
ClassicEngineBoot.getInstance().start();
|
||||||
this.context = context;
|
noPentaho = false;
|
||||||
}
|
|
||||||
|
this.context = context;
|
||||||
@Autowired
|
this.dataSource = dataSource;
|
||||||
private GenericDataService genericDataService;
|
}
|
||||||
|
|
||||||
@Override
|
@Autowired
|
||||||
public StreamingOutput retrieveReportCSV(final String name, final String type, final Map<String, String> queryParams) {
|
private GenericDataService genericDataService;
|
||||||
|
|
||||||
return new StreamingOutput() {
|
@Override
|
||||||
|
public StreamingOutput retrieveReportCSV(final String name,
|
||||||
@Override
|
final String type, final Map<String, String> queryParams) {
|
||||||
public void write(final OutputStream out) {
|
|
||||||
try {
|
return new StreamingOutput() {
|
||||||
|
|
||||||
GenericResultsetData result = retrieveGenericResultset(name, type, queryParams);
|
@Override
|
||||||
StringBuffer sb = generateCsvFileBuffer(result);
|
public void write(final OutputStream out) {
|
||||||
|
try {
|
||||||
InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
|
|
||||||
|
GenericResultsetData result = retrieveGenericResultset(
|
||||||
byte[] outputByte = new byte[4096];
|
name, type, queryParams);
|
||||||
Integer readLen = in.read(outputByte, 0, 4096);
|
StringBuffer sb = generateCsvFileBuffer(result);
|
||||||
|
|
||||||
while (readLen != -1) {
|
InputStream in = new ByteArrayInputStream(sb.toString()
|
||||||
out.write(outputByte, 0, readLen);
|
.getBytes("UTF-8"));
|
||||||
readLen = in.read(outputByte, 0, 4096);
|
|
||||||
}
|
byte[] outputByte = new byte[4096];
|
||||||
// in.close();
|
Integer readLen = in.read(outputByte, 0, 4096);
|
||||||
// out.flush();
|
|
||||||
// out.close();
|
while (readLen != -1) {
|
||||||
} catch (Exception e) {
|
out.write(outputByte, 0, readLen);
|
||||||
throw new PlatformDataIntegrityException("error.msg.exception.error", e.getMessage());
|
readLen = in.read(outputByte, 0, 4096);
|
||||||
}
|
}
|
||||||
}
|
// in.close();
|
||||||
};
|
// out.flush();
|
||||||
|
// out.close();
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
private StringBuffer generateCsvFileBuffer(final GenericResultsetData result) {
|
"error.msg.exception.error", e.getMessage());
|
||||||
StringBuffer writer = new StringBuffer();
|
}
|
||||||
|
}
|
||||||
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
};
|
||||||
logger.info("NO. of Columns: " + columnHeaders.size());
|
|
||||||
Integer chSize = columnHeaders.size();
|
}
|
||||||
for (int i = 0; i < chSize; i++) {
|
|
||||||
writer.append('"' + columnHeaders.get(i).getColumnName() + '"');
|
private StringBuffer generateCsvFileBuffer(final GenericResultsetData result) {
|
||||||
if (i < (chSize - 1)) writer.append(",");
|
StringBuffer writer = new StringBuffer();
|
||||||
}
|
|
||||||
writer.append('\n');
|
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
||||||
|
logger.info("NO. of Columns: " + columnHeaders.size());
|
||||||
List<ResultsetDataRow> data = result.getData();
|
Integer chSize = columnHeaders.size();
|
||||||
List<String> row;
|
for (int i = 0; i < chSize; i++) {
|
||||||
Integer rSize;
|
writer.append('"' + columnHeaders.get(i).getColumnName() + '"');
|
||||||
// String currCol;
|
if (i < (chSize - 1))
|
||||||
String currColType;
|
writer.append(",");
|
||||||
String currVal;
|
}
|
||||||
String doubleQuote = "\"";
|
writer.append('\n');
|
||||||
String twoDoubleQuotes = doubleQuote + doubleQuote;
|
|
||||||
logger.info("NO. of Rows: " + data.size());
|
List<ResultsetDataRow> data = result.getData();
|
||||||
for (int i = 0; i < data.size(); i++) {
|
List<String> row;
|
||||||
row = data.get(i).getRow();
|
Integer rSize;
|
||||||
rSize = row.size();
|
// String currCol;
|
||||||
for (int j = 0; j < rSize; j++) {
|
String currColType;
|
||||||
// currCol = columnHeaders.get(j).getColumnName();
|
String currVal;
|
||||||
currColType = columnHeaders.get(j).getColumnType();
|
String doubleQuote = "\"";
|
||||||
currVal = row.get(j);
|
String twoDoubleQuotes = doubleQuote + doubleQuote;
|
||||||
if (currVal != null) {
|
logger.info("NO. of Rows: " + data.size());
|
||||||
if (currColType.equals("DECIMAL") || currColType.equals("DOUBLE") || currColType.equals("BIGINT")
|
for (int i = 0; i < data.size(); i++) {
|
||||||
|| currColType.equals("SMALLINT") || currColType.equals("INT"))
|
row = data.get(i).getRow();
|
||||||
writer.append(currVal);
|
rSize = row.size();
|
||||||
else
|
for (int j = 0; j < rSize; j++) {
|
||||||
writer.append('"' + genericDataService.replace(currVal, doubleQuote, twoDoubleQuotes) + '"');
|
// currCol = columnHeaders.get(j).getColumnName();
|
||||||
|
currColType = columnHeaders.get(j).getColumnType();
|
||||||
}
|
currVal = row.get(j);
|
||||||
if (j < (rSize - 1)) writer.append(",");
|
if (currVal != null) {
|
||||||
}
|
if (currColType.equals("DECIMAL")
|
||||||
writer.append('\n');
|
|| currColType.equals("DOUBLE")
|
||||||
}
|
|| currColType.equals("BIGINT")
|
||||||
|
|| currColType.equals("SMALLINT")
|
||||||
return writer;
|
|| currColType.equals("INT"))
|
||||||
}
|
writer.append(currVal);
|
||||||
|
else
|
||||||
@Override
|
writer.append('"' + genericDataService.replace(currVal,
|
||||||
public GenericResultsetData retrieveGenericResultset(final String name, final String type, final Map<String, String> queryParams) {
|
doubleQuote, twoDoubleQuotes) + '"');
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
}
|
||||||
logger.info("STARTING REPORT: " + name + " Type: " + type);
|
if (j < (rSize - 1))
|
||||||
|
writer.append(",");
|
||||||
String sql;
|
}
|
||||||
if (name.equals(".")) {
|
writer.append('\n');
|
||||||
// this is to support api /reports - which isn't an important
|
}
|
||||||
// call. It isn't used in the default reporting UI. But there is a
|
|
||||||
// need to provide an api that does bring back 'permitted' reports
|
return writer;
|
||||||
// PERMITTED REPORTS SQL
|
}
|
||||||
sql = "select r.report_id, r.report_name, r.report_type, r.report_subtype, r.report_category,"
|
|
||||||
+ " rp.parameter_id, rp.report_parameter_name, p.parameter_name" + " from stretchy_report r"
|
@Override
|
||||||
+ " left join stretchy_report_parameter rp on rp.report_id = r.report_id"
|
public GenericResultsetData retrieveGenericResultset(final String name,
|
||||||
+ " left join stretchy_parameter p on p.parameter_id = rp.parameter_id" + " where exists" + " (select 'f'"
|
final String type, final Map<String, String> queryParams) {
|
||||||
+ " from m_appuser_role ur " + " join m_role r on r.id = ur.role_id"
|
|
||||||
+ " left join m_role_permission rp on rp.role_id = r.id" + " left join m_permission p on p.id = rp.permission_id"
|
long startTime = System.currentTimeMillis();
|
||||||
+ " where ur.appuser_id = " + context.authenticatedUser().getId()
|
logger.info("STARTING REPORT: " + name + " Type: " + type);
|
||||||
+ " and (p.code in ('ALL_FUNCTIONS', 'ALL_FUNCTIONS_READ') or p.code = concat('READ_', r.report_name))) "
|
|
||||||
+ " order by r.report_name, rp.parameter_id";
|
String sql;
|
||||||
} else {
|
if (name.equals(".")) {
|
||||||
sql = getSQLtoRun(name, type, queryParams);
|
// this is to support api /reports - which isn't an important
|
||||||
}
|
// call. It isn't used in the default reporting UI. But there is a
|
||||||
|
// need to provide an api that does bring back 'permitted' reports
|
||||||
GenericResultsetData result = genericDataService.fillGenericResultSet(sql);
|
// PERMITTED REPORTS SQL
|
||||||
|
sql = "select r.report_id, r.report_name, r.report_type, r.report_subtype, r.report_category,"
|
||||||
long elapsed = System.currentTimeMillis() - startTime;
|
+ " rp.parameter_id, rp.report_parameter_name, p.parameter_name"
|
||||||
logger.info("FINISHING Report/Request Name: " + name + " - " + type + " Elapsed Time: " + elapsed);
|
+ " from stretchy_report r"
|
||||||
return result;
|
+ " left join stretchy_report_parameter rp on rp.report_id = r.report_id"
|
||||||
}
|
+ " left join stretchy_parameter p on p.parameter_id = rp.parameter_id"
|
||||||
|
+ " where exists"
|
||||||
private String getSQLtoRun(final String name, final String type, final Map<String, String> queryParams) {
|
+ " (select 'f'"
|
||||||
|
+ " from m_appuser_role ur "
|
||||||
String sql = getSql(name, type);
|
+ " join m_role r on r.id = ur.role_id"
|
||||||
|
+ " left join m_role_permission rp on rp.role_id = r.id"
|
||||||
Set<String> keys = queryParams.keySet();
|
+ " left join m_permission p on p.id = rp.permission_id"
|
||||||
for (String key : keys) {
|
+ " where ur.appuser_id = "
|
||||||
String pValue = queryParams.get(key);
|
+ context.authenticatedUser().getId()
|
||||||
// logger.info("(" + key + " : " + pValue + ")");
|
+ " and (p.code in ('ALL_FUNCTIONS', 'ALL_FUNCTIONS_READ') or p.code = concat('READ_', r.report_name))) "
|
||||||
sql = genericDataService.replace(sql, key, pValue);
|
+ " order by r.report_name, rp.parameter_id";
|
||||||
}
|
} else {
|
||||||
|
sql = getSQLtoRun(name, type, queryParams);
|
||||||
AppUser currentUser = context.authenticatedUser();
|
}
|
||||||
// Allows sql query to restrict data by office hierarchy if required
|
|
||||||
sql = genericDataService.replace(sql, "${currentUserHierarchy}", currentUser.getOffice().getHierarchy());
|
GenericResultsetData result = genericDataService
|
||||||
// Allows sql query to restrict data by current user Id if required
|
.fillGenericResultSet(sql);
|
||||||
// (typically used to return report lists containing only reports
|
|
||||||
// permitted to be run by the user
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
sql = genericDataService.replace(sql, "${currentUserId}", currentUser.getId().toString());
|
logger.info("FINISHING Report/Request Name: " + name + " - " + type
|
||||||
|
+ " Elapsed Time: " + elapsed);
|
||||||
sql = genericDataService.wrapSQL(sql);
|
return result;
|
||||||
|
}
|
||||||
return sql;
|
|
||||||
|
private String getSQLtoRun(final String name, final String type,
|
||||||
}
|
final Map<String, String> queryParams) {
|
||||||
|
|
||||||
private String getSql(final String name, final String type) {
|
String sql = getSql(name, type);
|
||||||
|
|
||||||
String inputSql = "select " + type + "_sql as the_sql from stretchy_" + type + " where " + type + "_name = '" + name + "'";
|
Set<String> keys = queryParams.keySet();
|
||||||
inputSql = genericDataService.wrapSQL(inputSql);
|
for (String key : keys) {
|
||||||
|
String pValue = queryParams.get(key);
|
||||||
String sqlErrorMsg = "Sql: " + inputSql;
|
// logger.info("(" + key + " : " + pValue + ")");
|
||||||
CachedRowSet rs = genericDataService.getCachedResultSet(inputSql, sqlErrorMsg);
|
sql = genericDataService.replace(sql, key, pValue);
|
||||||
|
}
|
||||||
try {
|
|
||||||
if (rs.next()) { return rs.getString("the_sql"); }
|
AppUser currentUser = context.authenticatedUser();
|
||||||
throw new ReportNotFoundException(inputSql);
|
// Allows sql query to restrict data by office hierarchy if required
|
||||||
|
sql = genericDataService.replace(sql, "${currentUserHierarchy}",
|
||||||
} catch (SQLException e) {
|
currentUser.getOffice().getHierarchy());
|
||||||
logger.info("msg: " + e.getMessage() + " sql: " + inputSql);
|
// Allows sql query to restrict data by current user Id if required
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error", e.getMessage(), sqlErrorMsg);
|
// (typically used to return report lists containing only reports
|
||||||
}
|
// permitted to be run by the user
|
||||||
|
sql = genericDataService.replace(sql, "${currentUserId}", currentUser
|
||||||
}
|
.getId().toString());
|
||||||
|
|
||||||
@Override
|
sql = genericDataService.wrapSQL(sql);
|
||||||
public String getReportType(final String reportName) {
|
|
||||||
String sql = "SELECT ifnull(report_type,'') as report_type FROM `stretchy_report` where report_name = '" + reportName + "'";
|
return sql;
|
||||||
|
|
||||||
sql = genericDataService.wrapSQL(sql);
|
}
|
||||||
|
|
||||||
String sqlErrorMsg = "Report Name: " + reportName + " Sql: " + sql;
|
private String getSql(final String name, final String type) {
|
||||||
CachedRowSet rs = genericDataService.getCachedResultSet(sql, sqlErrorMsg);
|
|
||||||
|
String inputSql = "select " + type + "_sql as the_sql from stretchy_"
|
||||||
try {
|
+ type + " where " + type + "_name = '" + name + "'";
|
||||||
|
inputSql = genericDataService.wrapSQL(inputSql);
|
||||||
if (rs.next()) { return rs.getString("report_type"); }
|
|
||||||
throw new ReportNotFoundException(sql);
|
String sqlErrorMsg = "Sql: " + inputSql;
|
||||||
|
CachedRowSet rs = genericDataService.getCachedResultSet(inputSql,
|
||||||
} catch (SQLException e) {
|
sqlErrorMsg);
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error", e.getMessage(), sqlErrorMsg);
|
|
||||||
}
|
try {
|
||||||
|
if (rs.next()) {
|
||||||
}
|
return rs.getString("the_sql");
|
||||||
|
}
|
||||||
@Override
|
throw new ReportNotFoundException(inputSql);
|
||||||
public Response processPentahoRequest(final String reportName, final String outputTypeParam, final Map<String, String> queryParams) {
|
|
||||||
|
} catch (SQLException e) {
|
||||||
String outputType = "HTML";
|
logger.info("msg: " + e.getMessage() + " sql: " + inputSql);
|
||||||
if (StringUtils.isNotBlank(outputTypeParam)) outputType = outputTypeParam;
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
|
e.getMessage(), sqlErrorMsg);
|
||||||
if (!(outputType.equalsIgnoreCase("HTML") || outputType.equalsIgnoreCase("PDF") || outputType.equalsIgnoreCase("XLS") || outputType
|
}
|
||||||
.equalsIgnoreCase("CSV")))
|
|
||||||
throw new PlatformDataIntegrityException("error.msg.invalid.outputType", "No matching Output Type: " + outputType);
|
}
|
||||||
|
|
||||||
if (noPentaho)
|
@Override
|
||||||
throw new PlatformDataIntegrityException("error.msg.no.pentaho", "Pentaho is not enabled", "Pentaho is not enabled");
|
public String getReportType(final String reportName) {
|
||||||
|
String sql = "SELECT ifnull(report_type,'') as report_type FROM `stretchy_report` where report_name = '"
|
||||||
// TODO - JW - use pentaho location finder like Pawel does in Mifos
|
+ reportName + "'";
|
||||||
// String reportPath =
|
|
||||||
// "C:\\dev\\apache-tomcat-7.0.25\\webapps\\ROOT\\PentahoReports\\"
|
sql = genericDataService.wrapSQL(sql);
|
||||||
// + reportName + ".prpt";
|
|
||||||
String reportPath = FileUtils.MIFOSX_BASE_DIR + File.separator
|
String sqlErrorMsg = "Report Name: " + reportName + " Sql: " + sql;
|
||||||
+ ThreadLocalContextUtil.getTenant().getName().replaceAll(" ", "").trim() + File.separator + "reports" + File.separator
|
CachedRowSet rs = genericDataService.getCachedResultSet(sql,
|
||||||
+ reportName + ".prpt";
|
sqlErrorMsg);
|
||||||
logger.info("Report path: " + reportPath);
|
|
||||||
|
try {
|
||||||
// load report definition
|
|
||||||
ResourceManager manager = new ResourceManager();
|
if (rs.next()) {
|
||||||
manager.registerDefaults();
|
return rs.getString("report_type");
|
||||||
Resource res;
|
}
|
||||||
|
throw new ReportNotFoundException(sql);
|
||||||
try {
|
|
||||||
res = manager.createDirectly(reportPath, MasterReport.class);
|
} catch (SQLException e) {
|
||||||
MasterReport masterReport = (MasterReport) res.getResource();
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
|
e.getMessage(), sqlErrorMsg);
|
||||||
addParametersToReport(masterReport, queryParams);
|
}
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
}
|
||||||
|
|
||||||
if ("PDF".equalsIgnoreCase(outputType)) {
|
@Override
|
||||||
PdfReportUtil.createPDF(masterReport, baos);
|
public Response processPentahoRequest(final String reportName,
|
||||||
return Response.ok().entity(baos.toByteArray()).type("application/pdf").build();
|
final String outputTypeParam, final Map<String, String> queryParams) {
|
||||||
}
|
|
||||||
|
String outputType = "HTML";
|
||||||
if ("XLS".equalsIgnoreCase(outputType)) {
|
if (StringUtils.isNotBlank(outputTypeParam))
|
||||||
ExcelReportUtil.createXLS(masterReport, baos);
|
outputType = outputTypeParam;
|
||||||
return Response.ok().entity(baos.toByteArray()).type("application/vnd.ms-excel")
|
|
||||||
.header("Content-Disposition", "attachment;filename=" + reportName.replaceAll(" ", "") + ".xls").build();
|
if (!(outputType.equalsIgnoreCase("HTML")
|
||||||
}
|
|| outputType.equalsIgnoreCase("PDF")
|
||||||
|
|| outputType.equalsIgnoreCase("XLS") || outputType
|
||||||
if ("CSV".equalsIgnoreCase(outputType)) {
|
.equalsIgnoreCase("CSV")))
|
||||||
CSVReportUtil.createCSV(masterReport, baos, "UTF-8");
|
throw new PlatformDataIntegrityException(
|
||||||
return Response.ok().entity(baos.toByteArray()).type("application/x-msdownload")
|
"error.msg.invalid.outputType", "No matching Output Type: "
|
||||||
.header("Content-Disposition", "attachment;filename=" + reportName.replaceAll(" ", "") + ".csv").build();
|
+ outputType);
|
||||||
}
|
|
||||||
|
if (noPentaho)
|
||||||
if ("HTML".equalsIgnoreCase(outputType)) {
|
throw new PlatformDataIntegrityException("error.msg.no.pentaho",
|
||||||
HtmlReportUtil.createStreamHTML(masterReport, baos);
|
"Pentaho is not enabled", "Pentaho is not enabled");
|
||||||
return Response.ok().entity(baos.toByteArray()).type("text/html").build();
|
|
||||||
}
|
// TODO - JW - use pentaho location finder like Pawel does in Mifos
|
||||||
} catch (ResourceException e) {
|
// String reportPath =
|
||||||
throw new PlatformDataIntegrityException("error.msg.reporting.error", e.getMessage());
|
// "C:\\dev\\apache-tomcat-7.0.25\\webapps\\ROOT\\PentahoReports\\"
|
||||||
} catch (ReportProcessingException e) {
|
// + reportName + ".prpt";
|
||||||
throw new PlatformDataIntegrityException("error.msg.reporting.error", e.getMessage());
|
String reportPath = FileUtils.MIFOSX_BASE_DIR
|
||||||
} catch (IOException e) {
|
+ File.separator
|
||||||
throw new PlatformDataIntegrityException("error.msg.reporting.error", e.getMessage());
|
+ ThreadLocalContextUtil.getTenant().getName()
|
||||||
}
|
.replaceAll(" ", "").trim() + File.separator
|
||||||
|
+ "reports" + File.separator + reportName + ".prpt";
|
||||||
throw new PlatformDataIntegrityException("error.msg.invalid.outputType", "No matching Output Type: " + outputType);
|
logger.info("Report path: " + reportPath);
|
||||||
|
|
||||||
}
|
// load report definition
|
||||||
|
ResourceManager manager = new ResourceManager();
|
||||||
private void addParametersToReport(final MasterReport report, final Map<String, String> queryParams) {
|
manager.registerDefaults();
|
||||||
|
Resource res;
|
||||||
try {
|
|
||||||
ReportParameterValues rptParamValues = report.getParameterValues();
|
try {
|
||||||
ReportParameterDefinition paramsDefinition = report.getParameterDefinition();
|
res = manager.createDirectly(reportPath, MasterReport.class);
|
||||||
|
MasterReport masterReport = (MasterReport) res.getResource();
|
||||||
/*
|
|
||||||
* only allow integer, long, date and string parameter types and assume all
|
addParametersToReport(masterReport, queryParams);
|
||||||
* mandatory - could go more detailed like Pawel did in Mifos later
|
|
||||||
* and could match incoming and pentaho parameters better...
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
* currently assuming they come in ok... and if not an error
|
|
||||||
*/
|
if ("PDF".equalsIgnoreCase(outputType)) {
|
||||||
for (ParameterDefinitionEntry paramDefEntry : paramsDefinition.getParameterDefinitions()) {
|
PdfReportUtil.createPDF(masterReport, baos);
|
||||||
String paramName = paramDefEntry.getName();
|
return Response.ok().entity(baos.toByteArray())
|
||||||
String pValue = queryParams.get(paramName);
|
.type("application/pdf").build();
|
||||||
if (StringUtils.isBlank(pValue))
|
}
|
||||||
throw new PlatformDataIntegrityException("error.msg.reporting.error", "Pentaho Parameter: " + paramName
|
|
||||||
+ " - not Provided");
|
if ("XLS".equalsIgnoreCase(outputType)) {
|
||||||
|
ExcelReportUtil.createXLS(masterReport, baos);
|
||||||
Class<?> clazz = paramDefEntry.getValueType();
|
return Response
|
||||||
logger.info("addParametersToReport(" + paramName + " : " + pValue + " : " + clazz.getCanonicalName() + ")");
|
.ok()
|
||||||
if (clazz.getCanonicalName().equalsIgnoreCase("java.lang.Integer"))
|
.entity(baos.toByteArray())
|
||||||
rptParamValues.put(paramName, Integer.parseInt(pValue));
|
.type("application/vnd.ms-excel")
|
||||||
else if (clazz.getCanonicalName().equalsIgnoreCase("java.lang.Long"))
|
.header("Content-Disposition",
|
||||||
rptParamValues.put(paramName, Long.parseLong(pValue));
|
"attachment;filename="
|
||||||
else if (clazz.getCanonicalName().equalsIgnoreCase("java.sql.Date"))
|
+ reportName.replaceAll(" ", "")
|
||||||
rptParamValues.put(paramName, Date.valueOf(pValue));
|
+ ".xls").build();
|
||||||
else
|
}
|
||||||
rptParamValues.put(paramName, pValue);
|
|
||||||
}
|
if ("CSV".equalsIgnoreCase(outputType)) {
|
||||||
|
CSVReportUtil.createCSV(masterReport, baos, "UTF-8");
|
||||||
} catch (Exception e) {
|
return Response
|
||||||
throw new PlatformDataIntegrityException("error.msg.reporting.error", e.getMessage());
|
.ok()
|
||||||
}
|
.entity(baos.toByteArray())
|
||||||
}
|
.type("application/x-msdownload")
|
||||||
|
.header("Content-Disposition",
|
||||||
@Override
|
"attachment;filename="
|
||||||
public String retrieveReportPDF(final String reportName, final String type, final Map<String, String> queryParams) {
|
+ reportName.replaceAll(" ", "")
|
||||||
|
+ ".csv").build();
|
||||||
String fileLocation = FileUtils.MIFOSX_BASE_DIR + File.separator + "";
|
}
|
||||||
if (!new File(fileLocation).isDirectory()) {
|
|
||||||
new File(fileLocation).mkdirs();
|
if ("HTML".equalsIgnoreCase(outputType)) {
|
||||||
}
|
HtmlReportUtil.createStreamHTML(masterReport, baos);
|
||||||
|
return Response.ok().entity(baos.toByteArray())
|
||||||
String genaratePdf = fileLocation + File.separator + reportName + ".pdf";
|
.type("text/html").build();
|
||||||
|
}
|
||||||
try {
|
} catch (ResourceException e) {
|
||||||
GenericResultsetData result = retrieveGenericResultset(reportName, type, queryParams);
|
throw new PlatformDataIntegrityException(
|
||||||
|
"error.msg.reporting.error", e.getMessage());
|
||||||
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
} catch (ReportProcessingException e) {
|
||||||
List<ResultsetDataRow> data = result.getData();
|
throw new PlatformDataIntegrityException(
|
||||||
List<String> row;
|
"error.msg.reporting.error", e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
logger.info("NO. of Columns: " + columnHeaders.size());
|
throw new PlatformDataIntegrityException(
|
||||||
Integer chSize = columnHeaders.size();
|
"error.msg.reporting.error", e.getMessage());
|
||||||
|
}
|
||||||
Document document = new Document(PageSize.B0.rotate());
|
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
PdfWriter.getInstance(document, new FileOutputStream(new File(fileLocation + reportName + ".pdf")));
|
"error.msg.invalid.outputType", "No matching Output Type: "
|
||||||
document.open();
|
+ outputType);
|
||||||
|
|
||||||
PdfPTable table = new PdfPTable(chSize);
|
}
|
||||||
table.setWidthPercentage(100);
|
|
||||||
|
private void addParametersToReport(final MasterReport report,
|
||||||
for (int i = 0; i < chSize; i++) {
|
final Map<String, String> queryParams) {
|
||||||
|
|
||||||
table.addCell(columnHeaders.get(i).getColumnName());
|
//AppUser currentUser = context.authenticatedUser();
|
||||||
|
|
||||||
}
|
try {
|
||||||
table.completeRow();
|
ReportParameterValues rptParamValues = report.getParameterValues();
|
||||||
|
ReportParameterDefinition paramsDefinition = report
|
||||||
Integer rSize;
|
.getParameterDefinition();
|
||||||
String currColType;
|
|
||||||
String currVal;
|
/*
|
||||||
logger.info("NO. of Rows: " + data.size());
|
* only allow integer, long, date and string parameter types and
|
||||||
for (int i = 0; i < data.size(); i++) {
|
* assume all mandatory - could go more detailed like Pawel did in
|
||||||
row = data.get(i).getRow();
|
* Mifos later and could match incoming and pentaho parameters
|
||||||
rSize = row.size();
|
* better... currently assuming they come in ok... and if not an
|
||||||
for (int j = 0; j < rSize; j++) {
|
* error
|
||||||
currColType = columnHeaders.get(j).getColumnType();
|
*/
|
||||||
currVal = row.get(j);
|
for (ParameterDefinitionEntry paramDefEntry : paramsDefinition
|
||||||
if (currVal != null) {
|
.getParameterDefinitions()) {
|
||||||
if (currColType.equals("DECIMAL") || currColType.equals("DOUBLE") || currColType.equals("BIGINT")
|
String paramName = paramDefEntry.getName();
|
||||||
|| currColType.equals("SMALLINT") || currColType.equals("INT")) {
|
if (paramName != "tenantdb") {
|
||||||
|
String pValue = queryParams.get(paramName);
|
||||||
table.addCell(currVal.toString());
|
if (StringUtils.isBlank(pValue))
|
||||||
} else {
|
throw new PlatformDataIntegrityException(
|
||||||
table.addCell(currVal.toString());
|
"error.msg.reporting.error",
|
||||||
}
|
"Pentaho Parameter: " + paramName
|
||||||
}
|
+ " - not Provided");
|
||||||
}
|
|
||||||
}
|
Class<?> clazz = paramDefEntry.getValueType();
|
||||||
table.completeRow();
|
logger.info("addParametersToReport(" + paramName + " : "
|
||||||
document.add(table);
|
+ pValue + " : " + clazz.getCanonicalName() + ")");
|
||||||
document.close();
|
if (clazz.getCanonicalName().equalsIgnoreCase(
|
||||||
return genaratePdf;
|
"java.lang.Integer"))
|
||||||
}
|
rptParamValues.put(paramName, Integer.parseInt(pValue));
|
||||||
catch (Exception e) {
|
else if (clazz.getCanonicalName().equalsIgnoreCase(
|
||||||
throw new PlatformDataIntegrityException("error.msg.exception.error", e.getMessage());
|
"java.lang.Long"))
|
||||||
}
|
rptParamValues.put(paramName, Long.parseLong(pValue));
|
||||||
}
|
else if (clazz.getCanonicalName().equalsIgnoreCase(
|
||||||
|
"java.sql.Date"))
|
||||||
|
rptParamValues.put(paramName, Date.valueOf(pValue));
|
||||||
|
else
|
||||||
|
rptParamValues.put(paramName, pValue);
|
||||||
|
}
|
||||||
|
logger.info("db name:" + dataSource.getConnection().getCatalog());
|
||||||
|
rptParamValues.put("tenantdb", "mifostenants-heavensfamily" /*dataSource.getConnection().getCatalog()*/);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
|
"error.msg.reporting.error", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String retrieveReportPDF(final String reportName, final String type,
|
||||||
|
final Map<String, String> queryParams) {
|
||||||
|
|
||||||
|
String fileLocation = FileUtils.MIFOSX_BASE_DIR + File.separator + "";
|
||||||
|
if (!new File(fileLocation).isDirectory()) {
|
||||||
|
new File(fileLocation).mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
String genaratePdf = fileLocation + File.separator + reportName
|
||||||
|
+ ".pdf";
|
||||||
|
|
||||||
|
try {
|
||||||
|
GenericResultsetData result = retrieveGenericResultset(reportName,
|
||||||
|
type, queryParams);
|
||||||
|
|
||||||
|
List<ResultsetColumnHeader> columnHeaders = result
|
||||||
|
.getColumnHeaders();
|
||||||
|
List<ResultsetDataRow> data = result.getData();
|
||||||
|
List<String> row;
|
||||||
|
|
||||||
|
logger.info("NO. of Columns: " + columnHeaders.size());
|
||||||
|
Integer chSize = columnHeaders.size();
|
||||||
|
|
||||||
|
Document document = new Document(PageSize.B0.rotate());
|
||||||
|
|
||||||
|
PdfWriter.getInstance(document, new FileOutputStream(new File(
|
||||||
|
fileLocation + reportName + ".pdf")));
|
||||||
|
document.open();
|
||||||
|
|
||||||
|
PdfPTable table = new PdfPTable(chSize);
|
||||||
|
table.setWidthPercentage(100);
|
||||||
|
|
||||||
|
for (int i = 0; i < chSize; i++) {
|
||||||
|
|
||||||
|
table.addCell(columnHeaders.get(i).getColumnName());
|
||||||
|
|
||||||
|
}
|
||||||
|
table.completeRow();
|
||||||
|
|
||||||
|
Integer rSize;
|
||||||
|
String currColType;
|
||||||
|
String currVal;
|
||||||
|
logger.info("NO. of Rows: " + data.size());
|
||||||
|
for (int i = 0; i < data.size(); i++) {
|
||||||
|
row = data.get(i).getRow();
|
||||||
|
rSize = row.size();
|
||||||
|
for (int j = 0; j < rSize; j++) {
|
||||||
|
currColType = columnHeaders.get(j).getColumnType();
|
||||||
|
currVal = row.get(j);
|
||||||
|
if (currVal != null) {
|
||||||
|
if (currColType.equals("DECIMAL")
|
||||||
|
|| currColType.equals("DOUBLE")
|
||||||
|
|| currColType.equals("BIGINT")
|
||||||
|
|| currColType.equals("SMALLINT")
|
||||||
|
|| currColType.equals("INT")) {
|
||||||
|
|
||||||
|
table.addCell(currVal.toString());
|
||||||
|
} else {
|
||||||
|
table.addCell(currVal.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table.completeRow();
|
||||||
|
document.add(table);
|
||||||
|
document.close();
|
||||||
|
return genaratePdf;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
|
"error.msg.exception.error", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user