com.ba.forms.bookingForm.BABookingAction.java Source code

Java tutorial

Introduction

Here is the source code for com.ba.forms.bookingForm.BABookingAction.java

Source

/*
 * 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.bookingForm;

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 BABookingAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private int objPageCount = 0;
    private static Logger logger = Logger.getLogger(BABookingAction.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 loadBooking(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        logger.info(" loadBooking method starts here");
        //  request.setAttribute("stockItems", BaStockFactory.getInstanceOfBaStockFactory().getStockItemDtls());
        //request.setAttribute("lstRooms", BARoomFactory.getInstanceOfBARoomFactory().getRoomDetails());
        return mapping.findForward(SUCCESS);
    }

    public ActionForward baSave(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        GridServerHandler objGridServerHandler = new GridServerHandler(request, response);
        logger.info(" save method starts here");

        try {

            BABookingDTO dto = new BABookingDTO();
            int userId = (Integer) request.getSession().getAttribute("UserId");
            dto.setBookId(request.getParameter("bookId"));
            dto.setDate(request.getParameter("date"));
            dto.setCheckIn_Time(request.getParameter("checkIn_Time"));
            dto.setCust_Name(request.getParameter("cust_Name"));
            dto.setAddress(request.getParameter("address"));
            dto.setPhone(request.getParameter("phone"));
            dto.setAlternate_Phone(request.getParameter("alternate_Phone"));
            dto.setId_Type(request.getParameter("id_Type"));
            dto.setId_No(request.getParameter("id_No"));
            dto.setPurpose(request.getParameter("purpose"));
            dto.setTotalAdvancePaid(request.getParameter("totalAdvancePaid"));
            dto.setPaymentMode(request.getParameter("paymentMode"));
            dto.setLstInsertedRecords(objGridServerHandler.getInsertedRecords());
            dto.setLstUpdatedRecords(objGridServerHandler.getUpdatedRecords());
            dto.setLstDeletedRecords(objGridServerHandler.getDeletedRecords());

            int result = BABookingFactory.getInstanceOfBABookingFactory().insertBookingData(dto, userId);
            if (result == 0) {
                objGridServerHandler.printResponseText("Record Insertion Failed . Try again");

            }
            if (result > 0) {
                objGridServerHandler.printResponseText("Record Inserted Successfully.");

            }
            if (result < 0) {
                objGridServerHandler.printResponseText("Database Connection  Problem Please check");

            }
        } catch (Exception ex) {
            logger.error("The Exception is  :" + ex);
            ex.printStackTrace();
            if (ex.toString().contains("com.mysql.jdbc.MysqlDataTruncation")) {
                objGridServerHandler
                        .printResponseText("Data is not correctly inserted into Database.Insertion Failed");
            } else if (ex.toString().contains("MySQLIntegrityConstraintViolationException")) {
                objGridServerHandler.printResponseText("Record already Exists");
            } else {
                String errorMessage = BAHandleAllException.exceptionHandler(ex);
                objGridServerHandler.printResponseText(errorMessage);
            }

        }
        return null;
    }

    public ActionForward baUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        GridServerHandler objGridServerHandler = new GridServerHandler(request, response);
        logger.info(" baUpdate method starts here");

        try {

            BABookingDTO dto = new BABookingDTO();
            int userId = (Integer) request.getSession().getAttribute("UserId");
            dto.setBookId(request.getParameter("bookId"));
            dto.setDate(request.getParameter("date"));
            dto.setCheckIn_Time(request.getParameter("checkIn_Time"));
            dto.setCust_Name(request.getParameter("cust_Name"));
            dto.setAddress(request.getParameter("address"));
            dto.setPhone(request.getParameter("phone"));
            dto.setAlternate_Phone(request.getParameter("alternate_Phone"));
            dto.setId_Type(request.getParameter("id_Type"));
            dto.setId_No(request.getParameter("id_No"));
            dto.setPurpose(request.getParameter("purpose"));
            dto.setTotalAdvancePaid(request.getParameter("totalAdvancePaid"));
            dto.setPaymentMode(request.getParameter("paymentMode"));
            dto.setLstInsertedRecords(objGridServerHandler.getInsertedRecords());
            dto.setLstUpdatedRecords(objGridServerHandler.getUpdatedRecords());
            dto.setLstDeletedRecords(objGridServerHandler.getDeletedRecords());

            int result = BABookingFactory.getInstanceOfBABookingFactory().updateBookingData(dto, userId);
            if (result == 0) {
                objGridServerHandler.printResponseText("Record Updation Failed . Try again");

            }
            if (result > 0) {
                objGridServerHandler.printResponseText("Record Updated Successfully.");

            }
            if (result < 0) {
                objGridServerHandler.printResponseText("Database Connection  Problem Please check");

            }
        } catch (Exception ex) {
            logger.error("The Exception is  :" + ex);
            ex.printStackTrace();
            if (ex.toString().contains("com.mysql.jdbc.MysqlDataTruncation")) {
                objGridServerHandler
                        .printResponseText("Data is not correctly inserted into Database.Insertion Failed");
            } else if (ex.toString().contains("MySQLIntegrityConstraintViolationException")) {
                objGridServerHandler.printResponseText("Record already Exists");
            } else {
                String errorMessage = BAHandleAllException.exceptionHandler(ex);
                objGridServerHandler.printResponseText(errorMessage);
            }

        }
        return null;
    }

    public ActionForward baDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            logger.info(" delete method starts here");
            BABookingActionForm form2 = (BABookingActionForm) form;
            BABookingDTO dto = new BABookingDTO();
            BeanUtils.copyProperties(dto, form2);

            int result1 = BABookingFactory.getInstanceOfBABookingFactory().deleteBookingData(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 baView(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        BABookingDTO vo = new BABookingDTO();
        logger.info("view method starts here");
        try {
            // logger.info("======In GcsStockItemAction Action cmsGet method======");
            vo.setBookId((request.getParameter("bookId")));
            vo.setDate(request.getParameter("date"));
            vo.setCheckIn_Time(request.getParameter("checkIn_Time"));
            vo.setCust_Name(request.getParameter("cust_Name"));
            vo.setAddress(request.getParameter("address"));
            vo.setPhone(request.getParameter("phone"));
            vo.setAlternate_Phone((request.getParameter("alternate_Phone")));

            vo.setId_Type(request.getParameter("id_Type"));
            vo.setId_No((request.getParameter("id_No")));
            vo.setPurpose(request.getParameter("purpose"));

            vo.setTotalAdvancePaid(request.getParameter("totalAdvancePaid"));

            vo.setPaymentMode(request.getParameter("paymentMode"));

            List hashMpBookingDet = BABookingFactory.getInstanceOfBABookingFactory().getBookingDtls(1, vo);

            objPageCount = BABookingFactory.getInstanceOfBABookingFactory().getPageCount(vo);
            json.put("exception", "");
            json.put("BookingDets", hashMpBookingDet);
            json.put("BookingExit", hashMpBookingDet.size());
            json.put("objPageCount", objPageCount);
            // logger.warn("strCurren 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 baGet(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        BABookingDTO vo = new BABookingDTO();

        try {
            logger.info(" get method starts here");
            String foodId = request.getParameter("search");
            HashMap hashMpBookingDet = BABookingFactory.getInstanceOfBABookingFactory().getBookingDtls(foodId);
            json.put("exception", "");
            json.put("BookingDets", hashMpBookingDet);
            json.put("BookingExit", hashMpBookingDet.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 getPageRecords(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        BABookingDTO vo = new BABookingDTO();

        try {
            logger.info(" getPageRecords method starts here");
            vo.setBookId((request.getParameter("bookId")));
            vo.setDate(request.getParameter("date"));
            vo.setCheckIn_Time(request.getParameter("checkIn_Time"));
            vo.setCust_Name(request.getParameter("cust_Name"));
            vo.setAddress(request.getParameter("address"));
            vo.setPhone(request.getParameter("phone"));
            vo.setAlternate_Phone((request.getParameter("alternate_Phone")));
            vo.setId_Type(request.getParameter("id_Type"));
            vo.setId_No((request.getParameter("id_No")));
            vo.setPurpose(request.getParameter("purpose"));
            vo.setTotalAdvancePaid(request.getParameter("totalAdvancePaid"));
            vo.setPaymentMode(request.getParameter("paymentMode"));

            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 hashMpBookingDet = BABookingFactory.getInstanceOfBABookingFactory()
                    .getBookingDtls(strCurrentPageNo, vo);

            json.put("exception", "");
            json.put("BookingDets", hashMpBookingDet);
            json.put("BookingExit", hashMpBookingDet.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 baGetBookingGridData(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        GridServerHandler objGridServerHandler = new GridServerHandler(request, response);
        try {
            logger.info(" baGetBookingGridData method starts here");
            String bookingId = request.getParameter("bookingId");

            List<HashMap<String, String>> lstBookingGriddDets = BABookingFactory.getInstanceOfBABookingFactory()
                    .getBookingGridDtls(BAUtil.getIntValue(bookingId));
            objGridServerHandler.setTotalRowNum(lstBookingGriddDets.size());
            objGridServerHandler.setPageSize(lstBookingGriddDets.size());
            objGridServerHandler.setData(lstBookingGriddDets);
            logger.info(lstBookingGriddDets);
            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_booking.jrxml";
            String pdfFileName = "hotel_booking";
            String bookingId = request.getParameter("bookingId");

            con = BADatabaseUtil.getConnection();
            String reportFileName = JasperCompileManager
                    .compileReportToFile(request.getRealPath("/reports") + "/" + jrept);
            java.util.Map parameters = new java.util.HashMap();

            parameters.put("bookingId", bookingId);
            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 + "_" + bookingId + ".pdf");
            File f = new File(request.getRealPath("/PDF") + "/" + pdfFileName + "_" + bookingId + ".pdf");
            FileInputStream fin = new FileInputStream(f);
            //            
            String path = "http://" + request.getServerName() + ":" + request.getServerPort() + "/"
                    + request.getContextPath() + "/PDF" + "/" + pdfFileName + "_" + bookingId + ".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
}