Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
7283 kshitij.so 1
package in.shop2020.support.controllers;
2
 
8362 kshitij.so 3
import java.io.BufferedInputStream;
7524 kshitij.so 4
import java.io.File;
7741 kshitij.so 5
import java.io.FileInputStream;
8362 kshitij.so 6
import java.io.FileWriter;
7524 kshitij.so 7
import java.io.IOException;
8362 kshitij.so 8
import java.io.InputStream;
8620 kshitij.so 9
import java.util.Arrays;
7365 kshitij.so 10
import java.util.HashMap;
7283 kshitij.so 11
import java.util.List;
12
import java.util.Map;
13
 
7591 kshitij.so 14
import javax.servlet.ServletContext;
8362 kshitij.so 15
import javax.servlet.ServletOutputStream;
7283 kshitij.so 16
import javax.servlet.ServletRequest;
17
import javax.servlet.http.HttpServletRequest;
8168 kshitij.so 18
import javax.servlet.http.HttpServletResponse;
7591 kshitij.so 19
import javax.servlet.http.HttpSession;
7283 kshitij.so 20
 
21
import in.shop2020.model.v1.catalog.Amazonlisted;
22
import in.shop2020.model.v1.catalog.CatalogServiceException;
23
import in.shop2020.model.v1.catalog.Item;
24
import in.shop2020.model.v1.inventory.AmazonInventorySnapshot;
25
import in.shop2020.model.v1.catalog.CatalogService.Client;
26
import in.shop2020.model.v1.inventory.InventoryServiceException;
7741 kshitij.so 27
import in.shop2020.model.v1.order.AmazonOrder;
7591 kshitij.so 28
import in.shop2020.support.utils.ReportsUtils;
7283 kshitij.so 29
import in.shop2020.thrift.clients.CatalogClient;
30
import in.shop2020.thrift.clients.InventoryClient;
7365 kshitij.so 31
import in.shop2020.thrift.clients.LogisticsClient;
7741 kshitij.so 32
import in.shop2020.thrift.clients.TransactionClient;
7365 kshitij.so 33
import in.shop2020.logistics.DeliveryType;
34
import in.shop2020.logistics.LogisticsInfo;
35
import in.shop2020.logistics.LogisticsServiceException;
36
import in.shop2020.logistics.PickUpType;
7283 kshitij.so 37
 
7524 kshitij.so 38
import org.apache.commons.io.FileUtils;
7365 kshitij.so 39
import org.apache.commons.lang.xwork.StringUtils;
7741 kshitij.so 40
import org.apache.poi.hssf.usermodel.HSSFSheet;
41
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
8168 kshitij.so 42
import org.apache.poi.ss.usermodel.Cell;
7591 kshitij.so 43
import org.apache.struts2.convention.annotation.InterceptorRef;
44
import org.apache.struts2.convention.annotation.InterceptorRefs;
7283 kshitij.so 45
import org.apache.struts2.convention.annotation.Result;
46
import org.apache.struts2.convention.annotation.Results;
47
import org.apache.struts2.interceptor.ServletRequestAware;
8168 kshitij.so 48
import org.apache.struts2.interceptor.ServletResponseAware;
49
import org.apache.struts2.util.ServletContextAware;
7283 kshitij.so 50
import org.apache.thrift.TException;
7741 kshitij.so 51
import org.apache.thrift.transport.TTransportException;
7283 kshitij.so 52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54
 
55
import com.opensymphony.xwork2.ValidationAwareSupport;
56
 
57
@SuppressWarnings("serial")
8620 kshitij.so 58
 
7591 kshitij.so 59
@InterceptorRefs({
60
	@InterceptorRef("defaultStack"),
61
	@InterceptorRef("login")
62
})
7283 kshitij.so 63
@Results({
7524 kshitij.so 64
	@Result(name = "redirect", location = "${url}", type = "redirect"),
65
	@Result(name="authsuccess", type="redirectAction", params = {"actionName" , "reports"})
66
})
8168 kshitij.so 67
public class AmazonListController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
7283 kshitij.so 68
 
69
	private static Logger logger = LoggerFactory.getLogger(AmazonListController.class);
70
 
7591 kshitij.so 71
	private HttpServletRequest request;
72
	private HttpSession session;
73
	private ServletContext context;
8362 kshitij.so 74
	private HttpServletResponse response;
7283 kshitij.so 75
	private String url;
76
	private String id;
77
	private String itemId;
78
	private String isFba;
79
	private String isNonFba;
80
	private String isInventoryOverride;
81
	private String fbaPrice;
82
	private String sellingPrice;
83
	private String saholicPrice;
7365 kshitij.so 84
	private String isTime;
85
	private String handlingTime;
