Subversion Repositories SmartDukaan

Rev

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