Subversion Repositories SmartDukaan

Rev

Rev 7305 | Rev 7972 | 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());
7291 vikram.rag 641
 
642
        if(item.getAsin() != null) {
643
            tItem.setAsin(item.getAsin());
644
        }
7305 amit.gupta 645
        if(item.getHoldInventory()!= null) {
646
        	tItem.setHoldInventory(item.getHoldInventory());
647
        }
648
        else{
649
        	tItem.setHoldInventory(0);
650
        }	
651
        if(item.getDefaultInventory()!=null){
652
        	tItem.setDefaultInventory(item.getDefaultInventory());
653
        }
654
        else{
655
        	tItem.setDefaultInventory(0);
656
        }
2126 ankur.sing 657
        tItem.setBestDealText(item.getBestDealsText());
6777 vikram.rag 658
        tItem.setBestDealsDetailsText(item.getBestDealsDetailsText());
659
        tItem.setBestDealsDetailsLink(item.getBestDealsDetailsLink());
2489 ankur.sing 660
        if(item.getBestDealsValue() != null) {
2126 ankur.sing 661
            tItem.setBestDealValue(item.getBestDealsValue());
662
        }
3354 chandransh 663
 
2489 ankur.sing 664
        if(item.getBestSellingRank() != null) {
2126 ankur.sing 665
            tItem.setBestSellingRank(item.getBestSellingRank());
666
        }
3354 chandransh 667
 
2126 ankur.sing 668
        tItem.setDefaultForEntity(item.isDefaultForEntity());
2252 ankur.sing 669
        tItem.setRisky(item.isRisky());
5384 phani.kuma 670
        tItem.setHasItemNo(item.isHasItemNo());
671
 
672
        if(item.isItemType()) {
673
        	tItem.setType(ItemType.SERIALIZED);
674
        }
675
        else {
676
        	tItem.setType(ItemType.NON_SERIALIZED);
677
        }
678
 
2489 ankur.sing 679
        if(item.getStartDate() != null) {
680
            tItem.setStartDate(item.getStartDate());
681
        }
3354 chandransh 682
 
5217 amit.gupta 683
        if(item.getExpectedArrivalDate() != null) {
684
        	tItem.setExpectedArrivalDate(item.getExpectedArrivalDate());
685
        } else {
686
        	tItem.unsetComingSoonStartDate();
687
        	tItem.unsetExpectedArrivalDate();
688
        	if(status.COMING_SOON.equals(tItem.getItemStatus())){
6277 amit.gupta 689
        		tItem.setItemStatus(status.CONTENT_COMPLETE);
690
        		tItem.setStatus_description("Content generation completed");
5217 amit.gupta 691
        	}
692
        }
693
 
694
        if(item.getComingSoonStartDate() != null) {
695
        	tItem.setComingSoonStartDate(item.getComingSoonStartDate());
696
        } else {
697
        	tItem.unsetComingSoonStartDate();
698
        }
699
 
2489 ankur.sing 700
        if(item.getRetireDate() != null) {
701
            tItem.setRetireDate(item.getRetireDate());
702
        }
3354 chandransh 703
 
2126 ankur.sing 704
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
705
    }
706
 
707
    @Override
708
    public void pauseItem(long itemId) {
709
        try {
3129 rajveer 710
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 711
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 712
 
2126 ankur.sing 713
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
714
        } catch (Exception e) {
3354 chandransh 715
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 716
        }
2359 ankur.sing 717
 
2126 ankur.sing 718
    }
719
 
720
    @Override
721
    public void markInProcess(long itemId) {
722
        try {
3129 rajveer 723
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 724
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 725
 
2126 ankur.sing 726
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
727
        } catch (Exception e) {
3354 chandransh 728
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 729
        }
730
    }
2359 ankur.sing 731
 
2126 ankur.sing 732
    @Override
733
    public void phaseoutItem(long itemId) {
734
        try {
3129 rajveer 735
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 736
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 737
 
2126 ankur.sing 738
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
739
        } catch (Exception e) {
3354 chandransh 740
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 741
        }
742
    }
2359 ankur.sing 743
 
2126 ankur.sing 744
    @Override
745
    public void activateItem(long itemId) {
746
        try {
3129 rajveer 747
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 748
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 749
 
2126 ankur.sing 750
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
751
        } catch (Exception e) {
3354 chandransh 752
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 753
        }
754
    }
2359 ankur.sing 755
 
756
    @Override
757
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
758
        try {
4354 mandeep.dh 759
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 760
            // Initialize client for staging server
3129 rajveer 761
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 762
            Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 763
 
764
            // Initialize client for production server
3165 chandransh 765
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 766
                    ConfigClientKeys.catalog_service_server_port.toString());
