Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1961 ankur.sing 1
package in.shop2020.catalog.dashboard.server;
2
 
3
import in.shop2020.catalog.dashboard.client.CatalogService;
6530 vikram.rag 4
 
1961 ankur.sing 5
import in.shop2020.catalog.dashboard.shared.Item;
4431 phani.kuma 6
import in.shop2020.catalog.dashboard.shared.ItemInventory;
3850 chandransh 7
import in.shop2020.catalog.dashboard.shared.ItemStatus;
2068 ankur.sing 8
import in.shop2020.catalog.dashboard.shared.ItemsComparator;
3558 rajveer 9
import in.shop2020.catalog.dashboard.shared.SourcePricings;
2119 ankur.sing 10
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
11
import in.shop2020.catalog.dashboard.shared.VendorPricings;
5504 phani.kuma 12
import in.shop2020.catalog.dashboard.shared.VoucherItemMapping;
2378 ankur.sing 13
import in.shop2020.config.ConfigException;
3907 chandransh 14
import in.shop2020.content.ContentService;
15
import in.shop2020.content.ContentServiceException;
5946 rajveer 16
import in.shop2020.model.v1.catalog.CatalogService.Client;
6838 vikram.rag 17
import in.shop2020.model.v1.catalog.Insurer;
5706 amit.gupta 18
import in.shop2020.model.v1.catalog.ItemType;
2105 ankur.sing 19
import in.shop2020.model.v1.catalog.status;
6096 amit.gupta 20
import in.shop2020.model.v1.inventory.InventoryService;
6813 amar.kumar 21
import in.shop2020.model.v1.inventory.ItemStockPurchaseParams;
5504 phani.kuma 22
import in.shop2020.model.v1.user.VoucherType;
3129 rajveer 23
import in.shop2020.thrift.clients.CatalogClient;
3907 chandransh 24
import in.shop2020.thrift.clients.ContentClient;
5946 rajveer 25
import in.shop2020.thrift.clients.InventoryClient;
2378 ankur.sing 26
import in.shop2020.thrift.clients.config.ConfigClient;
2119 ankur.sing 27
import in.shop2020.utils.CategoryManager;
2359 ankur.sing 28
import in.shop2020.utils.ConfigClientKeys;
6530 vikram.rag 29
import in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems;
30
import in.shop2020.catalog.dashboard.shared.ItemWarehouse;
2427 ankur.sing 31
import java.io.BufferedReader;
32
import java.io.FileReader;
2359 ankur.sing 33
import java.io.IOException;
2427 ankur.sing 34
import java.text.DateFormat;
35
import java.text.SimpleDateFormat;
1961 ankur.sing 36
import java.util.ArrayList;
4957 phani.kuma 37
import java.util.Arrays;
2105 ankur.sing 38
import java.util.Calendar;
2068 ankur.sing 39
import java.util.Collections;
2427 ankur.sing 40
import java.util.Date;
1992 ankur.sing 41
import java.util.HashMap;
5118 mandeep.dh 42
import java.util.HashSet;
1961 ankur.sing 43
import java.util.List;
1992 ankur.sing 44
import java.util.Map;
2359 ankur.sing 45
import java.util.Map.Entry;
5118 mandeep.dh 46
import java.util.Set;
1961 ankur.sing 47
 
2427 ankur.sing 48
import org.apache.log4j.Logger;
3907 chandransh 49
import org.apache.thrift.TException;
50
import org.apache.thrift.transport.TTransportException;
2359 ankur.sing 51
 
1961 ankur.sing 52
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
53
 
54
@SuppressWarnings("serial")
55
public class CatalogServiceImpl extends RemoteServiceServlet implements CatalogService {
56
 
3354 chandransh 57
    private static Logger logger = Logger.getLogger(CatalogServiceImpl.class);
58
 
3922 chandransh 59
    private static Logger pushToProdLogger = Logger.getLogger("pushToProdLogger");
60
 
2427 ankur.sing 61
    private static Date pushToProdDate;
62
 
3922 chandransh 63
    private static String logFile;
64
 
65
    private static int maxCount;
66
 
67
    static {
68
        try {
69
            logFile = ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_log_file.toString());
70
            maxCount = Integer.parseInt(ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_max_count.toString()));    
71
        } catch(ConfigException ce) {
72
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
73
            logFile = "/var/log/services/pushToProductionCount/pushToProd.log";
74
            maxCount = 5;
75
        }
76
 
77
    }
78
 
3850 chandransh 79
    @Override
80
    public int getItemCountByStatus(boolean useStatus, ItemStatus itemStatus){
81
        int count = 0;
82
        try{
83
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 84
            Client catalogClient = catalogServiceClient.getClient();
3850 chandransh 85
 
86
            status stat = status.findByValue(itemStatus.getValue());
87
            count = catalogClient.getItemCountByStatus(useStatus, stat);
88
        }catch(Exception e){
89
            logger.error("Error while getting the count of items from the catalog service", e);
90
        }
91
        return count;
92
    }
93
 
94
    @Override
6530 vikram.rag 95
	public List<Item> getInventoryOutofSyncItems(int offset, int limit){
96
        List<Item> itemList = new ArrayList<Item>();
97
 
98
        try {
99
            CatalogClient catalogServiceClient = new CatalogClient();
100
            Client catalogClient = catalogServiceClient.getClient();
101
 
102
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllIgnoredInventoryUpdateItemsList(offset, limit);
103
 
104
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 105
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
6530 vikram.rag 106
            }
107
        } catch (Exception e) {
108
            logger.error("Error while getting all items from the catalog service", e);
109
        }
110
        return itemList;
111
    }
112
 
113
    @Override
114
    public int getInventoryOutofSyncItemsCount(){
115
	int count=0;
116
    	try {
117
            InventoryClient inventoryServiceClient = new InventoryClient();
118
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
119
 
120
            count = inventoryClient.getAllIgnoredInventoryupdateItemsCount();
121
 
122
            } catch (Exception e) {
123
            logger.error("Error while getting all items from the catalog service", e);
124
        }
125
    	return count;	
126
    }    
127
 
128
    @Override
3850 chandransh 129
    public List<Item> getAllItems(int start, int limit) {
1961 ankur.sing 130
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 131
 
1961 ankur.sing 132
        try {
3129 rajveer 133
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 134
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 135
 
3850 chandransh 136
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsInRange(start, limit);
2359 ankur.sing 137
 
2119 ankur.sing 138
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 139
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
2119 ankur.sing 140
            }
141
        } catch (Exception e) {
3354 chandransh 142
            logger.error("Error while getting all items from the catalog service", e);
2119 ankur.sing 143
        }
144
        return itemList;
145
    }
2208 ankur.sing 146
 
3850 chandransh 147
    public List<Item> getAllActiveItems(int start, int limit){
148
        return getItemsByStatus(status.ACTIVE, start, limit);
2208 ankur.sing 149
    }
2359 ankur.sing 150
 
151
    @Override
3850 chandransh 152
    public List<Item> getAllPhasedOutItems(int start, int limit){
153
        return getItemsByStatus(status.PHASED_OUT, start, limit);
2359 ankur.sing 154
    }
155
 
156
    @Override
3850 chandransh 157
    public List<Item> getAllPausedItems(int start, int limit){
158
        return getItemsByStatus(status.PAUSED, start, limit);
2208 ankur.sing 159
    }
2359 ankur.sing 160
 
161
    @Override
3850 chandransh 162
    public List<Item> getAllInProcessItems(int start, int limit) {
163
        return getItemsByStatus(status.IN_PROCESS, start, limit);
2359 ankur.sing 164
    }
165
 
166
    @Override
3850 chandransh 167
    public List<Item> getAllContentCompleteItems(int start, int limit) {
168
        return getItemsByStatus(status.CONTENT_COMPLETE, start, limit);
2359 ankur.sing 169
    }
170
 
