Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
5711 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.inventory.controllers;
5
 
10689 manish.sha 6
import in.shop2020.model.v1.catalog.CatalogService;
7
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
8
import in.shop2020.model.v1.inventory.InventoryService;
9
import in.shop2020.thrift.clients.CatalogClient;
10
import in.shop2020.thrift.clients.InventoryClient;
5711 mandeep.dh 11
import in.shop2020.thrift.clients.WarehouseClient;
10689 manish.sha 12
import in.shop2020.warehouse.AmazonTransferredSkuDetail;
5711 mandeep.dh 13
import in.shop2020.warehouse.InventoryAge;
10689 manish.sha 14
import in.shop2020.warehouse.Scan;
15
import in.shop2020.warehouse.ScanType;
5711 mandeep.dh 16
import in.shop2020.warehouse.WarehouseService.Client;
17
 
18
import java.io.BufferedInputStream;
19
import java.io.BufferedWriter;
20
import java.io.File;
21
import java.io.FileInputStream;
10689 manish.sha 22
import java.io.FileNotFoundException;
23
import java.io.FileReader;
5711 mandeep.dh 24
import java.io.FileWriter;
25
import java.io.InputStream;
10689 manish.sha 26
import java.util.ArrayList;
27
import java.util.HashMap;
5711 mandeep.dh 28
import java.util.List;
10689 manish.sha 29
import java.util.Map;
5711 mandeep.dh 30
 
31
import javax.servlet.ServletOutputStream;
32
 
33
import org.apache.commons.lang.StringUtils;
34
import org.apache.commons.logging.Log;
35
import org.apache.commons.logging.LogFactory;
36
 
10689 manish.sha 37
 
38
 
5711 mandeep.dh 39
/**
40
 * @author mandeep
41
 *
42
 */
