Subversion Repositories SmartDukaan

Rev

Rev 19253 | Rev 19988 | 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
			}
19588 kshitij.so 212
			long internalRank = (long) sheet.getRow(iterator).getCell(8).getNumericCellValue();
213
			b.setInternalRank(internalRank);
19248 kshitij.so 214
			b.setBrand_id(brand_id);
215
			b.setAvailable_price(t_item.getSellingPrice());
216
			b.setMrp(t_item.getMrp());
217
			b.setBrand(t_item.getBrand());
218
			b.setIdentifier(String.valueOf(t_item.getCatalogItemId()));
219
			b.setModel_name(t_item.getModelName()+" "+t_item.getModelNumber());
220
			b.setProduct_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
221
			b.setSource_product_name(t_item.getBrand()+" "+t_item.getModelName()+" "+t_item.getModelNumber());
222
			b.setQuantity(t_item.getPackQuantity());
223
			bulkItemsList.add(b);
224
		}
225
		if (sb.length()>0){
226
			setResult("Please correct error \n"+sb.toString());
227
			return "item-details-json";
228
		}
229
		in.shop2020.model.v1.dtr.DtrService.Client dc = new DtrClient("dtr_service_server_host","dtr_service_server_port").getClient(); 
230
		List<String> res = dc.addItemsInBulk(bulkItemsList);
231
		if (res.size()>0){
232
			for(String s : res){
233
				sb.append("Catalog ItemId not added "+s+"\n");
234
			}
235
			setResult(sb.toString());
236
		}else{
237
			setResult("Items added successfully");
238
		}
239
		return "item-details-json";
240
	}
19253 kshitij.so 241
 
19248 kshitij.so 242
	public String updateVirtualInventory() throws IOException, CatalogServiceException, TException, InventoryServiceException{
243
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
244
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
245
			return "authfail";
246
		}
19253 kshitij.so 247
 
19248 kshitij.so 248
		File fileToCreate = new File("/tmp/", "vendor-inventory.xls");
249
		FileUtils.copyFile(this.file, fileToCreate);
250
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
251
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
252
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 253
 
19248 kshitij.so 254
		List<BulkAddInventory> vendorInventory= new ArrayList<BulkAddInventory>();
19253 kshitij.so 255
 
19248 kshitij.so 256
		List<Long> items = new ArrayList<Long>();
257
 
258
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
259
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
260
			items.add(item_id);
261
		}
262
		Client cc = new CatalogClient().getClient();
263
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 264
 
19248 kshitij.so 265
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
266
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
267
			if (itemMap.get(itemId)==null){
268
				continue;
269
			}
270
			BulkAddInventory b = new BulkAddInventory();
271
			b.setItem_id(itemId);
272
			long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
273
			b.setWarehouse_id(warehouseId);
274
			long inventory = (long) sheet.getRow(iterator).getCell(2).getNumericCellValue();
275
			b.setInventory(inventory);
276
			vendorInventory.add(b);
277
		}
19253 kshitij.so 278
 
19248 kshitij.so 279
		InventoryClient inventoryServiceClient = new InventoryClient();
280
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
281
		inventoryClient.addInventoryInBulk(vendorInventory);
282
		setResult("Inventory updated successfully");
283
		return "item-details-json";
284
	}
19253 kshitij.so 285
 
19248 kshitij.so 286
	public String addVendorItemPricing() throws IOException, TException, CatalogServiceException{
287
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
288
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
289
			return "authfail";
290
		}
291
		File fileToCreate = new File("/tmp/", "vendor-pricing.xls");
292
		FileUtils.copyFile(this.file, fileToCreate);
293
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
294
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
295
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 296
 
19248 kshitij.so 297
		List<VendorItemPricing> vendorItemPricingList= new ArrayList<VendorItemPricing>();
298
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 299
 
19248 kshitij.so 300
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
301
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
302
			items.add(item_id);
303
		}
304
		Client cc = new CatalogClient().getClient();
305
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 306
 
19248 kshitij.so 307
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
308
			long itemId = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
309
			if (itemMap.get(itemId)==null){
310
				continue;
311
			}
312
			VendorItemPricing v = new VendorItemPricing();
313
			v.setItemId(itemId);
314
			long vendorId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
315
			v.setVendorId(vendorId);
316
			double mop = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
317
			v.setMop(mop);
318
			double dp = (double) sheet.getRow(iterator).getCell(3).getNumericCellValue();
319
			v.setDealerPrice(dp);
