Subversion Repositories SmartDukaan

Rev

Rev 10922 | Rev 11197 | 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
	}
10925 kshitij.so 321
 
322
	public String runFbbListingJob() 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/RunFbbListingJob.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
    }
336
 
8620 kshitij.so 337
 
8396 kshitij.so 338
	public String runFbaListingJob() throws IOException, InterruptedException {
339
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
340
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
341
			return "authfail";
342
		}
343
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
344
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/RunFbaListingJob.sh"};
345
		testProcess.command(command);
346
		logger.info(testProcess.command().toString());
347
		Process process = testProcess.start();
348
		process.waitFor();
349
		logger.info(String.valueOf(process.exitValue()));
350
		return "asinjob";
351
	}
8620 kshitij.so 352
 
8396 kshitij.so 353
	public String runNonFbaListingJob() throws IOException, InterruptedException {
354
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
355
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
356
			return "authfail";
357
		}
358
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
359
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/RunNonFbaListingJob.sh"};
360
		testProcess.command(command);
361
		logger.info(testProcess.command().toString());
362
		Process process = testProcess.start();
363
		process.waitFor();
364
		logger.info(String.valueOf(process.exitValue()));
365
		return "asinjob";
366
	}
10097 kshitij.so 367
 
368
	public void runFbaInventoryJob() throws IOException, InterruptedException {
369
		ProcessBuilder testProcess = new ProcessBuilder("/bin/bash");
370
		String[] command = {"/bin/bash", "-c", "sudo /root/code/trunk/AmazonFeeds/SubmitFBAInventoryFeed.sh"};
371
		testProcess.command(command);
372
		logger.info(testProcess.command().toString());
373
		Process process = testProcess.start();
374
		process.waitFor();
375
		logger.info(String.valueOf(process.exitValue()));
376
	}
8073 kshitij.so 377
 
8620 kshitij.so 378
	public void uploadAsinFile() throws IOException, TException{
379
		File fileToCreate = new File("/tmp/", "Amazon-asin-upload.xls");
380
		FileUtils.copyFile(this.file, fileToCreate);
381
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
382
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
383
		HSSFSheet sheet = workbook.getSheetAt(0);
384
		Map<Long,Item> amazonAsin = new HashMap<Long,Item>();
385
		StringBuilder sb =new StringBuilder();
386
		Client CatalogClient=null;
387
		try {
388
			CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
389
		} catch (TTransportException e) {
390
			e.printStackTrace();
391
		}
392
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
393
			Item item=null;
394
			Long sku;
395
			String asin;
396
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
397
				continue;
398
			}
399
			else {
400
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
401
				try {
402
					item = CatalogClient.getItem(sku);
403
				} catch (Exception e) {
404
					logger.info("Unable to fetch item details ID= "+sku);
405
					logger.info("Unable to fetch item. "+e);
406
					sb.append("Service exception ItemId not updated Id= "+sku+"\n");
407
					continue;
408
				} 
409
			}
410
 
411
			if(item.getId() == 0){
412
				logger.info("Item Id doesn't exist in catalog Id= "+sku);
413
				sb.append("Item Id doesn't exist in catalog Id= "+sku+"\n");
414
				continue;
415
			}
416
 
417
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
418
				asin = sheet.getRow(iterator).getCell(1).getStringCellValue();
419
				item.setAsin(asin);
420
			}
421
 
422
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
423
				long virtualInventory =(long) sheet.getRow(iterator).getCell(2).getNumericCellValue();
424
				item.setDefaultInventory(virtualInventory);
425
			}
426
 
427
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
428
				long riskyInventory =(long) sheet.getRow(iterator).getCell(3).getNumericCellValue();
429
				item.setHoldInventory(riskyInventory);
430
			}
431
 
432
			amazonAsin.put(sku, item);
433
		}
434
		CatalogClient.updateAsin(amazonAsin);
435
 
436
		logger.info("Amazon Asin Map "+amazonAsin.toString());
437
		CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
438
		CatalogClient.updateAsin(amazonAsin);
439
		File file = new File("/tmp/asinfile");
440
		FileWriter writer = new FileWriter(file);
441
		writer.append(sb.toString());
442
		writer.close();
443
		byte[] buffer = new byte[(int)file.length()];
444
		InputStream input = null;