171
    public List<Item> getBestDeals(){
2119 ankur.sing 172
        List<Item> itemList = new ArrayList<Item>();
173
        try {
3129 rajveer 174
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 175
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 176
 
177
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestDeals();
1992 ankur.sing 178
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 179
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
1992 ankur.sing 180
            }
2359 ankur.sing 181
        } catch(Exception e){
3354 chandransh 182
            logger.error("Error while getting the best deals from the catalog service", e);
1961 ankur.sing 183
        }
2562 chandransh 184
        //Collections.sort(itemList, new ItemsComparator());
1961 ankur.sing 185
        return itemList;
186
    }
187
 
2359 ankur.sing 188
    @Override
189
    public List<Item> getRiskyItems() {
1961 ankur.sing 190
        List<Item> itemList = new ArrayList<Item>();
191
        try {
3129 rajveer 192
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 193
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 194
 
195
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByRiskyFlag();
1992 ankur.sing 196
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 197
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
1992 ankur.sing 198
            }
1961 ankur.sing 199
        } catch(Exception e){
3354 chandransh 200
            logger.error("Error while getting the risky items from the catalog service", e);
1961 ankur.sing 201
        }
2068 ankur.sing 202
        Collections.sort(itemList, new ItemsComparator());
1961 ankur.sing 203
        return itemList;
204
    }
2359 ankur.sing 205
 
1961 ankur.sing 206
    public List<Item> getBestSellers(){
207
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 208
 
1961 ankur.sing 209
        try {
3129 rajveer 210
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 211
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 212
 
1961 ankur.sing 213
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();
1992 ankur.sing 214
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 215
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
1992 ankur.sing 216
            }
1961 ankur.sing 217
        } catch(Exception e){
3354 chandransh 218
            logger.error("Error while getting the best sellers from the catalog service", e);
1961 ankur.sing 219
        }
220
        return itemList;        
221
    }
6530 vikram.rag 222
 
223
    @Override
224
    public boolean addtoIgnoredInventoryUpdateItemsIdsWarehouseIds(long itemId,long warehouseId){
225
    	try {
226
				InventoryClient inventoryServiceClient = new InventoryClient();
227
				in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
228
				return inventoryClient.insertItemtoIgnoreInventoryUpdatelist(itemId, warehouseId);
229
			} catch (Exception e) {
230
            logger.error("Error while inserting item to ignore inventory update list", e);
231
            }
232
			return false;
233
    }
234
 
235
    @Override
236
    public boolean deleteFromIgnoredInventoryUpdateItemsIdsWarehouseIds(long itemId,long warehouseId){
237
    	try {
238
    			InventoryClient inventoryServiceClient = new InventoryClient();
239
    			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
240
    			return inventoryClient.deleteItemFromIgnoredInventoryUpdateList(itemId,warehouseId);
241
 
242
		} catch (Exception e) {
6532 amit.gupta 243
			logger.error("Error while inserting item to ignore inventory update list", e);
6530 vikram.rag 244
        }
245
		return false;
246
}
2359 ankur.sing 247
 
6530 vikram.rag 248
 
249
 
3872 chandransh 250
    @Override
1961 ankur.sing 251
    public List<Item> getLatestArrivals(){
252
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 253
 
1961 ankur.sing 254
        try {
3129 rajveer 255
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 256
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 257
 
1961 ankur.sing 258
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getLatestArrivals();
1992 ankur.sing 259
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5946 rajveer 260
                //List<in.shop2020.model.v1.inventory.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
6813 amar.kumar 261
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
1992 ankur.sing 262
            }
1961 ankur.sing 263
        } catch(Exception e){
3354 chandransh 264
            logger.error("Error while getting the latest arrivals from the catalog service", e);
1961 ankur.sing 265
        }
266
        return itemList;
267
    }
2359 ankur.sing 268
 
3872 chandransh 269
    @Override
270
    public List<Item> searchItems(int start, int limit, List<String> searchTerms) {
271
        List<Item> itemList = new ArrayList<Item>();
272
 
273
        try {
274
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 275
            Client catalogClient = catalogServiceClient.getClient();
3872 chandransh 276
 
277
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.searchItemsInRange(searchTerms, start, limit);
278
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 279
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
3872 chandransh 280
            }
281
        } catch(Exception e){
282
            logger.error("Error while getting the search results from the catalog service", e);
283
        }
284
        return itemList;
285
    }
286
 
287
    @Override
288
    public int getSearchResultCount(List<String> searchTerms){
289
        int count = 0;
290
        try {
291
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 292
            Client catalogClient = catalogServiceClient.getClient();
3872 chandransh 293
 
294
            count = catalogClient.getSearchResultCount(searchTerms);
295
        } catch(Exception e){
296
            logger.error("Error while getting the search results from the catalog service", e);
297
        }
298
 
299
        return count;
300
    }
301
 
1961 ankur.sing 302
    public Item getItem(long itemId){
303
        try{
3129 rajveer 304
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 305
            Client catalogClient = catalogServiceClient.getClient();
306
            InventoryClient inventoryServiceClient = new InventoryClient();
307
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
1961 ankur.sing 308
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);
5427 amit.gupta 309
            Long catalogItemId = thriftItem.getCatalogItemId();
310
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByCatalogId(catalogItemId);
311
            List <Long> sameItemsWithDifferentColors = new ArrayList<Long>();
312
            if(thriftItems.size()>1){
313
	            for(in.shop2020.model.v1.catalog.Item tItem : thriftItems ){
314
	            	Long id = tItem.getId();
315
		        	if(!id.equals(itemId)){
316
		        		sameItemsWithDifferentColors.add(id); 
317
		        	}
318
	            }
319
            }
5946 rajveer 320
            List<in.shop2020.model.v1.inventory.VendorItemPricing> vip = inventoryClient.getAllItemPricing(thriftItem.getId());
321
            List<in.shop2020.model.v1.inventory.VendorItemMapping> vim = inventoryClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 322
            List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
4423 phani.kuma 323
            List<in.shop2020.model.v1.catalog.Item> sit = catalogClient.getAllSimilarItems(thriftItem.getId());
5946 rajveer 324
            in.shop2020.model.v1.inventory.ItemInventory itemInventory = inventoryClient.getItemInventoryByItemId(thriftItem.getId());
6813 amar.kumar 325
            ItemStockPurchaseParams stockPurchaseParams = inventoryClient.getItemStockPurchaseParams(thriftItem.getId());
5504 phani.kuma 326
            List<in.shop2020.model.v1.catalog.VoucherItemMapping> tvouchers = catalogClient.getAllItemVouchers(thriftItem.getId());
6813 amar.kumar 327
            Item it = getItemFromThriftItem(thriftItem, vip, vim, sip, sit, itemInventory, tvouchers, stockPurchaseParams);
5427 amit.gupta 328
            it.setSameItemsWithDifferentColors(sameItemsWithDifferentColors);
329
            return it;
1961 ankur.sing 330
        }catch(Exception e){
3354 chandransh 331
            logger.error("Error while getting the item from the catalog service", e);
1961 ankur.sing 332
        }
333
        return null;
334
    }
2359 ankur.sing 335
 
1961 ankur.sing 336
    @Override
1992 ankur.sing 337
    public boolean updateItem(Item item) {
3354 chandransh 338
        logger.info("Updating item with Id: " + item.getId());
1961 ankur.sing 339
        try{
3129 rajveer 340
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 341
            Client catalogClient = catalogServiceClient.getClient();
342
            InventoryClient inventoryServiceClient = new InventoryClient();
343
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
344
 
1992 ankur.sing 345
            in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());
2126 ankur.sing 346
            setThriftItemParams(tItem, item);
2359 ankur.sing 347
 
2105 ankur.sing 348
            long rItemId;
349
            if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {
3354 chandransh 350
                logger.error("Error updating item, returned Item Id: " + rItemId);
2105 ankur.sing 351
                return false;
352
            }
3354 chandransh 353
            logger.info("Successfully updated item with id: " + item.getId());
2359 ankur.sing 354
 
355
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
2119 ankur.sing 356
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
5946 rajveer 357
                in.shop2020.model.v1.inventory.VendorItemMapping tVendorMapping;
2359 ankur.sing 358
 
359
                for(Entry<String, VendorItemMapping> e : vendorMappings.entrySet()) {
5946 rajveer 360
                    tVendorMapping = new in.shop2020.model.v1.inventory.VendorItemMapping();
2359 ankur.sing 361
                    VendorItemMapping v = e.getValue();
2119 ankur.sing 362
                    tVendorMapping.setVendorId(v.getVendorId());
363
                    tVendorMapping.setItemKey(v.getItemKey());
364
                    tVendorMapping.setItemId(item.getId());
5946 rajveer 365
                    inventoryClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
3354 chandransh 366
                    logger.info("Updates VendorItemMapping: " + tVendorMapping.toString());
2119 ankur.sing 367
                }
368
            }
