com.ba.forms.foodBill.BAFoodBillAction.java Source code

Java tutorial

Introduction

Here is the source code for com.ba.forms.foodBill.BAFoodBillAction.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.foodBill;

import com.ba.masters.room.BARoomFactory;
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 BAFoodBillAction extends DispatchAction {

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

    public ActionForward baSave(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        GridServerHandler objGridServerHandler = new GridServerHandler(request, response);
        try {
            logger.info("save method starts here");
            BAFoodBillDTO dto = new BAFoodBillDTO();
            int userId = (Integer) request.getSession().getAttribute("UserId");

            dto.setFoodId(request.getParameter("foodId"));
            dto.setDate(request.getParameter("date"));
            dto.setRoomId(request.getParameter("roomId"));
            dto.setCustomerName(request.getParameter("customerName"));
            dto.setOrderType(request.getParameter("orderType"));
            dto.setAmount(request.getParameter("amount"));
            dto.setServiceCharge(request.getParameter("serviceCharge"));
            dto.setDiscount(request.getParameter("discount"));
            dto.setServiceTax(request.getParameter("serviceTax"));
            dto.setVat(request.getParameter("vat"));
            dto.setNetAmount(request.getParameter("netAmount"));
            dto.setPaymentMode(request.getParameter("paymentMode"));
            dto.setBookingId(request.getParameter("bookingId"));
            dto.setLstInsertedRecords(objGridServerHandler.getInsertedRecords());
            dto.setLstUpdatedRecords(objGridServerHandler.getUpdatedRecords());
            dto.setLstDeletedRecords(objGridServerHandler.getDeletedRecords());

            int result = BAFoodBillFactory.getInstanceOfBAFoodBillFactory().insertFoodBillData(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);
            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);
        try {

            logger.info(" update method starts here");
            BAFoodBillActionForm form2 = (BAFoodBillActionForm) form;
            BAFoodBillDTO dto = new BAFoodBillDTO();
            BeanUtils.copyProperties(dto, form2);
            dto.setLstInsertedRecords(objGridServerHandler.getInsertedRecords());
            dto.setLstUpdatedRecords(objGridServerHandler.getUpdatedRecords());
            dto.setLstDeletedRecords(objGridServerHandler.getDeletedRecords());
            int userId = (Integer) request.getSession().getAttribute("UserId");
            int result = BAFoodBillFactory.getInstanceOfBAFoodBillFactory().updateFoodBillData(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);
            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");
            BAFoodBillActionForm form2 = (BAFoodBillActionForm) form;
            BAFoodBillDTO dto = new BAFoodBillDTO();
            BeanUtils.copyProperties(dto, form2);
            request.setAttribute("lstRooms", BARoomFactory.getInstanceOfBARoomFactory().getBookedRoomDetails());
            int result1 = BAFoodBillFactory.getInstanceOfBAFoodBillFactory().deleteFoodBillData(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();
        BAFoodBillDTO vo = new BAFoodBillDTO();
        try {
            logger.info("view method starts here");
            vo.setFoodId((request.getParameter("foodId")));
            vo.setDate(request.getParameter("date"));
            vo.setRoomId(request.getParameter("roomId"));
            vo.setCustomerName(request.getParameter("customerName"));
            vo.setOrderType((request.getParameter("orderType")));
            vo.setAmount(request.getParameter("amount"));
            vo.setServiceCharge((request.getParameter("serviceCharge")));
            vo.setDiscount(request.getParameter("discount"));
            vo.setServiceTax(request.getParameter("serviceTax"));
            vo.setVat(request.getParameter("vat"));
            vo.setNetAmount((request.getParameter("netAmount")));
            vo.setPaymentMode(request.getParameter("paymentMode"));
            vo.setBookingId(request.getParameter("bookingId"));

            List hashMpFoodBillDet = BAFoodBillFactory.getInstanceOfBAFoodBillFactory().getFoodBillDtls(1, vo);

            objPageCount = BAFoodBillFactory.getInstanceOfBAFoodBillFactory().getPageCount(vo);
            json.put("exception", "");
            json.put("FoodBillDets", hashMpFoodBillDet);
            json.put("FoodBillExit", hashMpFoodBillDet.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();
        BAFoodBillDTO vo = new BAFoodBillDTO();

        try {
            logger.info(" get method starts here");
            String foodId = request.getParameter("search");
            HashMap hashMpFoodBillDet = BAFoodBillFactory.getInstanceOfBAFoodBillFactory().getFoodBillDtls(foodId);
            json.put("exception", "");
            json.put("FoodBillDets", hashMpFoodBillDet);
            json.put("FoodBillExit", hashMpFoodBillDet.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();
        BAFoodBillDTO vo = new BAFoodBillDTO();

        try {
            logger.info(" getPageRecords method starts here");
            vo.setFoodId((request.getParameter("foodId")));
            vo.setDate(request.getParameter("date"));
            vo.setRoomId(request.getParameter("roomId"));
            vo.setCustomerName(request.getParameter("customerName"));
            vo.setOrderType((request.getParameter("orderType")));
            vo.setAmount(request.getParameter("amount"));
            vo.setServiceCharge((request.getParameter("serviceCharge")));
            vo.setDiscount(request.getParameter("discount"));
            vo.setServiceTax(request.getParameter("serviceTax"));
            vo.setVat(request.getParameter("vat"));
            vo.setNetAmount((request.getParameter("netAmount")));
            vo.setPaymentMode(request.getParameter("paymentMode"));
            vo.setBookingId(request.getParameter("bookingId"));

            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 hashMpFoodBillDet = BAFoodBillFactory.getInstanceOfBAFoodBillFactory()
                    .getFoodBillDtls(strCurrentPageNo, vo);

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

            List<HashMap<String, String>> lstSalesInvoiceGriddDets = BAFoodBillFactory
                    .getInstanceOfBAFoodBillFactory().getFoodBillGridDtls(BAUtil.getIntValue(foodId));
            objGridServerHandler.setTotalRowNum(lstSalesInvoiceGriddDets.size());
            objGridServerHandler.setPageSize(lstSalesInvoiceGriddDets.size());
            objGridServerHandler.setData(lstSalesInvoiceGriddDets);
            logger.info(lstSalesInvoiceGriddDets);
            response.getWriter().write(objGridServerHandler.getLoadResponseText());
        } catch (Exception ex) {
            logger.error("The Exception is  :" + ex);
            objGridServerHandler.printResponseText(BAHandleAllException.exceptionHandler(ex));
        }
        return null;
    }

    public ActionForward betBookingMastDets(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();

        try {
            logger.info(" betBookingMastDets method starts here");
            String roomId = request.getParameter("roomId");

            HashMap hashMpReceiptDet = BAFoodBillFactory.getInstanceOfBAFoodBillFactory()
                    .getBookingMasterDets(roomId);

            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_food.jrxml";
            String pdfFileName = "hotel_food";
            String foodBill = request.getParameter("foodBill");

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

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