Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
19248 kshitij.so 1
package in.shop2020.support.controllers;
2
 
3
 
4
import java.io.BufferedInputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
 
16
import in.shop2020.model.v1.catalog.CatalogServiceException;
17
import in.shop2020.model.v1.catalog.Item;
18
import in.shop2020.model.v1.catalog.ItemPricing;
19
import in.shop2020.model.v1.catalog.CatalogService.Client;
20
import in.shop2020.model.v1.dtr.BulkItems;
21
import in.shop2020.model.v1.inventory.BulkAddInventory;
22
import in.shop2020.model.v1.inventory.InventoryServiceException;
23
import in.shop2020.model.v1.inventory.Vendor;
24
import in.shop2020.model.v1.inventory.VendorItemPricing;
25
import in.shop2020.model.v1.inventory.Warehouse;
26
import in.shop2020.model.v1.order.AmazonOrder;
27
import in.shop2020.support.utils.ReportsUtils;
28
import in.shop2020.thrift.clients.CatalogClient;
29
import in.shop2020.thrift.clients.DtrClient;
30
import in.shop2020.thrift.clients.InventoryClient;
31
import in.shop2020.thrift.clients.TransactionClient;
32
 
33
import org.apache.commons.io.FileUtils;
34
import org.apache.commons.lang.xwork.StringUtils;
35
 
36
import javax.servlet.ServletContext;
37
import javax.servlet.ServletOutputStream;
38
import javax.servlet.http.HttpServletRequest;
39
import javax.servlet.http.HttpServletResponse;
40
import javax.servlet.http.HttpSession;
41
 
42
import org.apache.poi.hssf.usermodel.HSSFRow;
43
import org.apache.poi.hssf.usermodel.HSSFSheet;
44
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
45
import org.apache.poi.ss.usermodel.Cell;
46
import org.apache.struts2.convention.annotation.InterceptorRef;
47
import org.apache.struts2.convention.annotation.InterceptorRefs;
48
import org.apache.struts2.interceptor.ServletRequestAware;
49
import org.apache.struts2.interceptor.ServletResponseAware;
50
import org.apache.struts2.util.ServletContextAware;
51
import org.apache.thrift.TException;
52
import org.apache.thrift.transport.TTransportException;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55
 
56
import com.opensymphony.xwork2.ValidationAwareSupport;
57
 
58
@SuppressWarnings({"unused","deprecation"})
59
 
60
@InterceptorRefs({
61
	@InterceptorRef("defaultStack"),
62
	@InterceptorRef("login")
63
})
64
 
65
public class BulkAddController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
66
	/**
67
	 * 
68
	 */
69
	private static final long serialVersionUID = 1L;
70
 
71
 
72
	private static Logger logger = LoggerFactory.getLogger(BulkAddController.class);
73
 
74
 
75
	private HttpServletRequest request;
76
	private HttpServletResponse response;
77
	private HttpSession session;
78
	private ServletContext context;
79
	private String id;
80
	private String result;
81
	private File file;
82
 
83
	public String index() {
84
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
85
			return "authfail";
86
		}
87
		return "index";
88
	}
89
 
90
	public String edit() {
91
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
92
			return "authfail";
93
		}
94
		return "edit";
95
	}
96
 
19253 kshitij.so 97
 
19248 kshitij.so 98
	public String show() {
99
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
100
			return "authfail";
101
		}
102
		logger.info("Inside show for "+id);
19253 kshitij.so 103
 
19248 kshitij.so 104
		if (StringUtils.equals(id, "bulk-add-options")){
105
			return "bulk-add-options";
106
		}
107
		return "id";
108
	}
19253 kshitij.so 109
 
19248 kshitij.so 110
	public String loadVendorDiv(){
111
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
112
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
113
			return "authfail";
114
		}
115
		return "vendor-item-pricing-upload";
116
	}
19253 kshitij.so 117
 
19248 kshitij.so 118
	public String loadVirtualDiv(){
119
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
120
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
121
			return "authfail";
122
		}