5946 rajveer 767
            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
2359 ankur.sing 768
            catalogClient.changeItemRiskyFlag(itemId, risky);
769
 
770
            try{
771
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
772
            }catch(Exception e){
3354 chandransh 773
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 774
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
775
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
776
                return false;
777
            }
778
        } catch (Exception e) {
3354 chandransh 779
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 780
            return false;
781
        }
782
        return true;
783
    }
784
 
2427 ankur.sing 785
 
786
    /**
4762 phani.kuma 787
     * Retuns list of Items filtered by category (column is product_group in catalog.item table)
2427 ankur.sing 788
     * This list is used to generate master sheet (excel sheet)
789
     * @param category
4957 phani.kuma 790
     * @param brand
2427 ankur.sing 791
     * @return
792
     */
4957 phani.kuma 793
    public List<Item> getItemsForMasterSheet(String category, String brand) {
2359 ankur.sing 794
        List<Item> itemList = new ArrayList<Item>();
795
        try {
3129 rajveer 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();
2359 ankur.sing 800
 
4957 phani.kuma 801
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsForMasterSheet(category, brand);
2359 ankur.sing 802
 
803
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5946 rajveer 804
                List<in.shop2020.model.v1.inventory.VendorItemPricing> vip = inventoryClient.getAllItemPricing(thriftItem.getId());
805
                List<in.shop2020.model.v1.inventory.VendorItemMapping> vim = inventoryClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 806
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
7190 amar.kumar 807
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null, null, null, null, null));
2359 ankur.sing 808
            }
809
        } catch (Exception e) {
4762 phani.kuma 810
            logger.error("Error while getting items by category: " + category, e);
2359 ankur.sing 811
        }
812
        Collections.sort(itemList, new ItemsComparator());
813
        return itemList;
814
    }
815
 
816
    @Override
2427 ankur.sing 817
    public String updateItemOnProduction(Item item) {
5706 amit.gupta 818
    	if(!canPushToProduction()) {
819
    		// If we can't push to production, then return that message to user
820
    		// else move forward with update.
821
    		return "Maximum push to production count reached for today";
822
    	}
823
    	List<Long> itemIds = item.getSameItemsWithDifferentColors();
824
    	List<Item> items = new ArrayList<Item>();
825
    	items.add(item);
826
    	for(Long itemId : itemIds) {
827
    		try {
828
    			CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 829
    			Client catalogClient = catalogServiceClient.getClient();
830
    			InventoryClient inventoryServiceClient = new InventoryClient();
831
                in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
832
 
5706 amit.gupta 833
    			in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);
5946 rajveer 834
				List<in.shop2020.model.v1.inventory.VendorItemPricing> vip = inventoryClient.getAllItemPricing(thriftItem.getId());
835
				List<in.shop2020.model.v1.inventory.VendorItemMapping> vim = inventoryClient.getVendorItemMappings(thriftItem.getId());
5706 amit.gupta 836
				List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
7190 amar.kumar 837
				items.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null, null, null, null, null));
5706 amit.gupta 838
    		} catch (Exception e) {
839
    			logger.error("Could not fetch item for : " + itemId, e);
840
    			return "Could not push to production. No item got pushed.";
841
    		}
842
    	}
