Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
8739 vikram.rag 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.catalog.Amazonlisted;
4
import in.shop2020.model.v1.catalog.CatalogService.Client;
5
import in.shop2020.model.v1.catalog.SnapdealItem;
6
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
7
import in.shop2020.support.utils.ReportsUtils;
8
import in.shop2020.thrift.clients.CatalogClient;
9
import in.shop2020.thrift.clients.TransactionClient;
10
 
11
import java.io.BufferedInputStream;
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileNotFoundException;
15
import java.io.FileOutputStream;
16
import java.io.FileWriter;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.util.ArrayList;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
 
24
import javax.servlet.ServletContext;
25
import javax.servlet.ServletOutputStream;
26
import javax.servlet.http.HttpServletRequest;
27
import javax.servlet.http.HttpServletResponse;
28
import javax.servlet.http.HttpSession;
29
 
30
import org.apache.commons.io.FileUtils;
31
import org.apache.commons.lang.xwork.StringUtils;
32
import org.apache.poi.hssf.usermodel.HSSFRow;
33
import org.apache.poi.hssf.usermodel.HSSFSheet;
34
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35
import org.apache.poi.ss.usermodel.Cell;
36
import org.apache.struts2.convention.annotation.InterceptorRef;
37
import org.apache.struts2.convention.annotation.InterceptorRefs;
38
import org.apache.struts2.interceptor.ServletRequestAware;
39
import org.apache.struts2.interceptor.ServletResponseAware;
40
import org.apache.struts2.util.ServletContextAware;
41
import org.apache.thrift.TException;
42
import org.apache.thrift.transport.TTransportException;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45
 
46
import com.opensymphony.xwork2.ValidationAwareSupport;
47
 
48
public class SnapdealListController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
49
	private static Logger logger = LoggerFactory.getLogger(SnapdealListController.class);
50
 
51
 
52
	private HttpServletRequest request;
53
	private HttpServletResponse response;
54
	private HttpSession session;
55
	private ServletContext context;
56
	private String url;
57
	private String itemId;
58
	private String isSnapdealListed;
59
	private String exceptionPrice;
60
	private String warehouseId;
61
	private File file;
62
	private String errMsg;
63
	private String next;
64
	private String id;
65
 
66
	public String index() {
67
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
68
			return "authfail";
69
		}
70
		return "index";
71
	}
72
 
73
	public String uploadBulkSheet(){
74
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
75
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
76
			return "authfail";
77
		}
78
		return "snapdeal-bulk-upload";
79
	}
80
 
81
	public void downloadSnapdealListings() throws IOException, TException{
82
		File file = new File("/tmp/snapdeal-bulk-upload-template.xls");
83
		HSSFWorkbook hwb=new HSSFWorkbook();
84
		HSSFSheet sheet =  hwb.createSheet("Snapdeal-Listings");
85
		HSSFRow rowhead=   sheet.createRow((short)0);
86
		rowhead.createCell((short) 0).setCellValue("ITEM-ID");
87
		rowhead.createCell((short) 1).setCellValue("WAREHOUSE-ID");
88
		rowhead.createCell((short) 2).setCellValue("EXCEPTIONAL-PRICE");
89
		rowhead.createCell((short) 3).setCellValue("SNAPDEAL-LISTED");
90
		CatalogClient catalogServiceClient = null;
91
		List<SnapdealItem> snapdealItems = null;
92
		try {
93
			catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
94
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= catalogServiceClient.getClient();
95
			snapdealItems = catalogClient.getAllSnapdealItems();
96
		} catch (Exception e) {
97
			// TODO Auto-generated catch block
98
			e.printStackTrace();
99
		}
100
		int iterator=1;
101
		for(SnapdealItem snapdealItem:snapdealItems){
102
			HSSFRow row = sheet.createRow((short)iterator);
103
			row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());
104
			row.createCell((short) 1).setCellValue(snapdealItem.getWarehouseId());
105
			row.createCell((short) 2).setCellValue(snapdealItem.getExceptionPrice());
106
			if(snapdealItem.isIsListedOnSnapdeal()){
107
				row.createCell((short) 3).setCellValue(1);
108
			}
109
			else{
110
				row.createCell((short) 3).setCellValue(0);
111
			}
112
			iterator++;
113
		}