123
		return "virtual-bulk-add";
124
	}
19253 kshitij.so 125
 
19248 kshitij.so 126
	public String loadCatalogDiv(){
127
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
128
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
129
			return "authfail";
130
		}
131
		return "catalog-bulk-add";
132
	}
19253 kshitij.so 133
 
19248 kshitij.so 134
	public String loadDtrDiv(){
135
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
136
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
137
			return "authfail";
138
		}
139
		return "dtr-bulk-add";
140
	}
19253 kshitij.so 141
 
19248 kshitij.so 142
	public String uploadItemsDtr() throws IOException, CatalogServiceException, TException{
143
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
144
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
145
			return "authfail";
146
		}
147
		File fileToCreate = new File("/tmp/", "dtr-items.xls");
148
		FileUtils.copyFile(this.file, fileToCreate);
149
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
150
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
151
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 152
 
19248 kshitij.so 153
		List<BulkItems> bulkItemsList= new ArrayList<BulkItems>();
19253 kshitij.so 154
 
19248 kshitij.so 155
		List<Long> items = new ArrayList<Long>();
156
 
157
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
158
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
159
			items.add(item_id);
160
		}
161
 
162
		Client cc = new CatalogClient().getClient();
163
		Map<Long, Item> itemMap = cc.getItems(items);
164
		StringBuilder sb = new StringBuilder(); 
165
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
166
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
167
			Item t_item = itemMap.get(itemId);
168
			if (t_item==null){
169
				sb.append("Item is not valid "+itemId+"\n");
170
				continue;
171
			}
172
			BulkItems b = new BulkItems();
173
			b.setItem_id(itemId);
19253 kshitij.so 174
 
175
			if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
176
				b.setShowMrpFlag(false);
177
			}
178
			else{
179
				int show_mrp_flag = (int) sheet.getRow(iterator).getCell(1).getNumericCellValue();
180
				b.setShowMrpFlag(show_mrp_flag ==1 ? true : false);
181
			}
182
			if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
183
				b.setTagline("");
184
			}
185
			else{
186
				String tagline = sheet.getRow(iterator).getCell(2).getStringCellValue();
187
				b.setTagline(tagline);
188
			}
189
			if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
190
				b.setOffer("");
191
			}
192
			else{
193
				String offer = sheet.getRow(iterator).getCell(3).getStringCellValue();
194
				b.setOffer(offer);
195
			}
19248 kshitij.so 196
			long category_id = (long) sheet.getRow(iterator).getCell(4).getNumericCellValue();
197
			if (category_id!=6L){
198
				sb.append("Category Id is not valid "+itemId+"\n");
199
			}
200
			b.setCategory_id(category_id);
201
			long sub_category_id = (long) sheet.getRow(iterator).getCell(5).getNumericCellValue();
202
			if (sub_category_id==0){
203
				sb.append("SubCategory Id is not valid "+itemId+"\n");
204
			}
205
			b.setSubCategoryId(sub_category_id);
206
			int show_net_price = (int) sheet.getRow(iterator).getCell(6).getNumericCellValue();
207
			b.setShowNetPrice(show_net_price ==1 ? true : false);
208
			long brand_id = (long) sheet.getRow(iterator).getCell(7).getNumericCellValue();
209
			if (brand_id==0){
210
				sb.append("Brand Id is not valid "+itemId+"\n");
211
			}
212
			b.setBrand_id(brand_id);
213
			b.setAvailable_price(t_item.getSellingPrice());
214
			b.setMrp(t_item.getMrp());
215
			b.setBrand(t_item.getBrand());
216
			b.setIdentifier(String.valueOf(t_item.getCatalogItemId()));
217
			b.setModel_name(t_item.getModelName()+" "+t_item.getModelNumber());
218
			b.setProduct_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
219
			b.setSource_product_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
220
			b.setQuantity(t_item.getPackQuantity());
221
			bulkItemsList.add(b);
222
		}
223
		if (sb.length()>0){
224
			setResult("Please correct error \n"+sb.toString());
225
			return "item-details-json";
226
		}