843
    	for (Item it : items) {
844
	        logger.info("Update item on production call, Item Id: " + it.getId());
845
 
846
	        try{
847
	            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
848
	                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
849
	                    ConfigClientKeys.catalog_service_server_port.toString());
850
 
5946 rajveer 851
	            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
5706 amit.gupta 852
 
853
	            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(it.getId());
854
 
5946 rajveer 855
	            InventoryClient inventoryServiceClient = new InventoryClient();
856
	            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
857
 
5706 amit.gupta 858
	            if(it.getMrp() != null) {
859
	                tItem.setMrp(it.getMrp());
860
	            }
861
 
862
	            if(it.getSellingPrice() != null) {
863
	                tItem.setSellingPrice(it.getSellingPrice());
864
	            }
865
 
7700 amit.gupta 866
                tItem.setBestDealText(it.getBestDealsText());
7198 amar.kumar 867
 
6826 amit.gupta 868
	            if (it.getItemStatusValue()  == ItemStatus.ACTIVE.getValue() || it.getItemStatusValue()  == ItemStatus.PAUSED.getValue()) {
869
	            	tItem.setItemStatus(status.findByValue(it.getItemStatusValue()));
870
	            	tItem.setStartDate(it.getStartDate());
871
	            }
5706 amit.gupta 872
	            long rItemId;
873
	            if((rItemId = catalogClient_Prod.updateItem(tItem)) != it.getId()) {
874
	                logger.error("Error updating item on production, returned Item Id: " + rItemId);
875
	                return "Error updating item prices on production";
876
	            }
7198 amar.kumar 877
	            if(it.getFreebieItemId() !=null && it.getFreebieItemId()!=0) {
878
	            	in.shop2020.model.v1.catalog.FreebieItem freebieItem = new in.shop2020.model.v1.catalog.FreebieItem();
879
	            	freebieItem.setItemId(it.getId());
880
	            	freebieItem.setFreebieItemId(it.getFreebieItemId());
881
	            	catalogClient_Prod.addOrUpdateFreebieForItem(freebieItem);
882
	            }
5706 amit.gupta 883
	            logger.info("Successfully updated item: " + it.getId());
884
 
885
	            StringBuilder sb = new StringBuilder();
886
 
887
	            Map<Long, VendorPricings> vendorPricings = it.getVendorPricesMap();
888
	            if(vendorPricings != null && !vendorPricings.isEmpty()) {
5946 rajveer 889
	                in.shop2020.model.v1.inventory.VendorItemPricing tVendorPricing;
5706 amit.gupta 890
	                for(VendorPricings v : vendorPricings.values()) {
5946 rajveer 891
	                    tVendorPricing = new in.shop2020.model.v1.inventory.VendorItemPricing();
5706 amit.gupta 892
	                    tVendorPricing.setVendorId(v.getVendorId());
893
	                    tVendorPricing.setItemId(it.getId());
894
	                    tVendorPricing.setMop(v.getMop());
895
	                    tVendorPricing.setTransferPrice(v.getTransferPrice());
6759 amar.kumar 896
	                    tVendorPricing.setNlc(v.getNlc());
5706 amit.gupta 897
	                    tVendorPricing.setDealerPrice(v.getDealerPrice());
5946 rajveer 898
	                    inventoryClient.addVendorItemPricing(tVendorPricing);
5706 amit.gupta 899
	                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
6759 amar.kumar 900
	                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",NLC= " +v.getNlc() + ",DP=" + v.getDealerPrice() + "], ");
5706 amit.gupta 901
	                }
902
	            }
903
 
904
	            Map<String, VendorItemMapping> vendorMappings = it.getVendorKeysMap();
905
	            if(vendorMappings != null && !vendorMappings.isEmpty()) {
5946 rajveer 906
	                in.shop2020.model.v1.inventory.VendorItemMapping tVendorMapping;
5706 amit.gupta 907
	                for(VendorItemMapping vendorItemMapping : vendorMappings.values()) {
5946 rajveer 908
	                    tVendorMapping = new in.shop2020.model.v1.inventory.VendorItemMapping();
5706 amit.gupta 909
	                    tVendorMapping.setVendorId(vendorItemMapping.getVendorId());
910
	                    tVendorMapping.setItemId(it.getId());
911
	                    tVendorMapping.setItemKey(vendorItemMapping.getItemKey());
5946 rajveer 912
	                    inventoryClient.addVendorItemMapping(vendorItemMapping.getItemKey(), tVendorMapping);
5706 amit.gupta 913
	                    logger.info("VendorItemMapping updated. " + tVendorMapping.toString());
914
	                    sb.append("[VId:" + vendorItemMapping.getVendorId() + ",ItemKey=" + vendorItemMapping.getItemKey() + "], ");
915
	                }
916
	            }
917
 
918
	            Map<Long, SourcePricings> sourcePricings = it.getSourcePricesMap();
919
	            if(sourcePricings != null && !sourcePricings.isEmpty()){
920
	                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
921
	                for(SourcePricings s : sourcePricings.values()){
922
	                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
923
	                    tSourcePricing.setSourceId(s.getSourceId());
924
	                    tSourcePricing.setItemId(it.getId());
925
	                    tSourcePricing.setMrp(s.getMrp());
926
	                    tSourcePricing.setSellingPrice(s.getSellingPrice());
927
	                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
928
	                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
929
	                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
930
	                }
931
	            }
932
 
933
	            pushToProdLogger.info("Id=" + it.getId() + ",MRP=" + it.getMrp() + ",SP=" + " " + it.getSellingPrice() + sb.toString());
934
	        } catch (Exception e) {
935
	            logger.error("Error while updating prices on production", e);
936
	            return "Error while updating all prices on production. Some of the prices may not have been  updated";
937
	        }
938
    	}
2427 ankur.sing 939
 