2359 ankur.sing 369
 
6813 amar.kumar 370
            if(item.getMinStockLevel()!=0 || item.getNumOfDaysStock()!=0) {
371
            	inventoryClient.updateItemStockPurchaseParams(item.getId(), item.getNumOfDaysStock(),
372
            			item.getMinStockLevel());
373
            }
374
 
2119 ankur.sing 375
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
376
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
5946 rajveer 377
                in.shop2020.model.v1.inventory.VendorItemPricing tVendorPricing;
2119 ankur.sing 378
                for(VendorPricings v : vendorPricings.values()) {
5946 rajveer 379
                    tVendorPricing = new in.shop2020.model.v1.inventory.VendorItemPricing();
2105 ankur.sing 380
                    tVendorPricing.setVendorId(v.getVendorId());
381
                    tVendorPricing.setItemId(item.getId());
382
                    tVendorPricing.setMop(v.getMop());
383
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
384
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
6759 amar.kumar 385
                    tVendorPricing.setNlc(v.getNlc());
5946 rajveer 386
                    inventoryClient.addVendorItemPricing(tVendorPricing);
3354 chandransh 387
                    logger.info("Updated VendorItemPricing: " + tVendorPricing.toString());
2105 ankur.sing 388
                }
389
            }
3558 rajveer 390
 
391
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
392
            if(sourcePricings != null && !sourcePricings.isEmpty()) {
393
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricings;
394
                for(SourcePricings s : sourcePricings.values()) {
395
                	tSourcePricings = new in.shop2020.model.v1.catalog.SourceItemPricing();
396
                	tSourcePricings.setSourceId(s.getSourceId());
397
                    tSourcePricings.setItemId(item.getId());
398
                    tSourcePricings.setMrp(s.getMrp());
399
                    tSourcePricings.setSellingPrice(s.getSellingPrice());
400
                    catalogClient.addSourceItemPricing(tSourcePricings);
401
                    logger.info("Updated SourceItemPricing: " + tSourcePricings.toString());
402
                }
403
            }
404
 
2105 ankur.sing 405
        }catch(Exception e){
3354 chandransh 406
            logger.error("Error while updating item: ", e);
2427 ankur.sing 407
            return false;
2105 ankur.sing 408
        }
409
        return true;
1961 ankur.sing 410
    }
2359 ankur.sing 411
 
6530 vikram.rag 412
    @Override
413
    public List<Long> getMonitoredWarehouseIdsForVendors(List<Long> vendorIds){
414
    	List<Long> monitoredWarehouseids=null;
415
    	 try {
416
			InventoryClient inventoryServiceClient = new InventoryClient();
417
			 in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
418
			 monitoredWarehouseids = inventoryClient.getMonitoredWarehouseForVendors(vendorIds);
419
		} catch (Exception e) {
420
			logger.error("Error while getting Monitored warehouse for vendor: ", e);
421
		}
422
		return monitoredWarehouseids;
423
 
424
    }
425
 
426
    @Override
427
    public List<ItemWarehouse> getignoredInventoryUpdateItemsIdsWarehouseIds(){
428
		List<ItemWarehouse> ignoredItemIdwarehouseIdList = new ArrayList<ItemWarehouse>();
429
    	try {
2359 ankur.sing 430
 
6530 vikram.rag 431
			InventoryClient inventoryServiceClient = new InventoryClient();
432
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
433
			List<IgnoredInventoryUpdateItems> ignoredInventoryUpdateItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
434
			for(IgnoredInventoryUpdateItems ignoredInventoryUpdateItem:ignoredInventoryUpdateItems){
435
				ignoredItemIdwarehouseIdList.add(new ItemWarehouse(ignoredInventoryUpdateItem.getItemId(),ignoredInventoryUpdateItem.getWarehouseId()));
436
			}
437
 
438
 
439
		} catch (Exception e) {
440
			logger.error("Error while getting Monitored warehouseid and itemid : ", e);
441
		}
442
		return ignoredItemIdwarehouseIdList;
443
 
444
    }
2066 ankur.sing 445
    @Override
446
    public Map<Long, String> getAllVendors() {
447
        Map<Long, String> vendorMap = new HashMap<Long, String>();
448
        try {
5946 rajveer 449
            InventoryClient inventoryServiceClient = new InventoryClient();
450
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
451
 
452
            List<in.shop2020.model.v1.inventory.Vendor> vendors = inventoryClient.getAllVendors();
2359 ankur.sing 453
 
5946 rajveer 454
            for(in.shop2020.model.v1.inventory.Vendor v : vendors) {
2066 ankur.sing 455
                vendorMap.put(v.getId(), v.getName());
456
            }
457
        } catch (Exception e) {
3354 chandransh 458
            logger.error("Error while getting all the vendors: ", e);
2066 ankur.sing 459
        }
460
        return vendorMap;
461
    }
462
 
463
    @Override
3558 rajveer 464
    public Map<Long, String> getAllSources() {
465
        Map<Long, String> sourceMap = new HashMap<Long, String>();
466
        try {
467
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 468
            Client catalogClient = catalogServiceClient.getClient();
3558 rajveer 469
 
470
            List<in.shop2020.model.v1.catalog.Source> sources = catalogClient.getAllSources();
471
 
472
            for(in.shop2020.model.v1.catalog.Source s : sources) {
473
            	sourceMap.put(s.getId(), s.getName());
474
            }
475
        } catch (Exception e) {
476
            logger.error("Error while getting all the vendors: ", e);
477
        }
478
        return sourceMap;
479
    }
480
 
481
    @Override
5118 mandeep.dh 482
    public Map<Long, String> getShippingWarehouses() {
483
        Map<Long, String> warehouseMap = new HashMap<Long, String>();
484
        Set<Long> shippingLocations = new HashSet<Long>();
485
        try {
5946 rajveer 486
            InventoryClient inventoryServiceClient = new InventoryClient();
487
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
488
 
489
            List<in.shop2020.model.v1.inventory.Warehouse> warehouses = inventoryClient.getAllWarehouses(true);
5118 mandeep.dh 490
 
5946 rajveer 491
            for(in.shop2020.model.v1.inventory.Warehouse warehouse : warehouses) {
5118 mandeep.dh 492
                shippingLocations.add(warehouse.getShippingWarehouseId());
493
            }
494
 
5946 rajveer 495
            for(in.shop2020.model.v1.inventory.Warehouse warehouse : warehouses) {
5118 mandeep.dh 496
                if (shippingLocations.contains(warehouse.getId())) {
497
                    warehouseMap.put(warehouse.getId(), warehouse.getDisplayName());
498
                }                
499
            }
500
        } catch (Exception e) {
501
            logger.error("Error while getting all the warehouses:", e );
502
        }
503
        return warehouseMap;
504
    }
505
 
506
    @Override
2066 ankur.sing 507
    public Map<Long, String> getAllWarehouses() {
508
        Map<Long, String> warehouseMap = new HashMap<Long, String>();
509
        try {
5946 rajveer 510
        	InventoryClient inventoryServiceClient = new InventoryClient();
511
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
2359 ankur.sing 512
 
5946 rajveer 513
            List<in.shop2020.model.v1.inventory.Warehouse> warehouses = inventoryClient.getAllWarehouses(true);
2359 ankur.sing 514
 
5946 rajveer 515
            for(in.shop2020.model.v1.inventory.Warehouse w : warehouses) {
2066 ankur.sing 516
                warehouseMap.put(w.getId(), w.getDisplayName());
517
            }
518
        } catch (Exception e) {
3354 chandransh 519
            logger.error("Error while getting all the warehouses:", e );
2066 ankur.sing 520
        }
521
        return warehouseMap;
522
    }
