Subversion Repositories SmartDukaan

Rev

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