227
		in.shop2020.model.v1.dtr.DtrService.Client dc = new DtrClient("dtr_service_server_host","dtr_service_server_port").getClient(); 
228
		List<String> res = dc.addItemsInBulk(bulkItemsList);
229
		if (res.size()>0){
230
			for(String s : res){
231
				sb.append("Catalog ItemId not added "+s+"\n");
232
			}
233
			setResult(sb.toString());
234
		}else{
235
			setResult("Items added successfully");
236
		}
237
		return "item-details-json";
238
	}
19253 kshitij.so 239
 
19248 kshitij.so 240
	public String updateVirtualInventory() throws IOException, CatalogServiceException, TException, InventoryServiceException{
241
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
242
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
243
			return "authfail";
244
		}
19253 kshitij.so 245
 
19248 kshitij.so 246
		File fileToCreate = new File("/tmp/", "vendor-inventory.xls");
247
		FileUtils.copyFile(this.file, fileToCreate);
248
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
249
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
250
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 251
 
19248 kshitij.so 252
		List<BulkAddInventory> vendorInventory= new ArrayList<BulkAddInventory>();
19253 kshitij.so 253
 
19248 kshitij.so 254
		List<Long> items = new ArrayList<Long>();
255
 
256
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
257
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
258
			items.add(item_id);
259
		}
260
		Client cc = new CatalogClient().getClient();
261
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 262
 
19248 kshitij.so 263
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
264
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
265
			if (itemMap.get(itemId)==null){
266
				continue;
267
			}
268
			BulkAddInventory b = new BulkAddInventory();
269
			b.setItem_id(itemId);
270
			long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
271
			b.setWarehouse_id(warehouseId);
272
			long inventory = (long) sheet.getRow(iterator).getCell(2).getNumericCellValue();
273
			b.setInventory(inventory);
274
			vendorInventory.add(b);
275
		}
19253 kshitij.so 276
 
19248 kshitij.so 277
		InventoryClient inventoryServiceClient = new InventoryClient();
278
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
279
		inventoryClient.addInventoryInBulk(vendorInventory);
280
		setResult("Inventory updated successfully");
281
		return "item-details-json";
282
	}
19253 kshitij.so 283
 
19248 kshitij.so 284
	public String addVendorItemPricing() throws IOException, TException, CatalogServiceException{
285
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
286
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
287
			return "authfail";
288
		}
289
		File fileToCreate = new File("/tmp/", "vendor-pricing.xls");
290
		FileUtils.copyFile(this.file, fileToCreate);
291
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
292
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
293
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 294
 
19248 kshitij.so 295
		List<VendorItemPricing> vendorItemPricingList= new ArrayList<VendorItemPricing>();
296
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 297
 
19248 kshitij.so 298
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
299
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
300
			items.add(item_id);
301
		}
302
		Client cc = new CatalogClient().getClient();
303
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 304
 
19248 kshitij.so 305
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
306
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
307
			if (itemMap.get(itemId)==null){
308
				continue;
309
			}
310
			VendorItemPricing v = new VendorItemPricing();
311
			v.setItemId(itemId);
312
			long vendorId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
313
			v.setVendorId(vendorId);
314
			double mop = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
315
			v.setMop(mop);
316
			double dp = (double) sheet.getRow(iterator).getCell(3).getNumericCellValue();
317
			v.setDealerPrice(dp);
318
			double tp = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
319
			v.setTransferPrice(tp);
320
			double nlc = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
321
			v.setNlc(nlc);
322
			vendorItemPricingList.add(v);
19253 kshitij.so 323
		}
324
 
19248 kshitij.so 325
		InventoryClient inventoryServiceClient = new InventoryClient();
326
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
327
		List<Long> notUpdated = inventoryClient.addVendorItemPricingInBulk(vendorItemPricingList);
19253 kshitij.so 328
 
19248 kshitij.so 329
		if (notUpdated.size() > 0){
330
			StringBuilder sb = new StringBuilder();
331
			for (Long x : notUpdated){
332
				sb.append("Vendor Pricing not updated for "+x+"\n");
333
			}
334
			setResult(sb.toString());
335
		}
