Subversion Repositories SmartDukaan

Rev

Rev 11582 | Rev 11640 | 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());
211
				if(!itemIds.contains(invSnapShot.getItem_id())){
212
					itemIds.add(invSnapShot.getItem_id());
213
				}
214
				else{
215
					logger.info("AmazonFbaInventorySnapshot entry skipped" + invSnapShot.getItem_id());
216
				}
217
			}
218
 
10689 manish.sha 219
			Client client = new WarehouseClient().getClient();
11639 anikendra 220
 
10689 manish.sha 221
			List<AmazonTransferredSkuDetail> amazonTransferredSkuDetails = client.getAmazonTransferredSkuDetails(itemIds);
11582 manish.sha 222
			Map<Long, AmazonFbaInventorySnapshot> amazonFbaSnapshotMap = new HashMap<Long, AmazonFbaInventorySnapshot> ();
11639 anikendra 223
 
10689 manish.sha 224
			Map<Long, List<AmazonTransferredSkuDetail>> skuDetailsMap = new HashMap<Long, List<AmazonTransferredSkuDetail>>();
11639 anikendra 225
 
10689 manish.sha 226
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaInventorySnapshot){
227
				Long itemId= invSnapShot.getItem_id();
11582 manish.sha 228
				if(!amazonFbaSnapshotMap.containsKey(itemId)){
229
					amazonFbaSnapshotMap.put(itemId, invSnapShot);
11639 anikendra 230
					logger.info("Adding Availabity " + invSnapShot.getItem_id() + " Availability " +invSnapShot.getAvailability());
11582 manish.sha 231
				} else {
232
					AmazonFbaInventorySnapshot invSS = amazonFbaSnapshotMap.get(itemId);
233
					invSnapShot.setAvailability(invSnapShot.getAvailability() + invSS.getAvailability());
234
					amazonFbaSnapshotMap.put(itemId, invSnapShot);
11639 anikendra 235
					logger.info("Updating Availabity " + invSnapShot.getItem_id() + " Availability " +invSnapShot.getAvailability());
11582 manish.sha 236
				}
11639 anikendra 237
 
10689 manish.sha 238
				List<AmazonTransferredSkuDetail> mappedList = new ArrayList<AmazonTransferredSkuDetail>();
239
				for(AmazonTransferredSkuDetail detail: amazonTransferredSkuDetails){
240
					if(itemId == detail.getItemId()){
11639 anikendra 241
						logger.info("Amazon Transferred Sku Details Detail found for ITEM ID  " + detail.getItemId());
10689 manish.sha 242
						mappedList.add(detail);
243
					}
11639 anikendra 244
					else{
245
						logger.info("Amazon Transferred Sku Details Detail not found for ITEM ID  " + detail.getItemId());
246
					}
10689 manish.sha 247
				}
11639 anikendra 248
				if(mappedList.size()>0){
249
					skuDetailsMap.put(itemId, mappedList);
250
					logger.info("Amazon Sku Details transfer entries added Item ID " + itemId + " Total Entires " +mappedList.size());
251
				}
252
				else{
253
					logger.info("Amazon Sku Details transfer entries not found added Item ID " + itemId + " Total Entires " +mappedList.size());
254
				}
10689 manish.sha 255
			}
256
 
257
			//CatalogService.Client catalogClient = new CatalogClient().getClient();
11639 anikendra 258
 