114
 
115
		FileOutputStream fileOut = null;
116
		try {
117
			fileOut = new FileOutputStream(file);
118
		} catch (FileNotFoundException e) {
119
			// TODO Auto-generated catch block
120
			e.printStackTrace();
121
		}
122
		try {
123
			hwb.write(fileOut);
124
		} catch (IOException e) {
125
			// TODO Auto-generated catch block
126
			e.printStackTrace();
127
		}
128
		try {
129
			fileOut.close();
130
		} catch (IOException e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		}
134
		byte[] buffer = new byte[(int)file.length()];
135
		InputStream input = null;
136
		try {
137
			int totalBytesRead = 0;
138
			input = new BufferedInputStream(new FileInputStream(file));
139
			while(totalBytesRead < buffer.length){
140
				int bytesRemaining = buffer.length - totalBytesRead;
141
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
142
				if (bytesRead > 0){
143
					totalBytesRead = totalBytesRead + bytesRead;
144
				}
145
			}
146
		}
147
		finally {
148
			input.close();
149
			file.delete();
150
		}
151
 
152
		response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");
153
		response.setContentType("application/octet-stream");
154
		ServletOutputStream sos;
155
		try {
156
			sos = response.getOutputStream();
157
			sos.write(buffer);
158
			sos.flush();
159
		} catch (IOException e) {
160
			System.out.println("Unable to stream the manifest file");
161
		}   
162
 
163
 
164
	}
165
 
166
	public void uploadsnapdealBulkSheet() throws IOException, TException{
167
		File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");
168
		FileUtils.copyFile(this.file, fileToCreate);
169
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
170
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
171
		HSSFSheet sheet = workbook.getSheetAt(0);
172
		Client CatalogClient=null;
173
		StringBuilder sb = new StringBuilder();
174
		try {
175
			CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
176
		} catch (TTransportException e) {
177
			// TODO Auto-generated catch block
178
			e.printStackTrace();
179
		}
180
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
181
			SnapdealItem snapdealItem =null;
182
			Long sku;
183
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
184
				continue;
185
			}
186
			else {
8791 kshitij.so 187
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
188
				snapdealItem = CatalogClient.getSnapdealItem(sku);
8739 vikram.rag 189
			}
8791 kshitij.so 190
 
8739 vikram.rag 191
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
192
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
193
				snapdealItem.setWarehouseId(warehouseId);
194
			}
195
 
196
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
197
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
198
				snapdealItem.setExceptionPrice(exceptionPrice);
199
			}
200
 
201
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
202
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
203
					snapdealItem.setIsListedOnSnapdeal(true);
204
				}
205
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
206
					snapdealItem.setIsListedOnSnapdeal(false);
207
				}
208
			}
209
			if(!CatalogClient.addOrUpdateSnapdealItem(snapdealItem)){
210
				sb.append(sku + "\n");
211
			}	
212
		}
213
		//logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
214
		File file = new File("/tmp/amazonbulk");
215
		FileWriter writer = new FileWriter(file);
216
		writer.append(sb.toString());
217
		writer.close();
218
		byte[] buffer = new byte[(int)file.length()];
219
		InputStream input = null;
220
		try {
221
			int totalBytesRead = 0;
222
			input = new BufferedInputStream(new FileInputStream(file));
223
			while(totalBytesRead < buffer.length){
224
				int bytesRemaining = buffer.length - totalBytesRead;
225
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
226
				if (bytesRead > 0){
227
					totalBytesRead = totalBytesRead + bytesRead;
228
				}
229
			}
230
		}
231
		finally {
232
			input.close();
233
			file.delete();
234
		}
