| 21786 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
|
|
4 |
import java.io.InputStream;
|
| 22470 |
ashik.ali |
5 |
import java.io.OutputStream;
|
| 21786 |
ashik.ali |
6 |
import java.time.LocalDateTime;
|
|
|
7 |
import java.time.ZoneId;
|
|
|
8 |
import java.util.ArrayList;
|
|
|
9 |
import java.util.Date;
|
|
|
10 |
import java.util.List;
|
|
|
11 |
|
|
|
12 |
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
| 22470 |
ashik.ali |
13 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
14 |
import org.apache.poi.ss.usermodel.CellStyle;
|
| 21786 |
ashik.ali |
15 |
import org.apache.poi.ss.usermodel.CellType;
|
| 22470 |
ashik.ali |
16 |
import org.apache.poi.ss.usermodel.Font;
|
|
|
17 |
import org.apache.poi.ss.usermodel.Row;
|
| 21786 |
ashik.ali |
18 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
| 22470 |
ashik.ali |
19 |
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
20 |
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
|
|
21 |
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
| 22247 |
amit.gupta |
22 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
| 21786 |
ashik.ali |
23 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
24 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| 22226 |
amit.gupta |
25 |
import org.slf4j.Logger;
|
|
|
26 |
import org.slf4j.LoggerFactory;
|
| 21786 |
ashik.ali |
27 |
|
|
|
28 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22470 |
ashik.ali |
29 |
import com.spice.profitmandi.common.model.InventoryItemAgingModel;
|
|
|
30 |
import com.spice.profitmandi.common.model.InventoryItemAgingValue;
|
| 21786 |
ashik.ali |
31 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
32 |
import com.spice.profitmandi.common.model.TagListingModel;
|
|
|
33 |
|
|
|
34 |
public class ExcelUtils {
|
|
|
35 |
private static final String TAG_ID = "Tag Id";
|
|
|
36 |
private static final String TAG_LABEL = "Tag Label";
|
|
|
37 |
private static final String ITEM_ID = "Item Id";
|
|
|
38 |
private static final String BRAND = "Brand";
|
|
|
39 |
private static final String MODEL_NAME = "Model Name";
|
|
|
40 |
private static final String MODEL_NUMBER = "Model Number";
|
|
|
41 |
private static final String COLOR = "Color";
|
|
|
42 |
private static final String SELLING_PRICE = "Selling Price";
|
| 22204 |
amit.gupta |
43 |
private static final String MOP = "MOP";
|
| 21786 |
ashik.ali |
44 |
private static final String SUPPORT_PRICE = "Support Price";
|
|
|
45 |
private static final String START_DATE = "Start Date";
|
|
|
46 |
private static final String TAG_LISTING = "Tag Listing";
|
|
|
47 |
|
| 22226 |
amit.gupta |
48 |
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
|
| 21786 |
ashik.ali |
49 |
|
|
|
50 |
public static void main(String[] args) throws Throwable{
|
| 22470 |
ashik.ali |
51 |
//List<Integer> intervals = Arrays.asList(5, 10, 15, 20, 25);
|
|
|
52 |
//writeInventoryItemAgingModels(inventoryItemAgingModels, intervals);
|
| 21786 |
ashik.ali |
53 |
}
|
| 22247 |
amit.gupta |
54 |
public static List<TagListingModel> parse(InputStream inputStream) throws Throwable {
|
| 21786 |
ashik.ali |
55 |
|
|
|
56 |
List<TagListingModel> tagListings = new ArrayList<>();
|
|
|
57 |
XSSFWorkbook myWorkBook = null;
|
|
|
58 |
try{
|
|
|
59 |
//FileInputStream fileInputStream = new FileInputStream("/home/ashikali/tag_listing1.xlsx");
|
|
|
60 |
myWorkBook = new XSSFWorkbook (inputStream);
|
|
|
61 |
|
|
|
62 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
63 |
// Return first sheet from the XLSX workbook
|
|
|
64 |
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
|
| 22247 |
amit.gupta |
65 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
| 21786 |
ashik.ali |
66 |
|
| 22247 |
amit.gupta |
67 |
for(int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++){
|
|
|
68 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
69 |
LOGGER.info("row {}", row);
|
|
|
70 |
TagListingModel tagListing = new TagListingModel();
|
|
|
71 |
if(row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
72 |
tagListing.setTagId((Double.valueOf(row.getCell(0).getNumericCellValue())).intValue());
|
|
|
73 |
}else{
|
|
|
74 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(TAG_ID, row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
| 22226 |
amit.gupta |
75 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
| 21786 |
ashik.ali |
76 |
throw profitMandiBusinessException;
|
|
|
77 |
}
|
| 22247 |
amit.gupta |
78 |
|
|
|
79 |
if(row.getCell(2) != null && row.getCell(2).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
80 |
tagListing.setItemId(Double.valueOf(row.getCell(2).toString()).intValue());
|
|
|
81 |
}else{
|
|
|
82 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ITEM_ID, row.getCell(2).toString(), "TGLSTNG_VE_1010");
|
|
|
83 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
84 |
throw profitMandiBusinessException;
|
|
|
85 |
}
|
| 22204 |
amit.gupta |
86 |
|
| 22247 |
amit.gupta |
87 |
if(row.getCell(7) != null && row.getCell(7).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
88 |
tagListing.setSellingPrice(Double.valueOf(row.getCell(7).toString()).floatValue());
|
|
|
89 |
}else{
|
|
|
90 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SELLING_PRICE, row.getCell(7), "TGLSTNG_VE_1010");
|
| 22226 |
amit.gupta |
91 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
| 22209 |
amit.gupta |
92 |
throw profitMandiBusinessException;
|
| 21786 |
ashik.ali |
93 |
}
|
| 22247 |
amit.gupta |
94 |
if(row.getCell(8) != null && row.getCell(8).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
95 |
tagListing.setMop(Double.valueOf(row.getCell(8).toString()).floatValue());
|
|
|
96 |
}else{
|
|
|
97 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(MOP, row.getCell(8), "TGLSTNG_VE_1010");
|
|
|
98 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
99 |
throw profitMandiBusinessException;
|
|
|
100 |
}
|
|
|
101 |
if(row.getCell(9) != null && row.getCell(9).getCellTypeEnum() == CellType.NUMERIC){
|
|
|
102 |
tagListing.setSupportPrice(Double.valueOf(row.getCell(9).toString()).floatValue());
|
|
|
103 |
}else{
|
|
|
104 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(SUPPORT_PRICE, row.getCell(9).toString(), "TGLSTNG_VE_1010");
|
|
|
105 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
106 |
throw profitMandiBusinessException;
|
|
|
107 |
}
|
|
|
108 |
if(row.getCell(10) != null && HSSFDateUtil.isCellDateFormatted(row.getCell(10))){
|
|
|
109 |
Date date = row.getCell(10).getDateCellValue();
|
|
|
110 |
LocalDateTime startDate = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
|
|
111 |
tagListing.setStartDate(startDate);
|
|
|
112 |
}else{
|
|
|
113 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(START_DATE, row.getCell(10).toString(), "TGLSTNG_VE_1010");
|
|
|
114 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
115 |
throw profitMandiBusinessException;
|
|
|
116 |
}
|
|
|
117 |
tagListings.add(tagListing);
|
| 21786 |
ashik.ali |
118 |
}
|
|
|
119 |
myWorkBook.close();
|
| 22247 |
amit.gupta |
120 |
} catch(IOException ioException){
|
| 22206 |
amit.gupta |
121 |
ioException.printStackTrace();
|
| 21786 |
ashik.ali |
122 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(), "EXL_VE_1000");
|
| 22247 |
amit.gupta |
123 |
} finally {
|
| 21786 |
ashik.ali |
124 |
if(myWorkBook != null){
|
|
|
125 |
try {
|
|
|
126 |
myWorkBook.close();
|
|
|
127 |
} catch (IOException e) {
|
|
|
128 |
// TODO Auto-generated catch block
|
|
|
129 |
e.printStackTrace();
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
return tagListings;
|
|
|
134 |
}
|
| 22470 |
ashik.ali |
135 |
|
| 22486 |
ashik.ali |
136 |
public static OutputStream writeInventoryItemAgingModels(List<InventoryItemAgingModel> inventoryItemAgingModels, List<Integer> intervals, OutputStream outputStream){
|
| 22470 |
ashik.ali |
137 |
SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
138 |
|
|
|
139 |
//CreationHelper createHelper = workbook.getCreationHelper();
|
|
|
140 |
|
|
|
141 |
SXSSFSheet sheet = workbook.createSheet("InventoryItemAging");
|
|
|
142 |
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
|
|
|
143 |
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
|
|
|
144 |
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
|
|
|
145 |
sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
|
|
|
146 |
sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));
|
|
|
147 |
sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
|
|
|
148 |
sheet.trackAllColumnsForAutoSizing();
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
Row rowHeader = sheet.createRow(0);
|
|
|
152 |
Cell cellItemIdHeader = rowHeader.createCell(0);
|
|
|
153 |
cellItemIdHeader.setCellValue("Item Id");
|
|
|
154 |
Cell cellBrandHeader = rowHeader.createCell(1);
|
|
|
155 |
cellBrandHeader.setCellValue("Brand");
|
|
|
156 |
Cell cellModelNameHeader = rowHeader.createCell(2);
|
|
|
157 |
cellModelNameHeader.setCellValue("Model Name");
|
|
|
158 |
Cell cellModelNumberHeader = rowHeader.createCell(3);
|
|
|
159 |
cellModelNumberHeader.setCellValue("Model Number");
|
|
|
160 |
Cell cellColorHeader = rowHeader.createCell(4);
|
|
|
161 |
cellColorHeader.setCellValue("Color");
|
|
|
162 |
Cell cellTypeHeader = rowHeader.createCell(5);
|
|
|
163 |
cellTypeHeader.setCellValue("Item Type");
|
|
|
164 |
Row rowPriceQuantity = sheet.createRow(1);
|
|
|
165 |
for(int index = 0, colIndex = 6; index < intervals.size() + 1; index++, colIndex = colIndex + 2){
|
|
|
166 |
Cell cellHeader = rowHeader.createCell(colIndex);
|
|
|
167 |
if(index == 0){
|
|
|
168 |
cellHeader.setCellValue("Less Than "+intervals.get(index)+" Days");
|
|
|
169 |
}else if(index < intervals.size()){
|
|
|
170 |
cellHeader.setCellValue(intervals.get(index - 1) + " - " + intervals.get(index) + " Days");
|
|
|
171 |
}else{
|
|
|
172 |
cellHeader.setCellValue("More Than "+intervals.get(index - 1)+" Days");
|
|
|
173 |
}
|
|
|
174 |
sheet.addMergedRegion(new CellRangeAddress(0, 0, colIndex, colIndex + 1));
|
|
|
175 |
rowHeader.createCell(colIndex + 1);
|
|
|
176 |
Cell cellPrice = rowPriceQuantity.createCell(colIndex);
|
|
|
177 |
cellPrice.setCellValue("Price");
|
|
|
178 |
Cell cellQuantity = rowPriceQuantity.createCell(colIndex + 1);
|
|
|
179 |
cellQuantity.setCellValue("Quantity");
|
|
|
180 |
}
|
|
|
181 |
Font font = workbook.createFont();
|
|
|
182 |
CellStyle cellStyle = workbook.createCellStyle();
|
|
|
183 |
font.setBold(true);
|
|
|
184 |
//font.setFontHeight((short)16);
|
|
|
185 |
cellStyle.setFont(font);
|
| 22486 |
ashik.ali |
186 |
for(int i = 0; i < 8 + (intervals.size() * 2); i++){
|
| 22470 |
ashik.ali |
187 |
rowHeader.getCell(i).setCellStyle(cellStyle);
|
|
|
188 |
if(rowPriceQuantity.getCell(i) != null){
|
|
|
189 |
rowPriceQuantity.getCell(i).setCellStyle(cellStyle);
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
for(int index = 0; index < inventoryItemAgingModels.size(); index++){
|
|
|
194 |
InventoryItemAgingModel inventoryItemAgingModel = inventoryItemAgingModels.get(index);
|
|
|
195 |
Row rowValues = sheet.createRow(index + 2);
|
|
|
196 |
Cell cellItemId = rowValues.createCell(0);
|
|
|
197 |
cellItemId.setCellValue(inventoryItemAgingModel.getItemId());
|
|
|
198 |
Cell cellBrand = rowValues.createCell(1);
|
|
|
199 |
cellBrand.setCellValue(inventoryItemAgingModel.getBrand());
|
|
|
200 |
Cell cellModelName = rowValues.createCell(2);
|
|
|
201 |
cellModelName.setCellValue(inventoryItemAgingModel.getModelName());
|
|
|
202 |
Cell cellModelNumber = rowValues.createCell(3);
|
|
|
203 |
cellModelNumber.setCellValue(inventoryItemAgingModel.getModelNumber());
|
|
|
204 |
Cell cellColor = rowValues.createCell(4);
|
|
|
205 |
cellColor.setCellValue(inventoryItemAgingModel.getColor());
|
|
|
206 |
Cell cellType = rowValues.createCell(5);
|
|
|
207 |
cellType.setCellValue(inventoryItemAgingModel.getItemType().toString());
|
|
|
208 |
List<InventoryItemAgingValue> inventoryItemAgingValues = inventoryItemAgingModel.getValues();
|
|
|
209 |
//LOGGER.info("inventoryItemAgingValues {}", inventoryItemAgingValues);
|
|
|
210 |
for(int i = 0, colIndex = 6; i < inventoryItemAgingValues.size(); i++, colIndex = colIndex + 2){
|
|
|
211 |
Cell cellPrice = rowValues.createCell(colIndex);
|
|
|
212 |
InventoryItemAgingValue inventoryItemAgingValue = inventoryItemAgingValues.get(i);
|
|
|
213 |
//LOGGER.info("inventoryItemAgingValue {}", inventoryItemAgingValue);
|
|
|
214 |
Cell cellQuantity = rowValues.createCell(colIndex + 1);
|
|
|
215 |
if(inventoryItemAgingValue != null){
|
|
|
216 |
cellPrice.setCellValue(inventoryItemAgingValue.getPrice());
|
|
|
217 |
cellQuantity.setCellValue(inventoryItemAgingValue.getQuantity());
|
|
|
218 |
}else{
|
|
|
219 |
cellPrice.setCellValue("NA");
|
|
|
220 |
cellQuantity.setCellValue("NA");
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
| 22486 |
ashik.ali |
225 |
for(int index = 0; index < 8 + (intervals.size() * 2); index++){
|
| 22470 |
ashik.ali |
226 |
sheet.autoSizeColumn(index);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
try{
|
| 22486 |
ashik.ali |
230 |
workbook.write(outputStream);
|
| 22470 |
ashik.ali |
231 |
workbook.close();
|
|
|
232 |
}catch(IOException ioException){
|
| 22486 |
ashik.ali |
233 |
LOGGER.error("Unable to generate excel file", ioException);
|
| 22470 |
ashik.ali |
234 |
}
|
|
|
235 |
return null;
|
|
|
236 |
}
|
|
|
237 |
|
| 21786 |
ashik.ali |
238 |
}
|