11582 manish.sha 259
			for(AmazonFbaInventorySnapshot invSnapShot : amazonFbaSnapshotMap.values()){
10689 manish.sha 260
				try{
261
 
262
					Long item_id = invSnapShot.getItem_id();
263
					List<AmazonTransferredSkuDetail> amazonTranSkuDetails = skuDetailsMap.get(item_id);
264
					long amazonFbaInvAvailability = invSnapShot.getAvailability();
265
					List<AmazonTransferredSkuDetail> toBeIncludeList = new ArrayList<AmazonTransferredSkuDetail>();
266
 
267
					long freshCount = 0;
268
					long oneToTwoCount = 0;
269
					long twoToThreeCount = 0;
270
					long threeToFourCount = 0;
271
					long fourPlusCount = 0;
272
					long onePlusCount = 0;
273
					long threeMonthPlusCount = 0;
274
					long sixMonthPlusCount = 0;
11219 manish.sha 275
					long zeroToThreeMonthCount = 0;
276
					long threeToSixMonthsCount = 0;
277
					long sixToTwelveMonthsCount = 0;
278
					long twelveMonthsPlusCount = 0;					
10689 manish.sha 279
					long onePlusCost = 0;
280
					long zeroPlusCount = 0;
281
					long zeroPlusCost = 0;
282
					String brand ="";
283
					String modelName ="";
284
					String modelNumber ="";
285
					String color ="";
286
					String category ="";
287
 
288
					if(amazonTranSkuDetails!=null && amazonTranSkuDetails.size()>0){
289
						brand = amazonTranSkuDetails.get(0).getBrand();
290
						modelName = amazonTranSkuDetails.get(0).getModelName();
291
						modelNumber = amazonTranSkuDetails.get(0).getModelNumber();
292
						color = amazonTranSkuDetails.get(0).getColor();
293
						category = amazonTranSkuDetails.get(0).getCategory();
11639 anikendra 294
					}else{	
295
						logger.info("Amazon Sku Transfer details  skipped " + item_id);
10689 manish.sha 296
						continue;
297
					}
298
					double invAge = 0;
299
 
300
					for(AmazonTransferredSkuDetail amazonSkuDetail : amazonTranSkuDetails){
301
						if(amazonFbaInvAvailability == amazonSkuDetail.getQuantity()){
302
							toBeIncludeList.add(amazonSkuDetail);
303
							break;
304
						}
305
						if(amazonFbaInvAvailability > amazonSkuDetail.getQuantity()){
306
							toBeIncludeList.add(amazonSkuDetail);
307
							amazonFbaInvAvailability = amazonFbaInvAvailability- amazonSkuDetail.getQuantity();
308
							continue;
309
						}
310
						if(amazonFbaInvAvailability < amazonSkuDetail.getQuantity()){
311
							amazonSkuDetail.setQuantity(amazonFbaInvAvailability);
312
							toBeIncludeList.add(amazonSkuDetail);
313
							break;
314
						}
315
					}
316
 
317
					for(AmazonTransferredSkuDetail amazonSkuInfo : toBeIncludeList){
318
						if(!amazonSkuInfo.isSetPurchaseDate() || amazonSkuInfo.getPurchaseDate() == 0){
319
							if(!client.isAlive()){
320
								client = new WarehouseClient().getClient();
321
							}
322
							List<Scan> purchaseScans = client.getScansforPurchase(amazonSkuInfo.getPurchaseId(), ScanType.PURCHASE);
323
							amazonSkuInfo.setPurchaseDate(purchaseScans.get(0).getScannedAt());
324
						}
325
						invAge = (double)(currentTimestamp - amazonSkuInfo.getPurchaseDate())/ (24 * 60 * 60 * 1000 * 7);
326
						if(invAge < 1){
327
							freshCount = freshCount + amazonSkuInfo.getQuantity();
328
						}
329
						if(invAge >= 1 && invAge < 2){
330
							oneToTwoCount = oneToTwoCount + amazonSkuInfo.getQuantity();
331
						}
332
						if(invAge >= 2 && invAge < 3){
333
							twoToThreeCount = twoToThreeCount + amazonSkuInfo.getQuantity();
334
						}
335
						if(invAge >= 3 && invAge < 4){
336
							threeToFourCount = threeToFourCount + amazonSkuInfo.getQuantity();
337
						}
338
						if(invAge >= 4){
339
							fourPlusCount = fourPlusCount + amazonSkuInfo.getQuantity();
340
						}
341
						if(invAge >= 1){
342
							onePlusCount = onePlusCount + amazonSkuInfo.getQuantity();
343
							onePlusCost = (long)(onePlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
344
						}
345
						if(invAge >= 13){
346
							threeMonthPlusCount = threeMonthPlusCount + amazonSkuInfo.getQuantity();
347
						}
348
						if(invAge >= 26){
349
							sixMonthPlusCount = sixMonthPlusCount + amazonSkuInfo.getQuantity();
350
						}
11639 anikendra 351
 
11219 manish.sha 352
						if(invAge >0 && invAge <=13){
353
							zeroToThreeMonthCount = zeroToThreeMonthCount + amazonSkuInfo.getQuantity();
354
						}
11639 anikendra 355
 
11219 manish.sha 356
						if(invAge >13 && invAge <=26){
357
							threeToSixMonthsCount = threeToSixMonthsCount + amazonSkuInfo.getQuantity();
358
						}
11639 anikendra 359
 
11219 manish.sha 360
						if(invAge >26 && invAge <=52){
361
							sixToTwelveMonthsCount = sixToTwelveMonthsCount + amazonSkuInfo.getQuantity();
362
						}
11639 anikendra 363
 
11219 manish.sha 364
						if(invAge > 52){
365
							twelveMonthsPlusCount = twelveMonthsPlusCount + amazonSkuInfo.getQuantity();
366
						}
10689 manish.sha 367
 
368
						zeroPlusCount = zeroPlusCount + amazonSkuInfo.getQuantity(); 
11639 anikendra 369
 
10689 manish.sha 370
						zeroPlusCost = (long)(zeroPlusCost + ( amazonSkuInfo.getQuantity() * amazonSkuInfo.getUnitPrice() ));
371
					}
372
 
11639 anikendra 373
 
10689 manish.sha 374
					InventoryAge inventAge = new InventoryAge();
375
					inventAge.setItemId(item_id);
376
					inventAge.setBrand(brand);
377
					inventAge.setModelName(modelName);
378
					inventAge.setModelNumber(modelNumber);
379
					inventAge.setColor(color);
380
					inventAge.setFreshCount(freshCount);
381
					inventAge.setOneToTwoCount(oneToTwoCount);
382
					inventAge.setTwoToThreeCount(twoToThreeCount);
383
					inventAge.setThreeToFourCount(threeToFourCount);
384
					inventAge.setFourPlusCount(fourPlusCount);
385
					inventAge.setThreeMonthPlusCount(threeMonthPlusCount);
386
					inventAge.setSixMonthPlusCount(sixMonthPlusCount);
11219 manish.sha 387
					inventAge.setZeroToThreeMonthCount(zeroToThreeMonthCount);
388
					inventAge.setThreeToSixMonthCount(threeToSixMonthsCount);
389
					inventAge.setSixToTwelveMonthCount(sixToTwelveMonthsCount);
390
					inventAge.setTwelveMonthsPlusCount(twelveMonthsPlusCount);
10689 manish.sha 391
					inventAge.setZeroPlusCount(zeroPlusCount);
392
					inventAge.setOnePlusCount(onePlusCount);
393
					inventAge.setZeroPlusCost(zeroPlusCost);
394
					inventAge.setOnePlusCost(onePlusCost);
395
					inventAge.setCategory(category);
396
 
397
					amazonInventoryAge.add(inventAge);
398
				}
399
				catch(Exception e){
400
					e.printStackTrace();				
401
				}
402
			}
403
 
404
		}