445
		try {
446
			int totalBytesRead = 0;
447
			input = new BufferedInputStream(new FileInputStream(file));
448
			while(totalBytesRead < buffer.length){
449
				int bytesRemaining = buffer.length - totalBytesRead;
450
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
451
				if (bytesRead > 0){
452
					totalBytesRead = totalBytesRead + bytesRead;
453
				}
454
			}
455
		}
456
		finally {
457
			input.close();
458
			file.delete();
459
		}
460
 
461
		response.setHeader("Content-Type", "text/javascript");
462
 
463
		ServletOutputStream sos;
464
		try {
465
			sos = response.getOutputStream();
466
			sos.write(buffer);
467
			sos.flush();
468
		} catch (IOException e) {
469
			System.out.println("Unable to stream the manifest file");
470
		}   
471
	}
472
 
8168 kshitij.so 473
	public void uploadBulkFile() throws IOException, TException{
474
		File fileToCreate = new File("/tmp/", "Amazon-bulk-upload.xls");
475
		FileUtils.copyFile(this.file, fileToCreate);
476
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
477
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
10097 kshitij.so 478
 
8168 kshitij.so 479
		HSSFSheet sheet = workbook.getSheetAt(0);
480
		Map<Long,Amazonlisted> amazonBulkUpdate = new HashMap<Long,Amazonlisted>();
481
		Client CatalogClient=null;
8362 kshitij.so 482
		StringBuilder sb = new StringBuilder();
8168 kshitij.so 483
		try {
484
			CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
485
		} catch (TTransportException e) {
486
			// TODO Auto-generated catch block
487
			e.printStackTrace();
488
		}
489
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
490
			Amazonlisted amazonlisted =null;
491
			Long sku;
492
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
493
				continue;
494
			}
495
			else {
496
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
8362 kshitij.so 497
				try {
498
					amazonlisted = CatalogClient.getAmazonItemDetails(sku);
499
				} catch (TException e) {
500
					logger.info("Unable to fetch item details ID= "+sku);
501
					sb.append("Service exception ItemId not updated Id= "+sku+"\n");
502
					continue;
503
				}
8168 kshitij.so 504
			}
8620 kshitij.so 505
 
8362 kshitij.so 506
			if(amazonlisted.getItemid() == 0){
507
				logger.info("Item Id doesn't exist in amazonlisted Id= "+sku);
508
				sb.append("Item Id doesn't exist in amazonlisted Id= "+sku+"\n");
509
				continue;
510
			}
8168 kshitij.so 511
 
512
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
513
				double mfnPrice = sheet.getRow(iterator).getCell(1).getNumericCellValue();
514
				amazonlisted.setSellingPrice(mfnPrice);
515
			}
516
 
517
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
518
				double fbaSellingPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
519
				amazonlisted.setFbaPrice(fbaSellingPrice);
520
			}
10922 kshitij.so 521
 
522
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
523
                double fbbSellingPrice = sheet.getRow(iterator).getCell(3).getNumericCellValue();
524
                amazonlisted.setFbbPrice(fbbSellingPrice);
525
            }
8168 kshitij.so 526
 
10922 kshitij.so 527
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
528
				if ((long)sheet.getRow(iterator).getCell(4).getNumericCellValue()==1){
8168 kshitij.so 529
					amazonlisted.setIsNonFba(true);
530
				}
10922 kshitij.so 531
				if ((long)sheet.getRow(iterator).getCell(4).getNumericCellValue()==0){
8168 kshitij.so 532
					amazonlisted.setIsNonFba(false);
533
				}
534
			}
535
 
10922 kshitij.so 536
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
537
				if ((long)sheet.getRow(iterator).getCell(5).getNumericCellValue()==1){
8168 kshitij.so 538
					amazonlisted.setIsFba(true);
539
				}
10922 kshitij.so 540
				if ((long)sheet.getRow(iterator).getCell(5).getNumericCellValue()==0){
8168 kshitij.so 541
					amazonlisted.setIsFba(false);
542
				}
543
			}
10922 kshitij.so 544
 
545
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
546
                if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
547
                    amazonlisted.setIsFbb(true);
548
                }
549
                if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
550
                    amazonlisted.setIsFbb(false);
551
                }
552
            }
553
 
554
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
555
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
8168 kshitij.so 556
					amazonlisted.setIsInventoryOverride(true);
557
				}
10922 kshitij.so 558
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
8168 kshitij.so 559
					amazonlisted.setIsInventoryOverride(false);