43
public class InventoryAgeController extends BaseController {
10689 manish.sha 44
	private static Log logger = LogFactory.getLog(InventoryAgeController.class);
5711 mandeep.dh 45
 
10689 manish.sha 46
	public String index() {
47
		try {
48
			Client client = new WarehouseClient().getClient();
49
			List<InventoryAge> inventoryAge = client.getInventoryAge();
50
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
51
			byte[] buffer = null;
52
			File file = createFile(inventoryAge, inventoryAgeAmazon);
53
			Thread.sleep(30000);
54
			buffer = new byte[(int) file.length()];
55
			InputStream input = null;
56
			try {
57
				int totalBytesRead = 0;
58
				input = new BufferedInputStream(new FileInputStream(file));
59
				while (totalBytesRead < buffer.length) {
60
					int bytesRemaining = buffer.length - totalBytesRead;
61
					// input.read() returns -1, 0, or more :
62
					int bytesRead = input.read(buffer, totalBytesRead,
63
							bytesRemaining);
64
					if (bytesRead > 0) {
65
						totalBytesRead = totalBytesRead + bytesRead;
66
					}
67
				}
68
				/*
69
				 * the above style is a bit tricky: it places bytes into the
70
				 * 'buffer' array; 'buffer' is an output parameter; the while
71
				 * loop usually has a single iteration only.
72
				 */
73
			} finally {
74
				input.close();
75
			}
5711 mandeep.dh 76
 
77
 
78
 
79
 
10689 manish.sha 80
			response.setContentType("application/vnd.ms-excel");
81
			response.setHeader("Content-disposition", "inline; filename="
82
					+ file.getName());
5711 mandeep.dh 83
 
10689 manish.sha 84
			ServletOutputStream sos = response.getOutputStream();
85
			sos.write(buffer);
86
			sos.flush();      
5711 mandeep.dh 87
 
10689 manish.sha 88
		}
89
		catch (Exception e) {
90
		}
91
 
92
		return null;
93
	}
94
 
95
	/**
96
	 * @param inventoryAge
97
	 * @return
98
	 */
99
	private File createFile(List<InventoryAge> inventoryAge, List<InventoryAge> inventoryAgeAmazon) {
100
		Map<Long,InventoryAge> inventoryAgeMap = new HashMap<Long,InventoryAge>();
11639 anikendra 101
 
10689 manish.sha 102
		for(InventoryAge invAge : inventoryAge){
103
			inventoryAgeMap.put(invAge.getItemId(), invAge);
104
		}
11639 anikendra 105
 
10689 manish.sha 106
		for(InventoryAge invAge : inventoryAgeAmazon){
107
			if(inventoryAgeMap.containsKey(invAge.getItemId())){
108
				InventoryAge invAgeObj = inventoryAgeMap.get(invAge.getItemId());
109
				invAgeObj.setFreshCount(invAgeObj.getFreshCount()+ invAge.getFreshCount());
110
				invAgeObj.setOneToTwoCount(invAgeObj.getOneToTwoCount() + invAge.getOneToTwoCount());
111
				invAgeObj.setTwoToThreeCount(invAgeObj.getTwoToThreeCount() + invAge.getTwoToThreeCount());
11639 anikendra 112
 
10689 manish.sha 113
				invAgeObj.setThreeToFourCount(invAgeObj.getThreeToFourCount() + invAge.getThreeToFourCount());
114
				invAgeObj.setFourPlusCount(invAgeObj.getFourPlusCount() + invAge.getFourPlusCount());
115
				invAgeObj.setThreeMonthPlusCount(invAgeObj.getThreeMonthPlusCount() + invAge.getThreeMonthPlusCount());
116
				invAgeObj.setSixMonthPlusCount(invAgeObj.getSixMonthPlusCount() + invAge.getSixMonthPlusCount());
11219 manish.sha 117
				invAgeObj.setZeroToThreeMonthCount(invAgeObj.getZeroToThreeMonthCount() + invAge.getZeroToThreeMonthCount());
118
				invAgeObj.setThreeToSixMonthCount(invAgeObj.getThreeToSixMonthCount() + invAge.getThreeToSixMonthCount());
119
				invAgeObj.setSixToTwelveMonthCount(invAgeObj.getSixToTwelveMonthCount() + invAge.getSixToTwelveMonthCount());
120
				invAgeObj.setTwelveMonthsPlusCount(invAgeObj.getTwelveMonthsPlusCount() + invAge.getTwelveMonthsPlusCount());
10689 manish.sha 121
				invAgeObj.setZeroPlusCount(invAgeObj.getZeroPlusCount() + invAge.getZeroPlusCount());
122
				invAgeObj.setOnePlusCount(invAgeObj.getOnePlusCount() + invAge.getOnePlusCount());
123
				invAgeObj.setZeroPlusCost(invAgeObj.getZeroPlusCost() + invAge.getZeroPlusCost());
124
				invAgeObj.setOnePlusCost(invAgeObj.getOnePlusCost() + invAge.getOnePlusCost());
11639 anikendra 125
 
10689 manish.sha 126
				inventoryAgeMap.put(invAge.getItemId(), invAgeObj);
11639 anikendra 127
 
10689 manish.sha 128
			}
129
			else{
130
				inventoryAgeMap.put(invAge.getItemId(), invAge);
131
			}
132
		}
133
		List<InventoryAge> finalInventoryAge = new ArrayList<InventoryAge>();
134
		for(Long itemId : inventoryAgeMap.keySet()){
135
			finalInventoryAge.add(inventoryAgeMap.get(itemId));
136
		}
137
		try {
138
			File file = new File("/tmp/InventoryAge.xls");
139
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
140
			bufferedWriter.write(StringUtils.join(new String[] {
141
					"Item Id",
142
					"Category",
143
					"Brand",
144
					"Model Name",
145
					"Model Number",
146
					"Color",
147
					"Fresh",
148
					"1-2 week",
149
					"2-3 week",
150
					"3-4 week",
151
					"4+ week",
152
					"3+ month",
153
					"6+ month",
11219 manish.sha 154
					"0-3 month",
155
					"3-6 month",
156
					"6-12 month",
157
					"12+ month",
10689 manish.sha 158
					"1+ week",
159
					"1+ week cost",
160
					"All",
161
			"All cost" }, '\t'));
162
 
163
			for (InventoryAge item : finalInventoryAge) {
164
				bufferedWriter.newLine();
165
 
166
				bufferedWriter.write(StringUtils.join(
167
						new String[] {
168
								String.valueOf(item.getItemId()),
169
								item.getCategory(),
170
								item.getBrand(),
171
								item.getModelName(),
172
								item.getModelNumber(),
173
								item.getColor(),
174
								String.valueOf(item.getFreshCount()),
175
								String.valueOf(item.getOneToTwoCount()),
176
								String.valueOf(item.getTwoToThreeCount()),
177
								String.valueOf(item.getThreeToFourCount()),
178
								String.valueOf(item.getFourPlusCount()),
179
								String.valueOf(item.getThreeMonthPlusCount()),
180
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 181
								String.valueOf(item.getZeroToThreeMonthCount()),
182
								String.valueOf(item.getThreeToSixMonthCount()),
183
								String.valueOf(item.getSixToTwelveMonthCount()),
184
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 185
								String.valueOf(item.getOnePlusCount()),
186
								String.valueOf(item.getOnePlusCost()),
187
								String.valueOf(item.getZeroPlusCount()),
188
								String.valueOf(item.getZeroPlusCost())}, '\t'));
189
			}
190
 
191
			bufferedWriter.close();
192
			return file;
193
		} catch (Exception e) {
194
			return null;
195
		}
196
	}
197
 
198
 
199
	public List<InventoryAge> getAmazonInventoryAge(){
200
		Long currentTimestamp = System.currentTimeMillis();
201
 
202
		List<InventoryAge> amazonInventoryAge = new ArrayList<InventoryAge>();
203
		try{
204
			InventoryService.Client inventoryClient = new InventoryClient().getClient();
205
			List<AmazonFbaInventorySnapshot> amazonFbaInventorySnapshot = inventoryClient.getAllAmazonFbaItemInventory();
11639 anikendra 206
 
10689 manish.sha 207
			List<Long> itemIds = new ArrayList<Long>();
11639 anikendra 208
 
209
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
210
				logger.info("AmazonFbaInventorySnapshot entry " + invSnapShot.getItem_id());
11640 vikram.rag 211
				itemIds.add(invSnapShot.getItem_id());
11639 anikendra 212
			}
213
 
10689 manish.sha 214
			Client client = new WarehouseClient().getClient();
11639 anikendra 215
 
10689 manish.sha 216
			List<AmazonTransferredSkuDetail> amazonTransferredSkuDetails = client.getAmazonTransferredSkuDetails(itemIds);
11582 manish.sha 217
			Map<Long, AmazonFbaInventorySnapshot> amazonFbaSnapshotMap = new HashMap<Long, AmazonFbaInventorySnapshot> ();
11639 anikendra 218
 
10689 manish.sha 219
			Map<Long, List<AmazonTransferredSkuDetail>> skuDetailsMap = new HashMap<Long, List<AmazonTransferredSkuDetail>>();
11639 anikendra 220
 
10689 manish.sha 221
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
222
				Long itemId= invSnapShot.getItem_id();
11582 manish.sha 223
				if(!amazonFbaSnapshotMap.containsKey(itemId)){
224
					amazonFbaSnapshotMap.put(itemId, invSnapShot);
11639 anikendra 225
					logger.info("Adding Availabity " + invSnapShot.getItem_id() + " Availability " +invSnapShot.getAvailability());
11582 manish.sha 226
				} else {
227
					AmazonFbaInventorySnapshot invSS = amazonFbaSnapshotMap.get(itemId);
228
					invSnapShot.setAvailability(invSnapShot.getAvailability() + invSS.getAvailability());
229
					amazonFbaSnapshotMap.put(itemId, invSnapShot);
11639 anikendra 230
					logger.info("Updating Availabity " + invSnapShot.getItem_id() + " Availability " +invSnapShot.getAvailability());
11582 manish.sha 231
				}
11639 anikendra 232
 
10689 manish.sha 233
				List<AmazonTransferredSkuDetail> mappedList = new ArrayList<AmazonTransferredSkuDetail>();
234
				for(AmazonTransferredSkuDetail detail: amazonTransferredSkuDetails){
235
					if(itemId == detail.getItemId()){
11639 anikendra 236
						logger.info("Amazon Transferred Sku Details Detail found for ITEM ID  " + detail.getItemId());
10689 manish.sha 237
						mappedList.add(detail);
238
					}
11639 anikendra 239
					else{
240
						logger.info("Amazon Transferred Sku Details Detail not found for ITEM ID  " + detail.getItemId());
241
					}
10689 manish.sha 242
				}
11639 anikendra 243
				if(mappedList.size()>0){
244
					skuDetailsMap.put(itemId, mappedList);
245
					logger.info("Amazon Sku Details transfer entries added Item ID " + itemId + " Total Entires " +mappedList.size());
246
				}
247
				else{
11640 vikram.rag 248
					logger.info("Amazon Sku Details transfer details is 0 " + itemId + " Total Entires " +mappedList.size());
11639 anikendra 249
				}
10689 manish.sha 250
			}
251
 
252
			//CatalogService.Client catalogClient = new CatalogClient().getClient();
11639 anikendra 253
 
11582 manish.sha 254
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaSnapshotMap.values()){
10689 manish.sha 255
				try{
256
 
257
					Long item_id = invSnapShot.getItem_id();
258
					List<AmazonTransferredSkuDetail> amazonTranSkuDetails = skuDetailsMap.get(item_id);
259
					long amazonFbaInvAvailability = invSnapShot.getAvailability();
260
					List<AmazonTransferredSkuDetail> toBeIncludeList = new ArrayList<AmazonTransferredSkuDetail>();
261
 
262
					long freshCount = 0;
263
					long oneToTwoCount = 0;
264
					long twoToThreeCount = 0;
265
					long threeToFourCount = 0;
266
					long fourPlusCount = 0;
267
					long onePlusCount = 0;
268
					long threeMonthPlusCount = 0;
269
					long sixMonthPlusCount = 0;
11219 manish.sha 270
					long zeroToThreeMonthCount = 0;
271
					long threeToSixMonthsCount = 0;
272
					long sixToTwelveMonthsCount = 0;
273
					long twelveMonthsPlusCount = 0;					
10689 manish.sha 274
					long onePlusCost = 0;
275
					long zeroPlusCount = 0;
276
					long zeroPlusCost = 0;
277
					String brand ="";
278
					String modelName ="";
279
					String modelNumber ="";
280
					String color ="";
281
					String category ="";
282
 
283
					if(amazonTranSkuDetails!=null && amazonTranSkuDetails.size()>0){
284
						brand = amazonTranSkuDetails.get(0).getBrand();
285
						modelName = amazonTranSkuDetails.get(0).getModelName();
286
						modelNumber = amazonTranSkuDetails.get(0).getModelNumber();
287
						color = amazonTranSkuDetails.get(0).getColor();
288
						category = amazonTranSkuDetails.get(0).getCategory();
11639 anikendra 289
					}else{	
290
						logger.info("Amazon Sku Transfer details  skipped " + item_id);
10689 manish.sha 291
						continue;
292
					}
293
					double invAge = 0;
294
 
295
					for(AmazonTransferredSkuDetail amazonSkuDetail : amazonTranSkuDetails){
296
						if(amazonFbaInvAvailability == amazonSkuDetail.getQuantity()){
297
							toBeIncludeList.add(amazonSkuDetail);
298
							break;
299
						}
300
						if(amazonFbaInvAvailability > amazonSkuDetail.getQuantity()){
301
							toBeIncludeList.add(amazonSkuDetail);
302
							amazonFbaInvAvailability = amazonFbaInvAvailability- amazonSkuDetail.getQuantity();
303
							continue;
304
						}
305
						if(amazonFbaInvAvailability < amazonSkuDetail.getQuantity()){
306
							amazonSkuDetail.setQuantity(amazonFbaInvAvailability);
307
							toBeIncludeList.add(amazonSkuDetail);
308
							break;
309
						}
310
					}
311
 
312
					for(AmazonTransferredSkuDetail amazonSkuInfo : toBeIncludeList){
313
						if(!amazonSkuInfo.isSetPurchaseDate() || amazonSkuInfo.getPurchaseDate() == 0){
314
							if(!client.isAlive()){
315
								client = new WarehouseClient().getClient();
316
							}
317
							List<Scan> purchaseScans = client.getScansforPurchase(amazonSkuInfo.getPurchaseId(), ScanType.PURCHASE);
318
							amazonSkuInfo.setPurchaseDate(purchaseScans.get(0).getScannedAt());
319
						}
320
						invAge = (double)(currentTimestamp - amazonSkuInfo.getPurchaseDate())/ (24 * 60 * 60 * 1000 * 7);
321
						if(invAge < 1){
322
							freshCount = freshCount + amazonSkuInfo.getQuantity();
323
						}
324
						if(invAge >= 1 && invAge < 2){
325
							oneToTwoCount = oneToTwoCount + amazonSkuInfo.getQuantity();
326
						}
327
						if(invAge >= 2 && invAge < 3){
328
							twoToThreeCount = twoToThreeCount + amazonSkuInfo.getQuantity();
329
						}
330
						if(invAge >= 3 && invAge < 4){
331
							threeToFourCount = threeToFourCount + amazonSkuInfo.getQuantity();
332
						}
333
						if(invAge >= 4){
334
							fourPlusCount = fourPlusCount + amazonSkuInfo.getQuantity();
335
						}
336
						if(invAge >= 1){
337
							onePlusCount = onePlusCount + amazonSkuInfo.getQuantity();
338
							onePlusCost = (long)(onePlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
339
						}
340
						if(invAge >= 13){
341
							threeMonthPlusCount = threeMonthPlusCount + amazonSkuInfo.getQuantity();
342
						}
343
						if(invAge >= 26){
344
							sixMonthPlusCount = sixMonthPlusCount + amazonSkuInfo.getQuantity();
345
						}
11639 anikendra 346
 
11219 manish.sha 347
						if(invAge >0 && invAge <=13){
348
							zeroToThreeMonthCount = zeroToThreeMonthCount + amazonSkuInfo.getQuantity();
349
						}
11639 anikendra 350
 
11219 manish.sha 351
						if(invAge >13 && invAge <=26){
352
							threeToSixMonthsCount = threeToSixMonthsCount + amazonSkuInfo.getQuantity();
353
						}
11639 anikendra 354
 
11219 manish.sha 355
						if(invAge >26 && invAge <=52){
356
							sixToTwelveMonthsCount = sixToTwelveMonthsCount + amazonSkuInfo.getQuantity();
357
						}
11639 anikendra 358
 
11219 manish.sha 359
						if(invAge > 52){
360
							twelveMonthsPlusCount = twelveMonthsPlusCount + amazonSkuInfo.getQuantity();
361
						}
10689 manish.sha 362
 
363
						zeroPlusCount = zeroPlusCount + amazonSkuInfo.getQuantity(); 
11639 anikendra 364
 
10689 manish.sha 365
						zeroPlusCost = (long)(zeroPlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
366
					}
367
 
11639 anikendra 368
 
10689 manish.sha 369
					InventoryAge inventAge = new InventoryAge();
370
					inventAge.setItemId(item_id);
371
					inventAge.setBrand(brand);
372
					inventAge.setModelName(modelName);
373
					inventAge.setModelNumber(modelNumber);
374
					inventAge.setColor(color);
375
					inventAge.setFreshCount(freshCount);
376
					inventAge.setOneToTwoCount(oneToTwoCount);
377
					inventAge.setTwoToThreeCount(twoToThreeCount);
378
					inventAge.setThreeToFourCount(threeToFourCount);
379
					inventAge.setFourPlusCount(fourPlusCount);
380
					inventAge.setThreeMonthPlusCount(threeMonthPlusCount);
381
					inventAge.setSixMonthPlusCount(sixMonthPlusCount);
11219 manish.sha 382
					inventAge.setZeroToThreeMonthCount(zeroToThreeMonthCount);
383
					inventAge.setThreeToSixMonthCount(threeToSixMonthsCount);
384
					inventAge.setSixToTwelveMonthCount(sixToTwelveMonthsCount);
385
					inventAge.setTwelveMonthsPlusCount(twelveMonthsPlusCount);
10689 manish.sha 386
					inventAge.setZeroPlusCount(zeroPlusCount);
387
					inventAge.setOnePlusCount(onePlusCount);
388
					inventAge.setZeroPlusCost(zeroPlusCost);
389
					inventAge.setOnePlusCost(onePlusCost);
390
					inventAge.setCategory(category);
391
 
392
					amazonInventoryAge.add(inventAge);
393
				}
394
				catch(Exception e){
395
					e.printStackTrace();				
396
				}
397
			}
398
 
399
		}
400
		catch(Exception e){
401
			e.printStackTrace();				
402
		}
403
		return amazonInventoryAge;
404
	}
