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.Datasetfrw; import com.kcs.service.DatasetFrwService; 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 FrwImportAction extends BaseAction { static Logger logger = Logger.getLogger(FrwImportAction.class); private File toBeUploaded; private String toBeUploadedFileName; private String toBeUploadedContentType; private HttpServletRequest servletRequest; private PaggingList paging; private String dataSetDate; private List<Datasetfrw> resultsFromExcel; private String checkDatasetDate; @Autowired private DatasetFrwService datasetFrwService; private List<Datasetfrw> 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_FRW"); 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<Datasetfrw>()); int checkColName = 0; setCheckDatasetDate("true"); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); if (checkColName != 0) { Datasetfrw datasetfrw = new Datasetfrw(); 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; datasetfrw.setOrgId(row.getCell(index++).toString()); datasetfrw.setDataSetDate(convertDate(row.getCell(index++).toString())); datasetfrw.setCurrCode(row.getCell(index++).toString()); datasetfrw.setCommInLieuRateForDepsit(row.getCell(index++).toString()); datasetfrw.setMinCommInLieuForDepsit(row.getCell(index++).toString()); datasetfrw.setMaxCommInLieuForDepsit(row.getCell(index++).toString()); datasetfrw.setInwTransfFeeForDepsit(row.getCell(index++).toString()); datasetfrw.setMinInwTransfFeeForDepsit(row.getCell(index++).toString()); datasetfrw.setMaxInwTransfFeeForDepsit(row.getCell(index++).toString()); datasetfrw.setOthFeeDescForDepsit(row.getCell(index++).toString()); datasetfrw.setCommInLieuRateForWithdw(row.getCell(index++).toString()); datasetfrw.setMinCommInLieuRateFWithdw(row.getCell(index++).toString()); datasetfrw.setMaxCommInLieuRateFWithdw(row.getCell(index++).toString()); datasetfrw.setWithdwFeeForOthTransf(row.getCell(index++).toString()); datasetfrw.setEffectiveDate(convertDate(row.getCell(index++).toString())); datasetfrw.setEndDate(convertDate(row.getCell(index++).toString())); datasetfrw.setSeq(Long.valueOf(row.getCell(index++).toString())); datasetfrw.setUpdBy(getCurrentLoginId()); datasetfrw.setUpdDate(DateUtil.getCurrentDateTime()); index++; index++; datasetfrw.setSysCode(row.getCell(index++).toString()); getResultsFromExcel().add(datasetfrw); } checkColName++; } setResultsFromExcel(getResultsFromExcel()); session.put("EXCEL_TO_LIST_FRW", getResultsFromExcel()); } catch (IOException ex) { ex.printStackTrace(); logger.error("Error >>> " + ex.getMessage()); } } public String updateExcelToDB() throws Exception { logger.debug("= = = = = Into updateExcelToDB = = = = ="); setResultsFromExcel((List<Datasetfrw>) session.get("EXCEL_TO_LIST_FRW")); if (getCheckDatasetDate().equalsIgnoreCase("true")) { logger.debug("check datasetdate pass === " + getDataSetDate()); getDatasetFrwService().updateByList(getResultsFromExcel(), getDataSetDate()); setPaging(new PaggingList()); getPaging().setIndex(1); getPaging().setPageSize(10); setPaging(getDatasetFrwService().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; } } /** * @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; } /** * @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<Datasetfrw> getResultsFromExcel() { return resultsFromExcel; } /** * @param resultsFromExcel the resultsFromExcel to set */ public void setResultsFromExcel(List<Datasetfrw> 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 datasetFrwService */ public DatasetFrwService getDatasetFrwService() { return datasetFrwService; } /** * @param datasetFrwService the datasetFrwService to set */ public void setDatasetFrwService(DatasetFrwService datasetFrwService) { this.datasetFrwService = datasetFrwService; } /** * @return the resultList */ public List<Datasetfrw> getResultList() { return resultList; } /** * @param resultList the resultList to set */ public void setResultList(List<Datasetfrw> resultList) { this.resultList = resultList; } /** * @param servletRequest the servletRequest to set */ @Override public void setServletRequest(HttpServletRequest servletRequest) { this.servletRequest = servletRequest; } }