3907 chandransh 940
        try {
941
            ContentClient contentServiceClient = new ContentClient();
942
            ContentService.Client contentClient = contentServiceClient.getClient();
943
 
5706 amit.gupta 944
            contentClient.pushContentToProduction(items.get(0).getCatalogItemId());
3907 chandransh 945
        } catch (TTransportException e) {
946
            logger.error("Error while establishing connection to the content server", e);
947
            return "Error generating content. Prices updated successfully.";
948
        } catch (TException e) {
949
            logger.error("Error while pushing content to production", e);
950
            return "Error generating content. Prices updated successfully.";
951
        } catch (ContentServiceException e) {
952
            logger.error("Error while pushing content to production", e);
953
            return "Error generating content. Prices updated successfully.";
954
        }
955
 
2427 ankur.sing 956
        return "Prices updated and content generated successfully";
2359 ankur.sing 957
    }
2427 ankur.sing 958
 
2489 ankur.sing 959
    /**
3354 chandransh 960
     * Returns list of items with a particular status.
961
     * @param st
962
     * @return
963
     */
3850 chandransh 964
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 965
        List<Item> itemList = new ArrayList<Item>();
966
        try {
967
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 968
            Client catalogClient = catalogServiceClient.getClient();
3354 chandransh 969
 
3850 chandransh 970
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 971
 
972
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
7190 amar.kumar 973
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null, null));
3354 chandransh 974
            }
975
        } catch (Exception e) {
976
            logger.error("Error while getting items by status: " + st, e);
977
        }
978
        return itemList;
979
    }
980
 
981
    /**
982
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
4423 phani.kuma 983
     * Also creates a Map each for VendorItemPricing, VendorItemMapping, SourceItemPricing and SimilarItems and adds these maps to the
3354 chandransh 984
     * new Item object.
985
     * @param thriftItem
986
     * @param tVendorPricings
987
     * @param tVendorMappings
4423 phani.kuma 988
     * @param tSourceMappings
989
     * @param tSimilarItems
5504 phani.kuma 990
     * @param tVoucherMappings
6813 amar.kumar 991
     * @param stockPurchaseParams 
3354 chandransh 992
     * @return item object with attributes copied from thrift item object.
993
     */
994
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
5946 rajveer 995
            List<in.shop2020.model.v1.inventory.VendorItemPricing> tVendorPricings, 
996
            List<in.shop2020.model.v1.inventory.VendorItemMapping> tVendorMappings,
4423 phani.kuma 997
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings,
6813 amar.kumar 998
            List<in.shop2020.model.v1.catalog.Item> tSimilarItems, 
999
            in.shop2020.model.v1.inventory.ItemInventory tItemInventory, 
1000
            List<in.shop2020.model.v1.catalog.VoucherItemMapping> tVoucherMappings, 
7190 amar.kumar 1001
            ItemStockPurchaseParams tStockPurchaseParams, Long freebieItemId){
3354 chandransh 1002
 
1003
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
1004
        VendorItemMapping vItemMapping;
1005
        if(tVendorMappings != null) {
5946 rajveer 1006
            for(in.shop2020.model.v1.inventory.VendorItemMapping vim : tVendorMappings) {
3354 chandransh 1007
                vItemMapping = new VendorItemMapping();
1008
                vItemMapping.setVendorId(vim.getVendorId());
1009
                vItemMapping.setItemKey(vim.getItemKey());
1010
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
1011
            }
1012
        }
1013
 
1014
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
1015
        VendorPricings vPricings;
1016
        if(tVendorPricings != null) {
5946 rajveer 1017
            for(in.shop2020.model.v1.inventory.VendorItemPricing vip : tVendorPricings) {
3354 chandransh 1018
                vPricings = new VendorPricings();
1019
                vPricings.setVendorId(vip.getVendorId());
1020
                vPricings.setMop(vip.getMop());
1021
                vPricings.setDealerPrice(vip.getDealerPrice());
1022
                vPricings.setTransferPrice(vip.getTransferPrice());
6759 amar.kumar 1023
                vPricings.setNlc(vip.getNlc());
3354 chandransh 1024
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
1025
            }
1026
        }
1027
 
3558 rajveer 1028
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
1029
        SourcePricings sPricings;
1030
        if(tSourceMappings != null) {
1031
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
1032
                sPricings = new SourcePricings();
1033
                sPricings.setSourceId(sip.getSourceId());
1034
                sPricings.setMrp(sip.getMrp());
1035
                sPricings.setSellingPrice(sip.getSellingPrice());
1036
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
1037
            }
1038
        }
4423 phani.kuma 1039
 
