| 3364 |
chandransh |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import java.io.ByteArrayOutputStream;
|
|
|
4 |
import java.io.File;
|
|
|
5 |
import java.io.FileNotFoundException;
|
|
|
6 |
import java.io.FileOutputStream;
|
|
|
7 |
import java.io.IOException;
|
|
|
8 |
import java.util.ArrayList;
|
|
|
9 |
import java.util.Calendar;
|
|
|
10 |
import java.util.GregorianCalendar;
|
|
|
11 |
import java.util.List;
|
|
|
12 |
import java.util.Map;
|
|
|
13 |
|
|
|
14 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
|
|
15 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
16 |
import in.shop2020.model.v1.catalog.status;
|
|
|
17 |
import in.shop2020.support.utils.FileUtils;
|
|
|
18 |
import in.shop2020.support.utils.ReportsUtils;
|
|
|
19 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
20 |
|
|
|
21 |
import javax.servlet.ServletContext;
|
|
|
22 |
import javax.servlet.ServletOutputStream;
|
|
|
23 |
import javax.servlet.http.HttpServletRequest;
|
|
|
24 |
import javax.servlet.http.HttpServletResponse;
|
|
|
25 |
import javax.servlet.http.HttpSession;
|
|
|
26 |
|
|
|
27 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
28 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
29 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
30 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
31 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
32 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
33 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
34 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
35 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
36 |
import org.apache.thrift.TException;
|
|
|
37 |
import org.apache.thrift.transport.TTransportException;
|
|
|
38 |
import org.slf4j.Logger;
|
|
|
39 |
import org.slf4j.LoggerFactory;
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
@InterceptorRefs({
|
|
|
43 |
@InterceptorRef("defaultStack"),
|
|
|
44 |
@InterceptorRef("login")
|
|
|
45 |
})
|
|
|
46 |
|
|
|
47 |
public class StockReportsController implements ServletRequestAware, ServletResponseAware, ServletContextAware{
|
|
|
48 |
|
|
|
49 |
private static Logger logger = LoggerFactory.getLogger(StockReportsController.class);
|
|
|
50 |
|
|
|
51 |
private static final int ID = 0, BRAND = 1, MODEL_NUMBER = 2, MODEL_NAME = 3, COLOR = 4, QUANTITY = 5;
|
|
|
52 |
private static final String REPORT_DIR = "/inventory-report";
|
|
|
53 |
|
|
|
54 |
private HttpSession session;
|
|
|
55 |
private HttpServletRequest request;
|
|
|
56 |
private HttpServletResponse response;
|
|
|
57 |
private ServletContext context;
|
|
|
58 |
|
|
|
59 |
private String errorMsg = "";
|
|
|
60 |
|
|
|
61 |
public String index(){
|
|
|
62 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
63 |
return "exception";
|
|
|
64 |
}
|
|
|
65 |
return "index";
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public String create(){
|
|
|
69 |
|
|
|
70 |
Calendar date = new GregorianCalendar();
|
|
|
71 |
int year = date.get(Calendar.YEAR);
|
|
|
72 |
int month = date.get(Calendar.MONTH) +1;
|
|
|
73 |
int day = date.get(Calendar.DAY_OF_MONTH);
|
|
|
74 |
String filename = "inventory-report." + year + "-" + month + "-" + day + ".xls";
|
|
|
75 |
|
|
|
76 |
File inventoryReportFile = new File(REPORT_DIR + File.separator + filename);
|
|
|
77 |
|
|
|
78 |
if(!inventoryReportFile.exists()){
|
|
|
79 |
//Report doesn't exist. Need to generate it.
|
|
|
80 |
ByteArrayOutputStream baosXLS = generateInventoryStockReport();
|
|
|
81 |
if (baosXLS == null) {
|
|
|
82 |
errorMsg = "Could not get output";
|
|
|
83 |
return "index";
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
try {
|
|
|
87 |
FileOutputStream f = new FileOutputStream(inventoryReportFile);
|
|
|
88 |
baosXLS.writeTo(f);
|
|
|
89 |
f.close();
|
|
|
90 |
} catch (FileNotFoundException e) {
|
|
|
91 |
logger.error("Error while writing the inventory report", e);
|
|
|
92 |
return "index";
|
|
|
93 |
} catch (IOException e) {
|
|
|
94 |
logger.error("Error while writing the inventory report", e);
|
|
|
95 |
return "index";
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
response.setContentType("application/vnd.ms-excel");
|
|
|
100 |
response.setHeader("Content-disposition", "inline; filename=" + filename);
|
|
|
101 |
try {
|
|
|
102 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
103 |
baos.write(FileUtils.getBytesFromFile(new File(REPORT_DIR + File.separator + filename)));
|
|
|
104 |
ServletOutputStream sos = response.getOutputStream();
|
|
|
105 |
baos.writeTo(sos);
|
|
|
106 |
sos.flush();
|
|
|
107 |
errorMsg = "Report generated";
|
|
|
108 |
} catch (IOException e) {
|
|
|
109 |
logger.error("Unable to stream the inventory stock report", e);
|
|
|
110 |
errorMsg = "Failed to write to response.";
|
|
|
111 |
}
|
|
|
112 |
return "index";
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
private ByteArrayOutputStream generateInventoryStockReport(){
|
|
|
116 |
List<RowItem> outOfStockItems = new ArrayList<RowItem>();
|
|
|
117 |
List<RowItem> itemsDeliverableNextDay = new ArrayList<RowItem>();
|
|
|
118 |
List<RowItem> itemsNotDeliverableNextDay = new ArrayList<RowItem>();
|
|
|
119 |
|
|
|
120 |
CatalogClient catalogClientService;
|
|
|
121 |
try {
|
|
|
122 |
catalogClientService = new CatalogClient();
|
|
|
123 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
|
|
124 |
List<Item> items = client.getAllItems(false);
|
|
|
125 |
|
|
|
126 |
for(Item item : items){
|
|
|
127 |
if(!"Handsets".equals(item.getProductGroup()))
|
|
|
128 |
continue;
|
|
|
129 |
status state = item.getItemStatus();
|
|
|
130 |
switch(state){
|
|
|
131 |
case IN_PROCESS:
|
|
|
132 |
case CONTENT_COMPLETE:
|
|
|
133 |
case DELETED:
|
|
|
134 |
case PHASED_OUT:
|
|
|
135 |
continue;
|
|
|
136 |
case PAUSED:
|
|
|
137 |
case PAUSED_BY_RISK:
|
|
|
138 |
case ACTIVE:
|
|
|
139 |
RowItem rowItem = new RowItem(item);
|
|
|
140 |
if(state == status.ACTIVE){
|
|
|
141 |
if(rowItem.quantity >= 2)
|
|
|
142 |
itemsDeliverableNextDay.add(rowItem);
|
|
|
143 |
else
|
|
|
144 |
itemsNotDeliverableNextDay.add(rowItem);
|
|
|
145 |
}else
|
|
|
146 |
outOfStockItems.add(rowItem);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
} catch (TTransportException e) {
|
|
|
150 |
logger.error("Unable to get the items from the inventory", e);
|
|
|
151 |
return null;
|
|
|
152 |
} catch (InventoryServiceException e) {
|
|
|
153 |
logger.error("Error while getting the items from the inventory", e);
|
|
|
154 |
return null;
|
|
|
155 |
} catch (TException e) {
|
|
|
156 |
logger.error("Error while getting the items from the inventory", e);
|
|
|
157 |
return null;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
161 |
|
|
|
162 |
Workbook wb = new HSSFWorkbook();
|
|
|
163 |
|
|
|
164 |
createSheet(wb, "Items Deliverable on Next Day", itemsDeliverableNextDay);
|
|
|
165 |
createSheet(wb, "Items Not Deliverable on Next Day", itemsNotDeliverableNextDay);
|
|
|
166 |
createSheet(wb, "Out of Stock Items", outOfStockItems);
|
|
|
167 |
|
|
|
168 |
try {
|
|
|
169 |
wb.write(baosXLS);
|
|
|
170 |
baosXLS.close();
|
|
|
171 |
} catch (IOException e) {
|
|
|
172 |
logger.error("Error while streaming inventory stock report", e);
|
|
|
173 |
return null;
|
|
|
174 |
}
|
|
|
175 |
return baosXLS;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
private void createSheet(Workbook wb, String name, List<RowItem> items){
|
|
|
179 |
Sheet sheet = wb.createSheet(name);
|
|
|
180 |
short serialNo = 0;
|
|
|
181 |
Row headerRow = sheet.createRow(serialNo++);
|
|
|
182 |
headerRow.createCell(ID).setCellValue("Item Id");
|
|
|
183 |
headerRow.createCell(BRAND).setCellValue("Brand");
|
|
|
184 |
headerRow.createCell(MODEL_NUMBER).setCellValue("Model Number");
|
|
|
185 |
headerRow.createCell(MODEL_NAME).setCellValue("Model Name");
|
|
|
186 |
headerRow.createCell(COLOR).setCellValue("Color");
|
|
|
187 |
headerRow.createCell(QUANTITY).setCellValue("Quantity");
|
|
|
188 |
|
|
|
189 |
for(RowItem item : items){
|
|
|
190 |
Row contentRow = sheet.createRow(serialNo++);
|
|
|
191 |
contentRow.createCell(ID).setCellValue(item.id);
|
|
|
192 |
contentRow.createCell(BRAND).setCellValue(item.brand);
|
|
|
193 |
contentRow.createCell(MODEL_NUMBER).setCellValue(item.modelNumber);
|
|
|
194 |
contentRow.createCell(MODEL_NAME).setCellValue(item.modelName);
|
|
|
195 |
contentRow.createCell(COLOR).setCellValue(item.color);
|
|
|
196 |
contentRow.createCell(QUANTITY).setCellValue(item.quantity);
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
public String getErrorMsg() {
|
|
|
201 |
return errorMsg;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
@Override
|
|
|
205 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
206 |
this.request = req;
|
|
|
207 |
this.session = req.getSession();
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
@Override
|
|
|
211 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
212 |
this.response = res;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
@Override
|
|
|
216 |
public void setServletContext(ServletContext context) {
|
|
|
217 |
this.context = context;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
public String getServletContextPath() {
|
|
|
221 |
return context.getContextPath();
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public static void main(String[] args) {
|
|
|
225 |
StockReportsController src = new StockReportsController();
|
|
|
226 |
try {
|
|
|
227 |
String userHome = System.getProperty("user.home");
|
|
|
228 |
FileOutputStream f = new FileOutputStream(userHome + "/stock-report.xls");
|
|
|
229 |
ByteArrayOutputStream baosXLS = src.generateInventoryStockReport();
|
|
|
230 |
baosXLS.writeTo(f);
|
|
|
231 |
f.close();
|
|
|
232 |
} catch (FileNotFoundException e) {
|
|
|
233 |
logger.error("Error creating inventory stock report", e);
|
|
|
234 |
} catch (IOException e) {
|
|
|
235 |
logger.error("IO error while creating inventory stock report", e);
|
|
|
236 |
}
|
|
|
237 |
System.out.println("Successfully generated the inventory stock report");
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
class RowItem {
|
|
|
241 |
public long id;
|
|
|
242 |
public String brand;
|
|
|
243 |
public String modelNumber;
|
|
|
244 |
public String modelName;
|
|
|
245 |
public String color;
|
|
|
246 |
public long quantity;
|
|
|
247 |
|
|
|
248 |
public RowItem(Item item){
|
|
|
249 |
id = item.getId();
|
|
|
250 |
brand = item.getBrand();
|
|
|
251 |
modelNumber = item.getModelNumber();
|
|
|
252 |
modelName = item.getModelName();
|
|
|
253 |
color = item.getColor();
|
|
|
254 |
quantity = 0;
|
|
|
255 |
for(Map.Entry<Long, Long> entry : item.getItemInventory().getAvailability().entrySet()){
|
|
|
256 |
quantity += entry.getValue();
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
}
|