11639 anikendra 405
 
10689 manish.sha 406
	public File createFile(List<InventoryAge> inventoryAge, String name){
407
		try {
408
			File file = new File(name);
409
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
410
			bufferedWriter.write(StringUtils.join(new String[] {
411
					"Item Id",
412
					"Category",
413
					"Brand",
414
					"Model Name",
415
					"Model Number",
416
					"Color",
417
					"Fresh",
418
					"1-2 week",
419
					"2-3 week",
420
					"3-4 week",
421
					"4+ week",
422
					"3+ month",
423
					"6+ month",
11219 manish.sha 424
					"0-3 month",
425
					"3-6 month",
426
					"6-12 month",
427
					"12+ month",
10689 manish.sha 428
					"1+ week",
429
					"1+ week cost",
430
					"All",
431
			"All cost" }, '\t'));
432
 
433
			for (InventoryAge item : inventoryAge) {
434
				bufferedWriter.newLine();
435
 
436
				bufferedWriter.write(StringUtils.join(
437
						new String[] {
438
								String.valueOf(item.getItemId()),
439
								item.getCategory(),
440
								item.getBrand(),
441
								item.getModelName(),
442
								item.getModelNumber(),
443
								item.getColor(),
444
								String.valueOf(item.getFreshCount()),
445
								String.valueOf(item.getOneToTwoCount()),
446
								String.valueOf(item.getTwoToThreeCount()),
447
								String.valueOf(item.getThreeToFourCount()),
448
								String.valueOf(item.getFourPlusCount()),
449
								String.valueOf(item.getThreeMonthPlusCount()),
450
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 451
								String.valueOf(item.getZeroToThreeMonthCount()),
452
								String.valueOf(item.getThreeToSixMonthCount()),
453
								String.valueOf(item.getSixToTwelveMonthCount()),
454
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 455
								String.valueOf(item.getOnePlusCount()),
456
								String.valueOf(item.getOnePlusCost()),
457
								String.valueOf(item.getZeroPlusCount()),
458
								String.valueOf(item.getZeroPlusCost())}, '\t'));
459
			}
460
 
461
			bufferedWriter.close();
462
			return file;
463
		} catch (Exception e) {
464
			return null;
465
		}
11639 anikendra 466
 
10689 manish.sha 467
	}
