com.kcs.action.IrfImpExpAction.java Source code

Java tutorial

Introduction

Here is the source code for com.kcs.action.IrfImpExpAction.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.NumberUtil;
import com.kcs.core.utilities.PaggingList;
import com.kcs.model.Datasetirf;
import com.kcs.service.DatasetIrfService;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author vorawats
 */
public class IrfImpExpAction extends BaseAction {

    static Logger logger = Logger.getLogger(IrfImpExpAction.class);
    private String dataSetDate;
    private PaggingList paging;
    private String systemCode;
    private int resultSize;

    private File toBeUploaded;
    private String fileName;
    private InputStream excelStream;
    //    private String toBeUploadedFileName;
    //    private String toBeUploadedContentType;
    //    private HttpServletRequest servletRequest;

    @Autowired
    private DatasetIrfService datasetIrfService;
    private List<Datasetirf> resultList;

    @Override
    public String success() throws Exception {
        paging = datasetIrfService.findListIrf(createPaginate(), dataSetDate, systemCode);
        return super.success();

    }

    @Override
    public String retrieve() throws Exception {
        paging = datasetIrfService.findListIrf(createPaginate(), dataSetDate, systemCode);
        resultSize = paging.getList().size();
        return super.search();
    }

    @Override
    public String export() throws Exception {
        // paging = datasetIrfService.findListIrf(createPaginate(), dataSetDate, systemCode);
        resultList = datasetIrfService.findMainList(dataSetDate, "");
        resultSize = resultList.size();
        logger.debug("exportExcel : begin...");
        //        resultList = manageService.findListDinamicSql(sql);
        logger.debug("exportExcel : list >>> " + resultSize);

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        HSSFWorkbook myWorkBook = new HSSFWorkbook();
        HSSFSheet mySheet = myWorkBook.createSheet("Dataset IRF");
        setFileName("Export Data DS_IRF_" + dateFormat.format(new Date()) + "excel".concat(".xls"));

        Row headerRow = mySheet.createRow(0);
        if (null != resultList && resultList.size() > 0) {
            /* create header excel */
            //            Map<String, String[]> column = new HashMap<String, String[]>();
            //            column.put("column", new String[]{"orgId", "dataSetDate", "arrgmentTye", "invPartyTye", "currCode", "depsitTerm", "depsitTermUnt", "balTierAmt", "interestRate", "effectiveDate", "endDate", "seq", "updBy", "updDate", "sysCode"});
            //            Set<String> newRowsColumn = column.keySet();
            //Iterate over data and write to sheet
            //            Set<String> keyset = column.keySet();
            int rownum = 0;
            String[] column = new String[] { "orgId", "dataSetDate", "arrgmentTye", "invPartyTye", "currCode",
                    "depsitTerm", "depsitTermUnt", "balTierAmt", "interestRate", "effectiveDate", "endDate", "seq",
                    "updBy", "updDate", "sysCode" };
            //  Row rowColumn = mySheet.createRow(1);
            // int cellnum2 = 0;
            for (int i = 0; i < 1; i++) {

                //                logger.debug("PALM : columnName >>> " + columnName);
                //                Cell cell = rowColumn.createCell(cellnum2++);
                //                cell.setCellValue(columnName);
                Row row = mySheet.createRow(rownum++);
                int cellnum = 0;
                //----- orgId
                Cell cellOrgId = row.createCell(cellnum++);
                cellOrgId.setCellValue("orgId");
                //----- dataSetDate
                Cell cellDatasetDate = row.createCell(cellnum++);
                cellDatasetDate.setCellValue("dataSetDate");
                //----- arrgmentTye
                Cell cellArrgmentType = row.createCell(cellnum++);
                cellArrgmentType.setCellValue("arrgmentTye");
                //----- invPartyTye
                Cell cellInvPartyTye = row.createCell(cellnum++);
                cellInvPartyTye.setCellValue("invPartyTye");
                //----- currCode
                Cell cellCurrCode = row.createCell(cellnum++);
                cellCurrCode.setCellValue("currCode");
                //----- depsitTerm
                Cell cellDepsitTerm = row.createCell(cellnum++);
                cellDepsitTerm.setCellValue("depsitTerm");
                //----- depsitTerm Unit
                Cell cellDepsitTermUnit = row.createCell(cellnum++);
                cellDepsitTermUnit.setCellValue("depsitTermUnit");
                //----- balTierAmt
                Cell cellBalTierAmt = row.createCell(cellnum++);
                cellBalTierAmt.setCellValue("balTierAmt");
                //----- interestRate
                Cell cellInterestRate = row.createCell(cellnum++);
                cellInterestRate.setCellValue("interestRate");
                //----- effectiveDate
                Cell cellEffectiveDate = row.createCell(cellnum++);
                cellEffectiveDate.setCellValue("effectiveDate");
                //----- endDate
                Cell cellEndDate = row.createCell(cellnum++);
                cellEndDate.setCellValue("endDate");
                //----- seq
                Cell cellSeq = row.createCell(cellnum++);
                cellSeq.setCellValue("seq");
                //----- updBy
                Cell cellUpdBy = row.createCell(cellnum++);
                cellUpdBy.setCellValue("updBy");
                //----- updDate
                Cell cellUpdDate = row.createCell(cellnum++);
                cellUpdDate.setCellValue("updDate");
                //----- sysCode
                Cell cellSysCode = row.createCell(cellnum++);
                cellSysCode.setCellValue("sysCode");
            }
            //  rownum = 2;
            for (Datasetirf obj : resultList) {
                Row row = mySheet.createRow(rownum++);
                int cellnum = 0;
                //----- orgId
                Cell cellOrgId = row.createCell(cellnum++);
                cellOrgId.setCellValue(obj.getOrgId() + "");
                //----- dataSetDate
                Cell cellDatasetDate = row.createCell(cellnum++);
                cellDatasetDate.setCellValue(obj.getDataSetDate() + "");
                //----- arrgmentTye
                Cell cellArrgmentType = row.createCell(cellnum++);
                cellArrgmentType.setCellValue(obj.getArrgmentTye() + "");
                //----- invPartyTye
                Cell cellInvPartyTye = row.createCell(cellnum++);
                cellInvPartyTye.setCellValue(obj.getInvPartyTye() + "");
                //----- currCode
                Cell cellCurrCode = row.createCell(cellnum++);
                cellCurrCode.setCellValue(obj.getCurrCode() + "");
                //----- depsitTerm
                Cell cellDepsitTerm = row.createCell(cellnum++);
                cellDepsitTerm.setCellValue(obj.getDepsitTerm() + "");
                //----- depsitTerm
                Cell cellDepsitTermUnit = row.createCell(cellnum++);
                cellDepsitTermUnit.setCellValue(obj.getDepsitTermUnt() + "");
                //----- balTierAmt
                Cell cellBalTierAmt = row.createCell(cellnum++);
                cellBalTierAmt.setCellValue(obj.getBalTierAmt() + "");
                //----- interestRate
                Cell cellInterestRate = row.createCell(cellnum++);
                cellInterestRate.setCellValue(obj.getInterestRate() + "");
                //----- effective Date
                Cell cellEffectiveDate = row.createCell(cellnum++);
                cellEffectiveDate.setCellValue(obj.getEffectiveDate() + "");
                //----- endDate
                Cell cellEndDate = row.createCell(cellnum++);
                cellEndDate.setCellValue(obj.getEndDate() + "");
                //----- seq
                Cell cellSeq = row.createCell(cellnum++);
                cellSeq.setCellValue(obj.getSeq() + "");
                //----- updBy
                Cell cellUpdBy = row.createCell(cellnum++);
                cellUpdBy.setCellValue(obj.getUpdBy() + "");
                //----- updDate
                Cell cellUpdDate = row.createCell(cellnum++);
                cellUpdDate.setCellValue(obj.getUpdDate() + "");
                //----- sysCode
                Cell cellSysCode = row.createCell(cellnum++);
                cellSysCode.setCellValue(obj.getSysCode() + "");
            }
        }

        ByteArrayOutputStream boas = new ByteArrayOutputStream();
        myWorkBook.write(boas);
        setExcelStream(new ByteArrayInputStream(boas.toByteArray()));

        logger.debug("exportExcel : end...");
        return "excel";
    }

    public void readExcel() throws Exception {
        try {

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }

    public String getDataSetDate() {
        return dataSetDate;
    }

    public void setDataSetDate(String dataSetDate) {
        this.dataSetDate = dataSetDate;
    }

    public PaggingList getPaging() {
        return paging;
    }

    public void setPaging(PaggingList paging) {
        this.paging = paging;
    }

    /**
     * @return the systemCode
     */
    public String getSystemCode() {
        return systemCode;
    }

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

    /**
     * @return the resultSize
     */
    public int getResultSize() {
        return resultSize;
    }

    /**
     * @param resultSize the resultSize to set
     */
    public void setResultSize(int resultSize) {
        this.resultSize = resultSize;
    }

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

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

    //----- UPLOAD
    public File getToBeUploaded() {
        return toBeUploaded;
    }

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

    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

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

    /**
     * @return the excelStream
     */
    public InputStream getExcelStream() {
        return excelStream;
    }

    /**
     * @param excelStream the excelStream to set
     */
    public void setExcelStream(InputStream excelStream) {
        this.excelStream = excelStream;
    }
}