Subversion Repositories SmartDukaan

Rev

Rev 8791 | Rev 8887 | 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);
173
		Client CatalogClient=null;
174
		StringBuilder sb = new StringBuilder();
175
		try {
8886 vikram.rag 176
			//CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
177
			CatalogClient = new CatalogClient().getClient();
8739 vikram.rag 178
		} catch (TTransportException e) {
179
			// TODO Auto-generated catch block
180
			e.printStackTrace();
181
		}
182
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
183
			SnapdealItem snapdealItem =null;
184
			Long sku;
185
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
186
				continue;
187
			}
188
			else {
8791 kshitij.so 189
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
190
				snapdealItem = CatalogClient.getSnapdealItem(sku);
8886 vikram.rag 191
				if(snapdealItem.getItem_id()==0){
192
					snapdealItem = new SnapdealItem();
193
					snapdealItem.setItem_id(sku); 
194
				}
8739 vikram.rag 195
			}
8791 kshitij.so 196
 
8739 vikram.rag 197
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
198
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
199
				snapdealItem.setWarehouseId(warehouseId);
200
			}
201
 
202
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
203
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
204
				snapdealItem.setExceptionPrice(exceptionPrice);
205
			}
206
 
207
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
208
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
209
					snapdealItem.setIsListedOnSnapdeal(true);
210
				}
211
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
212
					snapdealItem.setIsListedOnSnapdeal(false);
213
				}
214
			}
215
			if(!CatalogClient.addOrUpdateSnapdealItem(snapdealItem)){
216
				sb.append(sku + "\n");
217
			}	
218
		}
219
		//logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
220
		File file = new File("/tmp/amazonbulk");
221
		FileWriter writer = new FileWriter(file);
222
		writer.append(sb.toString());
223
		writer.close();
224
		byte[] buffer = new byte[(int)file.length()];
225
		InputStream input = null;
226
		try {
227
			int totalBytesRead = 0;
228
			input = new BufferedInputStream(new FileInputStream(file));
229
			while(totalBytesRead < buffer.length){
230
				int bytesRemaining = buffer.length - totalBytesRead;
231
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
232
				if (bytesRead > 0){
233
					totalBytesRead = totalBytesRead + bytesRead;
234
				}
235
			}
236
		}
237
		finally {
238
			input.close();
239
			file.delete();
240
		}
241
 
242
		response.setHeader("Content-Type", "text/javascript");
243
 
244
		ServletOutputStream sos;
245
		try {
246
			sos = response.getOutputStream();
247
			sos.write(buffer);
248
			sos.flush();
249
		} catch (IOException e) {
250
			System.out.println("Unable to stream the manifest file");
251
		}   
252
	}
253
 
254
	public boolean checkEmptyString(Cell cell){
255
		if (cell==null){
256
			return true;
257
		}
258
		return false;
259
	}
260
 
261
	public String show() {
262
		logger.info("Before fetching role");
263
		logger.info(request.getSession().toString());
264
		logger.info(ReportsUtils.ROLE);
265
		logger.info(session.getAttribute(ReportsUtils.ROLE).toString());
266
		logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
267
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
268
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
269
			return "authfail";
270
		}
271
 
272
		if (StringUtils.equals(id, "snapdeal-options")){
273
			return "snapdeal-options";
274
		}
275
 
276
		return "id";
277
	}
278
 
279
	public void setId(String id) {
280
		logger.info(id);
281
		this.id = id;
282
	}
283
 
284
	public HttpServletRequest getRequest() {
285
		logger.info("set request"+request.toString());
286
		return request;
287
	}
288
 
289
	public void setRequest(HttpServletRequest request) {
290
		this.request = request;
291
	}
292
 
293
	public HttpServletResponse getResponse() {
294
		return response;
295
	}
296
 
297
	public void setResponse(HttpServletResponse response) {
298
		this.response = response;
299
	}
300
 
301
	public HttpSession getSession() {
302
		return session;
303
	}
304
 
305
	public void setSession(HttpSession session) {
306
		logger.info("set session"+session.toString());
307
		this.session = session;
308
	}
309
 
310
	public ServletContext getContext() {
311
		return context;
312
	}
313
 
314
	public void setContext(ServletContext context) {
315
		this.context = context;
316
	}
317
 
318
	public String getUrl() {
319
		return url;
320
	}
321
 
322
	public void setUrl(String url) {
323
		this.url = url;
324
	}
325
 
326
	public String getItemId() {
327
		return itemId;
328
	}
329
 
330
	public void setItemId(String itemId) {
331
		this.itemId = itemId;
332
	}
333
 
334
	public String getIsSnapdealListed() {
335
		return isSnapdealListed;
336
	}
337
 
338
	public void setIsSnapdealListed(String isSnapdealListed) {
339
		this.isSnapdealListed = isSnapdealListed;
340
	}
341
 
342
	public String getExceptionPrice() {
343
		return exceptionPrice;
344
	}
345
 
346
	public void setExceptionPrice(String exceptionPrice) {
347
		this.exceptionPrice = exceptionPrice;
348
	}
349
 
350
	public String getWarehouseId() {
351
		return warehouseId;
352
	}
353
 
354
	public void setWarehouseId(String warehouseId) {
355
		this.warehouseId = warehouseId;
356
	}
357
 
358
	public File getFile() {
359
		return file;
360
	}
361
 
362
	public void setFile(File file) {
363
		this.file = file;
364
	}
365
 
366
	public String getErrMsg() {
367
		return errMsg;
368
	}
369
 
370
	public void setErrMsg(String errMsg) {
371
		this.errMsg = errMsg;
372
	}
373
 
374
	public String getNext() {
375
		return next;
376
	}
377
 
378
	public void setNext(String next) {
379
		this.next = next;
380
	}
381
 
382
	public String getId() {
383
		return id;
384
	}
385
 
386
	public void setServletRequest(HttpServletRequest req) {
387
		this.request = req;
388
		this.session = req.getSession();        
389
	}
390
 
391
	public void setServletContext(ServletContext arg0) {
392
		// TODO Auto-generated method stub
393
 
394
	}
395
 
396
	public void setServletResponse(HttpServletResponse response) {
397
		this.response = response;
398
	}
399
 
400
}