1040
        Map<Long, Item> SimilarItemslist = new HashMap<Long, Item>();
1041
        Item sItems;
1042
        if(tSimilarItems != null) {
1043
            for(in.shop2020.model.v1.catalog.Item sit : tSimilarItems) {
1044
            	sItems = new Item();
1045
            	sItems.setCatalogItemId(sit.getCatalogItemId());
1046
            	sItems.setProductGroup(sit.getProductGroup());
1047
            	sItems.setBrand(sit.getBrand());
1048
            	sItems.setModelNumber(sit.getModelNumber());
1049
            	sItems.setModelName(sit.getModelName());
1050
            	sItems.setContentCategory(CategoryManager.getCategoryManager().getCategoryLabel(sit.getCategory()));
1051
            	SimilarItemslist.put(sit.getCatalogItemId(), sItems);
1052
            }
1053
        }
3558 rajveer 1054
 
4431 phani.kuma 1055
        Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
1056
        ItemInventory warehousedata;
5309 rajveer 1057
        if(tItemInventory != null) {
1058
        	Map<Long, Long> availabilityMap = tItemInventory.getAvailability();
1059
        	Map<Long, Long> reservedMap = tItemInventory.getReserved();
4431 phani.kuma 1060
            for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
1061
            	warehousedata = new ItemInventory();
1062
            	warehousedata.setWarehouseId(availability.getKey());
1063
            	warehousedata.setAvailability(availability.getValue());
1064
            	warehousedata.setReserved(reservedMap.get(availability.getKey()));
1065
            	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
1066
            }
1067
        }
1068
        else {
1069
        	itemInventoryMap = null;
1070
        }
5504 phani.kuma 1071
 
1072
        Map<String, VoucherItemMapping> voucherMap = new HashMap<String, VoucherItemMapping>();
1073
        VoucherItemMapping voucher;
1074
        if(tVoucherMappings != null) {
1075
            for(in.shop2020.model.v1.catalog.VoucherItemMapping tvoucher : tVoucherMappings) {
5516 phani.kuma 1076
            	voucher = new VoucherItemMapping(tvoucher.getAmount(), VoucherType.findByValue((int) tvoucher.getVoucherType()).name());
1077
            	voucherMap.put(VoucherType.findByValue((int) tvoucher.getVoucherType()).name(), voucher);
5504 phani.kuma 1078
            }
1079
        }
6813 amar.kumar 1080
 
1081
        in.shop2020.catalog.dashboard.shared.ItemStockPurchaseParams stockPurchaseParams = 
1082
        	new in.shop2020.catalog.dashboard.shared.ItemStockPurchaseParams();
1083
        if(tStockPurchaseParams != null) {
1084
        	stockPurchaseParams.setItem_id(tStockPurchaseParams.getItem_id());
1085
        	stockPurchaseParams.setMinStockLevel(tStockPurchaseParams.getMinStockLevel());
1086
        	stockPurchaseParams.setNumOfDaysStock(tStockPurchaseParams.getNumOfDaysStock());
1087
        }
1088
 
3354 chandransh 1089
        Item item = new Item(thriftItem.getId(),
1090
                thriftItem.getProductGroup(),
1091
                thriftItem.getBrand(),
1092
                thriftItem.getModelNumber(),
1093
                thriftItem.getModelName(),
1094
                thriftItem.getColor(),
1095
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
1096
                thriftItem.getCategory(),
1097
                thriftItem.getComments(),
1098
                thriftItem.getCatalogItemId(),
1099
                thriftItem.getFeatureId(),
1100
                thriftItem.getFeatureDescription(),
1101
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
1102
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
1103
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
1104
                thriftItem.getAddedOn(),
1105
                thriftItem.getStartDate(),
5217 amit.gupta 1106
                thriftItem.isSetComingSoonStartDate() ? thriftItem.getComingSoonStartDate() : null ,
1107
                thriftItem.isSetExpectedArrivalDate() ? thriftItem.getExpectedArrivalDate() : null ,
3354 chandransh 1108
                thriftItem.getRetireDate(),
1109
                thriftItem.getUpdatedOn(),
1110
                thriftItem.getItemStatus().name(),
1111
                thriftItem.getItemStatus().getValue(),
1112
                thriftItem.getStatus_description(),
1113
                thriftItem.getBestDealText(),
6777 vikram.rag 1114
                thriftItem.getBestDealsDetailsText(),
1115
                thriftItem.getBestDealsDetailsLink(),
3354 chandransh 1116
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
1117
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
1118
                thriftItem.isDefaultForEntity(),
1119
                thriftItem.isRisky(),
3359 chandransh 1120
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
4506 phani.kuma 1121
                thriftItem.isIsWarehousePreferenceSticky(),
5384 phani.kuma 1122
                thriftItem.isHasItemNo(),
1123
                ItemType.SERIALIZED.equals(thriftItem.getType()),
6241 amit.gupta 1124
                thriftItem.isSetShowSellingPrice() ? thriftItem.isShowSellingPrice() : false,
4506 phani.kuma 1125
                thriftItem.isSetPreferredVendor() ? thriftItem.getPreferredVendor() : null,
6838 vikram.rag 1126
                thriftItem.isSetPreferredInsurer() ? thriftItem.getPreferredInsurer() : null,		
4431 phani.kuma 1127
                itemInventoryMap,
3354 chandransh 1128
                vendorPricingMap,
3558 rajveer 1129
                vItemMap,
4423 phani.kuma 1130
                sourcePricingMap,
5504 phani.kuma 1131
                SimilarItemslist,
6813 amar.kumar 1132
                voucherMap, stockPurchaseParams.getNumOfDaysStock(), 
7291 vikram.rag 1133
                stockPurchaseParams.getMinStockLevel(), freebieItemId,
1134
                thriftItem.getAsin(),
1135
                thriftItem.getHoldInventory(),
1136
                thriftItem.getDefaultInventory());