86
	private String customHandlingTime;
7461 kshitij.so 87
	private String holdInventory;
88
	private String defaultInventory;
7524 kshitij.so 89
	private String fileUploadFileName;
90
	private String fileUploadContentType;
8168 kshitij.so 91
	private String suppressMfnPriceUpdate;
92
	private String suppressFbaPriceUpdate;
7524 kshitij.so 93
	private File file;
8362 kshitij.so 94
	private String errMsg;
8620 kshitij.so 95
	private String next;
96
	private List<Amazonlisted> amazonItems;
97
	private String searchText;
98
	private long searchCount;
99
	private long totalCount;
100
	private String taxCode;
10922 kshitij.so 101
	private String fbbTaxCode;
102
	private String isFbb;
103
	private String fbbPrice;
104
	private String suppressFbbPriceUpdate;
7283 kshitij.so 105
 
106
	public String index() {
8620 kshitij.so 107
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
7591 kshitij.so 108
			return "authfail";
109
		}
7283 kshitij.so 110
		return "index";
111
	}
112
 
8620 kshitij.so 113
	public String fetchItems() throws TException {
7408 kshitij.so 114
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
8620 kshitij.so 115
		if (searchText.length() == 0) {
116
			amazonItems = CatalogClient.getAmazonListedItems(Long.valueOf(next), 10);
117
			totalCount = CatalogClient.getCountForAmazonlistedItems();
118
			setSearchCount(totalCount);
119
		} else {
120
			List<String> subString = Arrays.asList(searchText.split(" "));
121
			amazonItems = CatalogClient.searchAmazonItems(subString,Long.valueOf(next), 10);
122
			totalCount = CatalogClient.getCountForAmazonlistedItems();
123
			searchCount = CatalogClient.getAmazonSearchResultCount(subString);
124
		}
125
		return "amazon-item-table";
7283 kshitij.so 126
	}
127
 
8620 kshitij.so 128
 
7408 kshitij.so 129
	public Amazonlisted fetchItemDetail() throws NumberFormatException, TException {
130
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
7283 kshitij.so 131
		return CatalogClient.getAmazonItemDetails(Long.valueOf(id));
132
	}
133
 
7408 kshitij.so 134
	public Item getSaholicItem(String id) throws NumberFormatException, CatalogServiceException, TException {
135
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
7283 kshitij.so 136
		return CatalogClient.getItem(Long.valueOf(id));
137
	}
7365 kshitij.so 138
 
139
 
140
 
7283 kshitij.so 141
	public String update() throws NumberFormatException, TException{
7408 kshitij.so 142
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
8168 kshitij.so 143
		boolean flag=false;
144
		boolean commit=false;
145
		long delay = Long.valueOf(customHandlingTime);
146
		if ( Boolean.valueOf(isTime)) {
147
			flag = true;
148
		}
149
		changeItemInventory();
150
		Amazonlisted amazonlisted = CatalogClient.getAmazonItemDetails(Long.valueOf(itemId));
151
		if (amazonlisted.getSellingPrice()!=Double.valueOf(sellingPrice)){
152
			amazonlisted.setSellingPrice(Double.valueOf(sellingPrice));
153
			commit=true;
154
		}
155
		if (amazonlisted.getFbaPrice()!=Double.valueOf(fbaPrice)){
156
			amazonlisted.setFbaPrice(Double.valueOf(fbaPrice));
157
			commit=true;
158
		}
159
		if (amazonlisted.isIsFba()!=Boolean.valueOf(isFba)){
160
			amazonlisted.setIsFba(Boolean.valueOf(isFba));
161
			commit=true;
162
		}
163
		if (amazonlisted.isIsNonFba()!=Boolean.valueOf(isNonFba)){
164
			amazonlisted.setIsNonFba(Boolean.valueOf(isNonFba));
165
			commit=true;
166
		}
167
		if (amazonlisted.isIsCustomTime()!=flag){
168
			amazonlisted.setIsCustomTime(flag);
169
			commit=true;
170
		}
171
		if (amazonlisted.getHandlingTime()!=delay){
172
			amazonlisted.setHandlingTime(delay);
173
			commit=true;
174
		}
175
		if(amazonlisted.isIsInventoryOverride()!=Boolean.valueOf(isInventoryOverride)){
176
			amazonlisted.setIsInventoryOverride(Boolean.valueOf(isInventoryOverride));
177
			commit=true;
178
		}
179
		if(amazonlisted.isSuppressMfnPriceUpdate()!=Boolean.valueOf(suppressMfnPriceUpdate)){
180
			amazonlisted.setSuppressMfnPriceUpdate(Boolean.valueOf(suppressMfnPriceUpdate));
181
			commit=true;
182
		}
183
		if(amazonlisted.isSuppressFbaPriceUpdate()!=Boolean.valueOf(suppressFbaPriceUpdate)){
184
			amazonlisted.setSuppressFbaPriceUpdate(Boolean.valueOf(suppressFbaPriceUpdate));
185
			commit=true;
186
		}
8620 kshitij.so 187
 
188
		if(!StringUtils.equals(amazonlisted.getTaxCode(), taxCode)){
189
			amazonlisted.setTaxCode(taxCode);
190
			commit=true;
191
		}
10922 kshitij.so 192
 
193
		if(!StringUtils.equals(amazonlisted.getFbbtaxCode(), fbbTaxCode)){
194
            amazonlisted.setFbbtaxCode(fbbTaxCode);
195
            commit=true;
196
        }
197
 
198
		if(amazonlisted.isSuppressFbbPriceUpdate()!=Boolean.valueOf(suppressFbbPriceUpdate)){
199
            amazonlisted.setSuppressFbbPriceUpdate(Boolean.valueOf(suppressFbbPriceUpdate));
200
            commit=true;
201
        }
202
 
203
		if (amazonlisted.getFbbPrice()!=Double.valueOf(fbbPrice)){
204
            amazonlisted.setFbbPrice(Double.valueOf(fbbPrice));
205
            commit=true;
206
        }
207
		if (amazonlisted.isIsFbb()!=Boolean.valueOf(isFbb)){
208
            amazonlisted.setIsFbb(Boolean.valueOf(isFbb));
209
            commit=true;
210
        }
8620 kshitij.so 211
 
8168 kshitij.so 212
		if(commit){
213
			CatalogClient.updateAmazonItemDetails(amazonlisted);
214
		}
215
		else{
216
			logger.info("Nothing new to commit");
217
		}
7591 kshitij.so 218
		return "index";
7283 kshitij.so 219
 
220
	}
