Subversion Repositories SmartDukaan

Rev

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