3354 chandransh 1137
        return item;
1138
    }
1139
 
1140
    /**
2489 ankur.sing 1141
     * 
3922 chandransh 1142
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 1143
     */
3922 chandransh 1144
    private boolean canPushToProduction() {
1145
        int count = getPushToProdCount(logFile);
1146
        return count < maxCount;
2427 ankur.sing 1147
    }
3922 chandransh 1148
 
2489 ankur.sing 1149
    /**
1150
     * If this is the first attempt to push to production for current date, returns 0
1151
     * else reads number of lines from daily rolling log file to get the count.
1152
     * @return push to production count for current date
1153
     */
2498 ankur.sing 1154
    private int getPushToProdCount(String logfile) {
1155
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
1156
        // this should also be set in log4j.properties
2427 ankur.sing 1157
        int lineCount = 0;
2498 ankur.sing 1158
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 1159
        Date currentDate = Calendar.getInstance().getTime();
1160
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
1161
            pushToProdDate = currentDate;
1162
            return lineCount;
1163
        }
1164
        try {
2498 ankur.sing 1165
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 1166
            while (br.readLine() != null) {
1167
                lineCount++;
1168
            }
1169
        } catch (IOException e) {
3354 chandransh 1170
            logger.error("Error while reading the log file", e);
2427 ankur.sing 1171
        }
1172
        return lineCount;
1173
    }
4423 phani.kuma 1174
 
1175
	@Override
1176
	public boolean deleteSimilarItem(long itemId, long catalogItemId) {
1177
		try{
1178
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1179
            Client catalogClient = catalogServiceClient.getClient();
4423 phani.kuma 1180
 
1181
            return catalogClient.deleteSimilarItem(itemId, catalogItemId);
1182
 
1183
        }catch(Exception e){
1184
            logger.error("Error while deleting the SimilarItems for: " + itemId, e);
1185
        }
1186
        return false;
1187
	}
1188
 
1189
	@Override
1190
	public Item addSimilarItem(long itemId, long catalogItemId) {
1191
		try{
1192
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1193
            Client catalogClient = catalogServiceClient.getClient();
4423 phani.kuma 1194
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.addSimilarItem(itemId, catalogItemId);;
1195
 
7190 amar.kumar 1196
            return getItemFromThriftItem(thriftItem, null, null, null, null, null, null, null, null);
4423 phani.kuma 1197
        }catch(Exception e){
1198
            logger.error("Error while adding the SimilarItems for: " + itemId, e);
1199
        }
1200
        return null;
1201
	}
4431 phani.kuma 1202
 
1203
	@Override
1204
	public Map<Long, ItemInventory> getProdItemInventory(long itemId) {
1205
		try {
1206
            // Initialize client for production server
1207
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
1208
                    ConfigClientKeys.catalog_service_server_port.toString());
5946 rajveer 1209
            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
1210
            InventoryClient inventoryServiceClient = new InventoryClient();
1211
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
1212
            in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(itemId);
4431 phani.kuma 1213
 
1214
            Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
1215
            ItemInventory warehousedata;
1216
            if(thriftItemInventory != null) {
1217
            	Map<Long, Long> availabilityMap = thriftItemInventory.getAvailability();
1218
            	Map<Long, Long> reservedMap = thriftItemInventory.getReserved();
1219
                for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
1220
                	warehousedata = new ItemInventory();
1221
                	warehousedata.setWarehouseId(availability.getKey());
1222
                	warehousedata.setAvailability(availability.getValue());
1223
                	warehousedata.setReserved(reservedMap.get(availability.getKey()));
1224
                	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
1225
                }
1226
            }
