Java tutorial
/* * 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.Datasetirf; 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.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 IrfImportAction extends BaseAction { static Logger logger = Logger.getLogger(IrfImportAction.class); private File toBeUploaded; private String toBeUploadedFileName; private String toBeUploadedContentType; private HttpServletRequest servletRequest; private PaggingList paging; private String dataSetDate; private List<Datasetirf> resultsFromExcel; private String checkDatasetDate; @Autowired private DatasetIrfService datasetIrfService; private List<Datasetirf> resultList; @Override public String success() throws Exception { logger.debug("= = = = = Into success = = = = ="); if (Utility.isNull(paging)) { paging = new PaggingList(); } return SUCCESS; } @Override public String retrieve() throws Exception { logger.debug(request.getParameterMap()); paging = datasetIrfService.findListIrf(createPaginate(), dataSetDate, ""); return super.search(); } public String readExcel() throws Exception { session.remove("EXCEL_TO_LIST"); logger.debug("= = = = = Into readExcel = = = = ="); excelToList(); return SUCCESS; } public void excelToList() throws Exception { String filePath = servletRequest.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(); resultsFromExcel = new ArrayList<Datasetirf>(); int checkColName = 0; setCheckDatasetDate("true"); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); if (checkColName != 0) { Datasetirf datasetirf = new Datasetirf(); Date dateFromJsp = DateUtil.convertDateFromJsp(dataSetDate); Date dateFromExcel = DateUtil.getDateFromString(row.getCell(1).toString(), DateUtil.DATE_FORMAT_YYYYMMDDX); if (dateFromJsp.compareTo(dateFromExcel) != 0) { setCheckDatasetDate("false"); break; } datasetirf.setOrgId(row.getCell(0).toString()); datasetirf.setDataSetDate( DateUtil.parse(row.getCell(1).toString(), DateUtil.DATE_FORMAT_YYYYMMDDX)); datasetirf.setArrgmentTye(row.getCell(2).toString()); datasetirf.setInvPartyTye(row.getCell(3).toString()); datasetirf.setCurrCode(row.getCell(4).toString()); datasetirf.setDepsitTerm(Long.valueOf(row.getCell(5).toString())); datasetirf.setDepsitTermUnt(row.getCell(6).toString()); datasetirf.setBalTierAmt(row.getCell(7).toString()); datasetirf .setInterestRate(NumberUtil.convertToBigDecimal(row.getCell(8).toString()).setScale(2)); datasetirf.setEffectiveDate( DateUtil.parse(row.getCell(9).toString(), DateUtil.DATE_FORMAT_YYYYMMDDX)); if (!row.getCell(10).toString().equals("null") || row.getCell(10).toString().equals(null)) { datasetirf.setEndDate( DateUtil.parse(row.getCell(10).toString(), DateUtil.DATE_FORMAT_YYYYMMDDX)); } else { datasetirf.setEndDate(null); } datasetirf.setSeq(Long.valueOf(row.getCell(11).toString())); datasetirf.setUpdBy(getCurrentLoginId()); datasetirf.setUpdDate(DateUtil.getCurrentDateTime()); datasetirf.setSysCode(row.getCell(14).toString()); resultsFromExcel.add(datasetirf); } checkColName++; } setResultsFromExcel(resultsFromExcel); session.put("EXCEL_TO_LIST", resultsFromExcel); } catch (IOException ex) { ex.printStackTrace(); logger.error("Error >>> " + ex.getMessage()); } } public String updateExcelToDB() throws Exception { logger.debug("= = = = = Into updateExcelToDB = = = = ="); resultsFromExcel = (List<Datasetirf>) session.get("EXCEL_TO_LIST"); if (checkDatasetDate.equalsIgnoreCase("true")) { logger.debug("check datasetdate pass === " + dataSetDate); getDatasetIrfService().updateByList(resultsFromExcel, dataSetDate); paging = new PaggingList(); paging.setIndex(1); paging.setPageSize(10); paging = getDatasetIrfService().findListIrf(paging, dataSetDate, ""); } setCheckDatasetDate(""); return SUCCESS; } 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 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<Datasetirf> getResultsFromExcel() { return resultsFromExcel; } /** * @param resultsFromExcel the resultsFromExcel to set */ public void setResultsFromExcel(List<Datasetirf> 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 datasetIrfService */ public DatasetIrfService getDatasetIrfService() { return datasetIrfService; } /** * @param datasetIrfService the datasetIrfService to set */ public void setDatasetIrfService(DatasetIrfService datasetIrfService) { this.datasetIrfService = datasetIrfService; } /** * @return the resultList */ public List<Datasetirf> getResultList() { return resultList; } /** * @param resultList the resultList to set */ public void setResultList(List<Datasetirf> resultList) { this.resultList = resultList; } }