Subversion Repositories SmartDukaan

Rev

Rev 7190 | Rev 7239 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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