11639 anikendra 468
 
10689 manish.sha 469
	public void downloadAmazonInventoryAge(){
470
		try {
471
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
472
			byte[] buffer = null;
473
			File file = createFile(inventoryAgeAmazon,"/tmp/AmazonInventoryAge.xls");
474
			Thread.sleep(10000);
475
			buffer = new byte[(int) file.length()];
476
			InputStream input = null;
477
			try {
478
				int totalBytesRead = 0;
479
				input = new BufferedInputStream(new FileInputStream(file));
480
				while (totalBytesRead < buffer.length) {
481
					int bytesRemaining = buffer.length - totalBytesRead;
482
					// input.read() returns -1, 0, or more :
483
					int bytesRead = input.read(buffer, totalBytesRead,
484
							bytesRemaining);
485
					if (bytesRead > 0) {
486
						totalBytesRead = totalBytesRead + bytesRead;
487
					}
488
				}
489
				/*
490
				 * the above style is a bit tricky: it places bytes into the
491
				 * 'buffer' array; 'buffer' is an output parameter; the while
492
				 * loop usually has a single iteration only.
493
				 */
494
			} finally {
495
				input.close();
496
			}
497
 
498
 
499
 
500
 
501
			response.setContentType("application/vnd.ms-excel");
502
			response.setHeader("Content-disposition", "inline; filename="
503
					+ file.getName());
504
 
505
			ServletOutputStream sos = response.getOutputStream();
506
			sos.write(buffer);
507
			sos.flush();      
508
 
509
		}
510
		catch (Exception e) {
511
		}
512
 
513
	}