2105 ankur.sing 523
 
524
    @Override
525
    public long addItem(Item item) {
526
        long itemId = 0;
527
        try {
3129 rajveer 528
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 529
            Client catalogClient = catalogServiceClient.getClient();
530
            InventoryClient inventoryServiceClient = new InventoryClient();
531
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
2126 ankur.sing 532
 
2105 ankur.sing 533
            in.shop2020.model.v1.catalog.Item tItem = new in.shop2020.model.v1.catalog.Item();
2126 ankur.sing 534
            setThriftItemParams(tItem, item);
2105 ankur.sing 535
            itemId = catalogClient.addItem(tItem);
536
 
2119 ankur.sing 537
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
538
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
5946 rajveer 539
                in.shop2020.model.v1.inventory.VendorItemPricing tVendorPricing;
2119 ankur.sing 540
                for(VendorPricings v : vendorPricings.values()) {
5946 rajveer 541
                    tVendorPricing = new in.shop2020.model.v1.inventory.VendorItemPricing();
2119 ankur.sing 542
                    tVendorPricing.setVendorId(v.getVendorId());
543
                    tVendorPricing.setItemId(itemId);
544
                    tVendorPricing.setMop(v.getMop());
545
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
6759 amar.kumar 546
                    tVendorPricing.setNlc(v.getNlc());
2119 ankur.sing 547
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
5946 rajveer 548
                    inventoryClient.addVendorItemPricing(tVendorPricing);
2119 ankur.sing 549
                }
550
            }
2359 ankur.sing 551
 
552
            Map<String, VendorItemMapping> vendorKeysMap = item.getVendorKeysMap();
2119 ankur.sing 553
            if(vendorKeysMap != null && !vendorKeysMap.isEmpty()) {
5946 rajveer 554
                in.shop2020.model.v1.inventory.VendorItemMapping tVendorMapping;
2359 ankur.sing 555
                for(Entry<String, VendorItemMapping> e : vendorKeysMap.entrySet()) {
5946 rajveer 556
                    tVendorMapping = new in.shop2020.model.v1.inventory.VendorItemMapping();
2359 ankur.sing 557
                    VendorItemMapping v = e.getValue();
2105 ankur.sing 558
                    tVendorMapping.setVendorId(v.getVendorId());
559
                    tVendorMapping.setItemKey(v.getItemKey());
560
                    tVendorMapping.setItemId(itemId);
5946 rajveer 561
                    inventoryClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
2105 ankur.sing 562
                }
563
            }
6813 amar.kumar 564
 
565
        	inventoryClient.updateItemStockPurchaseParams(itemId, item.getNumOfDaysStock(),
566
        			item.getMinStockLevel());
2105 ankur.sing 567
        } catch (Exception e) {
3354 chandransh 568
            logger.error("Error while adding an item: ", e);
2105 ankur.sing 569
        }
570
        return itemId;
571
    }
2119 ankur.sing 572
 
573
    @Override
4725 phani.kuma 574
    public long checkSimilarItem(String brand, String modelNumber, String modelName, String color) {
2359 ankur.sing 575
 
2119 ankur.sing 576
        try {
3129 rajveer 577
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 578
            Client catalogClient = catalogServiceClient.getClient();
4725 phani.kuma 579
            return catalogClient.checkSimilarItem(brand, modelNumber, modelName, color);
2119 ankur.sing 580
        } catch (Exception e) {
3354 chandransh 581
            logger.error("Error while checking for a similar item: ", e);
2119 ankur.sing 582
        }
583
        return 0;
584
    }
2359 ankur.sing 585
 
2126 ankur.sing 586
    private void setThriftItemParams(in.shop2020.model.v1.catalog.Item tItem, Item item) {
587
        tItem.setId(tItem.getId());
588
        tItem.setProductGroup(item.getProductGroup());
589
        tItem.setBrand(item.getBrand());
590
        tItem.setModelName(item.getModelName());
591
        tItem.setModelNumber(item.getModelNumber());
592
        tItem.setColor(item.getColor());
6241 amit.gupta 593
        tItem.setShowSellingPrice(item.isShowSellingPrice());
2359 ankur.sing 594
        tItem.setStatus_description(item.getItemStatusDesc());
2126 ankur.sing 595
        tItem.setComments(item.getComments());
2359 ankur.sing 596
 
2489 ankur.sing 597
        if(item.getMrp() != null) {
2126 ankur.sing 598
            tItem.setMrp(item.getMrp());
599
        }
3354 chandransh 600
 
5586 phani.kuma 601
        if(item.getCatalogItemId() != null) {
602
            tItem.setCatalogItemId(item.getCatalogItemId());
603
        }
604
 
2489 ankur.sing 605
        if(item.getSellingPrice() != null) {
2126 ankur.sing 606
            tItem.setSellingPrice(item.getSellingPrice());
607
        }
3354 chandransh 608
 
2489 ankur.sing 609
        if(item.getWeight() != null) {
2126 ankur.sing 610
            tItem.setWeight(item.getWeight());
611
        }
3354 chandransh 612
 
3359 chandransh 613
        if(item.getExpectedDelay() != null) {
614
            tItem.setExpectedDelay(item.getExpectedDelay());
2489 ankur.sing 615
        }
3354 chandransh 616
 
4413 anupam.sin 617
        tItem.setIsWarehousePreferenceSticky(item.isWarehouseStickiness());
618
 
4506 phani.kuma 619
        if(item.getPreferredVendor() != null) {
620
            tItem.setPreferredVendor(item.getPreferredVendor());
621
        }
6838 vikram.rag 622
        tItem.setPreferredInsurer(item.getPreferredInsurer());
4506 phani.kuma 623
 
2126 ankur.sing 624
        tItem.setBestDealText(item.getBestDealsText());
6777 vikram.rag 625
        tItem.setBestDealsDetailsText(item.getBestDealsDetailsText());
626
        tItem.setBestDealsDetailsLink(item.getBestDealsDetailsLink());
2489 ankur.sing 627
        if(item.getBestDealsValue() != null) {
2126 ankur.sing 628
            tItem.setBestDealValue(item.getBestDealsValue());
629
        }
3354 chandransh 630
 
2489 ankur.sing 631
        if(item.getBestSellingRank() != null) {
2126 ankur.sing 632
            tItem.setBestSellingRank(item.getBestSellingRank());
633
        }
3354 chandransh 634
 
2126 ankur.sing 635
        tItem.setDefaultForEntity(item.isDefaultForEntity());
2252 ankur.sing 636
        tItem.setRisky(item.isRisky());
5384 phani.kuma 637
        tItem.setHasItemNo(item.isHasItemNo());
5460 phani.kuma 638
        tItem.setClearance(item.isClearance());
5384 phani.kuma 639
 
640
        if(item.isItemType()) {
641
        	tItem.setType(ItemType.SERIALIZED);
642
        }
643
        else {
644
        	tItem.setType(ItemType.NON_SERIALIZED);
645
        }
646
 
2489 ankur.sing 647
        if(item.getStartDate() != null) {
648
            tItem.setStartDate(item.getStartDate());
649
        }
3354 chandransh 650
 
5217 amit.gupta 651
        if(item.getExpectedArrivalDate() != null) {
652
        	tItem.setExpectedArrivalDate(item.getExpectedArrivalDate());
653
        } else {
654
        	tItem.unsetComingSoonStartDate();
655
        	tItem.unsetExpectedArrivalDate();
656
        	if(status.COMING_SOON.equals(tItem.getItemStatus())){
6277 amit.gupta 657
        		tItem.setItemStatus(status.CONTENT_COMPLETE);
658
        		tItem.setStatus_description("Content generation completed");
5217 amit.gupta 659
        	}
660
        }
661
 
662
        if(item.getComingSoonStartDate() != null) {
663
        	tItem.setComingSoonStartDate(item.getComingSoonStartDate());
664
        } else {
665
        	tItem.unsetComingSoonStartDate();
666
        }
667
 
2489 ankur.sing 668
        if(item.getRetireDate() != null) {
669
            tItem.setRetireDate(item.getRetireDate());
670
        }
3354 chandransh 671
 
2126 ankur.sing 672
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
673
    }
