com.kcs.action.report.BotExchangeRateAction.java Source code

Java tutorial

Introduction

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

import com.kcs.core.actions.BaseAction;
import com.kcs.core.service.ReportService;
import com.kcs.core.utilities.DateUtil;
import com.kcs.core.utilities.DmsConstant;
import com.kcs.core.utilities.PaggingList;
import com.kcs.core.utilities.StringUtil;
import com.kcs.model.BexRate;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author vorawats
 */
public class BotExchangeRateAction extends BaseAction {
    Logger logger = Logger.getLogger(BotExchangeRateAction.class);
    private String exchangeRateDate;
    private PaggingList pagging;
    @Autowired
    private ReportService reportService;
    private String fileName;
    private InputStream excelStream;

    @Override
    public String export() throws Exception {
        List<BexRate> list = reportService.findBotExRateForOject(exchangeRateDate);

        String tmp = exchangeRateDate.replaceAll("/", "");
        HSSFWorkbook myWorkBook = new HSSFWorkbook();
        HSSFSheet mySheet = myWorkBook.createSheet(tmp);
        setFileName(DateUtil.getCurrentDateString(tmp).concat(".xls"));

        int rowNum = 0;
        Row headerRow = mySheet.createRow(rowNum++);

        if (null != list && list.size() > 0) {
            /* create header excel */
            int i = 0;
            Map mapHedder = (Map) list.get(0);
            Set h = mapHedder.keySet();
            Iterator it = h.iterator();
            while (it.hasNext()) {
                String its = (String) it.next();
                Cell titleCell = headerRow.createCell(i++);
                titleCell.setCellValue(DmsConstant.BEX_RATE.valueOf(its).getDesc());
            }

            /* create content excel */
            HSSFRow myRow = null;
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                Map map = (Map) iter.next();
                Set s = map.keySet();
                Iterator o = s.iterator();
                myRow = mySheet.createRow(rowNum++);

                int j = 0;
                while (o.hasNext()) {
                    String its = (String) o.next();
                    String str = String.valueOf(map.get(its));
                    Cell cell = myRow.createCell(j++);
                    cell.setCellValue(str);
                }
            }
        }

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

        return "excel"; //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String success() throws Exception {
        if (null == pagging) {
            pagging = new PaggingList();
        }
        exchangeRateDate = DateUtil.getCurrentDateString(DateUtil.DEFAULT_DATE_FORMAT);

        return super.success(); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String search() throws Exception {
        pagging = reportService.findBotExRate(createPaginate(), exchangeRateDate);

        return super.search(); //To change body of generated methods, choose Tools | Templates.
    }

    public String getExchangeRateDate() {
        return exchangeRateDate;
    }

    public void setExchangeRateDate(String exchangeRateDate) {
        this.exchangeRateDate = exchangeRateDate;
    }

    public PaggingList getPagging() {
        return pagging;
    }

    public void setPagging(PaggingList pagging) {
        this.pagging = pagging;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public InputStream getExcelStream() {
        return excelStream;
    }

    public void setExcelStream(InputStream excelStream) {
        this.excelStream = excelStream;
    }
}