221
 
8168 kshitij.so 222
	public void upload() throws IOException, TException {
7524 kshitij.so 223
		File fileToCreate = new File("/tmp/", "Amazon-shipping.xls");
224
		FileUtils.copyFile(this.file, fileToCreate);
7741 kshitij.so 225
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
226
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
227
		HSSFSheet sheet = workbook.getSheetAt(0);
7762 kshitij.so 228
		Map<Long,Map<String,String>> amazonOrderMap = new HashMap<Long,Map<String,String>>();
7741 kshitij.so 229
		for (int iterator=sheet.getFirstRowNum();iterator<=sheet.getLastRowNum();iterator++){
230
			TransactionClient tcl = new TransactionClient();
231
			List<AmazonOrder> orders = tcl.getClient().getAmazonOrderByAmazonOrderId(sheet.getRow(iterator).getCell(0).getStringCellValue());
232
			for (AmazonOrder order : orders){
7753 kshitij.so 233
				if (StringUtils.equals(order.getStatus(), "Order-Payment-Success")){
7762 kshitij.so 234
					Map<String,String> dateMap = new HashMap<String,String>();
235
					dateMap.put(sheet.getRow(iterator).getCell(1).getStringCellValue(), sheet.getRow(iterator).getCell(2).getStringCellValue());
236
					amazonOrderMap.put(order.getOrderId(), dateMap);
7741 kshitij.so 237
				}
238
			}
239
		}
240
		TransactionClient tcl = new TransactionClient();
241
		tcl.getClient().updateTimestampForAmazonOrder(amazonOrderMap);
7524 kshitij.so 242
	}
243
 
8620 kshitij.so 244
 
245
 
7461 kshitij.so 246
	private void changeItemInventory() throws NumberFormatException, TException {
247
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
7524 kshitij.so 248
 
7461 kshitij.so 249
		if (StringUtils.isEmpty(holdInventory)) {
250
			holdInventory = "0";
251
		}
252
		if (StringUtils.isEmpty(defaultInventory)) {
253
			defaultInventory = "0";
254
		}
255
		CatalogClient.updateItemInventory(Long.valueOf(itemId),Long.valueOf(holdInventory),Long.valueOf(defaultInventory));
256
	}
257
 
258
 
7283 kshitij.so 259
	public Map<Long, Long> getAvailableItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
260
		InventoryClient inventoryServiceClient = new InventoryClient();
261
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
262
		in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
263
		return thriftItemInventory.getAvailability();
264
 
265
	}
266
 
267
	public Map<Long, Long> getReservedItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
268
		InventoryClient inventoryServiceClient = new InventoryClient();
269
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
270
		in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
271
		return thriftItemInventory.getReserved();
272
 
273
	}
274
 