674
 
675
    @Override
676
    public void pauseItem(long itemId) {
677
        try {
3129 rajveer 678
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 679
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 680
 
2126 ankur.sing 681
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
682
        } catch (Exception e) {
3354 chandransh 683
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 684
        }
2359 ankur.sing 685
 
2126 ankur.sing 686
    }
687
 
688
    @Override
689
    public void markInProcess(long itemId) {
690
        try {
3129 rajveer 691
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 692
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 693
 
2126 ankur.sing 694
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
695
        } catch (Exception e) {
3354 chandransh 696
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 697
        }
698
    }
2359 ankur.sing 699
 
2126 ankur.sing 700
    @Override
701
    public void phaseoutItem(long itemId) {
702
        try {
3129 rajveer 703
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 704
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 705
 
2126 ankur.sing 706
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
707
        } catch (Exception e) {
3354 chandransh 708
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 709
        }
710
    }
2359 ankur.sing 711
 
2126 ankur.sing 712
    @Override
713
    public void activateItem(long itemId) {
714
        try {
3129 rajveer 715
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 716
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 717
 
2126 ankur.sing 718
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
719
        } catch (Exception e) {
3354 chandransh 720
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 721
        }
722
    }
2359 ankur.sing 723
 
724
    @Override
725
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
726
        try {
4354 mandeep.dh 727
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 728
            // Initialize client for staging server
3129 rajveer 729
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 730
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 731
 
732
            // Initialize client for production server
3165 chandransh 733
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 734
                    ConfigClientKeys.catalog_service_server_port.toString());
5946 rajveer 735
            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
2359 ankur.sing 736
            catalogClient.changeItemRiskyFlag(itemId, risky);
737
 
738
            try{
739
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
740
            }catch(Exception e){
3354 chandransh 741
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 742
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
743
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
744
                return false;
745
            }
746
        } catch (Exception e) {
3354 chandransh 747
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 748
            return false;
749
        }
750
        return true;
751
    }
752
 
2427 ankur.sing 753
 
754
    /**
4762 phani.kuma 755
     * Retuns list of Items filtered by category (column is product_group in catalog.item table)
2427 ankur.sing 756
     * This list is used to generate master sheet (excel sheet)
757
     * @param category
4957 phani.kuma 758
     * @param brand
2427 ankur.sing 759
     * @return
760
     */
4957 phani.kuma 761
    public List<Item> getItemsForMasterSheet(String category, String brand) {
2359 ankur.sing 762
        List<Item> itemList = new ArrayList<Item>();
763
        try {
3129 rajveer 764
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 765
            Client catalogClient = catalogServiceClient.getClient();
766
            InventoryClient inventoryServiceClient = new InventoryClient();
767
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
2359 ankur.sing 768
 
4957 phani.kuma 769
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsForMasterSheet(category, brand);
2359 ankur.sing 770
 
771
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5946 rajveer 772
                List<in.shop2020.model.v1.inventory.VendorItemPricing> vip = inventoryClient.getAllItemPricing(thriftItem.getId());
773
                List<in.shop2020.model.v1.inventory.VendorItemMapping> vim = inventoryClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 774
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
6813 amar.kumar 775
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null, null, null, null));
2359 ankur.sing 776
            }
777
        } catch (Exception e) {
4762 phani.kuma 778
            logger.error("Error while getting items by category: " + category, e);
2359 ankur.sing 779
        }
780
        Collections.sort(itemList, new ItemsComparator());
781
        return itemList;
782
    }
783
 
784
    @Override
2427 ankur.sing 785
    public String updateItemOnProduction(Item item) {
5706 amit.gupta 786
    	if(!canPushToProduction()) {
787
    		// If we can't push to production, then return that message to user
788
    		// else move forward with update.
789
    		return "Maximum push to production count reached for today";
790
    	}
791
    	List<Long> itemIds = item.getSameItemsWithDifferentColors();
792
    	List<Item> items = new ArrayList<Item>();
793
    	items.add(item);
794
    	for(Long itemId : itemIds) {
795
    		try {
796
    			CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 797
    			Client catalogClient = catalogServiceClient.getClient();
798
    			InventoryClient inventoryServiceClient = new InventoryClient();
799
                in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
800
 
5706 amit.gupta 801
    			in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);
5946 rajveer 802
				List<in.shop2020.model.v1.inventory.VendorItemPricing> vip = inventoryClient.getAllItemPricing(thriftItem.getId());
803
				List<in.shop2020.model.v1.inventory.VendorItemMapping> vim = inventoryClient.getVendorItemMappings(thriftItem.getId());
5706 amit.gupta 804
				List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
6813 amar.kumar 805
				items.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null, null, null, null));
5706 amit.gupta 806
    		} catch (Exception e) {
807
    			logger.error("Could not fetch item for : " + itemId, e);
808
    			return "Could not push to production. No item got pushed.";
809
    		}
810
    	}
811
    	for (Item it : items) {
812
	        logger.info("Update item on production call, Item Id: " + it.getId());
813
 
814
	        try{
815
	            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
816
	                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
817
	                    ConfigClientKeys.catalog_service_server_port.toString());
818
 
5946 rajveer 819
	            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
5706 amit.gupta 820
 
821
	            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(it.getId());
822
 
5946 rajveer 823
	            InventoryClient inventoryServiceClient = new InventoryClient();
824
	            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
825
 
5706 amit.gupta 826
	            if(it.getMrp() != null) {
827
	                tItem.setMrp(it.getMrp());
828
	            }
829
 
830
	            if(it.getSellingPrice() != null) {
831
	                tItem.setSellingPrice(it.getSellingPrice());
832
	            }
833
 
6826 amit.gupta 834
	            if (it.getItemStatusValue()  == ItemStatus.ACTIVE.getValue() || it.getItemStatusValue()  == ItemStatus.PAUSED.getValue()) {
835
	            	tItem.setItemStatus(status.findByValue(it.getItemStatusValue()));
836
	            	tItem.setStartDate(it.getStartDate());
837
	            }
5706 amit.gupta 838
	            long rItemId;
839
	            if((rItemId = catalogClient_Prod.updateItem(tItem)) != it.getId()) {
840
	                logger.error("Error updating item on production, returned Item Id: " + rItemId);
841
	                return "Error updating item prices on production";
842
	            }
843
	            logger.info("Successfully updated item: " + it.getId());
844
 
845
	            StringBuilder sb = new StringBuilder();
846
 
847
	            Map<Long, VendorPricings> vendorPricings = it.getVendorPricesMap();
848
	            if(vendorPricings != null && !vendorPricings.isEmpty()) {
5946 rajveer 849
	                in.shop2020.model.v1.inventory.VendorItemPricing tVendorPricing;
5706 amit.gupta 850
	                for(VendorPricings v : vendorPricings.values()) {
5946 rajveer 851
	                    tVendorPricing = new in.shop2020.model.v1.inventory.VendorItemPricing();
5706 amit.gupta 852
	                    tVendorPricing.setVendorId(v.getVendorId());
853
	                    tVendorPricing.setItemId(it.getId());
854
	                    tVendorPricing.setMop(v.getMop());
855
	                    tVendorPricing.setTransferPrice(v.getTransferPrice());
6759 amar.kumar 856
	                    tVendorPricing.setNlc(v.getNlc());
5706 amit.gupta 857
	                    tVendorPricing.setDealerPrice(v.getDealerPrice());
5946 rajveer 858
	                    inventoryClient.addVendorItemPricing(tVendorPricing);
5706 amit.gupta 859
	                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
6759 amar.kumar 860
	                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",NLC= " +v.getNlc() + ",DP=" + v.getDealerPrice() + "], ");
5706 amit.gupta 861
	                }
862
	            }
863
 
864
	            Map<String, VendorItemMapping> vendorMappings = it.getVendorKeysMap();