320
			double tp = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
321
			v.setTransferPrice(tp);
322
			double nlc = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
323
			v.setNlc(nlc);
324
			vendorItemPricingList.add(v);
19253 kshitij.so 325
		}
326
 
19248 kshitij.so 327
		InventoryClient inventoryServiceClient = new InventoryClient();
328
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
329
		List<Long> notUpdated = inventoryClient.addVendorItemPricingInBulk(vendorItemPricingList);
19253 kshitij.so 330
 
19248 kshitij.so 331
		if (notUpdated.size() > 0){
332
			StringBuilder sb = new StringBuilder();
333
			for (Long x : notUpdated){
334
				sb.append("Vendor Pricing not updated for "+x+"\n");
335
			}
336
			setResult(sb.toString());
337
		}
338
		else{
339
			setResult("Vendor item pricing updated.");
340
		}
341
		return "item-details-json";
342
	}
19253 kshitij.so 343
 
19248 kshitij.so 344
	public String updateItemPricing() throws IOException, CatalogServiceException, TException{
345
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
346
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
347
			return "authfail";
348
		}
349
		File fileToCreate = new File("/tmp/", "item-pricing.xls");
350
		FileUtils.copyFile(this.file, fileToCreate);
351
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
352
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
353
		HSSFSheet sheet = workbook.getSheetAt(0);
19253 kshitij.so 354
 
19248 kshitij.so 355
		List<ItemPricing> itemPricingList= new ArrayList<ItemPricing>();
356
		List<Long> items = new ArrayList<Long>();
19253 kshitij.so 357
 
19248 kshitij.so 358
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
359
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
360
			items.add(item_id);
361
		}
362
		Client cc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
363
		Map<Long, Item> itemMap = cc.getItems(items);
19253 kshitij.so 364
 
19248 kshitij.so 365
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
366
			ItemPricing i = new ItemPricing();
367
			long item_id = (long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
368
			i.setItem_id(item_id);
369
			if (itemMap.get(item_id)!=null){
370
				if (checkEmptyString(sheet.getRow(iterator).getCell(1))){
371
					i.setSelling_price(itemMap.get(item_id).getSellingPrice());
372
				}
373
				else{
374
					double sellingPrice = (double) sheet.getRow(iterator).getCell(1).getNumericCellValue();
375
					i.setSelling_price(sellingPrice);
376
				}
377
				if (checkEmptyString(sheet.getRow(iterator).getCell(2))){
378
					i.setMrp(itemMap.get(item_id).getMrp());
379
				}
380
				else{
381
					double mrp = (double) sheet.getRow(iterator).getCell(2).getNumericCellValue();
382
					i.setMrp(mrp);
383
				}
384
				if (checkEmptyString(sheet.getRow(iterator).getCell(3))){
385
					i.setPreferred_vendor(itemMap.get(item_id).getPreferredVendor());
386
				}
387
				else{
388
					long preferred_vendor = (long) sheet.getRow(iterator).getCell(3).getNumericCellValue();
389
					i.setPreferred_vendor(preferred_vendor);
390
				}
391
				if (checkEmptyString(sheet.getRow(iterator).getCell(4))){
392
					i.setPrivate_deal_price(0);
393
				}
394
				else{
395
					double private_deal_price = (double) sheet.getRow(iterator).getCell(4).getNumericCellValue();
396
					i.setPrivate_deal_price(private_deal_price);
397
				}
19249 kshitij.so 398
				if (checkEmptyString(sheet.getRow(iterator).getCell(5))){
399
					i.setWeight(itemMap.get(item_id).getWeight());
400
				}
401
				else{
402
					double weight = (double) sheet.getRow(iterator).getCell(5).getNumericCellValue();
19252 kshitij.so 403
					i.setWeight(weight);
19249 kshitij.so 404
				}
19253 kshitij.so 405
				itemPricingList.add(i);
19248 kshitij.so 406
			}
407
		}
408
		boolean res = cc.updateItemPricing(itemPricingList);
409
		if (res){
410
			setResult("Pricing updated successfully");
411
		}
412
		else{
413
			setResult("Unable to update pricing");
414
		}
415
		return "item-details-json";
416
	}
19253 kshitij.so 417
 
19248 kshitij.so 418
	public String downloadVendors() throws TException, IOException{
419
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
420
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
421
			return "authfail";
422
		}
423
		InventoryClient inventoryServiceClient = new InventoryClient();