275
	public String getWarehouseName(String warehouseId) throws NumberFormatException, TException { 
276
		InventoryClient inventoryServiceClient = new InventoryClient();
277
		return inventoryServiceClient.getClient().getWarehouseName(Long.valueOf(warehouseId));
278
	}
279
 
280
	public AmazonInventorySnapshot getInventoryForAmazonItem(String itemId) throws NumberFormatException, TException {
281
		InventoryClient inventoryServiceClient = new InventoryClient();
7365 kshitij.so 282
		try { 
283
			return inventoryServiceClient.getClient().getAmazonInventoryForItem(Long.valueOf(itemId));
284
		}
285
		catch (Exception e){
286
			return null;
287
		}
7283 kshitij.so 288
	}
289
 
8620 kshitij.so 290
	public long getFbaInventoryForAmazonItem(String itemId) throws NumberFormatException, TException {
291
		InventoryClient inventoryServiceClient = new InventoryClient();
292
		try { 
293
			return inventoryServiceClient.getClient().getAmazonFbaItemInventory(Long.valueOf(itemId));
294
		}
295
		catch (Exception e){
296
			return 0;
297
		}
298
	}
299
 
7365 kshitij.so 300
	public Long getTimetoShip(String itemId) throws LogisticsServiceException, TException{
301
		LogisticsClient logisticsClient = new LogisticsClient();
302
		return logisticsClient.getClient().getLogisticsInfo("110001", Long.valueOf(itemId), DeliveryType.COD, PickUpType.COURIER).getShippingTime();
303
 
304
	}
305
 
8168 kshitij.so 306
	public String runAsinJob() throws IOException, InterruptedException {
307
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
308
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
309
			return "authfail";
310
		}
311
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
8396 kshitij.so 312
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/RunAsinJob.sh"};
8168 kshitij.so 313
		testProcess.command(command);
314
		logger.info(testProcess.command().toString());
315
		Process process = testProcess.start();
316
		process.waitFor();
317
		logger.info(String.valueOf(process.exitValue()));
318
		return "asinjob";
8070 kshitij.so 319
 
8168 kshitij.so 320
	}
8620 kshitij.so 321
 
8396 kshitij.so 322
	public String runFbaListingJob() throws IOException, InterruptedException {
323
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
324
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
325
			return "authfail";
326
		}
327
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
328
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/RunFbaListingJob.sh"};
329
		testProcess.command(command);
330
		logger.info(testProcess.command().toString());
331
		Process process = testProcess.start();
332
		process.waitFor();
333
		logger.info(String.valueOf(process.exitValue()));
334
		return "asinjob";
335
	}
8620 kshitij.so 336
 
8396 kshitij.so 337
	public String runNonFbaListingJob() throws IOException, InterruptedException {
338
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
339
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
340
			return "authfail";
341
		}
342
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
343
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/RunNonFbaListingJob.sh"};
344
		testProcess.command(command);
345
		logger.info(testProcess.command().toString());
346
		Process process = testProcess.start();
347
		process.waitFor();
348
		logger.info(String.valueOf(process.exitValue()));
349
		return "asinjob";
350
	}
10097 kshitij.so 351
 
352
	public void runFbaInventoryJob() throws IOException, InterruptedException {
353
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
354
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/SubmitFBAInventoryFeed.sh"};
355
		testProcess.command(command);
356
		logger.info(testProcess.command().toString());
357
		Process process = testProcess.start();
358
		process.waitFor();
359
		logger.info(String.valueOf(process.exitValue()));
360
	}
8073 kshitij.so 361
 
8620 kshitij.so 362
	public void uploadAsinFile() throws IOException, TException{
363
		File fileToCreate = new File("/tmp/", "Amazon-asin-upload.xls");
364
		FileUtils.copyFile(this.file, fileToCreate);
365
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
366
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
367
		HSSFSheet sheet = workbook.getSheetAt(0);
368
		Map<Long,Item> amazonAsin = new HashMap<Long,Item>();
369
		StringBuilder sb =new StringBuilder();
370
		Client CatalogClient=null;
371
		try {
372
			CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
373
		} catch (TTransportException e) {
374
			e.printStackTrace();
375
		}
376
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
377
			Item item=null;
378
			Long sku;
379
			String asin;
380
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
381
				continue;
382
			}
383
			else {
384
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
385
				try {
386
					item = CatalogClient.getItem(sku);
387
				} catch (Exception e) {
388
					logger.info("Unable to fetch item details ID= "+sku);
389
					logger.info("Unable to fetch item. "+e);
390
					sb.append("Service exception ItemId not updated Id= "+sku+"\n");
391
					continue;
392
				} 
393
			}
394
 
