com.ba.masters.department.BADepartmentAction.java Source code

Java tutorial

Introduction

Here is the source code for com.ba.masters.department.BADepartmentAction.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.masters.department;

import com.ba.masters.configuration.BAConfigurationAction;
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.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 ramesh
 */
public class BADepartmentAction extends DispatchAction {
    private int objPageCount = 0;

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

    public ActionForward baSave(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        logger.info(" save method starts here");
        try {
            BADepartmentActionForm formBean = (BADepartmentActionForm) form;
            BADepartmentDTO dto = new BADepartmentDTO();
            BeanUtils.copyProperties(dto, formBean);
            int userId = (Integer) request.getSession().getAttribute("UserId");
            int result = BADepartmentFactory.getInstanceOfBaDepartmentFactory().insertDepartmentData(dto, userId);

            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);
            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 {
        logger.info(" update method starts here");
        try {
            BADepartmentActionForm formBean = (BADepartmentActionForm) form;
            BADepartmentDTO dto = new BADepartmentDTO();
            BeanUtils.copyProperties(dto, formBean);
            int userId = (Integer) request.getSession().getAttribute("UserId");
            int result = BADepartmentFactory.getInstanceOfBaDepartmentFactory().updateDepartmentData(dto, userId);

            if (result == 0) {
                request.setAttribute("response", "Record Update failed.");
            }
            if (result > 0) {
                request.setAttribute("response", "Record Updated successfully.");
            }
            if (result < 0) {
                request.setAttribute("response", "Database Connection failed. Please check.");
            }
        } catch (Exception ex) {
            logger.error("The Exception is  :" + ex);
            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 {
        logger.info(" delete method starts here");
        try {
            BADepartmentActionForm formBean = (BADepartmentActionForm) form;
            BADepartmentDTO dto = new BADepartmentDTO();
            BeanUtils.copyProperties(dto, formBean);
            int result = BADepartmentFactory.getInstanceOfBaDepartmentFactory().deleteDepartmentData(dto);
            if (result == 0) {
                request.setAttribute("response", "Record Deletion failed.");
            }
            if (result > 0) {
                request.setAttribute("response", "Record Deleted successfully.");
            }
            if (result < 0) {
                request.setAttribute("response", "Database Connection failed. Please check.");
            }
        } catch (Exception ex) {
            logger.error("The Exception is  :" + ex);
            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();
        BADepartmentDTO vo = new BADepartmentDTO();
        logger.info(" view method starts here");
        try {
            //            logger.info("======In GcsStockItemAction Action cmsGet method======");
            vo.setDepId(request.getParameter("depId"));
            vo.setDepName(request.getParameter("depName"));
            List hashMpDepartmentDet = BADepartmentFactory.getInstanceOfBaDepartmentFactory().getDepartmentDtls(1,
                    vo);

            objPageCount = BADepartmentFactory.getInstanceOfBaDepartmentFactory().getPageCount(vo);
            json.put("exception", "");
            json.put("DepartmentDets", hashMpDepartmentDet);
            json.put("DepartmentExit", hashMpDepartmentDet.size());
            json.put("objPageCount", objPageCount);
            logger.warn("strCurrent PageNo ------------->" + objPageCount);
            logger.error(" records " + hashMpDepartmentDet);

        } catch (Exception ex) {
            logger.error("The Exception is  :" + ex);
            ex.printStackTrace();
            json.put("exception", BAHandleAllException.exceptionHandler(ex));
        }
        response.getWriter().write(json.toString());
        return null;

    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2, where "method" is the value
     * specified in <action> element : ( <action parameter="method" .../> )
     */
    public ActionForward baGet(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        BADepartmentDTO vo = new BADepartmentDTO();
        try {
            logger.info(" get method starts here");
            String depId = request.getParameter("search");
            HashMap hashMpDepartmentDet = BADepartmentFactory.getInstanceOfBaDepartmentFactory()
                    .getDepartmentDtls(depId);
            json.put("exception", "");
            json.put("DepartmentDets", hashMpDepartmentDet);
            json.put("DepartmentExit", hashMpDepartmentDet.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 myAction2(ActionMapping mapping, ActionForm form,
    //            HttpServletRequest request, HttpServletResponse response)
    //            throws Exception 
    //    {
    //        
    //        return mapping.findForward(SUCCESS);
    //    }

    public ActionForward getPageRecords(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONObject json = new JSONObject();
        BADepartmentDTO vo = new BADepartmentDTO();
        logger.info(" getPageRecords method starts here");
        try {
            logger.error("strCurrent PageNo ------------->" + objPageCount);
            vo.setDepId(request.getParameter("depId"));
            vo.setDepName(request.getParameter("depName"));
            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.error("strCurrent PageNo ------------->" + strCurrentPageNo);
            List hashMpDepartmentDet = BADepartmentFactory.getInstanceOfBaDepartmentFactory()
                    .getDepartmentDtls(strCurrentPageNo, vo);

            json.put("exception", "");
            json.put("DepartmentDets", hashMpDepartmentDet);
            json.put("DepartmentExit", hashMpDepartmentDet.size());
            //            json.put("objPageCount", 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;
    }
}