424
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
425
		List<Vendor> vendors = inventoryClient.getAllVendors();
19253 kshitij.so 426
 
19248 kshitij.so 427
		File file = new File("/tmp/vendors.xls");
428
		HSSFWorkbook hwb=new HSSFWorkbook();
429
		HSSFSheet sheet =  hwb.createSheet("Vendors");
430
		HSSFRow rowhead=   sheet.createRow((short)0);
431
		rowhead.createCell((short) 0).setCellValue("VENDOR_ID");
432
		rowhead.createCell((short) 1).setCellValue("VENDOR_NAME");
19253 kshitij.so 433
 
19248 kshitij.so 434
		int iterator= 1;
435
		for (Vendor v : vendors){
436
			HSSFRow row = sheet.createRow((short)iterator);
437
			row.createCell((short) 0).setCellValue(v.getId());
438
			row.createCell((short) 1).setCellValue(v.getName());
439
			iterator++;
440
		}
19253 kshitij.so 441
 
19248 kshitij.so 442
		FileOutputStream fileOut = null;
443
		try {
444
			fileOut = new FileOutputStream(file);
445
		} catch (FileNotFoundException e) {
446
			// TODO Auto-generated catch block
447
			e.printStackTrace();
448
		}
449
		try {
450
			hwb.write(fileOut);
451
		} catch (IOException e) {
452
			// TODO Auto-generated catch block
453
			e.printStackTrace();
454
		}
455
		try {
456
			fileOut.close();
457
		} catch (IOException e) {
458
			// TODO Auto-generated catch block
459
			e.printStackTrace();
460
		}
461
		byte[] buffer = new byte[(int)file.length()];
462
		InputStream input = null;
463
		try {
464
			int totalBytesRead = 0;
465
			input = new BufferedInputStream(new FileInputStream(file));
466
			while(totalBytesRead < buffer.length){
467
				int bytesRemaining = buffer.length - totalBytesRead;
468
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
469
				if (bytesRead > 0){
470
					totalBytesRead = totalBytesRead + bytesRead;
471
				}
472
			}
473
		}
474
		finally {
475
			input.close();
476
			file.delete();
477
		}
478
 
479
		response.setHeader("Content-Disposition", "attachment; filename=\"Vendors.xls\"");
480
		response.setContentType("application/octet-stream");
481
		ServletOutputStream sos;
482
		try {
483
			sos = response.getOutputStream();
484
			sos.write(buffer);
485
			sos.flush();
486
		} catch (IOException e) {
487
			System.out.println("Unable to stream the manifest file");
488
		}
489
		setResult("Vendor Streaming done");
490
		return "item-details-json";
19253 kshitij.so 491
 
19248 kshitij.so 492
	}
19253 kshitij.so 493
 
19248 kshitij.so 494
	public String downloadWarehouses() throws TException, IOException{
495
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
496
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
497
			return "authfail";
498
		}
499
		InventoryClient inventoryServiceClient = new InventoryClient();
500
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
501
		List<Warehouse> warehouses = inventoryClient.getWarehouses(null, null, 0, 0, 0);
19253 kshitij.so 502
 
19248 kshitij.so 503
		File file = new File("/tmp/warehouses.xls");
504
		HSSFWorkbook hwb=new HSSFWorkbook();
505
		HSSFSheet sheet =  hwb.createSheet("Warehouses");
506
		HSSFRow rowhead=   sheet.createRow((short)0);
507
		rowhead.createCell((short) 0).setCellValue("warehouse_id");
508
		rowhead.createCell((short) 1).setCellValue("display_name");
509
		rowhead.createCell((short) 2).setCellValue("location");
510
		rowhead.createCell((short) 3).setCellValue("vendor_id");
511
		rowhead.createCell((short) 4).setCellValue("vendor_name");
512
		rowhead.createCell((short) 5).setCellValue("inventory_type");
513
		rowhead.createCell((short) 6).setCellValue("warehouse_type");
514
		rowhead.createCell((short) 7).setCellValue("logistics_location");
515
		rowhead.createCell((short) 8).setCellValue("state_id");
19253 kshitij.so 516
 
19248 kshitij.so 517
		int iterator= 1;