395
			if(item.getId() == 0){
396
				logger.info("Item Id doesn't exist in catalog Id= "+sku);
397
				sb.append("Item Id doesn't exist in catalog Id= "+sku+"\n");
398
				continue;
399
			}
400
 
401
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
402
				asin = sheet.getRow(iterator).getCell(1).getStringCellValue();
403
				item.setAsin(asin);
404
			}
405
 
406
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
407
				long virtualInventory =(long) sheet.getRow(iterator).getCell(2).getNumericCellValue();
408
				item.setDefaultInventory(virtualInventory);
409
			}
410
 
411
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
412
				long riskyInventory =(long) sheet.getRow(iterator).getCell(3).getNumericCellValue();
413
				item.setHoldInventory(riskyInventory);
414
			}
415
 
416
			amazonAsin.put(sku, item);
417
		}
418
		CatalogClient.updateAsin(amazonAsin);
419
 
420
		logger.info("Amazon Asin Map "+amazonAsin.toString());
421
		CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
422
		CatalogClient.updateAsin(amazonAsin);
423
		File file = new File("/tmp/asinfile");
424
		FileWriter writer = new FileWriter(file);
425
		writer.append(sb.toString());
426
		writer.close();
427
		byte[] buffer = new byte[(int)file.length()];
428
		InputStream input = null;
429
		try {
430
			int totalBytesRead = 0;
431
			input = new BufferedInputStream(new FileInputStream(file));
432
			while(totalBytesRead < buffer.length){
433
				int bytesRemaining = buffer.length - totalBytesRead;
434
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
435
				if (bytesRead > 0){
436
					totalBytesRead = totalBytesRead + bytesRead;
437
				}
438
			}
439
		}
440
		finally {
441
			input.close();
442
			file.delete();
443
		}
444
 
445
		response.setHeader("Content-Type", "text/javascript");
446
 
447
		ServletOutputStream sos;
448
		try {
449
			sos = response.getOutputStream();
450
			sos.write(buffer);
451
			sos.flush();
452
		} catch (IOException e) {
453
			System.out.println("Unable to stream the manifest file");
454
		}   
455
	}
456
 
8168 kshitij.so 457
	public void uploadBulkFile() throws IOException, TException{
458
		File fileToCreate = new File("/tmp/", "Amazon-bulk-upload.xls");
459
		FileUtils.copyFile(this.file, fileToCreate);
460
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
461
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
10097 kshitij.so 462
 
8168 kshitij.so 463
		HSSFSheet sheet = workbook.getSheetAt(0);
464
		Map<Long,Amazonlisted> amazonBulkUpdate = new HashMap<Long,Amazonlisted>();
465
		Client CatalogClient=null;
8362 kshitij.so 466
		StringBuilder sb = new StringBuilder();
8168 kshitij.so 467
		try {
468
			CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
469
		} catch (TTransportException e) {
470
			// TODO Auto-generated catch block
471
			e.printStackTrace();
472
		}
473
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
474
			Amazonlisted amazonlisted =null;
475
			Long sku;
476
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
477
				continue;
478
			}
479
			else {
480
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
8362 kshitij.so 481
				try {
482
					amazonlisted = CatalogClient.getAmazonItemDetails(sku);
483
				} catch (TException e) {
484
					logger.info("Unable to fetch item details ID= "+sku);
485
					sb.append("Service exception ItemId not updated Id= "+sku+"\n");
486
					continue;
487
				}
8168 kshitij.so 488
			}
8620 kshitij.so 489
 
8362 kshitij.so 490
			if(amazonlisted.getItemid() == 0){
491
				logger.info("Item Id doesn't exist in amazonlisted Id= "+sku);
492
				sb.append("Item Id doesn't exist in amazonlisted Id= "+sku+"\n");
493
				continue;
494
			}
8168 kshitij.so 495
 
496
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
497
				double mfnPrice = sheet.getRow(iterator).getCell(1).getNumericCellValue();
498
				amazonlisted.setSellingPrice(mfnPrice);
499
			}
500
 
501
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
502
				double fbaSellingPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
503
				amazonlisted.setFbaPrice(fbaSellingPrice);
504
			}
10922 kshitij.so 505
 
506
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
507
                double fbbSellingPrice = sheet.getRow(iterator).getCell(3).getNumericCellValue();
508
                amazonlisted.setFbbPrice(fbbSellingPrice);
509
            }
8168 kshitij.so 510
 
10922 kshitij.so 511
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
512
				if ((long)sheet.getRow(iterator).getCell(4).getNumericCellValue()==1){
8168 kshitij.so 513
					amazonlisted.setIsNonFba(true);
514
				}
10922 kshitij.so 515
				if ((long)sheet.getRow(iterator).getCell(4).getNumericCellValue()==0){
8168 kshitij.so 516
					amazonlisted.setIsNonFba(false);
517
				}
