com.ba.masters.configuration.BAConfigurationAction.java Source code

Java tutorial

Introduction

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

import com.ba.masters.configuration.BAConfigurationDTO;
import com.ba.masters.configuration.BAConfigurationFactory;
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 RAPID17
 */
public class BAConfigurationAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private int objPageCount = 0;
    private static Logger logger = Logger.getLogger(BAConfigurationAction.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 loadConfiguration(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        logger.info(" loadConfiguration method starts here");
        int recordCount = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().isRecordsExists();
        HashMap hashMpConfigurationDet = null;
        if (recordCount > 0) {
            hashMpConfigurationDet = BAConfigurationFactory.getInstanceOfBAConfigurationFactory()
                    .getConfigurationDtls();

        }
        request.setAttribute("configurationDets", hashMpConfigurationDet);
        request.setAttribute("recordExist", recordCount);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward baSave(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            logger.info(" save method starts here");
            BAConfigurationActionForm form2 = (BAConfigurationActionForm) form;
            BAConfigurationDTO dto = new BAConfigurationDTO();
            BeanUtils.copyProperties(dto, form2);

            int userId = (Integer) request.getSession().getAttribute("UserId");
            int result1 = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().insertConfigurationData(dto,
                    userId);
            if (result1 == 0) {
                request.setAttribute("response", "Record Insertion Failed. ");
            }
            if (result1 > 0) {
                request.setAttribute("response", "Record Inserted Successfully");
            }
            if (result1 < 0) {
                request.setAttribute("response", "Database Connection Failed. Please check");
            }
            int recordCount = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().isRecordsExists();
            HashMap hashMpConfigurationDet = null;
            if (recordCount > 0) {
                hashMpConfigurationDet = BAConfigurationFactory.getInstanceOfBAConfigurationFactory()
                        .getConfigurationDtls();

            }
            request.setAttribute("configurationDets", hashMpConfigurationDet);
            request.setAttribute("recordExist", recordCount);

        } 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");
            BAConfigurationActionForm form2 = (BAConfigurationActionForm) form;
            BAConfigurationDTO dto = new BAConfigurationDTO();
            BeanUtils.copyProperties(dto, form2);
            int userId = (Integer) request.getSession().getAttribute("UserId");
            int result1 = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().updateConfigurationData(dto,
                    userId);
            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");
            }
            int recordCount = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().isRecordsExists();
            HashMap hashMpConfigurationDet = null;
            if (recordCount > 0) {
                hashMpConfigurationDet = BAConfigurationFactory.getInstanceOfBAConfigurationFactory()
                        .getConfigurationDtls();

            }
            request.setAttribute("configurationDets", hashMpConfigurationDet);
            request.setAttribute("recordExist", recordCount);
        } 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");
            BAConfigurationActionForm form2 = (BAConfigurationActionForm) form;
            BAConfigurationDTO dto = new BAConfigurationDTO();
            BeanUtils.copyProperties(dto, form2);
            int result1 = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().deleteConfigurationData(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();
        BAConfigurationDTO vo = new BAConfigurationDTO();

        try {
            logger.info("view method starts here");
            vo.setOrder((request.getParameter("order")));
            vo.setCheckOutTime(request.getParameter("checkOutTime"));
            vo.setCompany(request.getParameter("company"));
            vo.setAddress(request.getParameter("address").toString().trim());
            vo.setSmsId((request.getParameter("smsId")));
            vo.setPassword(request.getParameter("password"));
            vo.setEmail((request.getParameter("email")));
            vo.setPop(request.getParameter("pop"));
            vo.setSmtp(request.getParameter("smtp"));
            List hashMpConfigurationDet = BAConfigurationFactory.getInstanceOfBAConfigurationFactory()
                    .getConfigurationDtls(1, vo);

            objPageCount = BAConfigurationFactory.getInstanceOfBAConfigurationFactory().getPageCount(vo);
            json.put("exception", "");
            json.put("ConfigurationDets", hashMpConfigurationDet);
            json.put("ConfigurationExit", hashMpConfigurationDet.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();
        BAConfigurationDTO vo = new BAConfigurationDTO();
        try {
            logger.info(" get method starts here");
            String order = request.getParameter("search");
            HashMap hashMpConfigurationDet = BAConfigurationFactory.getInstanceOfBAConfigurationFactory()
                    .getConfigurationDtls(order);
            json.put("exception", "");
            json.put("ConfigurationDets", hashMpConfigurationDet);
            json.put("ConfigurationExit", hashMpConfigurationDet.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();
        BAConfigurationDTO vo = new BAConfigurationDTO();
        try {
            logger.info("get page records starts");
            vo.setOrder((request.getParameter("order")));
            vo.setCheckOutTime(request.getParameter("checkOutTime"));
            vo.setCompany(request.getParameter("company"));
            vo.setAddress(request.getParameter("address").toString().trim());
            vo.setSmsId((request.getParameter("smsId")));
            vo.setPassword(request.getParameter("password"));
            vo.setEmail((request.getParameter("email")));
            vo.setPop(request.getParameter("pop"));
            vo.setSmtp(request.getParameter("smtp"));

            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 hashMpConfigurationDet = BAConfigurationFactory.getInstanceOfBAConfigurationFactory()
                    .getConfigurationDtls(strCurrentPageNo, vo);

            json.put("exception", "");
            json.put("ConfigurationDets", hashMpConfigurationDet);
            json.put("ConfigurationExit", hashMpConfigurationDet.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;
    }
}