String msg = null;
if (!jasperFile.exists()) {
msg = "Cannot find jasper file: " + jasperFileName;
log.warning(msg);
throw new ControllerException(msg);
}
Map clientMap = null;
Map titleMap = null;
GenericController genericController = GenericController.getInstance();
SpecificController specificController =
SpecificController.getInstance();
try {
clientMap =
genericController.findWhere("client", "client_id=" + clientID);
} catch (FinderException fe) {
throw new ControllerException("No client record found");
}
try {
titleMap =
genericController.findWhere("title", "title_id=" + clientMap.get("title_id"));
} catch (FinderException fe) {
throw new ControllerException("No title record found for title_id =" + clientMap.get("title_id"));
}
Map reportMap = new HashMap();
// Get sender address
Map parameterMap = null;
// Sender address must be decoded from Base64
reportMap.put(
"SenderAddress",
Configuration.getDecodedProperty(
"report.envelopreport.sender.address",
""));
StringBuffer receiverAddress = new StringBuffer();
if (titleMap != null && titleMap.get("title_code") != null) {
receiverAddress.append(titleMap.get("title_code"));
}
if (clientMap.get("first_name") != null) {
receiverAddress.append(" " + clientMap.get("first_name"));
}
// if (clientMap.get("middle_name") != null) {
// if (receiverAddress.length() > 0) {
// receiverAddress.append(",");
// }
//
// receiverAddress.append(" " + clientMap.get("middle_name"));
// }
if (clientMap.get("last_name") != null) {
receiverAddress.append(" ");
receiverAddress.append(clientMap.get("last_name"));
receiverAddress.append("\n");
}
if (clientMap.get("address1") != null) {
receiverAddress.append(clientMap.get("address1"));
receiverAddress.append("\n");
}
if (clientMap.get("address2") != null) {
receiverAddress.append(clientMap.get("address2"));
receiverAddress.append("\n");
}
if (clientMap.get("suburb") != null) {
receiverAddress.append(clientMap.get("suburb"));
receiverAddress.append("\n");
}
if (clientMap.get("city") != null) {
receiverAddress.append(clientMap.get("city"));
receiverAddress.append("\n");
}
reportMap.put("ReceiverAddress", receiverAddress.toString().trim());
try {
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperFileName,
reportMap,
new JREmptyDataSource());
// For print attributes see API docs:
// j2sdk-1_4_2/docs/api/index.html
PrintRequestAttributeSet printRequestAttributeSet =
new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);
printRequestAttributeSet.add(MediaSizeName.ISO_A4);
PrintServiceAttributeSet printServiceAttributeSet =
new HashPrintServiceAttributeSet();
// Linux: print queues like "draft", "normal", "photo", etc
// Windows: use physical printername like "Epson Stylus COLOR 680"
// or "HP LaserJet 5000 Series PCL"
String printerNameTemp =
Configuration.getDecodedProperty(
"report.envelopreport.printer",
Constants.REPORT_ENVELOPREPORT_PRINTER);
if (printerNameTemp.equals("")) {
} else {
PrinterName printerName =
new PrinterName(printerNameTemp, Locale.getDefault());
printServiceAttributeSet.add(printerName);
}
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(
JRExporterParameter.JASPER_PRINT,
jasperPrint);
exporter.setParameter(
JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
printRequestAttributeSet);
exporter.setParameter(
JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
printServiceAttributeSet);
exporter.setParameter(
JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
Boolean.FALSE);
exporter.setParameter(
JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
Boolean.TRUE);
if (printPreview) {
// preview is started in non_modal
JasperViewer.viewReport(jasperPrint, false);
} else {
exporter.exportReport();
}
} catch (JRException je) {
msg =
"\nError printing envelop:\nPlease check printer parameter.\n"
+ je.getMessage();
log.warning(msg);
throw new ControllerException(msg);
}
}