518
		for (Warehouse w : warehouses){
519
			HSSFRow row = sheet.createRow((short)iterator);
520
			row.createCell((short) 0).setCellValue(w.getId());
521
			row.createCell((short) 1).setCellValue(w.getDisplayName());
522
			row.createCell((short) 2).setCellValue(w.getLocation());
523
			row.createCell((short) 3).setCellValue(w.getVendor().getId());
524
			row.createCell((short) 4).setCellValue(w.getVendor().getName());
525
			row.createCell((short) 5).setCellValue(w.getInventoryType().toString());
526
			row.createCell((short) 6).setCellValue(w.getWarehouseType().toString());
527
			row.createCell((short) 7).setCellValue(w.getLogisticsLocation().toString());
528
			row.createCell((short) 7).setCellValue(w.getStateId());
529
			iterator++;
530
		}
19253 kshitij.so 531
 
19248 kshitij.so 532
		FileOutputStream fileOut = null;
533
		try {
534
			fileOut = new FileOutputStream(file);
535
		} catch (FileNotFoundException e) {
536
			// TODO Auto-generated catch block
537
			e.printStackTrace();
538
		}
539
		try {
540
			hwb.write(fileOut);
541
		} catch (IOException e) {
542
			// TODO Auto-generated catch block
543
			e.printStackTrace();
544
		}
545
		try {
546
			fileOut.close();
547
		} catch (IOException e) {
548
			// TODO Auto-generated catch block
549
			e.printStackTrace();
550
		}
551
		byte[] buffer = new byte[(int)file.length()];
552
		InputStream input = null;
553
		try {
554
			int totalBytesRead = 0;
555
			input = new BufferedInputStream(new FileInputStream(file));
556
			while(totalBytesRead < buffer.length){
557
				int bytesRemaining = buffer.length - totalBytesRead;
558
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
559
				if (bytesRead > 0){
560
					totalBytesRead = totalBytesRead + bytesRead;
561
				}
562
			}
563
		}
564
		finally {
565
			input.close();
566
			file.delete();
567
		}
568
 
569
		response.setHeader("Content-Disposition", "attachment; filename=\"Warehouses.xls\"");
570
		response.setContentType("application/octet-stream");
571
		ServletOutputStream sos;
572
		try {
573
			sos = response.getOutputStream();
574
			sos.write(buffer);
575
			sos.flush();
576
		} catch (IOException e) {
577
			System.out.println("Unable to stream the manifest file");
578
		}
579
		setResult("Warehouse Streaming done");
580
		return "item-details-json";
19253 kshitij.so 581
 
19248 kshitij.so 582
	}
583
 
19253 kshitij.so 584
 
19248 kshitij.so 585
	public boolean checkEmptyString(Cell cell){
586
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
587
			return true;
588
		}
589
		return false;
590
	}
19253 kshitij.so 591
 
19248 kshitij.so 592
	public String getItemDetails(){
593
		return result;
594
	}
595
 
596
	public void setId(String id) {
597
		this.id = id;
598
	}
599
 
600
	public String getUserName(){
601
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
602
	}
603
 
604
	public HttpServletRequest getRequest() {
605
		logger.info("set request"+request.toString());
606
		return request;
607
	}
608
 
609
	public void setRequest(HttpServletRequest request) {
610
		this.request = request;
611
	}
612
 
613
	public HttpServletResponse getResponse() {
614
		return response;
615
	}
616
 
617
	public void setResponse(HttpServletResponse response) {
618
		this.response = response;
619
	}
620
 
621
	public HttpSession getSession() {
622
		return session;
623
	}
624
 
625
	public void setSession(HttpSession session) {
626
		this.session = session;
627
	}
628
 
629
	public ServletContext getContext() {
630
		return context;
631
	}
632
 
633
	public void setContext(ServletContext context) {
634
		this.context = context;
635
	}
636
 
19253 kshitij.so 637
 
19248 kshitij.so 638
	public void setServletRequest(HttpServletRequest req) {
639
		this.request = req;
640
		this.session = req.getSession();        
641
	}
642
 
643
	public void setServletContext(ServletContext arg0) {
644
		// TODO Auto-generated method stub
645
 
646
	}
647
 
648
	public void setServletResponse(HttpServletResponse response) {
649
		this.response = response;
650
	}
19253 kshitij.so 651
 
19248 kshitij.so 652
	public String getResult() {
653
		return result;
654
	}
655
 
656
	public void setResult(String result) {
657
		this.result = result;
658
	}
19253 kshitij.so 659
 
19248 kshitij.so 660
	public File getFile() {
661
		return file;
662
	}
663
 
664
	public void setFile(File file) {
665
		this.file = file;
666
	}
667
 
19253 kshitij.so 668
 
19248 kshitij.so 669
}