com.kcs.action.FrfImportAction.java Source code

Java tutorial

Introduction

Here is the source code for com.kcs.action.FrfImportAction.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.kcs.action;

import com.kcs.core.actions.BaseAction;
import com.kcs.core.utilities.DateUtil;
import com.kcs.core.utilities.NumberUtil;
import com.kcs.core.utilities.PaggingList;
import com.kcs.core.utilities.StringUtil;
import com.kcs.core.utilities.Utility;
import com.kcs.model.Datasetfrf;
import com.kcs.model.Datasetirf;
import com.kcs.service.DatasetFrfService;
import com.kcs.service.DatasetIrfService;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author chawachote
 */
public class FrfImportAction extends BaseAction {

    static Logger logger = Logger.getLogger(FrfImportAction.class);
    private File toBeUploaded;
    private String toBeUploadedFileName;
    private String toBeUploadedContentType;
    private HttpServletRequest servletRequest;
    private PaggingList paging;
    private String dataSetDate;
    private List<Datasetfrf> resultsFromExcel;
    private String checkDatasetDate;

    @Autowired
    private DatasetFrfService datasetFrfService;
    private List<Datasetfrf> resultList;

    @Override
    public String success() throws Exception {
        logger.debug("= = = = = Into success = = = = =");
        if (Utility.isNull(getPaging())) {
            setPaging(new PaggingList());

        }
        return SUCCESS;
    }

    public String readExcel() throws Exception {
        session.remove("EXCEL_TO_LIST_FRF");
        logger.debug("= = = = = Into readExcel = = = = =");
        excelToList();
        return SUCCESS;
    }

