Subversion Repositories SmartDukaan

Rev

Rev 9110 | Rev 9242 | 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
	}
9113 vikram.rag 81
 
8739 vikram.rag 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;
9113 vikram.rag 92
		List<SnapdealItem> snapdealItems = null;
8739 vikram.rag 93
		try {
9113 vikram.rag 94
			//catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
8903 vikram.rag 95
			catalogServiceClient = new CatalogClient();
8739 vikram.rag 96
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= catalogServiceClient.getClient();
97
			snapdealItems = catalogClient.getAllSnapdealItems();
98
		} catch (Exception e) {
9113 vikram.rag 99
			// TODO Auto-generated catch block
8739 vikram.rag 100
			e.printStackTrace();
101
		}
102
		int iterator=1;
9113 vikram.rag 103
		for(SnapdealItem snapdealItem:snapdealItems){
8739 vikram.rag 104
			HSSFRow row = sheet.createRow((short)iterator);
105
			row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());
106
			row.createCell((short) 1).setCellValue(snapdealItem.getWarehouseId());
107
			row.createCell((short) 2).setCellValue(snapdealItem.getExceptionPrice());
108
			if(snapdealItem.isIsListedOnSnapdeal()){
109
				row.createCell((short) 3).setCellValue(1);
110
			}
111
			else{
112
				row.createCell((short) 3).setCellValue(0);
113
			}
114
			iterator++;
115
		}
9113 vikram.rag 116
 
8739 vikram.rag 117
		FileOutputStream fileOut = null;
118
		try {
119
			fileOut = new FileOutputStream(file);
120
		} catch (FileNotFoundException e) {
121
			// TODO Auto-generated catch block
122
			e.printStackTrace();
123
		}
124
		try {
125
			hwb.write(fileOut);
126
		} catch (IOException e) {
127
			// TODO Auto-generated catch block
128
			e.printStackTrace();
129
		}
130
		try {
131
			fileOut.close();
132
		} catch (IOException e) {
133
			// TODO Auto-generated catch block
134
			e.printStackTrace();
135
		}
136
		byte[] buffer = new byte[(int)file.length()];
137
		InputStream input = null;
138
		try {
139
			int totalBytesRead = 0;
140
			input = new BufferedInputStream(new FileInputStream(file));
141
			while(totalBytesRead < buffer.length){
142
				int bytesRemaining = buffer.length - totalBytesRead;
143
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
144
				if (bytesRead > 0){
145
					totalBytesRead = totalBytesRead + bytesRead;
146
				}
147
			}
148
		}
149
		finally {
150
			input.close();
151
			file.delete();
152
		}
153
 
154
		response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");
155
		response.setContentType("application/octet-stream");
156
		ServletOutputStream sos;
157
		try {
158
			sos = response.getOutputStream();
159
			sos.write(buffer);
160
			sos.flush();
161
		} catch (IOException e) {
162
			System.out.println("Unable to stream the manifest file");
163
		}   
164
 
9113 vikram.rag 165
 
8739 vikram.rag 166
	}
167
 
168
	public void uploadsnapdealBulkSheet() throws IOException, TException{
169
		File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");
170
		FileUtils.copyFile(this.file, fileToCreate);
171
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
172
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
173
		HSSFSheet sheet = workbook.getSheetAt(0);
8887 vikram.rag 174
		Client catalogClient=null;
8739 vikram.rag 175
		StringBuilder sb = new StringBuilder();
176
		try {
9113 vikram.rag 177
			//catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
8903 vikram.rag 178
			catalogClient = new CatalogClient().getClient();
8739 vikram.rag 179
		} catch (TTransportException e) {
9113 vikram.rag 180
			// TODO Auto-generated catch block
8739 vikram.rag 181
			e.printStackTrace();
182
		}
183
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
184
			SnapdealItem snapdealItem =null;
185
			Long sku;
186
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
187
				continue;
188
			}
189
			else {
8791 kshitij.so 190
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
8887 vikram.rag 191
				snapdealItem = catalogClient.getSnapdealItem(sku);
8886 vikram.rag 192
				if(snapdealItem.getItem_id()==0){
193
					snapdealItem = new SnapdealItem();
194
					snapdealItem.setItem_id(sku); 
195
				}
8739 vikram.rag 196
			}
9113 vikram.rag 197
 
8739 vikram.rag 198
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
199
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
200
				snapdealItem.setWarehouseId(warehouseId);
201
			}
202
 
203
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
204
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
205
				snapdealItem.setExceptionPrice(exceptionPrice);
206
			}
207
 
208
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
209
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
210
					snapdealItem.setIsListedOnSnapdeal(true);
211
				}
212
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
213
					snapdealItem.setIsListedOnSnapdeal(false);
214
				}
215
			}