865
	            if(vendorMappings != null && !vendorMappings.isEmpty()) {
5946 rajveer 866
	                in.shop2020.model.v1.inventory.VendorItemMapping tVendorMapping;
5706 amit.gupta 867
	                for(VendorItemMapping vendorItemMapping : vendorMappings.values()) {
5946 rajveer 868
	                    tVendorMapping = new in.shop2020.model.v1.inventory.VendorItemMapping();
5706 amit.gupta 869
	                    tVendorMapping.setVendorId(vendorItemMapping.getVendorId());
870
	                    tVendorMapping.setItemId(it.getId());
871
	                    tVendorMapping.setItemKey(vendorItemMapping.getItemKey());
5946 rajveer 872
	                    inventoryClient.addVendorItemMapping(vendorItemMapping.getItemKey(), tVendorMapping);
5706 amit.gupta 873
	                    logger.info("VendorItemMapping updated. " + tVendorMapping.toString());
874
	                    sb.append("[VId:" + vendorItemMapping.getVendorId() + ",ItemKey=" + vendorItemMapping.getItemKey() + "], ");
875
	                }
876
	            }
877
 
878
	            Map<Long, SourcePricings> sourcePricings = it.getSourcePricesMap();
879
	            if(sourcePricings != null && !sourcePricings.isEmpty()){
880
	                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
881
	                for(SourcePricings s : sourcePricings.values()){
882
	                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
883
	                    tSourcePricing.setSourceId(s.getSourceId());
884
	                    tSourcePricing.setItemId(it.getId());
885
	                    tSourcePricing.setMrp(s.getMrp());
886
	                    tSourcePricing.setSellingPrice(s.getSellingPrice());
887
	                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
888
	                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
889
	                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
890
	                }
891
	            }
892
 
893
	            pushToProdLogger.info("Id=" + it.getId() + ",MRP=" + it.getMrp() + ",SP=" + " " + it.getSellingPrice() + sb.toString());
894
	        } catch (Exception e) {
895
	            logger.error("Error while updating prices on production", e);
896
	            return "Error while updating all prices on production. Some of the prices may not have been  updated";
897
	        }
898
    	}
2427 ankur.sing 899
 
3907 chandransh 900
        try {
901
            ContentClient contentServiceClient = new ContentClient();
902
            ContentService.Client contentClient = contentServiceClient.getClient();
903
 
5706 amit.gupta 904
            contentClient.pushContentToProduction(items.get(0).getCatalogItemId());
3907 chandransh 905
        } catch (TTransportException e) {
906
            logger.error("Error while establishing connection to the content server", e);
907
            return "Error generating content. Prices updated successfully.";
908
        } catch (TException e) {
909
            logger.error("Error while pushing content to production", e);
910
            return "Error generating content. Prices updated successfully.";
911
        } catch (ContentServiceException e) {
912
            logger.error("Error while pushing content to production", e);
913
            return "Error generating content. Prices updated successfully.";
914
        }
915
 
2427 ankur.sing 916
        return "Prices updated and content generated successfully";
2359 ankur.sing 917
    }
2427 ankur.sing 918
 
2489 ankur.sing 919
    /**
3354 chandransh 920
     * Returns list of items with a particular status.
921
     * @param st
922
     * @return
923
     */
3850 chandransh 924
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 925
        List<Item> itemList = new ArrayList<Item>();
926
        try {
927
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 928
            Client catalogClient = catalogServiceClient.getClient();
3354 chandransh 929
 
3850 chandransh 930
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 931
 
932
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
6813 amar.kumar 933
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null));
3354 chandransh 934
            }
935
        } catch (Exception e) {
936
            logger.error("Error while getting items by status: " + st, e);
937
        }
938
        return itemList;
939
    }
940
 
941
    /**
942
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
4423 phani.kuma 943
     * Also creates a Map each for VendorItemPricing, VendorItemMapping, SourceItemPricing and SimilarItems and adds these maps to the
3354 chandransh 944
     * new Item object.
945
     * @param thriftItem
946
     * @param tVendorPricings
947
     * @param tVendorMappings
4423 phani.kuma 948
     * @param tSourceMappings
949
     * @param tSimilarItems
5504 phani.kuma 950
     * @param tVoucherMappings
6813 amar.kumar 951
     * @param stockPurchaseParams 
3354 chandransh 952
     * @return item object with attributes copied from thrift item object.
953
     */
954
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
5946 rajveer 955
            List<in.shop2020.model.v1.inventory.VendorItemPricing> tVendorPricings, 
956
            List<in.shop2020.model.v1.inventory.VendorItemMapping> tVendorMappings,
4423 phani.kuma 957
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings,
6813 amar.kumar 958
            List<in.shop2020.model.v1.catalog.Item> tSimilarItems, 
959
            in.shop2020.model.v1.inventory.ItemInventory tItemInventory, 
960
            List<in.shop2020.model.v1.catalog.VoucherItemMapping> tVoucherMappings, 
961
            ItemStockPurchaseParams tStockPurchaseParams){
3354 chandransh 962
 
963
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
964
        VendorItemMapping vItemMapping;
965
        if(tVendorMappings != null) {
5946 rajveer 966
            for(in.shop2020.model.v1.inventory.VendorItemMapping vim : tVendorMappings) {
3354 chandransh 967
                vItemMapping = new VendorItemMapping();
968
                vItemMapping.setVendorId(vim.getVendorId());
969
                vItemMapping.setItemKey(vim.getItemKey());
970
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
971
            }
972
        }
973
 
974
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
975
        VendorPricings vPricings;
976
        if(tVendorPricings != null) {
5946 rajveer 977
            for(in.shop2020.model.v1.inventory.VendorItemPricing vip : tVendorPricings) {
3354 chandransh 978
                vPricings = new VendorPricings();
979
                vPricings.setVendorId(vip.getVendorId());
980
                vPricings.setMop(vip.getMop());
981
                vPricings.setDealerPrice(vip.getDealerPrice());
982
                vPricings.setTransferPrice(vip.getTransferPrice());
6759 amar.kumar 983
                vPricings.setNlc(vip.getNlc());
3354 chandransh 984
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
985
            }
986
        }
987
 
3558 rajveer 988
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
989
        SourcePricings sPricings;
990
        if(tSourceMappings != null) {
991
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
992
                sPricings = new SourcePricings();
993
                sPricings.setSourceId(sip.getSourceId());
994
                sPricings.setMrp(sip.getMrp());
995
                sPricings.setSellingPrice(sip.getSellingPrice());
996
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
997
            }
998
        }
4423 phani.kuma 999
 
1000
        Map<Long, Item> SimilarItemslist = new HashMap<Long, Item>();
1001
        Item sItems;
1002
        if(tSimilarItems != null) {
1003
            for(in.shop2020.model.v1.catalog.Item sit : tSimilarItems) {
1004
            	sItems = new Item();
1005
            	sItems.setCatalogItemId(sit.getCatalogItemId());
1006
            	sItems.setProductGroup(sit.getProductGroup());
1007
            	sItems.setBrand(sit.getBrand());
1008
            	sItems.setModelNumber(sit.getModelNumber());
1009
            	sItems.setModelName(sit.getModelName());
1010
            	sItems.setContentCategory(CategoryManager.getCategoryManager().getCategoryLabel(sit.getCategory()));
1011
            	SimilarItemslist.put(sit.getCatalogItemId(), sItems);
1012
            }
1013
        }
3558 rajveer 1014
 
4431 phani.kuma 1015
        Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
1016
        ItemInventory warehousedata;
5309 rajveer 1017
        if(tItemInventory != null) {
1018
        	Map<Long, Long> availabilityMap = tItemInventory.getAvailability();
1019
        	Map<Long, Long> reservedMap = tItemInventory.getReserved();
4431 phani.kuma 1020
            for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
1021
            	warehousedata = new ItemInventory();
1022
            	warehousedata.setWarehouseId(availability.getKey());
1023
            	warehousedata.setAvailability(availability.getValue());
1024
            	warehousedata.setReserved(reservedMap.get(availability.getKey()));
1025
            	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
1026
            }