560
				}
561
			}
10922 kshitij.so 562
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
563
				if ((long)sheet.getRow(iterator).getCell(8).getNumericCellValue()==1){
8168 kshitij.so 564
					amazonlisted.setSuppressMfnPriceUpdate(true);
565
				}
10922 kshitij.so 566
				if ((long)sheet.getRow(iterator).getCell(8).getNumericCellValue()==0){
8168 kshitij.so 567
					amazonlisted.setSuppressMfnPriceUpdate(false);
568
				}
569
			}
570
 
10922 kshitij.so 571
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
572
				if ((long)sheet.getRow(iterator).getCell(9).getNumericCellValue()==1){
8168 kshitij.so 573
					amazonlisted.setSuppressFbaPriceUpdate(true);
574
				}
10922 kshitij.so 575
				if ((long)sheet.getRow(iterator).getCell(9).getNumericCellValue()==0){
8168 kshitij.so 576
					amazonlisted.setSuppressFbaPriceUpdate(false);
577
				}
578
			}
10922 kshitij.so 579
 
580
			if (!checkEmptyString(sheet.getRow(iterator).getCell(10))){
581
                if ((long)sheet.getRow(iterator).getCell(10).getNumericCellValue()==1){
582
                    amazonlisted.setSuppressFbbPriceUpdate(true);
583
                }
584
                if ((long)sheet.getRow(iterator).getCell(10).getNumericCellValue()==0){
585
                    amazonlisted.setSuppressFbbPriceUpdate(false);
586
                }
587
            }
8620 kshitij.so 588
 
10922 kshitij.so 589
			if (!checkEmptyString(sheet.getRow(iterator).getCell(11))){
590
				String taxCode = sheet.getRow(iterator).getCell(11).getStringCellValue();
8620 kshitij.so 591
				amazonlisted.setTaxCode(taxCode);
592
			}
10922 kshitij.so 593
 
594
			if (!checkEmptyString(sheet.getRow(iterator).getCell(12))){
595
                String fbbTaxCode = sheet.getRow(iterator).getCell(12).getStringCellValue();
596
                amazonlisted.setFbbtaxCode(fbbTaxCode);
597
            }
598
 
8168 kshitij.so 599
			amazonBulkUpdate.put(sku, amazonlisted);
600
		}
601
		logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
602
		CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
10097 kshitij.so 603
		boolean result = CatalogClient.updateAmazonAttributesInBulk(amazonBulkUpdate);
604
		if (!result){
605
			sb = new StringBuilder();
606
			sb.append("Error : Unable to update");
607
		}
8620 kshitij.so 608
		File file = new File("/tmp/amazonbulk");
8362 kshitij.so 609
		FileWriter writer = new FileWriter(file);
610
		writer.append(sb.toString());
611
		writer.close();
612
		byte[] buffer = new byte[(int)file.length()];
613
		InputStream input = null;
614
		try {
615
			int totalBytesRead = 0;
616
			input = new BufferedInputStream(new FileInputStream(file));
617
			while(totalBytesRead < buffer.length){
618
				int bytesRemaining = buffer.length - totalBytesRead;
619
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
620
				if (bytesRead > 0){
621
					totalBytesRead = totalBytesRead + bytesRead;
622
				}
623
			}
624
		}
625
		finally {
626
			input.close();
627
			file.delete();
628
		}
629
 
630
		response.setHeader("Content-Type", "text/javascript");
631
 
632
		ServletOutputStream sos;
633
		try {
634
			sos = response.getOutputStream();
635
			sos.write(buffer);
636
			sos.flush();
637
		} catch (IOException e) {
638
			System.out.println("Unable to stream the manifest file");
639
		}   
8168 kshitij.so 640
	}
641
 
642
 
8620 kshitij.so 643
 
644
 
8168 kshitij.so 645
	public boolean checkEmptyString(Cell cell){
646
		if (cell==null){
647
			return true;
648
		}
649
		return false;
650
	}
651
 
652
	public String uploadSheet(){
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-shipping-upload";
658
	}
659
 
8620 kshitij.so 660
	public String uploadAsin(){
661
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
662
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
663
			return "authfail";
664
		}
665
		return "amazon-asin-upload";
666
	}
667
 
8168 kshitij.so 668
	public String uploadBulkSheet(){
669
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
670
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
671
			return "authfail";
672
		}