8888 vikram.rag 216
			if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){
8739 vikram.rag 217
				sb.append(sku + "\n");
218
			}	
219
		}
220
		//logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
221
		File file = new File("/tmp/amazonbulk");
222
		FileWriter writer = new FileWriter(file);
223
		writer.append(sb.toString());
224
		writer.close();
225
		byte[] buffer = new byte[(int)file.length()];
226
		InputStream input = null;
227
		try {
228
			int totalBytesRead = 0;
229
			input = new BufferedInputStream(new FileInputStream(file));
230
			while(totalBytesRead < buffer.length){
231
				int bytesRemaining = buffer.length - totalBytesRead;
232
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
233
				if (bytesRead > 0){
234
					totalBytesRead = totalBytesRead + bytesRead;
235
				}
236
			}
237
		}
238
		finally {
239
			input.close();
240
			file.delete();
241
		}
242
 
243
		response.setHeader("Content-Type", "text/javascript");
244
 
245
		ServletOutputStream sos;
246
		try {
247
			sos = response.getOutputStream();
248
			sos.write(buffer);
249
			sos.flush();
250
		} catch (IOException e) {
251
			System.out.println("Unable to stream the manifest file");
252
		}   
253
	}
254
 
255
	public boolean checkEmptyString(Cell cell){
256
		if (cell==null){
257
			return true;
258
		}
259
		return false;
260
	}
261
 
262
	public String show() {
263
		logger.info("Before fetching role");
264
		logger.info(request.getSession().toString());
265
		logger.info(ReportsUtils.ROLE);
266
		logger.info(session.getAttribute(ReportsUtils.ROLE).toString());
267
		logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
268
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
269
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
270
			return "authfail";
271
		}
272
 
273
		if (StringUtils.equals(id, "snapdeal-options")){
274
			return "snapdeal-options";
275
		}
276
 
277
		return "id";
278
	}
279
 
280
	public void setId(String id) {
281
		logger.info(id);
282
		this.id = id;
283
	}
284
 
285
	public HttpServletRequest getRequest() {
286
		logger.info("set request"+request.toString());
287
		return request;
288
	}
289
 
290
	public void setRequest(HttpServletRequest request) {
291
		this.request = request;
292
	}
293
 
294
	public HttpServletResponse getResponse() {
295
		return response;
296
	}
297
 
298
	public void setResponse(HttpServletResponse response) {
299
		this.response = response;
300
	}
301
 
302
	public HttpSession getSession() {
303
		return session;
304
	}
305
 
306
	public void setSession(HttpSession session) {
307
		logger.info("set session"+session.toString());
308
		this.session = session;
309
	}
310
 
311
	public ServletContext getContext() {
312
		return context;
313
	}
314
 
315
	public void setContext(ServletContext context) {
316
		this.context = context;
317
	}
318
 
319
	public String getUrl() {
320
		return url;
321
	}
322
 
323
	public void setUrl(String url) {
324
		this.url = url;
325
	}
326
 
327
	public String getItemId() {
328
		return itemId;
329
	}
330
 
331
	public void setItemId(String itemId) {
332
		this.itemId = itemId;
333
	}
334
 
335
	public String getIsSnapdealListed() {
336
		return isSnapdealListed;
337
	}
338
 
339
	public void setIsSnapdealListed(String isSnapdealListed) {
340
		this.isSnapdealListed = isSnapdealListed;
341
	}
342
 
343
	public String getExceptionPrice() {
344
		return exceptionPrice;
345
	}
346
 
347
	public void setExceptionPrice(String exceptionPrice) {
348
		this.exceptionPrice = exceptionPrice;
349
	}
350
 
351
	public String getWarehouseId() {
352
		return warehouseId;
353
	}
354
 
355
	public void setWarehouseId(String warehouseId) {
356
		this.warehouseId = warehouseId;
357
	}
358
 
359
	public File getFile() {
360
		return file;
361
	}
362
 
363
	public void setFile(File file) {
364
		this.file = file;
365
	}
366
 
367
	public String getErrMsg() {
368
		return errMsg;
369
	}
370
 
371
	public void setErrMsg(String errMsg) {
372
		this.errMsg = errMsg;
373
	}
374
 
375
	public String getNext() {
376
		return next;
377
	}
378
 
379
	public void setNext(String next) {
380
		this.next = next;
381
	}
382
 
383
	public String getId() {
384
		return id;
385
	}
386
 
387
	public void setServletRequest(HttpServletRequest req) {
388
		this.request = req;
389
		this.session = req.getSession();        
390
	}
391
 
392
	public void setServletContext(ServletContext arg0) {
393
		// TODO Auto-generated method stub
394
 
395
	}
396
 
397
	public void setServletResponse(HttpServletResponse response) {
398
		this.response = response;
399
	}
400
 
401
}