Subversion Repositories SmartDukaan

Rev

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