| 12256 |
kshitij.so |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.catalog.CatalogService.Client;
|
|
|
4 |
import in.shop2020.model.v1.catalog.CompetitorPricing;
|
|
|
5 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
6 |
import in.shop2020.support.utils.ReportsUtils;
|
|
|
7 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
8 |
|
|
|
9 |
import java.io.BufferedInputStream;
|
|
|
10 |
import java.io.File;
|
|
|
11 |
import java.io.FileInputStream;
|
|
|
12 |
import java.io.FileNotFoundException;
|
|
|
13 |
import java.io.FileOutputStream;
|
|
|
14 |
import java.io.IOException;
|
|
|
15 |
import java.io.InputStream;
|
|
|
16 |
import java.util.Calendar;
|
|
|
17 |
import java.util.GregorianCalendar;
|
|
|
18 |
import java.util.HashMap;
|
|
|
19 |
import java.util.List;
|
|
|
20 |
import java.util.Map;
|
|
|
21 |
|
|
|
22 |
import javax.servlet.ServletContext;
|
|
|
23 |
import javax.servlet.ServletOutputStream;
|
|
|
24 |
import javax.servlet.http.HttpServletRequest;
|
|
|
25 |
import javax.servlet.http.HttpServletResponse;
|
|
|
26 |
import javax.servlet.http.HttpSession;
|
|
|
27 |
|
|
|
28 |
import org.apache.commons.io.FileUtils;
|
|
|
29 |
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
30 |
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
31 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
32 |
import org.apache.poi.ss.usermodel.Cell;
|
| 12260 |
kshitij.so |
33 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
34 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
35 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
36 |
import org.apache.struts2.convention.annotation.Results;
|
| 12256 |
kshitij.so |
37 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
38 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
39 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
40 |
import org.apache.thrift.TException;
|
|
|
41 |
import org.apache.thrift.transport.TTransportException;
|
|
|
42 |
import org.slf4j.Logger;
|
|
|
43 |
import org.slf4j.LoggerFactory;
|
|
|
44 |
|
|
|
45 |
import com.opensymphony.xwork2.ValidationAwareSupport;
|
|
|
46 |
|
| 12260 |
kshitij.so |
47 |
@SuppressWarnings({"unused","deprecation"})
|
|
|
48 |
|
|
|
49 |
@InterceptorRefs({
|
|
|
50 |
@InterceptorRef("defaultStack"),
|
|
|
51 |
@InterceptorRef("login")
|
|
|
52 |
})
|
|
|
53 |
@Results({
|
|
|
54 |
@Result(name = "redirect", location = "${url}", type = "redirect"),
|
|
|
55 |
@Result(name="authsuccess", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
56 |
})
|
|
|
57 |
|
| 12256 |
kshitij.so |
58 |
public class CompetitionScrapingController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
*
|
|
|
62 |
*/
|
|
|
63 |
|
|
|
64 |
private static Logger logger = LoggerFactory.getLogger(CompetitionScrapingController.class);
|
|
|
65 |
|
|
|
66 |
private HttpServletRequest request;
|
|
|
67 |
private HttpServletResponse response;
|
|
|
68 |
private HttpSession session;
|
|
|
69 |
private ServletContext context;
|
|
|
70 |
private static final long serialVersionUID = -4056242323967836986L;
|
|
|
71 |
private File file;
|
|
|
72 |
private List<CompetitorPricing> previousUploads;
|
|
|
73 |
private String[] snapdeal;
|
|
|
74 |
private String[] flipkart;
|
|
|
75 |
private String[] amazon;
|
|
|
76 |
private String[] itemId;
|
|
|
77 |
private String requestId;
|
|
|
78 |
private List<CompetitorPricing> data;
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
public String processItems() throws TException{
|
|
|
82 |
long uploadId = getTimeInMilliseconds();
|
|
|
83 |
String userEmail = getUserEmail();
|
|
|
84 |
Map<Long,CompetitorPricing> itemScrapingMap = new HashMap<Long,CompetitorPricing>();
|
|
|
85 |
for(int i=0;i<itemId.length;i++){
|
|
|
86 |
CompetitorPricing cp = new CompetitorPricing();
|
|
|
87 |
cp.setItem_id(Long.valueOf(itemId[i]));
|
|
|
88 |
cp.setUploadId(uploadId);
|
|
|
89 |
cp.setUserEmail(userEmail);
|
|
|
90 |
cp.setIsSnapdeal(Boolean.valueOf(snapdeal[i]));
|
|
|
91 |
cp.setIsFlipkart(Boolean.valueOf(flipkart[i]));
|
|
|
92 |
cp.setIsAmazon(Boolean.valueOf(amazon[i]));
|
|
|
93 |
itemScrapingMap.put(cp.getItem_id(), cp);
|
|
|
94 |
}
|
| 12261 |
kshitij.so |
95 |
Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
| 12256 |
kshitij.so |
96 |
cc.addCompetitorScraping(itemScrapingMap);
|
|
|
97 |
return getPreviousData();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
public String index() throws TException{
|
| 12260 |
kshitij.so |
101 |
if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
|
|
|
102 |
return "authfail";
|
|
|
103 |
}
|
| 12256 |
kshitij.so |
104 |
return "index";
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public String getUploadData() throws NumberFormatException, TException{
|
|
|
108 |
Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
|
|
109 |
data = cc.getUploadResultById(Long.valueOf(this.requestId));
|
|
|
110 |
return "show";
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
public String getPreviousData() throws TException{
|
|
|
114 |
Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
|
|
115 |
setPreviousUploads(cc.getPreviousCompetitorScraping(10));
|
|
|
116 |
return "previous";
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public void downloadReport() throws NumberFormatException, TException, IOException{
|
|
|
120 |
File file = new File("/tmp/"+this.requestId+".xls");
|
|
|
121 |
HSSFWorkbook hwb=new HSSFWorkbook();
|
|
|
122 |
HSSFSheet sheet = hwb.createSheet(this.requestId);
|
|
|
123 |
HSSFRow rowhead= sheet.createRow((short)0);
|
|
|
124 |
rowhead.createCell((short) 0).setCellValue("ITEM-ID");
|
|
|
125 |
rowhead.createCell((short) 1).setCellValue("PRODUCT");
|
|
|
126 |
rowhead.createCell((short) 2).setCellValue("Our Snapdeal SP");
|
|
|
127 |
rowhead.createCell((short) 3).setCellValue("Our Snapdeal Offer Price");
|
|
|
128 |
rowhead.createCell((short) 4).setCellValue("Lowest Snapdeal Seller");
|
|
|
129 |
rowhead.createCell((short) 5).setCellValue("Lowest Snapdeal SP");
|
|
|
130 |
rowhead.createCell((short) 6).setCellValue("Lowest Offer Price");
|
|
|
131 |
rowhead.createCell((short) 7).setCellValue("Our Snapdeal Inventory");
|
|
|
132 |
rowhead.createCell((short) 8).setCellValue("Lowest Seller Snapdeal Inventory");
|
|
|
133 |
rowhead.createCell((short) 9).setCellValue("Our Flipkart SP");
|
|
|
134 |
rowhead.createCell((short) 10).setCellValue("Lowest Seller Flipkart");
|
|
|
135 |
rowhead.createCell((short) 11).setCellValue("Lowest Flipkart SP");
|
|
|
136 |
rowhead.createCell((short) 12).setCellValue("Our Flipkart Inventory");
|
|
|
137 |
rowhead.createCell((short) 13).setCellValue("MFN Price");
|
|
|
138 |
rowhead.createCell((short) 14).setCellValue("FBA Price");
|
|
|
139 |
rowhead.createCell((short) 15).setCellValue("Lowest Seller Amazon");
|
|
|
140 |
rowhead.createCell((short) 16).setCellValue("Lowest Amazon SP");
|
|
|
141 |
rowhead.createCell((short) 17).setCellValue("MFN Inventory");
|
|
|
142 |
rowhead.createCell((short) 18).setCellValue("FBA Inventory");
|
|
|
143 |
Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
|
|
144 |
data = cc.getUploadResultById(Long.valueOf(this.requestId));
|
|
|
145 |
int iterator=1;
|
|
|
146 |
for (CompetitorPricing item : data){
|
|
|
147 |
HSSFRow row = sheet.createRow((short)iterator);
|
|
|
148 |
row.createCell((short) 0).setCellValue(item.getItem_id());
|
|
|
149 |
row.createCell((short) 1).setCellValue(item.getProductName());
|
|
|
150 |
row.createCell((short) 2).setCellValue(item.getOurSnapdealPrice());
|
|
|
151 |
row.createCell((short) 3).setCellValue(item.getOurSnapdealOfferPrice());
|
|
|
152 |
row.createCell((short) 4).setCellValue(item.getLowestSnapdealSeller());
|
|
|
153 |
row.createCell((short) 5).setCellValue(item.getLowestSnapdealPrice());
|
|
|
154 |
row.createCell((short) 6).setCellValue(item.getLowestSnapdealOfferPrice());
|
|
|
155 |
row.createCell((short) 7).setCellValue(item.getOurSnapdealInventory());
|
|
|
156 |
row.createCell((short) 8).setCellValue(item.getLowestSnapdealSellerInventory());
|
|
|
157 |
row.createCell((short) 9).setCellValue(item.getOurFlipkartPrice());
|
|
|
158 |
row.createCell((short) 10).setCellValue(item.getLowestFlipkartSeller());
|
|
|
159 |
row.createCell((short) 11).setCellValue(item.getLowestFlipkartPrice());
|
|
|
160 |
row.createCell((short) 12).setCellValue(item.getOurFlipkartInventory());
|
|
|
161 |
row.createCell((short) 13).setCellValue(item.getOurMfnPrice());
|
|
|
162 |
row.createCell((short) 14).setCellValue(item.getOurFbaPrice());
|
|
|
163 |
row.createCell((short) 15).setCellValue(item.getLowestAmazonSeller());
|
|
|
164 |
row.createCell((short) 16).setCellValue(item.getLowestAmazonPrice());
|
|
|
165 |
row.createCell((short) 17).setCellValue(item.getOurMfnInventory());
|
|
|
166 |
row.createCell((short) 18).setCellValue(item.getOurFbaInventory());
|
|
|
167 |
iterator++;
|
|
|
168 |
}
|
|
|
169 |
FileOutputStream fileOut = null;
|
|
|
170 |
try {
|
|
|
171 |
fileOut = new FileOutputStream(file);
|
|
|
172 |
} catch (FileNotFoundException e) {
|
|
|
173 |
// TODO Auto-generated catch block
|
|
|
174 |
e.printStackTrace();
|
|
|
175 |
}
|
|
|
176 |
try {
|
|
|
177 |
hwb.write(fileOut);
|
|
|
178 |
} catch (IOException e) {
|
|
|
179 |
// TODO Auto-generated catch block
|
|
|
180 |
e.printStackTrace();
|
|
|
181 |
}
|
|
|
182 |
try {
|
|
|
183 |
fileOut.close();
|
|
|
184 |
} catch (IOException e) {
|
|
|
185 |
// TODO Auto-generated catch block
|
|
|
186 |
e.printStackTrace();
|
|
|
187 |
}
|
|
|
188 |
byte[] buffer = new byte[(int)file.length()];
|
|
|
189 |
InputStream input = null;
|
|
|
190 |
try {
|
|
|
191 |
int totalBytesRead = 0;
|
|
|
192 |
input = new BufferedInputStream(new FileInputStream(file));
|
|
|
193 |
while(totalBytesRead < buffer.length){
|
|
|
194 |
int bytesRemaining = buffer.length - totalBytesRead;
|
|
|
195 |
int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining);
|
|
|
196 |
if (bytesRead > 0){
|
|
|
197 |
totalBytesRead = totalBytesRead + bytesRead;
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
finally {
|
|
|
202 |
input.close();
|
|
|
203 |
file.delete();
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
response.setHeader("Content-Disposition", "attachment; filename="+this.requestId+".xls");
|
|
|
207 |
response.setContentType("application/octet-stream");
|
|
|
208 |
ServletOutputStream sos;
|
|
|
209 |
try {
|
|
|
210 |
sos = response.getOutputStream();
|
|
|
211 |
sos.write(buffer);
|
|
|
212 |
sos.flush();
|
|
|
213 |
} catch (IOException e) {
|
|
|
214 |
System.out.println("Unable to stream the manifest file");
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
public String uploadSheet(){
|
|
|
219 |
return "upload";
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
public String addItems(){
|
|
|
223 |
return "addItem";
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
public String uploadBulkFile() throws IOException, TException{
|
|
|
227 |
long uploadId = getTimeInMilliseconds();
|
|
|
228 |
String userEmail = getUserEmail();
|
| 12269 |
kshitij.so |
229 |
File fileToCreate = new File("/temp-uploads", "competition-scraping-"+String.valueOf(uploadId)+".xls");
|
| 12256 |
kshitij.so |
230 |
FileUtils.copyFile(this.file, fileToCreate);
|
|
|
231 |
FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
|
|
|
232 |
HSSFWorkbook workbook = new HSSFWorkbook(iFile);
|
|
|
233 |
HSSFSheet sheet = workbook.getSheetAt(0);
|
|
|
234 |
Map<Long,CompetitorPricing> itemScrapingMap = new HashMap<Long,CompetitorPricing>();
|
|
|
235 |
for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
|
|
|
236 |
CompetitorPricing cp = new CompetitorPricing();
|
|
|
237 |
if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
|
|
|
238 |
continue;
|
|
|
239 |
}
|
|
|
240 |
else {
|
|
|
241 |
cp.setItem_id((long) sheet.getRow(iterator).getCell(0).getNumericCellValue());
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
|
|
|
245 |
if ((long)sheet.getRow(iterator).getCell(1).getNumericCellValue()==1){
|
|
|
246 |
cp.setIsSnapdeal(true);
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
|
|
|
251 |
if ((long)sheet.getRow(iterator).getCell(2).getNumericCellValue()==1){
|
|
|
252 |
cp.setIsFlipkart(true);
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
|
|
|
256 |
if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
|
|
|
257 |
cp.setIsAmazon(true);
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
cp.setUploadId(uploadId);
|
|
|
261 |
cp.setUserEmail(userEmail);
|
|
|
262 |
itemScrapingMap.put(cp.getItem_id(), cp);
|
|
|
263 |
}
|
| 12262 |
kshitij.so |
264 |
Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
| 12256 |
kshitij.so |
265 |
cc.addCompetitorScraping(itemScrapingMap);
|
|
|
266 |
return getPreviousData();
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
public boolean checkEmptyString(Cell cell){
|
|
|
270 |
if (cell==null){
|
|
|
271 |
return true;
|
|
|
272 |
}
|
|
|
273 |
return false;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
public long getTimeInMilliseconds(){
|
|
|
277 |
Calendar cal=GregorianCalendar.getInstance();
|
|
|
278 |
return cal.getTimeInMillis();
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
public String getTimestamp(long timestamp){
|
|
|
282 |
return new java.util.Date(timestamp).toLocaleString();
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
public String getUserEmail(){
|
|
|
286 |
logger.info(session.getAttribute(ReportsUtils.USER_NAME).toString());
|
|
|
287 |
logger.info(session.getAttribute(ReportsUtils.USER_EMAIL).toString());
|
|
|
288 |
return session.getAttribute(ReportsUtils.USER_EMAIL).toString();
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
public File getFile() {
|
|
|
293 |
return file;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
public void setFile(File file) {
|
|
|
297 |
this.file = file;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
public void setPreviousUploads(List<CompetitorPricing> previousUploads) {
|
|
|
301 |
this.previousUploads = previousUploads;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
public List<CompetitorPricing> getPreviousUploads() {
|
|
|
305 |
return previousUploads;
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
public HttpServletRequest getRequest() {
|
|
|
309 |
logger.info("set request"+request.toString());
|
|
|
310 |
return request;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
public void setRequest(HttpServletRequest request) {
|
|
|
314 |
this.request = request;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
public HttpServletResponse getResponse() {
|
|
|
318 |
return response;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
public void setResponse(HttpServletResponse response) {
|
|
|
322 |
this.response = response;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
public HttpSession getSession() {
|
|
|
326 |
return session;
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
public void setSession(HttpSession session) {
|
|
|
330 |
this.session = session;
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
public ServletContext getContext() {
|
|
|
334 |
return context;
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
public void setContext(ServletContext context) {
|
|
|
338 |
this.context = context;
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
342 |
this.request = req;
|
|
|
343 |
this.session = req.getSession();
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
public void setServletContext(ServletContext arg0) {
|
|
|
347 |
// TODO Auto-generated method stub
|
|
|
348 |
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
352 |
this.response = response;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
public void setSnapdeal(String[] snapdeal) {
|
|
|
356 |
this.snapdeal = snapdeal;
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
public String[] getSnapdeal() {
|
|
|
360 |
return snapdeal;
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
public void setFlipkart(String[] flipkart) {
|
|
|
364 |
this.flipkart = flipkart;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
public String[] getFlipkart() {
|
|
|
368 |
return flipkart;
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
public void setAmazon(String[] amazon) {
|
|
|
372 |
this.amazon = amazon;
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
public String[] getAmazon() {
|
|
|
376 |
return amazon;
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
public void setItemId(String[] itemId) {
|
|
|
380 |
this.itemId = itemId;
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
public String[] getItemId() {
|
|
|
384 |
return itemId;
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
public List<CompetitorPricing> getData() {
|
|
|
389 |
return data;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
public void setData(List<CompetitorPricing> data) {
|
|
|
393 |
this.data = data;
|
|
|
394 |
}
|
|
|
395 |
|
|
|
396 |
public String getRequestId() {
|
|
|
397 |
return requestId;
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
public void setRequestId(String requestId) {
|
|
|
401 |
this.requestId = requestId;
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
|
408 |
}
|