518
			}
519
 
10922 kshitij.so 520
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
521
				if ((long)sheet.getRow(iterator).getCell(5).getNumericCellValue()==1){
8168 kshitij.so 522
					amazonlisted.setIsFba(true);
523
				}
10922 kshitij.so 524
				if ((long)sheet.getRow(iterator).getCell(5).getNumericCellValue()==0){
8168 kshitij.so 525
					amazonlisted.setIsFba(false);
526
				}
527
			}
10922 kshitij.so 528
 
529
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
530
                if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
531
                    amazonlisted.setIsFbb(true);
532
                }
533
                if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
534
                    amazonlisted.setIsFbb(false);
535
                }
536
            }
537
 
538
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
539
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
8168 kshitij.so 540
					amazonlisted.setIsInventoryOverride(true);
541
				}
10922 kshitij.so 542
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
8168 kshitij.so 543
					amazonlisted.setIsInventoryOverride(false);
544
				}
545
			}
10922 kshitij.so 546
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
547
				if ((long)sheet.getRow(iterator).getCell(8).getNumericCellValue()==1){
8168 kshitij.so 548
					amazonlisted.setSuppressMfnPriceUpdate(true);
549
				}
10922 kshitij.so 550
				if ((long)sheet.getRow(iterator).getCell(8).getNumericCellValue()==0){
8168 kshitij.so 551
					amazonlisted.setSuppressMfnPriceUpdate(false);
552
				}
553
			}
554
 
10922 kshitij.so 555
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
556
				if ((long)sheet.getRow(iterator).getCell(9).getNumericCellValue()==1){
8168 kshitij.so 557
					amazonlisted.setSuppressFbaPriceUpdate(true);
558
				}
10922 kshitij.so 559
				if ((long)sheet.getRow(iterator).getCell(9).getNumericCellValue()==0){
8168 kshitij.so 560
					amazonlisted.setSuppressFbaPriceUpdate(false);
561
				}
562
			}
10922 kshitij.so 563
 
564
			if (!checkEmptyString(sheet.getRow(iterator).getCell(10))){
565
                if ((long)sheet.getRow(iterator).getCell(10).getNumericCellValue()==1){
566
                    amazonlisted.setSuppressFbbPriceUpdate(true);
567
                }
568
                if ((long)sheet.getRow(iterator).getCell(10).getNumericCellValue()==0){
569
                    amazonlisted.setSuppressFbbPriceUpdate(false);
570
                }
571
            }
8620 kshitij.so 572
 
10922 kshitij.so 573
			if (!checkEmptyString(sheet.getRow(iterator).getCell(11))){
574
				String taxCode = sheet.getRow(iterator).getCell(11).getStringCellValue();
8620 kshitij.so 575
				amazonlisted.setTaxCode(taxCode);
576
			}
10922 kshitij.so 577
 
578
			if (!checkEmptyString(sheet.getRow(iterator).getCell(12))){
579
                String fbbTaxCode = sheet.getRow(iterator).getCell(12).getStringCellValue();
580
                amazonlisted.setFbbtaxCode(fbbTaxCode);
581
            }
582
 
8168 kshitij.so 583
			amazonBulkUpdate.put(sku, amazonlisted);
584
		}
585
		logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
586
		CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
10097 kshitij.so 587
		boolean result = CatalogClient.updateAmazonAttributesInBulk(amazonBulkUpdate);
588
		if (!result){
589
			sb = new StringBuilder();
590
			sb.append("Error : Unable to update");
591
		}
8620 kshitij.so 592
		File file = new File("/tmp/amazonbulk");
8362 kshitij.so 593
		FileWriter writer = new FileWriter(file);
594
		writer.append(sb.toString());
595
		writer.close();
596
		byte[] buffer = new byte[(int)file.length()];
597
		InputStream input = null;
598
		try {
599
			int totalBytesRead = 0;
600
			input = new BufferedInputStream(new FileInputStream(file));
601
			while(totalBytesRead < buffer.length){
602
				int bytesRemaining = buffer.length - totalBytesRead;
603
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
604
				if (bytesRead > 0){
605
					totalBytesRead = totalBytesRead + bytesRead;
606
				}
607
			}
608
		}
609
		finally {
610
			input.close();
611
			file.delete();
612
		}
613
 
614
		response.setHeader("Content-Type", "text/javascript");
615
 
616
		ServletOutputStream sos;
617
		try {
618
			sos = response.getOutputStream();
619
			sos.write(buffer);
620
			sos.flush();
621
		} catch (IOException e) {
622
			System.out.println("Unable to stream the manifest file");
623
		}   
8168 kshitij.so 624
	}
