Subversion Repositories SmartDukaan

Rev

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