336
		else{
337
			setResult("Vendor item pricing updated.");
338
		}
339
		return "item-details-json";
340
	}
19253 kshitij.so 341
 
19248 kshitij.so 342
	public String updateItemPricing() throws IOException, CatalogServiceException, TException{
343
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
344
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
345
			return "authfail";
346
		}
347
		File fileToCreate = new File("/tmp/", "item-pricing.xls");
348
		FileUtils.copyFile(this.file, fileToCreate);
349
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
350
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
351
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 352
 
19248 kshitij.so 353
		List<ItemPricing> itemPricingList= new ArrayList<ItemPricing>();
354
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 355
 
19248 kshitij.so 356
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
357
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
358
			items.add(item_id);
359
		}
360
		Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
361
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 362
 
19248 kshitij.so 363
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
364
			ItemPricing i = new ItemPricing();
365
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
366
			i.setItem_id(item_id);
367
			if (itemMap.get(item_id)!=null){
368
				if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
369
					i.setSelling_price(itemMap.get(item_id).getSellingPrice());
370
				}
371
				else{
372
					double sellingPrice = (double) sheet.getRow(iterator).getCell(1).getNumericCellValue();
373
					i.setSelling_price(sellingPrice);
374
				}
375
				if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
376
					i.setMrp(itemMap.get(item_id).getMrp());
377
				}
378
				else{
379
					double mrp = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
380
					i.setMrp(mrp);
381
				}
382
				if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
383
					i.setPreferred_vendor(itemMap.get(item_id).getPreferredVendor());
384
				}
385
				else{
386
					long preferred_vendor = (long) sheet.getRow(iterator).getCell(3).getNumericCellValue();
387
					i.setPreferred_vendor(preferred_vendor);
388
				}
389
				if (checkEmptyString(sheet.getRow(iterator).getCell(4))){
390
					i.setPrivate_deal_price(0);
391
				}
392
				else{
393
					double private_deal_price = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
394
					i.setPrivate_deal_price(private_deal_price);
395
				}
19249 kshitij.so 396
				if (checkEmptyString(sheet.getRow(iterator).getCell(5))){
397
					i.setWeight(itemMap.get(item_id).getWeight());
398
				}
399
				else{
400
					double weight = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
19252 kshitij.so 401
					i.setWeight(weight);
19249 kshitij.so 402
				}
19253 kshitij.so 403
				itemPricingList.add(i);
19248 kshitij.so 404
			}
405
		}
406
		boolean res = cc.updateItemPricing(itemPricingList);
407
		if (res){
408
			setResult("Pricing updated successfully");
409
		}
410
		else{
411
			setResult("Unable to update pricing");
412
		}
413
		return "item-details-json";
414
	}
19253 kshitij.so 415
 
19248 kshitij.so 416
	public String downloadVendors() throws TException, IOException{
417
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
418
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
419
			return "authfail";
420
		}
421
		InventoryClient inventoryServiceClient = new InventoryClient();
422
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
423
		List<Vendor> vendors = inventoryClient.getAllVendors();
19253 kshitij.so 424
 
19248 kshitij.so 425
		File file = new File("/tmp/vendors.xls");
426
		HSSFWorkbook hwb=new HSSFWorkbook();
427
		HSSFSheet sheet =  hwb.createSheet("Vendors");
428
		HSSFRow rowhead=   sheet.createRow((short)0);
429
		rowhead.createCell((short) 0).setCellValue("VENDOR_ID");
430
		rowhead.createCell((short) 1).setCellValue("VENDOR_NAME");
19253 kshitij.so 431
 
19248 kshitij.so 432
		int iterator= 1;
433
		for (Vendor v : vendors){
434
			HSSFRow row = sheet.createRow((short)iterator);
435
			row.createCell((short) 0).setCellValue(v.getId());
436
			row.createCell((short) 1).setCellValue(v.getName());
437
			iterator++;
438
		}