235
 
236
		response.setHeader("Content-Type", "text/javascript");
237
 
238
		ServletOutputStream sos;
239
		try {
240
			sos = response.getOutputStream();
241
			sos.write(buffer);
242
			sos.flush();
243
		} catch (IOException e) {
244
			System.out.println("Unable to stream the manifest file");
245
		}   
246
	}
247
 
248
	public boolean checkEmptyString(Cell cell){
249
		if (cell==null){
250
			return true;
251
		}
252
		return false;
253
	}
254
 
255
	public String show() {
256
		logger.info("Before fetching role");
257
		logger.info(request.getSession().toString());
258
		logger.info(ReportsUtils.ROLE);
259
		logger.info(session.getAttribute(ReportsUtils.ROLE).toString());
260
		logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
261
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
262
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
263
			return "authfail";
264
		}
265
 
266
		if (StringUtils.equals(id, "snapdeal-options")){
267
			return "snapdeal-options";
268
		}
269
 
270
		return "id";
271
	}
272
 
273
	public void setId(String id) {
274
		logger.info(id);
275
		this.id = id;
276
	}
277
 
278
	public HttpServletRequest getRequest() {
279
		logger.info("set request"+request.toString());
280
		return request;
281
	}
282
 
283
	public void setRequest(HttpServletRequest request) {
284
		this.request = request;
285
	}
286
 
287
	public HttpServletResponse getResponse() {
288
		return response;
289
	}
290
 
291
	public void setResponse(HttpServletResponse response) {
292
		this.response = response;
293
	}
294
 
295
	public HttpSession getSession() {
296
		return session;
297
	}
298
 
299
	public void setSession(HttpSession session) {
300
		logger.info("set session"+session.toString());
301
		this.session = session;
302
	}
303
 
304
	public ServletContext getContext() {
305
		return context;
306
	}
307
 
308
	public void setContext(ServletContext context) {
309
		this.context = context;
310
	}
311
 
312
	public String getUrl() {
313
		return url;
314
	}
315
 
316
	public void setUrl(String url) {
317
		this.url = url;
318
	}
319
 
320
	public String getItemId() {
321
		return itemId;
322
	}
323
 
324
	public void setItemId(String itemId) {
325
		this.itemId = itemId;
326
	}
327
 
328
	public String getIsSnapdealListed() {
329
		return isSnapdealListed;
330
	}
331
 
332
	public void setIsSnapdealListed(String isSnapdealListed) {
333
		this.isSnapdealListed = isSnapdealListed;
334
	}
335
 
336
	public String getExceptionPrice() {
337
		return exceptionPrice;
338
	}
339
 
340
	public void setExceptionPrice(String exceptionPrice) {
341
		this.exceptionPrice = exceptionPrice;
342
	}
343
 
344
	public String getWarehouseId() {
345
		return warehouseId;
346
	}
347
 
348
	public void setWarehouseId(String warehouseId) {
349
		this.warehouseId = warehouseId;
350
	}
351
 
352
	public File getFile() {
353
		return file;
354
	}
355
 
356
	public void setFile(File file) {
357
		this.file = file;
358
	}
359
 
360
	public String getErrMsg() {
361
		return errMsg;
362
	}
363
 
364
	public void setErrMsg(String errMsg) {
365
		this.errMsg = errMsg;
366
	}
367
 
368
	public String getNext() {
369
		return next;
370
	}
371
 
372
	public void setNext(String next) {
373
		this.next = next;
374
	}
375
 
376
	public String getId() {
377
		return id;
378
	}
379
 
380
	public void setServletRequest(HttpServletRequest req) {
381
		this.request = req;
382
		this.session = req.getSession();        
383
	}
384
 
385
	public void setServletContext(ServletContext arg0) {
386
		// TODO Auto-generated method stub
387
 
388
	}
389
 
390
	public void setServletResponse(HttpServletResponse response) {
391
		this.response = response;
392
	}
393
 
394
}