11639 anikendra 514
 
10689 manish.sha 515
	public void downloadSaholicInventoryAge(){
516
		try {
517
			Client client = new WarehouseClient().getClient();
518
			List<InventoryAge> inventoryAge = client.getInventoryAge();
519
			byte[] buffer = null;
520
			File file = createFile(inventoryAge,"/tmp/SaholicInventoryAge.xls");
521
			Thread.sleep(10000);
522
			buffer = new byte[(int) file.length()];
523
			InputStream input = null;
524
			try {
525
				int totalBytesRead = 0;
526
				input = new BufferedInputStream(new FileInputStream(file));
527
				while (totalBytesRead < buffer.length) {
528
					int bytesRemaining = buffer.length - totalBytesRead;
529
					// input.read() returns -1, 0, or more :
530
					int bytesRead = input.read(buffer, totalBytesRead,
531
							bytesRemaining);
532
					if (bytesRead > 0) {
533
						totalBytesRead = totalBytesRead + bytesRead;
534
					}
535
				}
536
				/*
537
				 * the above style is a bit tricky: it places bytes into the
538
				 * 'buffer' array; 'buffer' is an output parameter; the while
539
				 * loop usually has a single iteration only.
540
				 */
541
			} finally {
542
				input.close();
543
			}
544
 
545
 
546
 
547
 
548
			response.setContentType("application/vnd.ms-excel");
549
			response.setHeader("Content-disposition", "inline; filename="
550
					+ file.getName());
551
 
552
			ServletOutputStream sos = response.getOutputStream();
553
			sos.write(buffer);
554
			sos.flush();      
555
 
556
		}
557
		catch (Exception e) {
558
		}
559
 
560
	}
561
 
562
	/*public static void main(String[] args) throws Exception{
11639 anikendra 563
 
10689 manish.sha 564
	}*/
5711 mandeep.dh 565
}