com.ba.forms.advanceBookingForm.BAAdvanceBookingAction.java Source code

Java tutorial

Introduction

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

import com.ba.forms.advanceBookingForm.BAAdvanceBookingDTO;
import com.ba.forms.advanceBookingForm.BAAdvanceBookingFactory;
import com.ba.util.BAHandleAllException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private int objPageCount = 0;
    private static Logger logger = Logger.getLogger(BAAdvanceBookingAction.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 loadAdvanceBooking(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        logger.info(" loadAdvanceBooking method starts here");
        return mapping.findForward(SUCCESS);
    }

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

            BAAdvanceBookingActionForm form1 = (BAAdvanceBookingActionForm) form;
            BAAdvanceBookingDTO dto = new BAAdvanceBookingDTO();
            BeanUtils.copyProperties(dto, form1);
            int userId = (Integer) request.getSession().getAttribute("UserId");
            logger.info("copied  Form values to bean ");
            int result = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1()
                    .insertAdvanceBookingData(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");
            BAAdvanceBookingActionForm form1 = (BAAdvanceBookingActionForm) form;
            BAAdvanceBookingDTO dto = new BAAdvanceBookingDTO();
            BeanUtils.copyProperties(dto, form1);
            int userId = (Integer) request.getSession().getAttribute("UserId");
            logger.info("copied  Form values to bean ");
            int result1 = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1()
                    .updateAdvanceBookingData(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");
            BAAdvanceBookingActionForm form1 = (BAAdvanceBookingActionForm) form;
            BAAdvanceBookingDTO dto = new BAAdvanceBookingDTO();
            BeanUtils.copyProperties(dto, form1);
            logger.info("copied  Form values to bean ");
            int result1 = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1()
                    .deleteAdvanceBookingData(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();
        BAAdvanceBookingDTO vo = new BAAdvanceBookingDTO();
        try {
            logger.info(" view method starts here");
            vo.setAdvanceId((request.getParameter("advanceId")));
            vo.setDate(request.getParameter("date"));
            vo.setFormDate(request.getParameter("formDate"));
            vo.setToDate(request.getParameter("toDate"));
            vo.setCheck_In_Time((request.getParameter("check_In_Time")));
            vo.setRoomType(request.getParameter("roomType"));
            vo.setNo_Of_Rooms((request.getParameter("no_Of_Rooms")));
            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.setRequest_Type(request.getParameter("request_Type"));

            List hashMpAdvanceBookingDet = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1()
                    .getAdvanceBookingDtls(1, vo);

            objPageCount = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1().getPageCount(vo);
            json.put("exception", "");
            json.put("AdvanceBookingDets", hashMpAdvanceBookingDet);
            json.put("AdvanceBookingExit", hashMpAdvanceBookingDet.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 baGet(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        BAAdvanceBookingDTO dto = new BAAdvanceBookingDTO();
        try {
            logger.info(" get method starts here");
            String advanceId = request.getParameter("search");
            HashMap hashMpAdvanceBookingDet = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1()
                    .getAdvanceBookingDtls(advanceId);
            json.put("exception", "");
            json.put("AdvanceBookingDets", hashMpAdvanceBookingDet);
            json.put("AdvanceBookingExit", hashMpAdvanceBookingDet.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();
        BAAdvanceBookingDTO vo = new BAAdvanceBookingDTO();
        try {
            logger.info(" getpageRecords method starts here");
            vo.setAdvanceId((request.getParameter("advanceId")));
            vo.setDate(request.getParameter("date"));
            vo.setFormDate(request.getParameter("formDate"));
            vo.setToDate(request.getParameter("toDate"));
            vo.setCheck_In_Time((request.getParameter("check_In_Time")));
            vo.setRoomType(request.getParameter("roomType"));
            vo.setNo_Of_Rooms((request.getParameter("no_Of_Rooms")));
            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.setRequest_Type(request.getParameter("request_Type"));

            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 hashMpAdvanceBookingDet = BAAdvanceBookingFactory.getInstanceOfBAAdvanceBookingFactory1()
                    .getAdvanceBookingDtls(strCurrentPageNo, vo);

            json.put("exception", "");
            json.put("AdvanceBookingDets", hashMpAdvanceBookingDet);
            json.put("AdvanceBookingExit", hashMpAdvanceBookingDet.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;
    }
}