1027
        }
1028
        else {
1029
        	itemInventoryMap = null;
1030
        }
5504 phani.kuma 1031
 
1032
        Map<String, VoucherItemMapping> voucherMap = new HashMap<String, VoucherItemMapping>();
1033
        VoucherItemMapping voucher;
1034
        if(tVoucherMappings != null) {
1035
            for(in.shop2020.model.v1.catalog.VoucherItemMapping tvoucher : tVoucherMappings) {
5516 phani.kuma 1036
            	voucher = new VoucherItemMapping(tvoucher.getAmount(), VoucherType.findByValue((int) tvoucher.getVoucherType()).name());
1037
            	voucherMap.put(VoucherType.findByValue((int) tvoucher.getVoucherType()).name(), voucher);
5504 phani.kuma 1038
            }
1039
        }
6813 amar.kumar 1040
 
1041
        in.shop2020.catalog.dashboard.shared.ItemStockPurchaseParams stockPurchaseParams = 
1042
        	new in.shop2020.catalog.dashboard.shared.ItemStockPurchaseParams();
1043
        if(tStockPurchaseParams != null) {
1044
        	stockPurchaseParams.setItem_id(tStockPurchaseParams.getItem_id());
1045
        	stockPurchaseParams.setMinStockLevel(tStockPurchaseParams.getMinStockLevel());
1046
        	stockPurchaseParams.setNumOfDaysStock(tStockPurchaseParams.getNumOfDaysStock());
1047
        }
1048
 
3354 chandransh 1049
        Item item = new Item(thriftItem.getId(),
1050
                thriftItem.getProductGroup(),
1051
                thriftItem.getBrand(),
1052
                thriftItem.getModelNumber(),
1053
                thriftItem.getModelName(),
1054
                thriftItem.getColor(),
1055
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
1056
                thriftItem.getCategory(),
1057
                thriftItem.getComments(),
1058
                thriftItem.getCatalogItemId(),
1059
                thriftItem.getFeatureId(),
1060
                thriftItem.getFeatureDescription(),
1061
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
1062
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
1063
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
1064
                thriftItem.getAddedOn(),
1065
                thriftItem.getStartDate(),
5217 amit.gupta 1066
                thriftItem.isSetComingSoonStartDate() ? thriftItem.getComingSoonStartDate() : null ,
1067
                thriftItem.isSetExpectedArrivalDate() ? thriftItem.getExpectedArrivalDate() : null ,
3354 chandransh 1068
                thriftItem.getRetireDate(),
1069
                thriftItem.getUpdatedOn(),
1070
                thriftItem.getItemStatus().name(),
1071
                thriftItem.getItemStatus().getValue(),
1072
                thriftItem.getStatus_description(),
1073
                thriftItem.getBestDealText(),
6777 vikram.rag 1074
                thriftItem.getBestDealsDetailsText(),
1075
                thriftItem.getBestDealsDetailsLink(),
3354 chandransh 1076
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
1077
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
1078
                thriftItem.isDefaultForEntity(),
1079
                thriftItem.isRisky(),
3359 chandransh 1080
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
4506 phani.kuma 1081
                thriftItem.isIsWarehousePreferenceSticky(),
5384 phani.kuma 1082
                thriftItem.isHasItemNo(),
1083
                ItemType.SERIALIZED.equals(thriftItem.getType()),
5460 phani.kuma 1084
                thriftItem.isClearance(),
6241 amit.gupta 1085
                thriftItem.isSetShowSellingPrice() ? thriftItem.isShowSellingPrice() : false,
4506 phani.kuma 1086
                thriftItem.isSetPreferredVendor() ? thriftItem.getPreferredVendor() : null,
6838 vikram.rag 1087
                thriftItem.isSetPreferredInsurer() ? thriftItem.getPreferredInsurer() : null,		
4431 phani.kuma 1088
                itemInventoryMap,
3354 chandransh 1089
                vendorPricingMap,
3558 rajveer 1090
                vItemMap,
4423 phani.kuma 1091
                sourcePricingMap,
5504 phani.kuma 1092
                SimilarItemslist,
6813 amar.kumar 1093
                voucherMap, stockPurchaseParams.getNumOfDaysStock(), 
1094
                stockPurchaseParams.getMinStockLevel());
3354 chandransh 1095
        return item;
1096
    }
1097
 
1098
    /**
2489 ankur.sing 1099
     * 
3922 chandransh 1100
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 1101
     */
3922 chandransh 1102
    private boolean canPushToProduction() {
1103
        int count = getPushToProdCount(logFile);
1104
        return count < maxCount;
2427 ankur.sing 1105
    }
3922 chandransh 1106
 
2489 ankur.sing 1107
    /**
1108
     * If this is the first attempt to push to production for current date, returns 0
1109
     * else reads number of lines from daily rolling log file to get the count.
1110
     * @return push to production count for current date
1111
     */
2498 ankur.sing 1112
    private int getPushToProdCount(String logfile) {
1113
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
1114
        // this should also be set in log4j.properties
2427 ankur.sing 1115
        int lineCount = 0;
2498 ankur.sing 1116
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 1117
        Date currentDate = Calendar.getInstance().getTime();
1118
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
1119
            pushToProdDate = currentDate;
1120
            return lineCount;
1121
        }
1122
        try {
2498 ankur.sing 1123
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 1124
            while (br.readLine() != null) {
1125
                lineCount++;
1126
            }
1127
        } catch (IOException e) {
3354 chandransh 1128
            logger.error("Error while reading the log file", e);
2427 ankur.sing 1129
        }
1130
        return lineCount;
1131
    }
4423 phani.kuma 1132
 
1133
	@Override
1134
	public boolean deleteSimilarItem(long itemId, long catalogItemId) {
1135
		try{
1136
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1137
            Client catalogClient = catalogServiceClient.getClient();
4423 phani.kuma 1138
 
1139
            return catalogClient.deleteSimilarItem(itemId, catalogItemId);
1140
 
1141
        }catch(Exception e){
1142
            logger.error("Error while deleting the SimilarItems for: " + itemId, e);
1143
        }
1144
        return false;
1145
	}
1146
 
1147
	@Override
1148
	public Item addSimilarItem(long itemId, long catalogItemId) {
1149
		try{
1150
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1151
            Client catalogClient = catalogServiceClient.getClient();
4423 phani.kuma 1152
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.addSimilarItem(itemId, catalogItemId);;
1153
 
6813 amar.kumar 1154
            return getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null);
4423 phani.kuma 1155
        }catch(Exception e){
1156
            logger.error("Error while adding the SimilarItems for: " + itemId, e);
1157
        }
1158
        return null;
1159
	}
4431 phani.kuma 1160
 
1161
	@Override
1162
	public Map<Long, ItemInventory> getProdItemInventory(long itemId) {
1163
		try {
1164
            // Initialize client for production server
1165
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
1166
                    ConfigClientKeys.catalog_service_server_port.toString());
5946 rajveer 1167
            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
1168
            InventoryClient inventoryServiceClient = new InventoryClient();
1169
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
1170
            in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(itemId);
4431 phani.kuma 1171
 
1172
            Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
1173
            ItemInventory warehousedata;
1174
            if(thriftItemInventory != null) {
1175
            	Map<Long, Long> availabilityMap = thriftItemInventory.getAvailability();
1176
            	Map<Long, Long> reservedMap = thriftItemInventory.getReserved();
1177
                for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
1178
                	warehousedata = new ItemInventory();
1179
                	warehousedata.setWarehouseId(availability.getKey());
1180
                	warehousedata.setAvailability(availability.getValue());
1181
                	warehousedata.setReserved(reservedMap.get(availability.getKey()));
1182
                	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
1183
                }
1184
            }
1185
            else {
1186
            	itemInventoryMap = null;
1187
            }
1188
            return itemInventoryMap;
1189
        } catch (Exception e) {
1190
            logger.error("Error while updating item's risky flag on staging", e);
1191
        }
1192
		return null;
1193
	}
4649 phani.kuma 1194
 
1195
	@Override