673
		return "amazon-bulk-upload";
674
	}
675
 
676
 
7283 kshitij.so 677
	public String edit() {
7591 kshitij.so 678
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
679
			return "authfail";
680
		}
7283 kshitij.so 681
		return "edit";
682
	}
683
 
684
	public String show() {
8168 kshitij.so 685
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
686
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
687
			return "authfail";
688
		}
689
 
690
		if (StringUtils.equals(id, "options")){
691
			return "options";
692
		}
8620 kshitij.so 693
 
694
		if (StringUtils.equals(id, "item-table")){
695
			return "amazon-item-table";
696
		}
697
 
8168 kshitij.so 698
		return "id";
7283 kshitij.so 699
	}
700
 
701
	public String editNew() {
702
		return "editNew";
8168 kshitij.so 703
	}	
7283 kshitij.so 704
 
705
	public void setId(String id) {
8168 kshitij.so 706
		logger.info(id);
7283 kshitij.so 707
		this.id = id;
708
	}
709
 
710
	public void setUrl(String url) {
711
		this.url = url;
712
	}
713
 
714
	public String getUrl() {
715
		return url;
716
	}
717
 
718
	public String getId() {
719
		return id;
720
	}
721
 
722
	public void setItemId(String itemId) {
723
		this.itemId = itemId;
724
	}
725
 
726
	public String getItemId() {
727
		return itemId;
728
	}
729
 
730
	public void setIsFba(String isFba) {
731
		this.isFba = isFba;
732
	}
733
 
734
	public String getIsFba() {
735
		return isFba;
736
	}
737
 
738
	public void setIsNonFba(String isNonFba) {
739
		this.isNonFba = isNonFba;
740
	}
741
 
742
	public String getIsNonFba() {
743
		return isNonFba;
744
	}
745
 
746
	public void setIsInventoryOverride(String isInventoryOverride) {
747
		this.isInventoryOverride = isInventoryOverride;
748
	}
749
 
750
	public String getIsInventoryOverride() {
751
		return isInventoryOverride;
752
	}
753
 
754
	public void setSellingPrice(String sellingPrice) {
755
		this.sellingPrice = sellingPrice;
756
	}
757
 
758
	public String getSellingPrice() {
759
		return sellingPrice;
760
	}
761
 
762
	public void setFbaPrice(String fbaPrice) {
763
		this.fbaPrice = fbaPrice;
764
	}
765
 
766
	public String getFbaPrice() {
767
		return fbaPrice;
768
	}
7365 kshitij.so 769
 
7283 kshitij.so 770
	public void setSaholicPrice(String saholicPrice) {
771
		this.saholicPrice = saholicPrice;
772
	}
773
 
774
	public String getSaholicPrice() {
775
		return saholicPrice;
776
	}
777
 
7365 kshitij.so 778
	public void setIsTime(String isTime){
8620 kshitij.so 779
		logger.info("set istime"+isTime);
7365 kshitij.so 780
		this.isTime = isTime;
781
	}
782
 
7461 kshitij.so 783
	public String getIsTime(){
7365 kshitij.so 784
		return isTime;
785
	}
786
 
787
	public void setHandlingTime(String handlingTime){
788
		this.handlingTime = handlingTime;
789
	}
790
 
7461 kshitij.so 791
	public String getHandlingTime(){
7365 kshitij.so 792
		return handlingTime;
793
	}
8168 kshitij.so 794
 
7365 kshitij.so 795
	public void setCustomHandlingTime(String customHandlingTime){
8620 kshitij.so 796
		logger.info("set custom handling time"+customHandlingTime);
7365 kshitij.so 797
		this.customHandlingTime = customHandlingTime;
798
	}
799
 
7461 kshitij.so 800
	public String getCustomHandlingTime(){
7365 kshitij.so 801
		return customHandlingTime;
802
	}
7524 kshitij.so 803
 
7461 kshitij.so 804
	public void setHoldInventory(String holdInventory){
805
		this.holdInventory = holdInventory;
806
	}
7365 kshitij.so 807
 
7461 kshitij.so 808
	public String getHoldInventory(){
809
		return holdInventory;
810
	}
7524 kshitij.so 811
 
7461 kshitij.so 812
	public void setDefaultInventory(String defaultInventory){
813
		this.defaultInventory = defaultInventory;
814
	}
