Subversion Repositories SmartDukaan

Rev

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