19253 kshitij.so 439
 
19248 kshitij.so 440
		FileOutputStream fileOut = null;
441
		try {
442
			fileOut = new FileOutputStream(file);
443
		} catch (FileNotFoundException e) {
444
			// TODO Auto-generated catch block
445
			e.printStackTrace();
446
		}
447
		try {
448
			hwb.write(fileOut);
449
		} catch (IOException e) {
450
			// TODO Auto-generated catch block
451
			e.printStackTrace();
452
		}
453
		try {
454
			fileOut.close();
455
		} catch (IOException e) {
456
			// TODO Auto-generated catch block
457
			e.printStackTrace();
458
		}
459
		byte[] buffer = new byte[(int)file.length()];
460
		InputStream input = null;
461
		try {
462
			int totalBytesRead = 0;
463
			input = new BufferedInputStream(new FileInputStream(file));
464
			while(totalBytesRead < buffer.length){
465
				int bytesRemaining = buffer.length - totalBytesRead;
466
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
467
				if (bytesRead > 0){
468
					totalBytesRead = totalBytesRead + bytesRead;
469
				}
470
			}
471
		}
472
		finally {
473
			input.close();
474
			file.delete();
475
		}
476
 
477
		response.setHeader("Content-Disposition", "attachment; filename=\"Vendors.xls\"");
478
		response.setContentType("application/octet-stream");
479
		ServletOutputStream sos;
480
		try {
481
			sos = response.getOutputStream();
482
			sos.write(buffer);
483
			sos.flush();
484
		} catch (IOException e) {
485
			System.out.println("Unable to stream the manifest file");
486
		}
487
		setResult("Vendor Streaming done");
488
		return "item-details-json";
19253 kshitij.so 489
 
19248 kshitij.so 490
	}
19253 kshitij.so 491
 
19248 kshitij.so 492
	public String downloadWarehouses() throws TException, IOException{
493
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
494
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
495
			return "authfail";
496
		}
497
		InventoryClient inventoryServiceClient = new InventoryClient();
498
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
499
		List<Warehouse> warehouses = inventoryClient.getWarehouses(null, null, 0, 0, 0);
19253 kshitij.so 500
 
19248 kshitij.so 501
		File file = new File("/tmp/warehouses.xls");
502
		HSSFWorkbook hwb=new HSSFWorkbook();
503
		HSSFSheet sheet =  hwb.createSheet("Warehouses");
504
		HSSFRow rowhead=   sheet.createRow((short)0);
505
		rowhead.createCell((short) 0).setCellValue("warehouse_id");
506
		rowhead.createCell((short) 1).setCellValue("display_name");
507
		rowhead.createCell((short) 2).setCellValue("location");
508
		rowhead.createCell((short) 3).setCellValue("vendor_id");
509
		rowhead.createCell((short) 4).setCellValue("vendor_name");
510
		rowhead.createCell((short) 5).setCellValue("inventory_type");
511
		rowhead.createCell((short) 6).setCellValue("warehouse_type");
512
		rowhead.createCell((short) 7).setCellValue("logistics_location");
513
		rowhead.createCell((short) 8).setCellValue("state_id");
19253 kshitij.so 514
 
19248 kshitij.so 515
		int iterator= 1;
516
		for (Warehouse w : warehouses){
517
			HSSFRow row = sheet.createRow((short)iterator);
518
			row.createCell((short) 0).setCellValue(w.getId());
519
			row.createCell((short) 1).setCellValue(w.getDisplayName());
520
			row.createCell((short) 2).setCellValue(w.getLocation());
521
			row.createCell((short) 3).setCellValue(w.getVendor().getId());
522
			row.createCell((short) 4).setCellValue(w.getVendor().getName());
523
			row.createCell((short) 5).setCellValue(w.getInventoryType().toString());
524
			row.createCell((short) 6).setCellValue(w.getWarehouseType().toString());
525
			row.createCell((short) 7).setCellValue(w.getLogisticsLocation().toString());
526
			row.createCell((short) 7).setCellValue(w.getStateId());
527
			iterator++;
528
		}
