Subversion Repositories SmartDukaan

Rev

Rev 22209 | Rev 22227 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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