| 21786 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
|
|
4 |
import java.io.InputStream;
|
|
|
5 |
import java.time.LocalDateTime;
|
|
|
6 |
import java.time.ZoneId;
|
|
|
7 |
import java.util.ArrayList;
|
|
|
8 |
import java.util.Date;
|
|
|
9 |
import java.util.List;
|
|
|
10 |
|
|
|
11 |
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
| 22247 |
amit.gupta |
12 |
import org.apache.poi.sl.usermodel.Sheet;
|
| 21786 |
ashik.ali |
13 |
import org.apache.poi.ss.usermodel.CellType;
|
|
|
14 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
| 22247 |
amit.gupta |
15 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
| 21786 |
ashik.ali |
16 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
17 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| 22226 |
amit.gupta |
18 |
import org.slf4j.Logger;
|
|
|
19 |
import org.slf4j.LoggerFactory;
|
| 21786 |
ashik.ali |
20 |
|
|
|
21 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
22 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
23 |
import com.spice.profitmandi.common.model.TagListingModel;
|
|
|
24 |
|
|
|
25 |
public class ExcelUtils {
|
|
|
26 |
private static final String TAG_ID = "Tag Id";
|
|
|
27 |
private static final String TAG_LABEL = "Tag Label";
|
|
|
28 |
private static final String ITEM_ID = "Item Id";
|
|
|
29 |
private static final String BRAND = "Brand";
|
|
|
30 |
private static final String MODEL_NAME = "Model Name";
|
|
|
31 |
private static final String MODEL_NUMBER = "Model Number";
|
|
|
32 |
private static final String COLOR = "Color";
|
|
|
33 |
private static final String SELLING_PRICE = "Selling Price";
|
| 22204 |
amit.gupta |
34 |
private static final String MOP = "MOP";
|
| 21786 |
ashik.ali |
35 |
private static final String SUPPORT_PRICE = "Support Price";
|
|
|
36 |
private static final String START_DATE = "Start Date";
|
|
|
37 |
private static final String TAG_LISTING = "Tag Listing";
|
|
|
38 |
|
| 22226 |
amit.gupta |
39 |
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
|
| 21786 |
ashik.ali |
40 |
|
|
|
41 |
public static void main(String[] args) throws Throwable{
|
|
|
42 |
parse(null);
|
|
|
43 |
}
|
| 22247 |
amit.gupta |
44 |
public static List<TagListingModel> parse(InputStream inputStream) throws Throwable {
|
| 21786 |
ashik.ali |
45 |
|
|
|
46 |
List<TagListingModel> tagListings = new ArrayList<>();
|
|
|
47 |
XSSFWorkbook myWorkBook = null;
|
|
|
48 |
try{
|
|
|
49 |
//FileInputStream fileInputStream = new FileInputStream("/home/ashikali/tag_listing1.xlsx");
|
|
|
50 |
myWorkBook = new XSSFWorkbook (inputStream);
|
|
|
51 |
|
|
|
52 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
53 |
// Return first sheet from the XLSX workbook
|
|
|
54 |
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
|
| 22247 |
amit.gupta |
55 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
| 21786 |
ashik.ali |
56 |
|
| 22247 |
amit.gupta |
57 |
for(int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++){
|
|
|
58 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
59 |
LOGGER.info("row {}", row);
|
|
|
60 |
TagListingModel tagListing = new TagListingModel();
|
|
|
61 |
if(row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
62 |
tagListing.setTagId((Double.valueOf(row.getCell(0).getNumericCellValue())).intValue());
|
|
|
63 |
}else{
|
|
|
64 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_ID, row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
| 22226 |
amit.gupta |
65 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
| 21786 |
ashik.ali |
66 |
throw profitMandiBusinessException;
|
|
|
67 |
}
|
| 22247 |
amit.gupta |
68 |
|
|
|
69 |
if(row.getCell(2) != null && row.getCell(2).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
70 |
tagListing.setItemId(Double.valueOf(row.getCell(2).toString()).intValue());
|
|
|
71 |
}else{
|
|
|
72 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ITEM_ID, row.getCell(2).toString(), "TGLSTNG_VE_1010");
|
|
|
73 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
74 |
throw profitMandiBusinessException;
|
|
|
75 |
}
|
| 22204 |
amit.gupta |
76 |
|
| 22247 |
amit.gupta |
77 |
if(row.getCell(7) != null && row.getCell(7).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
78 |
tagListing.setSellingPrice(Double.valueOf(row.getCell(7).toString()).floatValue());
|
|
|
79 |
}else{
|
|
|
80 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SELLING_PRICE, row.getCell(7), "TGLSTNG_VE_1010");
|
| 22226 |
amit.gupta |
81 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
| 22209 |
amit.gupta |
82 |
throw profitMandiBusinessException;
|
| 21786 |
ashik.ali |
83 |
}
|
| 22247 |
amit.gupta |
84 |
if(row.getCell(8) != null && row.getCell(8).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
85 |
tagListing.setMop(Double.valueOf(row.getCell(8).toString()).floatValue());
|
|
|
86 |
}else{
|
|
|
87 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(MOP, row.getCell(8), "TGLSTNG_VE_1010");
|
|
|
88 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
89 |
throw profitMandiBusinessException;
|
|
|
90 |
}
|
|
|
91 |
if(row.getCell(9) != null && row.getCell(9).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
92 |
tagListing.setSupportPrice(Double.valueOf(row.getCell(9).toString()).floatValue());
|
|
|
93 |
}else{
|
|
|
94 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SUPPORT_PRICE, row.getCell(9).toString(), "TGLSTNG_VE_1010");
|
|
|
95 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
96 |
throw profitMandiBusinessException;
|
|
|
97 |
}
|
|
|
98 |
if(row.getCell(10) != null && HSSFDateUtil.isCellDateFormatted(row.getCell(10))){
|
|
|
99 |
Date date = row.getCell(10).getDateCellValue();
|
|
|
100 |
LocalDateTime startDate = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
|
|
101 |
tagListing.setStartDate(startDate);
|
|
|
102 |
}else{
|
|
|
103 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(START_DATE, row.getCell(10).toString(), "TGLSTNG_VE_1010");
|
|
|
104 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
105 |
throw profitMandiBusinessException;
|
|
|
106 |
}
|
|
|
107 |
tagListings.add(tagListing);
|
| 21786 |
ashik.ali |
108 |
}
|
|
|
109 |
myWorkBook.close();
|
| 22247 |
amit.gupta |
110 |
} catch(IOException ioException){
|
| 22206 |
amit.gupta |
111 |
ioException.printStackTrace();
|
| 21786 |
ashik.ali |
112 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(), "EXL_VE_1000");
|
| 22247 |
amit.gupta |
113 |
} finally {
|
| 21786 |
ashik.ali |
114 |
if(myWorkBook != null){
|
|
|
115 |
try {
|
|
|
116 |
myWorkBook.close();
|
|
|
117 |
} catch (IOException e) {
|
|
|
118 |
// TODO Auto-generated catch block
|
|
|
119 |
e.printStackTrace();
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
return tagListings;
|
|
|
124 |
}
|
|
|
125 |
}
|