405
		catch(Exception e){
406
			e.printStackTrace();				
407
		}
408
		return amazonInventoryAge;
409
	}
11639 anikendra 410
 
10689 manish.sha 411
	public File createFile(List<InventoryAge> inventoryAge, String name){
412
		try {
413
			File file = new File(name);
414
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
415
			bufferedWriter.write(StringUtils.join(new String[] {
416
					"Item Id",
417
					"Category",
418
					"Brand",
419
					"Model Name",
420
					"Model Number",
421
					"Color",
422
					"Fresh",
423
					"1-2 week",
424
					"2-3 week",
425
					"3-4 week",
426
					"4+ week",
427
					"3+ month",
428
					"6+ month",
11219 manish.sha 429
					"0-3 month",
430
					"3-6 month",
431
					"6-12 month",
432
					"12+ month",
10689 manish.sha 433
					"1+ week",
434
					"1+ week cost",
435
					"All",
436
			"All cost" }, '\t'));
437
 
438
			for (InventoryAge item : inventoryAge) {
439
				bufferedWriter.newLine();
440
 
441
				bufferedWriter.write(StringUtils.join(
442
						new String[] {
443
								String.valueOf(item.getItemId()),
444
								item.getCategory(),
445
								item.getBrand(),
446
								item.getModelName(),
447
								item.getModelNumber(),
448
								item.getColor(),
449
								String.valueOf(item.getFreshCount()),
450
								String.valueOf(item.getOneToTwoCount()),
451
								String.valueOf(item.getTwoToThreeCount()),
452
								String.valueOf(item.getThreeToFourCount()),
453
								String.valueOf(item.getFourPlusCount()),
454
								String.valueOf(item.getThreeMonthPlusCount()),
455
								String.valueOf(item.getSixMonthPlusCount()),
11219 manish.sha 456
								String.valueOf(item.getZeroToThreeMonthCount()),
457
								String.valueOf(item.getThreeToSixMonthCount()),
458
								String.valueOf(item.getSixToTwelveMonthCount()),
459
								String.valueOf(item.getTwelveMonthsPlusCount()),
10689 manish.sha 460
								String.valueOf(item.getOnePlusCount()),
461
								String.valueOf(item.getOnePlusCost()),
462
								String.valueOf(item.getZeroPlusCount()),
463
								String.valueOf(item.getZeroPlusCost())}, '\t'));
464
			}
465
 
466
			bufferedWriter.close();
467
			return file;
468
		} catch (Exception e) {
469
			return null;
470
		}
11639 anikendra 471
 
10689 manish.sha 472
	}