1196
	public boolean addAuthorizationLog(long itemId, String username, String message) {
1197
		try{
1198
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1199
            Client catalogClient = catalogServiceClient.getClient();
4649 phani.kuma 1200
 
1201
            return catalogClient.addAuthorizationLog(itemId, username, message);
1202
 
1203
        }catch(Exception e){
1204
            logger.error("Error while adding the event for: " + itemId, e);
1205
        }
1206
		return false;
1207
	}
1208
 
1209
	@Override
1210
	public Map<String, String> getConfigdataforPriceCompare() {
1211
		Map<String, String> ConfigMap = new HashMap<String, String>();
1212
		try {
1213
			ConfigMap.put("courier_cost_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_cost_factor.toString()));
1214
            ConfigMap.put("courier_weight_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_weight_factor.toString()));
1215
            ConfigMap.put("breakeven_divisor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_divisor.toString()));
1216
            ConfigMap.put("breakeven_additon_factor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_additon_factor.toString()));
1217
            ConfigMap.put("transfer_price_percentage", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_percentage.toString()));
1218
            ConfigMap.put("transfer_price_factor", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_factor.toString()));
1219
        } catch(ConfigException ce) {
1220
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1221
            ConfigMap.put("courier_cost_factor", "60");
1222
            ConfigMap.put("courier_weight_factor", "500");
1223
            ConfigMap.put("breakeven_divisor", "0.98");
1224
            ConfigMap.put("breakeven_additon_factor", "50");
1225
            ConfigMap.put("transfer_price_percentage", "2");
1226
            ConfigMap.put("transfer_price_factor", "50");
1227
        }
1228
		return ConfigMap;
1229
	}
4957 phani.kuma 1230
 
1231
	@Override
1232
	public List<String> getAllCategories() {
1233
		List<String> categoryList = new ArrayList<String>();
1234
		List<Long> parentCategoryIDs = Arrays.asList(new Long[] {(long) 0,(long) 10001,(long) 10009,(long) 10049});
1235
        try {
1236
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1237
            Client catalogClient = catalogServiceClient.getClient();
4957 phani.kuma 1238
 
1239
            List<in.shop2020.model.v1.catalog.Category> categories = catalogClient.getAllCategories();
1240
 
1241
            for(in.shop2020.model.v1.catalog.Category category : categories) {
1242
            	if(!parentCategoryIDs.contains(category.getParent_category_id())){
1243
            		categoryList.add(category.getDisplay_name());
1244
            	}
1245
            	else{
1246
            		continue;
1247
            	}
1248
            }
1249
        } catch (Exception e) {
1250
            logger.error("Error while getting all the categories: ", e);
1251
        }
1252
        return categoryList;
1253
	}
1254
 
1255
	@Override
1256
	public List<String> getAllBrands() {
1257
		List<String> brandsList = new ArrayList<String>();
1258
        try {
1259
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1260
            Client catalogClient = catalogServiceClient.getClient();
4957 phani.kuma 1261
 
1262
            brandsList = catalogClient.getAllBrands();
1263
        } catch (Exception e) {
1264
            logger.error("Error while getting all the categories: ", e);
1265
        }
1266
        return brandsList;
1267
	}
5427 amit.gupta 1268
 
5504 phani.kuma 1269
	@Override
5516 phani.kuma 1270
	public boolean deleteVoucher(Long catalogItemId, Long voucherType) {
5504 phani.kuma 1271
		try{
1272
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1273
            Client catalogClient = catalogServiceClient.getClient();
5504 phani.kuma 1274
 
1275
            return catalogClient.deleteVoucherForItem(catalogItemId, voucherType);
1276
 
1277
        }catch(Exception e){
1278
            logger.error("Error while deleting the Voucher for: " + catalogItemId, e);
1279
        }
1280
        return false;
1281
	}
1282
 
1283
	@Override
5516 phani.kuma 1284
	public boolean addVoucher(Long catalogItemId, Long voucherType, long voucherAmount) {
5504 phani.kuma 1285
		try{
1286
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1287
            Client catalogClient = catalogServiceClient.getClient();
5504 phani.kuma 1288
 
1289
            return catalogClient.addupdateVoucherForItem(catalogItemId, voucherType, voucherAmount);
1290
 
1291
        }catch(Exception e){
1292
            logger.error("Error while adding the Voucher for: " + catalogItemId, e);
1293
        }
1294
		return false;
1295
	}
1296
	@Override
5516 phani.kuma 1297
	public Map<Long, String> getvoucherTypes() {
1298
		Map<Long, String> voucherType = new HashMap<Long, String>();
5504 phani.kuma 1299
		for(VoucherType voucher : VoucherType.values())
1300
		{
5516 phani.kuma 1301
			voucherType.put((long) voucher.getValue(), voucher.name());
5504 phani.kuma 1302
		}
1303
		return voucherType;
1304
	}
1305
 
5586 phani.kuma 1306
	@Override
1307
	public boolean getConfigforentityIdMandatory() {
1308
		try {
1309
			String entityId_mandatory = ConfigClient.getClient().get(ConfigClientKeys.entityId_mandatory.toString());
1310
			if (entityId_mandatory.equals("TRUE")) {
1311
				return true;
1312
			}
1313
        } catch(ConfigException ce) {
1314
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1315
        }
1316
		return false;
1317
	}
1318
 
1319
	@Override
1320
	public boolean checkEntityId(long entityId) {
1321
		try {
1322
            CatalogClient catalogServiceClient = new CatalogClient(ConfigClientKeys.catalog_service_server_host_master.toString(), ConfigClientKeys.catalog_service_server_port.toString());
5946 rajveer 1323
            Client catalogClient = catalogServiceClient.getClient();
5586 phani.kuma 1324
            return catalogClient.isValidCatalogItemId(entityId);
1325
        } catch (Exception e) {
1326
            logger.error("Error while checking for a Catalog Item Id: ", e);
1327
        }
1328
		return false;
1329
	}
1330
 
6096 amit.gupta 1331
	@Override
1332
	public String updateExpectedDelayOnProd(Item item) {
1333
		try{
1334
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
1335
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
1336
                    ConfigClientKeys.catalog_service_server_port.toString());
1337
 
1338
            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
1339
 
1340
 
1341
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
1342
            if(item.getExpectedDelay() != null) {
1343
            	tItem.setExpectedDelay(item.getExpectedDelay());
1344
 
1345
	            long rItemId;
1346
	            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
1347
	                logger.error("Error updating item on production, returned Item Id: " + rItemId);
1348
	                return "Error updating expected delay on production";
1349
	            }
6241 amit.gupta 1350
	            InventoryClient inventoryServiceClient= new InventoryClient();
1351
	            InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
6096 amit.gupta 1352
	            inventoryClient.clearItemAvailabilityCacheForItem(rItemId);
1353
	            logger.info("Successfully updated item: " + item.getId());
1354
	            return "Expected delay successfully updated on prod"; 
1355
            } else {
1356
            	return "Error:expected delay should not be null";
1357
            }
1358
        } catch (Exception e) {
6098 amit.gupta 1359
            logger.error("Error while updating expected delay on production", e);
1360
            return "Error while updating expected delay on production";
6096 amit.gupta 1361
        }
1362
	}
1363
 
6838 vikram.rag 1364
	@Override
1365
	public Map<Long, String> getAllInsurers() {
1366
		 Map<Long, String> insurersMap = new HashMap<Long, String>();
1367
	        try {
1368
	            CatalogClient catalogServiceClient = new CatalogClient();
1369
	            in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
1370
 
1371
	            List<Insurer> insurers = catalogClient.getAllInsurers();
6096 amit.gupta 1372
 
6838 vikram.rag 1373
	            for(in.shop2020.model.v1.catalog.Insurer i : insurers) {
1374
	                insurersMap.put(i.getId(), i.getName());
1375
	            }
1376
	        } catch (Exception e) {
1377
	            logger.error("Error while getting all the vendors: ", e);
1378
	        }
1379
	        return insurersMap;
1380
	}
1381
 
1382
 
6096 amit.gupta 1383
 
1384
 
1385
 
6530 vikram.rag 1386
}