19253 kshitij.so 529
 
19248 kshitij.so 530
		FileOutputStream fileOut = null;
531
		try {
532
			fileOut = new FileOutputStream(file);
533
		} catch (FileNotFoundException e) {
534
			// TODO Auto-generated catch block
535
			e.printStackTrace();
536
		}
537
		try {
538
			hwb.write(fileOut);
539
		} catch (IOException e) {
540
			// TODO Auto-generated catch block
541
			e.printStackTrace();
542
		}
543
		try {
544
			fileOut.close();
545
		} catch (IOException e) {
546
			// TODO Auto-generated catch block
547
			e.printStackTrace();
548
		}
549
		byte[] buffer = new byte[(int)file.length()];
550
		InputStream input = null;
551
		try {
552
			int totalBytesRead = 0;
553
			input = new BufferedInputStream(new FileInputStream(file));
554
			while(totalBytesRead < buffer.length){
555
				int bytesRemaining = buffer.length - totalBytesRead;
556
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
557
				if (bytesRead > 0){
558
					totalBytesRead = totalBytesRead + bytesRead;
559
				}
560
			}
561
		}
562
		finally {
563
			input.close();
564
			file.delete();
565
		}
566
 
567
		response.setHeader("Content-Disposition", "attachment; filename=\"Warehouses.xls\"");
568
		response.setContentType("application/octet-stream");
569
		ServletOutputStream sos;
570
		try {
571
			sos = response.getOutputStream();
572
			sos.write(buffer);
573
			sos.flush();
574
		} catch (IOException e) {
575
			System.out.println("Unable to stream the manifest file");
576
		}
577
		setResult("Warehouse Streaming done");
578
		return "item-details-json";
19253 kshitij.so 579
 
19248 kshitij.so 580
	}
581
 
19253 kshitij.so 582
 
19248 kshitij.so 583
	public boolean checkEmptyString(Cell cell){
584
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
585
			return true;
586
		}
587
		return false;
588
	}
19253 kshitij.so 589
 
19248 kshitij.so 590
	public String getItemDetails(){
591
		return result;
592
	}
593
 
594
	public void setId(String id) {
595
		this.id = id;
596
	}
597
 
598
	public String getUserName(){
599
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
600
	}
601
 
602
	public HttpServletRequest getRequest() {
603
		logger.info("set request"+request.toString());
604
		return request;
605
	}
606
 
607
	public void setRequest(HttpServletRequest request) {
608
		this.request = request;
609
	}
610
 
611
	public HttpServletResponse getResponse() {
612
		return response;
613
	}
614
 
615
	public void setResponse(HttpServletResponse response) {
616
		this.response = response;
617
	}
618
 
619
	public HttpSession getSession() {
620
		return session;
621
	}
622
 
623
	public void setSession(HttpSession session) {
624
		this.session = session;
625
	}
626
 
627
	public ServletContext getContext() {
628
		return context;
629
	}
630
 
631
	public void setContext(ServletContext context) {
632
		this.context = context;
633
	}
634
 
19253 kshitij.so 635
 
19248 kshitij.so 636
	public void setServletRequest(HttpServletRequest req) {
637
		this.request = req;
638
		this.session = req.getSession();        
639
	}
640
 
641
	public void setServletContext(ServletContext arg0) {
642
		// TODO Auto-generated method stub
643
 
644
	}
645
 
646
	public void setServletResponse(HttpServletResponse response) {
647
		this.response = response;
648
	}
19253 kshitij.so 649
 
19248 kshitij.so 650
	public String getResult() {
651
		return result;
652
	}
653
 
654
	public void setResult(String result) {
655
		this.result = result;
656
	}
19253 kshitij.so 657
 
19248 kshitij.so 658
	public File getFile() {
659
		return file;
660
	}
661
 
662
	public void setFile(File file) {
663
		this.file = file;
664
	}
665
 
19253 kshitij.so 666
 
19248 kshitij.so 667
}