815
 
816
	public String getDefaultInventory(){
817
		return defaultInventory;
818
	}
819
 
7524 kshitij.so 820
	public File getFile() {
821
		return file;
822
	}
823
 
824
	public void setFile(File file) {
825
		this.file = file;
826
	}
827
 
828
	public String getFileUploadContentType() {
829
		return fileUploadContentType;
830
	}
831
 
832
	public void setFileUploadContentType(String fileUploadContentType) {
833
		this.fileUploadContentType = fileUploadContentType;
834
	}
835
 
836
	public String getFileUploadFileName() {
837
		return fileUploadFileName;
838
	}
839
 
840
	public void setFileUploadFileName(String fileUploadFileName) {
841
		this.fileUploadFileName = fileUploadFileName;
842
	}
843
 
7591 kshitij.so 844
	public void setServletRequest(HttpServletRequest req) {
845
		this.request = req;
846
		this.session = req.getSession();        
7283 kshitij.so 847
	}
848
 
8168 kshitij.so 849
	@Override
850
	public void setServletContext(ServletContext arg0) {
851
		// TODO Auto-generated method stub
852
 
853
	}
854
 
855
 
856
	public void setSuppressMfnPriceUpdate(String suppressMfnPriceUpdate) {
857
		this.suppressMfnPriceUpdate = suppressMfnPriceUpdate;
858
	}
859
 
860
	public String getSuppressMfnPriceUpdate() {
861
		return suppressMfnPriceUpdate;
862
	}
863
 
864
	public void setSuppressFbaPriceUpdate(String suppressFbaPriceUpdate) {
865
		this.suppressFbaPriceUpdate = suppressFbaPriceUpdate;
866
	}
867
 
868
	public String getSuppressFbaPriceUpdate() {
869
		return suppressFbaPriceUpdate;
870
	}
8362 kshitij.so 871
 
872
	public void setErrMsg(String errMsg) {
873
		this.errMsg = errMsg;
874
	}
875
 
876
	public String getErrMsg() {
877
		return errMsg;
878
	}
8620 kshitij.so 879
 
8362 kshitij.so 880
	public void setServletResponse(HttpServletResponse response) {
881
		this.response = response;
882
	}
883
 
8620 kshitij.so 884
	public void setNext(String next) {
885
		logger.info("next is"+next);
886
		this.next = next;
887
	}
888
 
889
	public String getNext() {
890
		return next;
891
	}
892
 
893
	public void setAmazonItems(List<Amazonlisted> amazonItems) {
894
		this.amazonItems = amazonItems;
895
	}
896
 
897
	public List<Amazonlisted> getAmazonItems() {
898
		return amazonItems;
899
	}
900
 
901
	public void setSearchText(String searchText) {
902
		this.searchText = searchText;
903
	}
904
 
905
	public String getSearchText() {
906
		return searchText;
907
	}
908
 
909
	public long getSearchCount() {
910
		return searchCount;
911
	}
912
 
913
	public long getTotalCount() {
914
		return totalCount;
915
	}
916
 
917
	public void setSearchCount(long count) {
918
		this.searchCount = count;
919
	}
920
 
921
	public void setTaxCode(String taxCode) {
922
		this.taxCode = taxCode;
923
	}
924
 
925
	public String getTaxCode() {
926
		return taxCode;
927
	}
928
 
10922 kshitij.so 929
    public void setFbbTaxCode(String fbbTaxCode) {
930
        this.fbbTaxCode = fbbTaxCode;
931
    }
932
 
933
    public String get() {
934
        return fbbTaxCode;
935
    }
936
 
937
    public void setIsFbb(String isFbb) {
938
        this.isFbb = isFbb;
939
    }
940
 
941
    public String getIsFbb() {
942
        return isFbb;
943
    }
944
 
945
    public void setFbbPrice(String fbbPrice) {
946
        this.fbbPrice = fbbPrice;
947
    }
948
 
949
    public String getFbbPrice() {
950
        return fbbPrice;
951
    }
952
 
953
    public void setSuppressFbbPriceUpdate(String suppressFbbPriceUpdate) {
954
        this.suppressFbbPriceUpdate = suppressFbbPriceUpdate;
955
    }
956
 
957
    public String getSuppressFbbPriceUpdate() {
958
        return suppressFbbPriceUpdate;
959
    }
960
 
7283 kshitij.so 961
}