625
 
626
 
8620 kshitij.so 627
 
628
 
8168 kshitij.so 629
	public boolean checkEmptyString(Cell cell){
630
		if (cell==null){
631
			return true;
632
		}
633
		return false;
634
	}
635
 
636
	public String uploadSheet(){
637
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
638
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
639
			return "authfail";
640
		}
641
		return "amazon-shipping-upload";
642
	}
643
 
8620 kshitij.so 644
	public String uploadAsin(){
645
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
646
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
647
			return "authfail";
648
		}
649
		return "amazon-asin-upload";
650
	}
651
 
8168 kshitij.so 652
	public String uploadBulkSheet(){
653
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
654
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
655
			return "authfail";
656
		}
657
		return "amazon-bulk-upload";
658
	}
659
 
660
 
7283 kshitij.so 661
	public String edit() {
7591 kshitij.so 662
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
663
			return "authfail";
664
		}
7283 kshitij.so 665
		return "edit";
666
	}
667
 
668
	public String show() {
8168 kshitij.so 669
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
670
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
671
			return "authfail";
672
		}
673
 
674
		if (StringUtils.equals(id, "options")){
675
			return "options";
676
		}
8620 kshitij.so 677
 
678
		if (StringUtils.equals(id, "item-table")){
679
			return "amazon-item-table";
680
		}
681
 
8168 kshitij.so 682
		return "id";
7283 kshitij.so 683
	}
684
 
685
	public String editNew() {
686
		return "editNew";
8168 kshitij.so 687
	}	
7283 kshitij.so 688
 
689
	public void setId(String id) {
8168 kshitij.so 690
		logger.info(id);
7283 kshitij.so 691
		this.id = id;
692
	}
693
 
694
	public void setUrl(String url) {
695
		this.url = url;
696
	}
697
 
698
	public String getUrl() {
699
		return url;
700
	}
701
 
702
	public String getId() {
703
		return id;
704
	}
705
 
706
	public void setItemId(String itemId) {
707
		this.itemId = itemId;
708
	}
709
 
710
	public String getItemId() {
711
		return itemId;
712
	}
713
 
714
	public void setIsFba(String isFba) {
715
		this.isFba = isFba;
716
	}
717
 
718
	public String getIsFba() {
719
		return isFba;
720
	}
721
 
722
	public void setIsNonFba(String isNonFba) {
723
		this.isNonFba = isNonFba;
724
	}
725
 
726
	public String getIsNonFba() {
727
		return isNonFba;
728
	}
729
 
730
	public void setIsInventoryOverride(String isInventoryOverride) {
731
		this.isInventoryOverride = isInventoryOverride;
732
	}
733
 
734
	public String getIsInventoryOverride() {
735
		return isInventoryOverride;
736
	}
737
 
738
	public void setSellingPrice(String sellingPrice) {
739
		this.sellingPrice = sellingPrice;
740
	}
741
 
742
	public String getSellingPrice() {
743
		return sellingPrice;
744
	}
745
 
746
	public void setFbaPrice(String fbaPrice) {
747
		this.fbaPrice = fbaPrice;
748
	}
749
 
750
	public String getFbaPrice() {
751
		return fbaPrice;
752
	}
7365 kshitij.so 753
 
7283 kshitij.so 754
	public void setSaholicPrice(String saholicPrice) {
755
		this.saholicPrice = saholicPrice;
756
	}
757
 
758
	public String getSaholicPrice() {
759
		return saholicPrice;
760
	}
761
 
7365 kshitij.so 762
	public void setIsTime(String isTime){
8620 kshitij.so 763
		logger.info("set istime"+isTime);
7365 kshitij.so 764
		this.isTime = isTime;
765
	}
766
 
7461 kshitij.so 767
	public String getIsTime(){
7365 kshitij.so 768
		return isTime;
769
	}
770
 
771
	public void setHandlingTime(String handlingTime){
772
		this.handlingTime = handlingTime;
773
	}
774
 
7461 kshitij.so 775
	public String getHandlingTime(){
7365 kshitij.so 776
		return handlingTime;
777
	}
8168 kshitij.so 778
 
7365 kshitij.so 779
	public void setCustomHandlingTime(String customHandlingTime){
8620 kshitij.so 780
		logger.info("set custom handling time"+customHandlingTime);
7365 kshitij.so 781
		this.customHandlingTime = customHandlingTime;
782
	}
783
 
7461 kshitij.so 784
	public String getCustomHandlingTime(){
7365 kshitij.so 785
		return customHandlingTime;
786
	}
7524 kshitij.so 787
 
7461 kshitij.so 788
	public void setHoldInventory(String holdInventory){
789
		this.holdInventory = holdInventory;
790
	}
