Subversion Repositories SmartDukaan

Rev

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