com.kcs.action.OffImportAction.java Source code

Java tutorial

Introduction

Here is the source code for com.kcs.action.OffImportAction.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.model.Datasetoff;
import com.kcs.service.DatasetFrfService;
import com.kcs.service.DatasetIrfService;
import com.kcs.service.DatasetOffService;
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 OffImportAction extends BaseAction {

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

    @Autowired
    private DatasetOffService datasetOffService;
    private List<Datasetoff> 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.getToBeUploaded()));

            POIFSFileSystem fs = new POIFSFileSystem(input);

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

            setResultsFromExcel(new ArrayList<Datasetoff>());

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

                    Datasetoff datasetoff = new Datasetoff();

                    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;
                    datasetoff.setOrgId(row.getCell(index++).toString());
                    datasetoff.setDataSetDate(convertDate(row.getCell(index++).toString()));
                    datasetoff.setArrgmentTye(row.getCell(index++).toString());
                    datasetoff.setCurrCode(row.getCell(index++).toString());
                    datasetoff.setMinimOpenAcctAmt(row.getCell(index++).toString());
                    datasetoff.setMinimBalForMaintenanceFee(row.getCell(index++).toString());
                    datasetoff.setMaintenanceFee(row.getCell(index++).toString());
                    datasetoff.setInactiveTerm(convertBigDecimal(row.getCell(index++).toString()));
                    datasetoff.setInactiveTermUnt(row.getCell(index++).toString());
                    datasetoff.setInactiveFee(row.getCell(index++).toString());
                    datasetoff.setEffectiveDate(convertDate(row.getCell(index++).toString()));
                    datasetoff.setEndDate(convertDate(row.getCell(index++).toString()));
                    datasetoff.setSeq(Long.valueOf(row.getCell(index++).toString()));
                    datasetoff.setUpdBy(getCurrentLoginId());
                    datasetoff.setUpdDate(DateUtil.getCurrentDateTime());
                    index++;
                    index++;
                    datasetoff.setSysCode(row.getCell(index++).toString());

                    getResultsFromExcel().add(datasetoff);

                }
                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<Datasetoff>) session.get("EXCEL_TO_LIST_FRF"));

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

            logger.debug("check datasetdate pass === " + getDataSetDate());
            datasetOffService.updateByList(getResultsFromExcel(), getDataSetDate());
            setPaging(new PaggingList());
            getPaging().setIndex(1);
            getPaging().setPageSize(10);
            setPaging(datasetOffService.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;
        }
    }

    private BigDecimal convertBigDecimal(String str) {
        if (str.equals("null") || str.equals(null)) {
            return new BigDecimal(0);
        } else {
            return new BigDecimal(str);
        }

    }

    /**
     * @return the toBeUploaded
     */
    public File getToBeUploaded() {
        return toBeUploaded;
    }

    /**
     * @param toBeUploaded the toBeUploaded to set
     */
    public void setToBeUploaded(File toBeUploaded) {
        this.toBeUploaded = toBeUploaded;
    }

    /**
     * @return the toBeUploadedFileName
     */
    public String getToBeUploadedFileName() {
        return toBeUploadedFileName;
    }

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

    /**
     * @return the toBeUploadedContentType
     */
    public String getToBeUploadedContentType() {
        return toBeUploadedContentType;
    }

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

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

    /**
     * @param servletRequest the servletRequest to set
     */
    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = 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<Datasetoff> getResultsFromExcel() {
        return resultsFromExcel;
    }

    /**
     * @param resultsFromExcel the resultsFromExcel to set
     */
    public void setResultsFromExcel(List<Datasetoff> 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 datasetOffService
     */
    public DatasetOffService getDatasetOffService() {
        return datasetOffService;
    }

    /**
     * @param datasetOffService the datasetOffService to set
     */
    public void setDatasetOffService(DatasetOffService datasetOffService) {
        this.datasetOffService = datasetOffService;
    }

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

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

}