7365 kshitij.so 791
 
7461 kshitij.so 792
	public String getHoldInventory(){
793
		return holdInventory;
794
	}
7524 kshitij.so 795
 
7461 kshitij.so 796
	public void setDefaultInventory(String defaultInventory){
797
		this.defaultInventory = defaultInventory;
798
	}
799
 
800
	public String getDefaultInventory(){
801
		return defaultInventory;
802
	}
803
 
7524 kshitij.so 804
	public File getFile() {
805
		return file;
806
	}
807
 
808
	public void setFile(File file) {
809
		this.file = file;
810
	}
811
 
812
	public String getFileUploadContentType() {
813
		return fileUploadContentType;
814
	}
815
 
816
	public void setFileUploadContentType(String fileUploadContentType) {
817
		this.fileUploadContentType = fileUploadContentType;
818
	}
819
 
820
	public String getFileUploadFileName() {
821
		return fileUploadFileName;
822
	}
823
 
824
	public void setFileUploadFileName(String fileUploadFileName) {
825
		this.fileUploadFileName = fileUploadFileName;
826
	}
827
 
7591 kshitij.so 828
	public void setServletRequest(HttpServletRequest req) {
829
		this.request = req;
830
		this.session = req.getSession();        
7283 kshitij.so 831
	}
832
 
8168 kshitij.so 833
	@Override
834
	public void setServletContext(ServletContext arg0) {
835
		// TODO Auto-generated method stub
836
 
837
	}
838
 
839
 
840
	public void setSuppressMfnPriceUpdate(String suppressMfnPriceUpdate) {
841
		this.suppressMfnPriceUpdate = suppressMfnPriceUpdate;
842
	}
843
 
844
	public String getSuppressMfnPriceUpdate() {
845
		return suppressMfnPriceUpdate;
846
	}
847
 
848
	public void setSuppressFbaPriceUpdate(String suppressFbaPriceUpdate) {
849
		this.suppressFbaPriceUpdate = suppressFbaPriceUpdate;
850
	}
851
 
852
	public String getSuppressFbaPriceUpdate() {
853
		return suppressFbaPriceUpdate;
854
	}
8362 kshitij.so 855
 
856
	public void setErrMsg(String errMsg) {
857
		this.errMsg = errMsg;
858
	}
859
 
860
	public String getErrMsg() {
861
		return errMsg;
862
	}
8620 kshitij.so 863
 
8362 kshitij.so 864
	public void setServletResponse(HttpServletResponse response) {
865
		this.response = response;
866
	}
867
 
8620 kshitij.so 868
	public void setNext(String next) {
869
		logger.info("next is"+next);
870
		this.next = next;
871
	}
872
 
873
	public String getNext() {
874
		return next;
875
	}
876
 
877
	public void setAmazonItems(List<Amazonlisted> amazonItems) {
878
		this.amazonItems = amazonItems;
879
	}
880
 
881
	public List<Amazonlisted> getAmazonItems() {
882
		return amazonItems;
883
	}
884
 
885
	public void setSearchText(String searchText) {
886
		this.searchText = searchText;
887
	}
888
 
889
	public String getSearchText() {
890
		return searchText;
891
	}
892
 
893
	public long getSearchCount() {
894
		return searchCount;
895
	}
896
 
897
	public long getTotalCount() {
898
		return totalCount;
899
	}
900
 
901
	public void setSearchCount(long count) {
902
		this.searchCount = count;
903
	}
904
 
905
	public void setTaxCode(String taxCode) {
906
		this.taxCode = taxCode;
907
	}
908
 
909
	public String getTaxCode() {
910
		return taxCode;
911
	}
912
 
10922 kshitij.so 913
    public void setFbbTaxCode(String fbbTaxCode) {
914
        this.fbbTaxCode = fbbTaxCode;
915
    }
916
 
917
    public String get() {
918
        return fbbTaxCode;
919
    }
920
 
921
    public void setIsFbb(String isFbb) {
922
        this.isFbb = isFbb;
923
    }
924
 
925
    public String getIsFbb() {
926
        return isFbb;
927
    }
928
 
929
    public void setFbbPrice(String fbbPrice) {
930
        this.fbbPrice = fbbPrice;
931
    }
932
 
933
    public String getFbbPrice() {
934
        return fbbPrice;
935
    }
936
 
937
    public void setSuppressFbbPriceUpdate(String suppressFbbPriceUpdate) {
938
        this.suppressFbbPriceUpdate = suppressFbbPriceUpdate;
939
    }
940
 
941
    public String getSuppressFbbPriceUpdate() {
942
        return suppressFbbPriceUpdate;
943
    }
944
 
7283 kshitij.so 945
}