1227
            else {
1228
            	itemInventoryMap = null;
1229
            }
1230
            return itemInventoryMap;
1231
        } catch (Exception e) {
1232
            logger.error("Error while updating item's risky flag on staging", e);
1233
        }
1234
		return null;
1235
	}
4649 phani.kuma 1236
 
1237
	@Override
1238
	public boolean addAuthorizationLog(long itemId, String username, String message) {
1239
		try{
1240
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1241
            Client catalogClient = catalogServiceClient.getClient();
4649 phani.kuma 1242
 
1243
            return catalogClient.addAuthorizationLog(itemId, username, message);
1244
 
1245
        }catch(Exception e){
1246
            logger.error("Error while adding the event for: " + itemId, e);
1247
        }
1248
		return false;
1249
	}
1250
 
1251
	@Override
1252
	public Map<String, String> getConfigdataforPriceCompare() {
1253
		Map<String, String> ConfigMap = new HashMap<String, String>();
1254
		try {
1255
			ConfigMap.put("courier_cost_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_cost_factor.toString()));
1256
            ConfigMap.put("courier_weight_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_weight_factor.toString()));
1257
            ConfigMap.put("breakeven_divisor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_divisor.toString()));
1258
            ConfigMap.put("breakeven_additon_factor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_additon_factor.toString()));
1259
            ConfigMap.put("transfer_price_percentage", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_percentage.toString()));
1260
            ConfigMap.put("transfer_price_factor", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_factor.toString()));
1261
        } catch(ConfigException ce) {
1262
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1263
            ConfigMap.put("courier_cost_factor", "60");
1264
            ConfigMap.put("courier_weight_factor", "500");
1265
            ConfigMap.put("breakeven_divisor", "0.98");
1266
            ConfigMap.put("breakeven_additon_factor", "50");
1267
            ConfigMap.put("transfer_price_percentage", "2");
1268
            ConfigMap.put("transfer_price_factor", "50");
1269
        }
1270
		return ConfigMap;
1271
	}
4957 phani.kuma 1272
 
1273
	@Override
1274
	public List<String> getAllCategories() {
1275
		List<String> categoryList = new ArrayList<String>();
1276
		List<Long> parentCategoryIDs = Arrays.asList(new Long[] {(long) 0,(long) 10001,(long) 10009,(long) 10049});
1277
        try {
1278
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1279
            Client catalogClient = catalogServiceClient.getClient();
4957 phani.kuma 1280
 
1281
            List<in.shop2020.model.v1.catalog.Category> categories = catalogClient.getAllCategories();
1282
 
1283
            for(in.shop2020.model.v1.catalog.Category category : categories) {
1284
            	if(!parentCategoryIDs.contains(category.getParent_category_id())){
1285
            		categoryList.add(category.getDisplay_name());
1286
            	}
1287
            	else{
1288
            		continue;
1289
            	}
1290
            }
1291
        } catch (Exception e) {
1292
            logger.error("Error while getting all the categories: ", e);
1293
        }
1294
        return categoryList;
1295
	}
1296
 
1297
	@Override
1298
	public List<String> getAllBrands() {
1299
		List<String> brandsList = new ArrayList<String>();
1300
        try {
1301
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1302
            Client catalogClient = catalogServiceClient.getClient();
4957 phani.kuma 1303
 
1304
            brandsList = catalogClient.getAllBrands();
1305
        } catch (Exception e) {
1306
            logger.error("Error while getting all the categories: ", e);
1307
        }
1308
        return brandsList;
1309
	}
5427 amit.gupta 1310
 
5504 phani.kuma 1311
	@Override
5516 phani.kuma 1312
	public boolean deleteVoucher(Long catalogItemId, Long voucherType) {
5504 phani.kuma 1313
		try{
1314
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1315
            Client catalogClient = catalogServiceClient.getClient();
5504 phani.kuma 1316
 
1317
            return catalogClient.deleteVoucherForItem(catalogItemId, voucherType);
1318
 
1319
        }catch(Exception e){
1320
            logger.error("Error while deleting the Voucher for: " + catalogItemId, e);
1321
        }
1322
        return false;
1323
	}
1324
 
1325
	@Override
5516 phani.kuma 1326
	public boolean addVoucher(Long catalogItemId, Long voucherType, long voucherAmount) {
5504 phani.kuma 1327
		try{
1328
            CatalogClient catalogServiceClient = new CatalogClient();
5946 rajveer 1329
            Client catalogClient = catalogServiceClient.getClient();
5504 phani.kuma 1330
 
1331
            return catalogClient.addupdateVoucherForItem(catalogItemId, voucherType, voucherAmount);
1332
 
1333
        }catch(Exception e){
1334
            logger.error("Error while adding the Voucher for: " + catalogItemId, e);
1335
        }
1336
		return false;
1337
	}
1338
	@Override
5516 phani.kuma 1339
	public Map<Long, String> getvoucherTypes() {
1340
		Map<Long, String> voucherType = new HashMap<Long, String>();
5504 phani.kuma 1341
		for(VoucherType voucher : VoucherType.values())
1342
		{
5516 phani.kuma 1343
			voucherType.put((long) voucher.getValue(), voucher.name());
5504 phani.kuma 1344
		}
1345
		return voucherType;
1346
	}
1347
 
5586 phani.kuma 1348
	@Override
1349
	public boolean getConfigforentityIdMandatory() {
1350
		try {
1351
			String entityId_mandatory = ConfigClient.getClient().get(ConfigClientKeys.entityId_mandatory.toString());
1352
			if (entityId_mandatory.equals("TRUE")) {
1353
				return true;
1354
			}
1355
        } catch(ConfigException ce) {
1356
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1357
        }
1358
		return false;
1359
	}
1360
 
1361
	@Override
1362
	public boolean checkEntityId(long entityId) {
1363
		try {
1364
            CatalogClient catalogServiceClient = new CatalogClient(ConfigClientKeys.catalog_service_server_host_master.toString(), ConfigClientKeys.catalog_service_server_port.toString());
5946 rajveer 1365
            Client catalogClient = catalogServiceClient.getClient();
5586 phani.kuma 1366
            return catalogClient.isValidCatalogItemId(entityId);
1367
        } catch (Exception e) {
1368
            logger.error("Error while checking for a Catalog Item Id: ", e);
1369
        }
1370
		return false;
1371
	}
1372
 
6096 amit.gupta 1373
	@Override
1374
	public String updateExpectedDelayOnProd(Item item) {
1375
		try{
1376
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
1377
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
1378
                    ConfigClientKeys.catalog_service_server_port.toString());
1379
 
1380
            Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
1381
 
1382
 
1383
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
1384
            if(item.getExpectedDelay() != null) {
1385
            	tItem.setExpectedDelay(item.getExpectedDelay());
7182 amit.gupta 1386
            	if(item.isItemType()) {
1387
                	tItem.setType(ItemType.SERIALIZED);
1388
                }
1389
                else {
1390
                	tItem.setType(ItemType.NON_SERIALIZED);
1391
                }
6096 amit.gupta 1392
	            long rItemId;
1393
	            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
1394
	                logger.error("Error updating item on production, returned Item Id: " + rItemId);
1395
	                return "Error updating expected delay on production";
1396
	            }
6241 amit.gupta 1397
	            InventoryClient inventoryServiceClient= new InventoryClient();
1398
	            InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
6096 amit.gupta 1399
	            inventoryClient.clearItemAvailabilityCacheForItem(rItemId);
1400
	            logger.info("Successfully updated item: " + item.getId());
1401
	            return "Expected delay successfully updated on prod"; 
1402
            } else {
1403
            	return "Error:expected delay should not be null";
1404
            }
1405
        } catch (Exception e) {
6098 amit.gupta 1406
            logger.error("Error while updating expected delay on production", e);
1407
            return "Error while updating expected delay on production";
6096 amit.gupta 1408
        }
1409
	}
1410
 
6838 vikram.rag 1411
	@Override
1412
	public Map<Long, String> getAllInsurers() {
1413
		 Map<Long, String> insurersMap = new HashMap<Long, String>();
1414
	        try {
1415
	            CatalogClient catalogServiceClient = new CatalogClient();
1416
	            in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
1417
 
1418
	            List<Insurer> insurers = catalogClient.getAllInsurers();
6096 amit.gupta 1419
 
6838 vikram.rag 1420
	            for(in.shop2020.model.v1.catalog.Insurer i : insurers) {
1421
	                insurersMap.put(i.getId(), i.getName());
1422
	            }
1423
	        } catch (Exception e) {
1424
	            logger.error("Error while getting all the vendors: ", e);
1425
	        }
1426
	        return insurersMap;
1427
	}
1428
 
1429
 
6096 amit.gupta 1430
 
1431
 
1432
 
6530 vikram.rag 1433
}