Subversion Repositories SmartDukaan

Rev

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