| 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;
|
|
|
12 |
import org.apache.poi.ss.usermodel.CellType;
|
|
|
13 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
14 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
|
|
15 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
16 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
17 |
|
|
|
18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
19 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
20 |
import com.spice.profitmandi.common.model.TagListingModel;
|
|
|
21 |
|
|
|
22 |
public class ExcelUtils {
|
|
|
23 |
private static final String TAG_ID = "Tag Id";
|
|
|
24 |
private static final String TAG_LABEL = "Tag Label";
|
|
|
25 |
private static final String ITEM_ID = "Item Id";
|
|
|
26 |
private static final String BRAND = "Brand";
|
|
|
27 |
private static final String MODEL_NAME = "Model Name";
|
|
|
28 |
private static final String MODEL_NUMBER = "Model Number";
|
|
|
29 |
private static final String COLOR = "Color";
|
|
|
30 |
private static final String SELLING_PRICE = "Selling Price";
|
| 22204 |
amit.gupta |
31 |
private static final String MOP = "MOP";
|
| 21786 |
ashik.ali |
32 |
private static final String SUPPORT_PRICE = "Support Price";
|
|
|
33 |
private static final String START_DATE = "Start Date";
|
|
|
34 |
private static final String TAG_LISTING = "Tag Listing";
|
|
|
35 |
|
|
|
36 |
//private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
|
|
|
37 |
|
|
|
38 |
public static void main(String[] args) throws Throwable{
|
|
|
39 |
parse(null);
|
|
|
40 |
}
|
|
|
41 |
public static List<TagListingModel> parse(InputStream inputStream) throws ProfitMandiBusinessException{
|
|
|
42 |
|
|
|
43 |
List<TagListingModel> tagListings = new ArrayList<>();
|
|
|
44 |
XSSFWorkbook myWorkBook = null;
|
|
|
45 |
try{
|
|
|
46 |
//FileInputStream fileInputStream = new FileInputStream("/home/ashikali/tag_listing1.xlsx");
|
|
|
47 |
myWorkBook = new XSSFWorkbook (inputStream);
|
|
|
48 |
|
|
|
49 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
50 |
// Return first sheet from the XLSX workbook
|
|
|
51 |
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
|
|
|
52 |
|
|
|
53 |
for(int rowNumber = mySheet.getFirstRowNum(); rowNumber <= mySheet.getLastRowNum(); rowNumber++){
|
|
|
54 |
Row row = mySheet.getRow(rowNumber);
|
|
|
55 |
if(rowNumber == 0 && row != null){
|
| 22204 |
amit.gupta |
56 |
if(row.getPhysicalNumberOfCells() == 11){
|
| 21786 |
ashik.ali |
57 |
if(!(TAG_ID.equals(row.getCell(0).toString()) || row.getCell(0).getCellTypeEnum() != CellType.STRING)){
|
|
|
58 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_ID, row.getCell(0).toString(), "TGLSTNG_VE_1000");
|
|
|
59 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
60 |
throw profitMandiBusinessException;
|
|
|
61 |
}
|
|
|
62 |
if(!(ITEM_ID.equals(row.getCell(2).toString()) || row.getCell(2).getCellTypeEnum() != CellType.STRING)){
|
|
|
63 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ITEM_ID, row.getCell(2).toString(), "TGLSTNG_VE_1002");
|
|
|
64 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
65 |
throw profitMandiBusinessException;
|
|
|
66 |
}
|
| 22204 |
amit.gupta |
67 |
if(!(SELLING_PRICE.equals(row.getCell(7).toString()) || row.getCell(7).getCellTypeEnum() != CellType.STRING)){
|
|
|
68 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SELLING_PRICE, row.getCell(7).toString(), "TGLSTNG_VE_1007");
|
| 21786 |
ashik.ali |
69 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
70 |
throw profitMandiBusinessException;
|
|
|
71 |
}
|
| 22204 |
amit.gupta |
72 |
if(!(MOP.equals(row.getCell(8).toString()) || row.getCell(8).getCellTypeEnum() != CellType.STRING)){
|
| 21786 |
ashik.ali |
73 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SELLING_PRICE, row.getCell(7).toString(), "TGLSTNG_VE_1007");
|
|
|
74 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
75 |
throw profitMandiBusinessException;
|
|
|
76 |
}
|
| 22204 |
amit.gupta |
77 |
if(!(SUPPORT_PRICE.equals(row.getCell(9).toString()) || row.getCell(9).getCellTypeEnum() != CellType.STRING)){
|
| 21786 |
ashik.ali |
78 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SUPPORT_PRICE, row.getCell(8).toString(), "TGLSTNG_VE_1008");
|
|
|
79 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
80 |
throw profitMandiBusinessException;
|
|
|
81 |
}
|
| 22204 |
amit.gupta |
82 |
if(!(START_DATE.equals(row.getCell(10).toString()) || row.getCell(10).getCellTypeEnum() != CellType.STRING)){
|
| 21786 |
ashik.ali |
83 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(START_DATE, row.getCell(9).toString(), "TGLSTNG_VE_1009");
|
|
|
84 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
85 |
throw profitMandiBusinessException;
|
|
|
86 |
}
|
|
|
87 |
}else{
|
|
|
88 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_LISTING, row.getPhysicalNumberOfCells(), "TGLSTNG_VE_1011");
|
|
|
89 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
90 |
throw profitMandiBusinessException;
|
|
|
91 |
}
|
|
|
92 |
}else if(rowNumber == 0 && row == null){
|
|
|
93 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_LISTING, "", "TGLSTNG_VE_1012");
|
|
|
94 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
95 |
throw profitMandiBusinessException;
|
|
|
96 |
// missing headers in first row
|
|
|
97 |
}
|
| 22208 |
amit.gupta |
98 |
if(rowNumber > 0 && row != null && row.getLastCellNum() == 11){
|
| 21786 |
ashik.ali |
99 |
TagListingModel tagListing = new TagListingModel();
|
|
|
100 |
if(row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
101 |
tagListing.setTagId((Double.valueOf(row.getCell(0).getNumericCellValue())).intValue());
|
|
|
102 |
}else{
|
|
|
103 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_ID, row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
|
|
104 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
105 |
throw profitMandiBusinessException;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
if(row.getCell(2) != null && row.getCell(2).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
110 |
tagListing.setItemId(Double.valueOf(row.getCell(2).toString()).intValue());
|
|
|
111 |
}else{
|
|
|
112 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ITEM_ID, row.getCell(2).toString(), "TGLSTNG_VE_1010");
|
|
|
113 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
114 |
throw profitMandiBusinessException;
|
|
|
115 |
}
|
| 22204 |
amit.gupta |
116 |
|
| 21786 |
ashik.ali |
117 |
if(row.getCell(7) != null && row.getCell(7).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
118 |
tagListing.setSellingPrice(Double.valueOf(row.getCell(7).toString()).floatValue());
|
|
|
119 |
}else{
|
|
|
120 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SELLING_PRICE, row.getCell(7), "TGLSTNG_VE_1010");
|
|
|
121 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
122 |
throw profitMandiBusinessException;
|
|
|
123 |
}
|
|
|
124 |
if(row.getCell(8) != null && row.getCell(8).getCellTypeEnum() == CellType.NUMERIC){
|
| 22204 |
amit.gupta |
125 |
tagListing.setMop(Double.valueOf(row.getCell(8).toString()).floatValue());
|
|
|
126 |
}else{
|
|
|
127 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(MOP, row.getCell(7), "TGLSTNG_VE_1010");
|
|
|
128 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
129 |
throw profitMandiBusinessException;
|
|
|
130 |
}
|
|
|
131 |
if(row.getCell(9) != null && row.getCell(9).getCellTypeEnum() == CellType.NUMERIC){
|
| 21786 |
ashik.ali |
132 |
tagListing.setSupportPrice(Double.valueOf(row.getCell(8).toString()).floatValue());
|
|
|
133 |
}else{
|
|
|
134 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SUPPORT_PRICE, row.getCell(8).toString(), "TGLSTNG_VE_1010");
|
|
|
135 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
136 |
throw profitMandiBusinessException;
|
|
|
137 |
}
|
| 22204 |
amit.gupta |
138 |
if(row.getCell(10) != null && HSSFDateUtil.isCellDateFormatted(row.getCell(10))){
|
|
|
139 |
Date date = row.getCell(10).getDateCellValue();
|
| 21786 |
ashik.ali |
140 |
LocalDateTime startDate = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
|
|
141 |
tagListing.setStartDate(startDate);
|
|
|
142 |
}else{
|
|
|
143 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(START_DATE, row.getCell(9).toString(), "TGLSTNG_VE_1010");
|
|
|
144 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
145 |
throw profitMandiBusinessException;
|
|
|
146 |
}
|
|
|
147 |
tagListings.add(tagListing);
|
| 22209 |
amit.gupta |
148 |
} else {
|
|
|
149 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_LISTING, "", "TGLSTNG_VE_1012");
|
|
|
150 |
//LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
151 |
throw profitMandiBusinessException;
|
| 21786 |
ashik.ali |
152 |
}
|
|
|
153 |
|
|
|
154 |
}
|
|
|
155 |
myWorkBook.close();
|
|
|
156 |
}catch(IOException ioException){
|
| 22206 |
amit.gupta |
157 |
ioException.printStackTrace();
|
| 21786 |
ashik.ali |
158 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(), "EXL_VE_1000");
|
|
|
159 |
}finally{
|
|
|
160 |
if(myWorkBook != null){
|
|
|
161 |
try {
|
|
|
162 |
myWorkBook.close();
|
|
|
163 |
} catch (IOException e) {
|
|
|
164 |
// TODO Auto-generated catch block
|
|
|
165 |
e.printStackTrace();
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
return tagListings;
|
|
|
170 |
}
|
|
|
171 |
}
|