11639 anikendra 473
 
10689 manish.sha 474
	public void downloadAmazonInventoryAge(){
475
		try {
476
			List<InventoryAge> inventoryAgeAmazon = getAmazonInventoryAge();
477
			byte[] buffer = null;
478
			File file = createFile(inventoryAgeAmazon,"/tmp/AmazonInventoryAge.xls");
479
			Thread.sleep(10000);
480
			buffer = new byte[(int) file.length()];
481
			InputStream input = null;
482
			try {
483
				int totalBytesRead = 0;
484
				input = new BufferedInputStream(new FileInputStream(file));
485
				while (totalBytesRead < buffer.length) {
486
					int bytesRemaining = buffer.length - totalBytesRead;
487
					// input.read() returns -1, 0, or more :
488
					int bytesRead = input.read(buffer, totalBytesRead,
489
							bytesRemaining);
490
					if (bytesRead > 0) {
491
						totalBytesRead = totalBytesRead + bytesRead;
492
					}
493
				}
494
				/*
495
				 * the above style is a bit tricky: it places bytes into the
496
				 * 'buffer' array; 'buffer' is an output parameter; the while
497
				 * loop usually has a single iteration only.
498
				 */
499
			} finally {
500
				input.close();
501
			}
502
 
503
 
504
 
505
 
506
			response.setContentType("application/vnd.ms-excel");
507
			response.setHeader("Content-disposition", "inline; filename="
508
					+ file.getName());
509
 
510
			ServletOutputStream sos = response.getOutputStream();
511
			sos.write(buffer);
512
			sos.flush();      
513
 
514
		}
515
		catch (Exception e) {
516
		}
517
 
518
	}
11639 anikendra 519
 
10689 manish.sha 520
	public void downloadSaholicInventoryAge(){
521
		try {
522
			Client client = new WarehouseClient().getClient();
523
			List<InventoryAge> inventoryAge = client.getInventoryAge();
524
			byte[] buffer = null;
525
			File file = createFile(inventoryAge,"/tmp/SaholicInventoryAge.xls");
526
			Thread.sleep(10000);
527
			buffer = new byte[(int) file.length()];
528
			InputStream input = null;
529
			try {
530
				int totalBytesRead = 0;
531
				input = new BufferedInputStream(new FileInputStream(file));
532
				while (totalBytesRead < buffer.length) {
533
					int bytesRemaining = buffer.length - totalBytesRead;
534
					// input.read() returns -1, 0, or more :
535
					int bytesRead = input.read(buffer, totalBytesRead,
536
							bytesRemaining);
537
					if (bytesRead > 0) {
538
						totalBytesRead = totalBytesRead + bytesRead;
539
					}
540
				}
541
				/*
542
				 * the above style is a bit tricky: it places bytes into the
543
				 * 'buffer' array; 'buffer' is an output parameter; the while
544
				 * loop usually has a single iteration only.
545
				 */
546
			} finally {
547
				input.close();
548
			}
549
 
550
 
551
 
552
 
553
			response.setContentType("application/vnd.ms-excel");
554
			response.setHeader("Content-disposition", "inline; filename="
555
					+ file.getName());
556
 
557
			ServletOutputStream sos = response.getOutputStream();
558
			sos.write(buffer);
559
			sos.flush();      
560
 
561
		}
562
		catch (Exception e) {
563
		}
564
 
565
	}
566
 
567
	/*public static void main(String[] args) throws Exception{
11639 anikendra 568
 
10689 manish.sha 569
	}*/
5711 mandeep.dh 570
}