Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.ba.forms.receipt; import com.ba.forms.bookingForm.BABookingFactory; import com.ba.util.BADatabaseUtil; import com.ba.util.BAHandleAllException; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import org.apache.struts.actions.DispatchAction; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; import org.apache.commons.beanutils.BeanUtils; import org.apache.log4j.Logger; import org.json.simple.JSONObject; /** * * @author RAPID17 */ public class BAReceiptAction extends DispatchAction { /* forward name="success" path="" */ private final static String SUCCESS = "success"; private int objPageCount = 0; private static Logger logger = Logger.getLogger(BAReceiptAction.class); /** * This is the Struts action method called on * http://.../actionPath?method=myAction1, where "method" is the value * specified in <action> element : ( <action parameter="method" .../> ) */ public ActionForward loadReceipt(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { logger.info(" loadReceipt method starts here"); request.setAttribute("lstBooking", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails()); request.setAttribute("lstBooking1", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails1()); return mapping.findForward(SUCCESS); } public ActionForward baSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { logger.info(" save method starts here"); BAReceiptActionForm form1 = (BAReceiptActionForm) form; BAReceiptDTO dto = new BAReceiptDTO(); BeanUtils.copyProperties(dto, form1); int userId = (Integer) request.getSession().getAttribute("UserId"); logger.info("copied Form values to bean "); request.setAttribute("lstBooking", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails()); request.setAttribute("lstBooking1", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails1()); int result = BAReceiptFactory.getInstanceOfBAReceiptFactory().insertReceiptData(dto, userId); logger.info("result=>" + result); if (result == 0) { request.setAttribute("response", "Record Insertion Failed. "); } if (result > 0) { request.setAttribute("response", "Record Inserted Successfully"); } if (result < 0) { request.setAttribute("response", "Database Connection Failed. Please check"); } } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); if (ex.toString().contains("com.mysql.jdbc.MysqlDataTruncation")) { request.setAttribute("ErrorMessage", "Data is not correctly inserted into Database.Insertion Failed"); } else if (ex.toString().contains("MySQLIntegrityConstraintViolationException")) { request.setAttribute("ErrorMessage", "Record already Exists"); } else { String errorMessage = BAHandleAllException.exceptionHandler(ex); request.setAttribute("ErrorMessage", errorMessage); } } return mapping.findForward(SUCCESS); } public ActionForward baUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { logger.info(" update method starts here"); BAReceiptActionForm form1 = (BAReceiptActionForm) form; BAReceiptDTO dto = new BAReceiptDTO(); BeanUtils.copyProperties(dto, form1); int userId = (Integer) request.getSession().getAttribute("UserId"); logger.info("copied Form values to bean "); request.setAttribute("lstBooking", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails()); request.setAttribute("lstBooking1", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails1()); int result1 = BAReceiptFactory.getInstanceOfBAReceiptFactory().updateReceiptData(dto, userId); logger.info("result1 =>" + result1); if (result1 == 0) { request.setAttribute("response", "Record updation Failed. "); } if (result1 > 0) { request.setAttribute("response", "Record updated Successfully"); } if (result1 < 0) { request.setAttribute("response", "Database Connection Failed. Please check"); } } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); if (ex.toString().contains("com.mysql.jdbc.MysqlDataTruncation")) { request.setAttribute("ErrorMessage", "Data is not correctly inserted into Database.Insertion Failed"); } else if (ex.toString().contains("MySQLIntegrityConstraintViolationException")) { request.setAttribute("ErrorMessage", "Record already Exists"); } else { String errorMessage = BAHandleAllException.exceptionHandler(ex); request.setAttribute("ErrorMessage", errorMessage); } } return mapping.findForward(SUCCESS); } public ActionForward baDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { logger.info(" delete method starts here"); BAReceiptActionForm form1 = (BAReceiptActionForm) form; BAReceiptDTO dto = new BAReceiptDTO(); BeanUtils.copyProperties(dto, form1); logger.info("copied Form values to bean "); request.setAttribute("lstBooking", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails()); request.setAttribute("lstBooking1", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails1()); int result1 = BAReceiptFactory.getInstanceOfBAReceiptFactory().deleteReceiptData(dto); logger.info("result1 =>" + result1); if (result1 == 0) { request.setAttribute("response", "Record deletion Failed. "); } if (result1 > 0) { request.setAttribute("response", "Record deleted Successfully"); } if (result1 < 0) { request.setAttribute("response", "Database Connection Failed. Please check"); } } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); if (ex.toString().contains("com.mysql.jdbc.MysqlDataTruncation")) { request.setAttribute("ErrorMessage", "Data is not correctly inserted into Database.Insertion Failed"); } else if (ex.toString().contains("MySQLIntegrityConstraintViolationException")) { request.setAttribute("ErrorMessage", "Record already Exists"); } else { String errorMessage = BAHandleAllException.exceptionHandler(ex); request.setAttribute("ErrorMessage", errorMessage); } } return mapping.findForward(SUCCESS); } public ActionForward baView(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); BAReceiptDTO vo = new BAReceiptDTO(); try { logger.info(" view method starts here"); vo.setTransNo((request.getParameter("transNo"))); vo.setDate(request.getParameter("date")); vo.setType(request.getParameter("type")); vo.setBookingId(request.getParameter("bookingId")); vo.setCustomerName((request.getParameter("customerName"))); vo.setRoomId(request.getParameter("roomId")); vo.setReceipt((request.getParameter("receipt"))); vo.setMode(request.getParameter("mode")); List hashMpReceiptDet = BAReceiptFactory.getInstanceOfBAReceiptFactory().getReceiptDtls(1, vo); objPageCount = BAReceiptFactory.getInstanceOfBAReceiptFactory().getPageCount(vo); json.put("exception", ""); json.put("ReceiptDets", hashMpReceiptDet); json.put("ReceiptExit", hashMpReceiptDet.size()); json.put("objPageCount", objPageCount); logger.warn("strCurrent PageNo ------------->" + objPageCount); } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); json.put("exception", BAHandleAllException.exceptionHandler(ex)); } response.getWriter().write(json.toString()); return null; } public ActionForward betBookingMastDets(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); BAReceiptDTO vo = new BAReceiptDTO(); try { logger.info(" betBookingMastDets method starts here"); String bookingId = request.getParameter("bookingId"); List<HashMap<String, String>> hashMpReceiptDet = BAReceiptFactory.getInstanceOfBAReceiptFactory() .getBookingMasterDets(bookingId); json.put("exception", ""); json.put("ReceiptDets", hashMpReceiptDet); json.put("ReceiptExit", hashMpReceiptDet.size()); } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); json.put("exception", BAHandleAllException.exceptionHandler(ex)); } response.getWriter().write(json.toString()); return null; } public ActionForward baGet(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); BAReceiptDTO dto = new BAReceiptDTO(); try { logger.info(" get method starts here"); String advanceId = request.getParameter("search"); HashMap hashMpReceiptDet = BAReceiptFactory.getInstanceOfBAReceiptFactory().getReceiptDtls(advanceId); json.put("exception", ""); json.put("ReceiptDets", hashMpReceiptDet); json.put("ReceiptExit", hashMpReceiptDet.size()); // logger.warn("strCurrent PageNo ------------->"+objPageCount); } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); json.put("exception", BAHandleAllException.exceptionHandler(ex)); } response.getWriter().write(json.toString()); return null; } //Navigation code public ActionForward getPageRecords(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); BAReceiptDTO vo = new BAReceiptDTO(); try { logger.info(" getpageRecords method starts here"); vo.setTransNo((request.getParameter("transNo"))); vo.setDate(request.getParameter("date")); vo.setType(request.getParameter("type")); vo.setBookingId(request.getParameter("bookingId")); vo.setCustomerName((request.getParameter("customerName"))); vo.setRoomId(request.getParameter("roomId")); vo.setReceipt((request.getParameter("receipt"))); vo.setMode(request.getParameter("mode")); String strNavigation = request.getParameter("navigation"); int strCurrentPageNo = Integer.parseInt(request.getParameter("currentPageNo")); if (strNavigation != null && !strNavigation.equals("")) { if (strNavigation.equals("pre")) { strCurrentPageNo = strCurrentPageNo - 1; if (strCurrentPageNo <= 0) strCurrentPageNo = 1; } else if (strNavigation.equals("next")) { strCurrentPageNo = strCurrentPageNo + 1; if (strCurrentPageNo > objPageCount) strCurrentPageNo = objPageCount; } } logger.info("strCurrent PageNo ------------->" + strCurrentPageNo); List hashMpReceiptDet = BAReceiptFactory.getInstanceOfBAReceiptFactory() .getReceiptDtls(strCurrentPageNo, vo); json.put("exception", ""); json.put("ReceiptDets", hashMpReceiptDet); json.put("ReceiptExit", hashMpReceiptDet.size()); } catch (Exception ex) { logger.error("The Exception is :" + ex); ex.printStackTrace(); json.put("exception", BAHandleAllException.exceptionHandler(ex)); } response.getWriter().write(json.toString()); return null; } public ActionForward baPrint(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Connection con = null; com.fins.org.json.JSONObject json = new com.fins.org.json.JSONObject(); try { logger.info("hotel_booking"); // title is the title of the report.Here we are passing dynamically.So that this class is useful to remaining reports also. String jrept = "hotel_receipt.jrxml"; String pdfFileName = "hotel_receipt"; String receipt = request.getParameter("receipt"); con = BADatabaseUtil.getConnection(); String reportFileName = JasperCompileManager .compileReportToFile(request.getRealPath("/reports") + "/" + jrept); java.util.Map parameters = new java.util.HashMap(); parameters.put("receipt", receipt); File reportFile = new File(reportFileName); if (!reportFile.exists()) { throw new JRRuntimeException( "File WebappReport.jasper not found. The report design must be compiled first."); } JasperPrint jasperPrint = JasperFillManager.fillReport(reportFileName, parameters, con); JasperExportManager.exportReportToPdfFile(jasperPrint, request.getRealPath("/PDF") + "/" + pdfFileName + "_" + receipt + ".pdf"); File f = new File(request.getRealPath("/PDF") + "/" + pdfFileName + "_" + receipt + ".pdf"); FileInputStream fin = new FileInputStream(f); // String path = "http://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getContextPath() + "/PDF" + "/" + pdfFileName + "_" + receipt + ".pdf"; request.setAttribute("path", path); // outStream.flush(); fin.close(); // outStream.close(); logger.info("print feed dc"); json.put("exception", ""); json.put("bookingDets", path); json.put("bookingExit", 1); } catch (Exception ex) { ex.printStackTrace(); json.put("exception", BAHandleAllException.exceptionHandler(ex)); } finally { BADatabaseUtil.closeConnection(con); } logger.info("CmsReasonMasterDaoImpl In CmsReasonMasterAction :: cmsGet() ends Here "); response.getWriter().write(json.toString()); return null; }// End of cmsNew method }