    public void excelToList() throws Exception {
        String filePath = getServletRequest().getSession().getServletContext().getRealPath("/");
        try {
            InputStream input = new BufferedInputStream(new FileInputStream(this.toBeUploaded));

            POIFSFileSystem fs = new POIFSFileSystem(input);

            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            Iterator rows = sheet.rowIterator();

            setResultsFromExcel(new ArrayList<Datasetfrf>());

            int checkColName = 0;
            setCheckDatasetDate("true");
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                if (checkColName != 0) {

                    Datasetfrf datasetfrf = new Datasetfrf();

                    Date dateFromJsp = DateUtil.convertDateFromJsp(getDataSetDate());
                    Date dateFromExcel = DateUtil.getDateFromString(row.getCell(1).toString(),
                            DateUtil.DATE_FORMAT_YYYYMMDDX);
                    if (dateFromJsp.compareTo(dateFromExcel) != 0) {
                        setCheckDatasetDate("false");
                        break;
                    }
                    int index = 0;
                    datasetfrf.setOrgId(row.getCell(index++).toString());
                    datasetfrf.setDataSetDate(convertDate(row.getCell(index++).toString()));
                    datasetfrf.setLoanDepsitTrnTye(row.getCell(index++).toString());
                    datasetfrf.setCurrCode(row.getCell(index++).toString());
                    datasetfrf.setPaymentMethod(row.getCell(index++).toString());
                    datasetfrf.setBrOrBcFlg(row.getCell(index++).toString());
                    datasetfrf.setCommInLieuRate(row.getCell(index++).toString());
                    datasetfrf.setMinCommInLieu(row.getCell(index++).toString());
                    datasetfrf.setMaxCommInLieu(row.getCell(index++).toString());
                    datasetfrf.setOthFeeDesc(row.getCell(index++).toString());
                    datasetfrf.setEffectiveDate(convertDate(row.getCell(index++).toString()));
                    datasetfrf.setEndDate(convertDate(row.getCell(index++).toString()));
                    datasetfrf.setSeq(Long.valueOf(row.getCell(index++).toString()));
                    datasetfrf.setUpdBy(getCurrentLoginId());
                    datasetfrf.setUpdDate(DateUtil.getCurrentDateTime());
                    index++;
                    index++;
                    datasetfrf.setSysCode(row.getCell(index++).toString());

                    getResultsFromExcel().add(datasetfrf);

                }
                checkColName++;
            }

            setResultsFromExcel(getResultsFromExcel());
            session.put("EXCEL_TO_LIST_FRF", getResultsFromExcel());

        } catch (IOException ex) {
            ex.printStackTrace();
            logger.error("Error >>> " + ex.getMessage());
        }
    }

    public String updateExcelToDB() throws Exception {
        logger.debug("= = = = = Into updateExcelToDB = = = = =");
        setResultsFromExcel((List<Datasetfrf>) session.get("EXCEL_TO_LIST_FRF"));

        if (getCheckDatasetDate().equalsIgnoreCase("true")) {

            logger.debug("check datasetdate pass === " + getDataSetDate());
            datasetFrfService.updateByList(getResultsFromExcel(), getDataSetDate());
            setPaging(new PaggingList());
            getPaging().setIndex(1);
            getPaging().setPageSize(10);
            setPaging(datasetFrfService.findMainListPaging(getPaging(), getDataSetDate(), ""));

        }
        setCheckDatasetDate("");
        return SUCCESS;
    }

    private Date convertDate(String date) throws ParseException {
        if (!date.equals("null") || date.equals(null)) {
            return DateUtil.parse(date, DateUtil.DATE_FORMAT_YYYYMMDDX);
        } else {
            return null;
        }
    }

    public File getToBeUploaded() {
        return toBeUploaded;
    }

    public void setToBeUploaded(File toBeUploaded) {
        this.toBeUploaded = toBeUploaded;
    }

    public String getToBeUploadedFileName() {
        return toBeUploadedFileName;
    }

    public void setToBeUploadedFileName(String toBeUploadedFileName) {
        this.toBeUploadedFileName = toBeUploadedFileName;
    }

    public String getToBeUploadedContentType() {
        return toBeUploadedContentType;
    }

    public void setToBeUploadedContentType(String toBeUploadedFileNameContentType) {
        this.toBeUploadedContentType = toBeUploadedFileNameContentType;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;

    }

    /**
     * @return the servletRequest
     */
    public HttpServletRequest getServletRequest() {
        return servletRequest;
    }

    /**
     * @return the paging
     */
    public PaggingList getPaging() {
        return paging;
    }

    /**
     * @param paging the paging to set
     */
    public void setPaging(PaggingList paging) {
        this.paging = paging;
    }

    /**
     * @return the dataSetDate
     */
    public String getDataSetDate() {
        return dataSetDate;
    }

    /**
     * @param dataSetDate the dataSetDate to set
     */
    public void setDataSetDate(String dataSetDate) {
        this.dataSetDate = dataSetDate;
    }

    /**
     * @return the resultsFromExcel
     */
    public List<Datasetfrf> getResultsFromExcel() {
        return resultsFromExcel;
    }

    /**
     * @param resultsFromExcel the resultsFromExcel to set
     */
    public void setResultsFromExcel(List<Datasetfrf> resultsFromExcel) {
        this.resultsFromExcel = resultsFromExcel;
    }

    /**
     * @return the checkDatasetDate
     */
    public String getCheckDatasetDate() {
        return checkDatasetDate;
    }

    /**
     * @param checkDatasetDate the checkDatasetDate to set
     */
    public void setCheckDatasetDate(String checkDatasetDate) {
        this.checkDatasetDate = checkDatasetDate;
    }

    /**
     * @return the datasetFrfService
     */
    public DatasetFrfService getDatasetFrfService() {
        return datasetFrfService;
    }

    /**
     * @param datasetFrfService the datasetFrfService to set
     */
    public void setDatasetFrfService(DatasetFrfService datasetFrfService) {
        this.datasetFrfService = datasetFrfService;
    }

    /**
     * @return the resultList
     */
    public List<Datasetfrf> getResultList() {
        return resultList;
    }

    /**
     * @param resultList the resultList to set
     */
    public void setResultList(List<Datasetfrf> resultList) {
        this.resultList = resultList;
    }

}