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.settlement; import com.ba.forms.bookingForm.BABookingFactory; import com.ba.masters.stock.BaStockFactory; import com.ba.util.BADatabaseUtil; import com.ba.util.BAHandleAllException; import com.ba.util.BAUtil; import com.fins.gt.server.GridServerHandler; 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.commons.beanutils.BeanUtils; import org.apache.log4j.Logger; 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.json.simple.JSONObject; /** * * @author RAPID17 */ public class BASettlementAction extends DispatchAction { /* forward name="success" path="" */ private final static String SUCCESS = "success"; private int objPageCount = 0; private static Logger logger = Logger.getLogger(BASettlementAction.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 loadSettlement(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { logger.info(" loadFoodBill method starts here"); // request.setAttribute("stockItems", BaStockFactory.getInstanceOfBaStockFactory().getStockItemDtls()); request.setAttribute("lstBooking", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails()); return mapping.findForward(SUCCESS); } public ActionForward baSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { logger.info("save method starts here"); BASettlementlActionForm bean = (BASettlementlActionForm) form; logger.info("checkin time " + bean.getCheckOut_time()); logger.info("checkout time " + request.getParameter("CheckOut_time")); bean.setCheckOut_time((String) request.getParameter("CheckOut_time")); BASettlementDTO dto = new BASettlementDTO(); BeanUtils.copyProperties(dto, bean); int userId = (Integer) request.getSession().getAttribute("UserId"); request.setAttribute("lstBooking", BABookingFactory.getInstanceOfBABookingFactory().getBookingDetails()); int result = BASettlementFactory.getInstanceOfBASettlementFactory().insertOrUpdatedSettlementData(dto, userId); if (result == 0) { request.setAttribute("response", "Settlement Record Failed. "); } if (result > 0) { request.setAttribute("response", "Settlement done 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 baDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { logger.info(" delete method starts here"); BASettlementlActionForm form2 = (BASettlementlActionForm) form; BASettlementDTO dto = new BASettlementDTO(); BeanUtils.copyProperties(dto, form2); int result1 = BASettlementFactory.getInstanceOfBASettlementFactory().deleteSettlementData(dto); 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 baGet(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); BASettlementDTO vo = new BASettlementDTO(); try { logger.info(" get method starts here"); String roomId = request.getParameter("roomId"); String bookingId = request.getParameter("bookingId"); HashMap hashMpSettlementDet = BASettlementFactory.getInstanceOfBASettlementFactory() .getSettlementDtls(bookingId, roomId); json.put("exception", ""); json.put("SettlementDets", hashMpSettlementDet); json.put("SettlementExit", hashMpSettlementDet.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; } public ActionForward getRoomRentDets(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); BASettlementDTO vo = new BASettlementDTO(); try { logger.info(" get method starts here"); String roomId = request.getParameter("roomId"); String bookingId = request.getParameter("bookingId"); HashMap hashMpRoomTariffDet = BASettlementFactory.getInstanceOfBASettlementFactory() .getRoomRentDets(bookingId, roomId); json.put("exception", ""); json.put("SettlementDets", hashMpRoomTariffDet); json.put("SettlementExit", hashMpRoomTariffDet.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; } public ActionForward cmsGetStockItems(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { GridServerHandler objGridServerHandler = new GridServerHandler(request, response); try { //getting DaoImpl class instance //getting DaoImpl class instance List<HashMap<String, String>> lstStockItems = BaStockFactory.getInstanceOfBaStockFactory() .getStockItemDetialsForView(); objGridServerHandler.setTotalRowNum(lstStockItems.size()); objGridServerHandler.setPageSize(lstStockItems.size()); objGridServerHandler.setData(lstStockItems); response.getWriter().write(objGridServerHandler.getLoadResponseText()); } catch (Exception ex) { logger.error("The Exception is :" + ex); objGridServerHandler.printResponseText(BAHandleAllException.exceptionHandler(ex)); } return null; } public ActionForward baGetsettlementGridData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { GridServerHandler objGridServerHandler = new GridServerHandler(request, response); try { logger.info(" baGetFoodGridData method starts here"); String bookingId = request.getParameter("bookingId"); String roomId = request.getParameter("roomId"); List<HashMap<String, String>> lstSettlementDetsGriddDets = BASettlementFactory .getInstanceOfBASettlementFactory() .getSettlementGridDtls(BAUtil.getIntValue(bookingId), BAUtil.getIntValue(roomId)); logger.info(lstSettlementDetsGriddDets.size() + " list size"); if (lstSettlementDetsGriddDets.size() == 0) { logger.info(" i am in if conditions"); objGridServerHandler.printResponseText("No records found with this Booking ID and Room ID"); } objGridServerHandler.setTotalRowNum(lstSettlementDetsGriddDets.size()); objGridServerHandler.setPageSize(lstSettlementDetsGriddDets.size()); objGridServerHandler.setData(lstSettlementDetsGriddDets); logger.info(lstSettlementDetsGriddDets); response.getWriter().write(objGridServerHandler.getLoadResponseText()); } catch (Exception ex) { logger.error("The Exception is :" + ex); objGridServerHandler.printResponseText(BAHandleAllException.exceptionHandler(ex)); } 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 settlement.jrxml"; String pdfFileName = "hotel settlement"; String sqlBookid = request.getParameter("sqlBookid"); String sqlRoomid = request.getParameter("sqlRoomid"); con = BADatabaseUtil.getConnection(); String reportFileName = JasperCompileManager .compileReportToFile(request.getRealPath("/reports") + "/" + jrept); java.util.Map parameters = new java.util.HashMap(); parameters.put("sqlBookid", Integer.parseInt(sqlBookid)); parameters.put("sqlRoomid", Integer.parseInt(sqlRoomid)); 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 + "_" + sqlBookid + "_" + sqlRoomid + ".pdf"); File f = new File( request.getRealPath("/PDF") + "/" + pdfFileName + "_" + sqlBookid + "_" + sqlRoomid + ".pdf"); FileInputStream fin = new FileInputStream(f); // String path = "http://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getContextPath() + "/PDF" + "/" + pdfFileName + "_" + sqlBookid + "_" + sqlRoomid + ".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 }