Subversion Repositories SmartDukaan

Rev

Rev 4649 | Rev 4762 | 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;
4
import in.shop2020.catalog.dashboard.shared.Item;
4431 phani.kuma 5
import in.shop2020.catalog.dashboard.shared.ItemInventory;
3850 chandransh 6
import in.shop2020.catalog.dashboard.shared.ItemStatus;
2068 ankur.sing 7
import in.shop2020.catalog.dashboard.shared.ItemsComparator;
3558 rajveer 8
import in.shop2020.catalog.dashboard.shared.SourcePricings;
2119 ankur.sing 9
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
10
import in.shop2020.catalog.dashboard.shared.VendorPricings;
2378 ankur.sing 11
import in.shop2020.config.ConfigException;
3907 chandransh 12
import in.shop2020.content.ContentService;
13
import in.shop2020.content.ContentServiceException;
1961 ankur.sing 14
import in.shop2020.model.v1.catalog.InventoryService;
3911 chandransh 15
import in.shop2020.model.v1.catalog.SourceItemPricing;
2105 ankur.sing 16
import in.shop2020.model.v1.catalog.status;
3129 rajveer 17
import in.shop2020.thrift.clients.CatalogClient;
3907 chandransh 18
import in.shop2020.thrift.clients.ContentClient;
2378 ankur.sing 19
import in.shop2020.thrift.clients.config.ConfigClient;
2119 ankur.sing 20
import in.shop2020.utils.CategoryManager;
2359 ankur.sing 21
import in.shop2020.utils.ConfigClientKeys;
1961 ankur.sing 22
 
2427 ankur.sing 23
import java.io.BufferedReader;
24
import java.io.FileReader;
2359 ankur.sing 25
import java.io.IOException;
2427 ankur.sing 26
import java.text.DateFormat;
27
import java.text.SimpleDateFormat;
1961 ankur.sing 28
import java.util.ArrayList;
2105 ankur.sing 29
import java.util.Calendar;
2068 ankur.sing 30
import java.util.Collections;
2427 ankur.sing 31
import java.util.Date;
1992 ankur.sing 32
import java.util.HashMap;
1961 ankur.sing 33
import java.util.List;
1992 ankur.sing 34
import java.util.Map;
2359 ankur.sing 35
import java.util.Map.Entry;
1961 ankur.sing 36
 
2427 ankur.sing 37
import org.apache.log4j.Logger;
3907 chandransh 38
import org.apache.thrift.TException;
39
import org.apache.thrift.transport.TTransportException;
2359 ankur.sing 40
 
1961 ankur.sing 41
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
42
 
43
@SuppressWarnings("serial")
44
public class CatalogServiceImpl extends RemoteServiceServlet implements CatalogService {
45
 
3354 chandransh 46
    private static Logger logger = Logger.getLogger(CatalogServiceImpl.class);
47
 
3922 chandransh 48
    private static Logger pushToProdLogger = Logger.getLogger("pushToProdLogger");
49
 
2427 ankur.sing 50
    private static Date pushToProdDate;
51
 
3922 chandransh 52
    private static String logFile;
53
 
54
    private static int maxCount;
55
 
56
    static {
57
        try {
58
            logFile = ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_log_file.toString());
59
            maxCount = Integer.parseInt(ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_max_count.toString()));    
60
        } catch(ConfigException ce) {
61
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
62
            logFile = "/var/log/services/pushToProductionCount/pushToProd.log";
63
            maxCount = 5;
64
        }
65
 
66
    }
67
 
3850 chandransh 68
    @Override
69
    public int getItemCountByStatus(boolean useStatus, ItemStatus itemStatus){
70
        int count = 0;
71
        try{
72
            CatalogClient catalogServiceClient = new CatalogClient();
73
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
74
 
75
            status stat = status.findByValue(itemStatus.getValue());
76
            count = catalogClient.getItemCountByStatus(useStatus, stat);
77
        }catch(Exception e){
78
            logger.error("Error while getting the count of items from the catalog service", e);
79
        }
80
        return count;
81
    }
82
 
83
    @Override
84
    public List<Item> getAllItems(int start, int limit) {
1961 ankur.sing 85
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 86
 
1961 ankur.sing 87
        try {
3129 rajveer 88
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 89
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 90
 
3850 chandransh 91
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsInRange(start, limit);
2359 ankur.sing 92
 
2119 ankur.sing 93
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 94
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
2119 ankur.sing 95
            }
96
        } catch (Exception e) {
3354 chandransh 97
            logger.error("Error while getting all items from the catalog service", e);
2119 ankur.sing 98
        }
99
        return itemList;
100
    }
2208 ankur.sing 101
 
3850 chandransh 102
    public List<Item> getAllActiveItems(int start, int limit){
103
        return getItemsByStatus(status.ACTIVE, start, limit);
2208 ankur.sing 104
    }
2359 ankur.sing 105
 
106
    @Override
3850 chandransh 107
    public List<Item> getAllPhasedOutItems(int start, int limit){
108
        return getItemsByStatus(status.PHASED_OUT, start, limit);
2359 ankur.sing 109
    }
110
 
111
    @Override
3850 chandransh 112
    public List<Item> getAllPausedItems(int start, int limit){
113
        return getItemsByStatus(status.PAUSED, start, limit);
2208 ankur.sing 114
    }
2359 ankur.sing 115
 
116
    @Override
3850 chandransh 117
    public List<Item> getAllInProcessItems(int start, int limit) {
118
        return getItemsByStatus(status.IN_PROCESS, start, limit);
2359 ankur.sing 119
    }
120
 
121
    @Override
3850 chandransh 122
    public List<Item> getAllContentCompleteItems(int start, int limit) {
123
        return getItemsByStatus(status.CONTENT_COMPLETE, start, limit);
2359 ankur.sing 124
    }
125
 
126
    public List<Item> getBestDeals(){
2119 ankur.sing 127
        List<Item> itemList = new ArrayList<Item>();
128
        try {
3129 rajveer 129
            CatalogClient catalogServiceClient = new CatalogClient();
2119 ankur.sing 130
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 131
 
132
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestDeals();
1992 ankur.sing 133
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 134
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
1992 ankur.sing 135
            }
2359 ankur.sing 136
        } catch(Exception e){
3354 chandransh 137
            logger.error("Error while getting the best deals from the catalog service", e);
1961 ankur.sing 138
        }
2562 chandransh 139
        //Collections.sort(itemList, new ItemsComparator());
1961 ankur.sing 140
        return itemList;
141
    }
142
 
2359 ankur.sing 143
    @Override
144
    public List<Item> getRiskyItems() {
1961 ankur.sing 145
        List<Item> itemList = new ArrayList<Item>();
146
        try {
3129 rajveer 147
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 148
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 149
 
150
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByRiskyFlag();
1992 ankur.sing 151
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 152
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
1992 ankur.sing 153
            }
1961 ankur.sing 154
        } catch(Exception e){
3354 chandransh 155
            logger.error("Error while getting the risky items from the catalog service", e);
1961 ankur.sing 156
        }
2068 ankur.sing 157
        Collections.sort(itemList, new ItemsComparator());
1961 ankur.sing 158
        return itemList;
159
    }
2359 ankur.sing 160
 
1961 ankur.sing 161
    public List<Item> getBestSellers(){
162
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 163
 
1961 ankur.sing 164
        try {
3129 rajveer 165
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 166
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 167
 
1961 ankur.sing 168
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();
1992 ankur.sing 169
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 170
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
1992 ankur.sing 171
            }
1961 ankur.sing 172
        } catch(Exception e){
3354 chandransh 173
            logger.error("Error while getting the best sellers from the catalog service", e);
1961 ankur.sing 174
        }
175
        return itemList;        
176
    }
2359 ankur.sing 177
 
3872 chandransh 178
    @Override
1961 ankur.sing 179
    public List<Item> getLatestArrivals(){
180
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 181
 
1961 ankur.sing 182
        try {
3129 rajveer 183
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 184
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 185
 
1961 ankur.sing 186
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getLatestArrivals();
1992 ankur.sing 187
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
2105 ankur.sing 188
                //List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
4423 phani.kuma 189
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
1992 ankur.sing 190
            }
1961 ankur.sing 191
        } catch(Exception e){
3354 chandransh 192
            logger.error("Error while getting the latest arrivals from the catalog service", e);
1961 ankur.sing 193
        }
194
        return itemList;
195
    }
2359 ankur.sing 196
 
3872 chandransh 197
    @Override
198
    public List<Item> searchItems(int start, int limit, List<String> searchTerms) {
199
        List<Item> itemList = new ArrayList<Item>();
200
 
201
        try {
202
            CatalogClient catalogServiceClient = new CatalogClient();
203
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
204
 
205
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.searchItemsInRange(searchTerms, start, limit);
206
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 207
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
3872 chandransh 208
            }
209
        } catch(Exception e){
210
            logger.error("Error while getting the search results from the catalog service", e);
211
        }
212
        return itemList;
213
    }
214
 
215
    @Override
216
    public int getSearchResultCount(List<String> searchTerms){
217
        int count = 0;
218
        try {
219
            CatalogClient catalogServiceClient = new CatalogClient();
220
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
221
 
222
            count = catalogClient.getSearchResultCount(searchTerms);
223
        } catch(Exception e){
224
            logger.error("Error while getting the search results from the catalog service", e);
225
        }
226
 
227
        return count;
228
    }
229
 
1961 ankur.sing 230
    public Item getItem(long itemId){
231
        try{
3129 rajveer 232
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 233
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
234
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);
2359 ankur.sing 235
 
1992 ankur.sing 236
            List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
2119 ankur.sing 237
            List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 238
            List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
4423 phani.kuma 239
            List<in.shop2020.model.v1.catalog.Item> sit = catalogClient.getAllSimilarItems(thriftItem.getId());
240
            return getItemFromThriftItem(thriftItem, vip, vim, sip, sit);
1961 ankur.sing 241
        }catch(Exception e){
3354 chandransh 242
            logger.error("Error while getting the item from the catalog service", e);
1961 ankur.sing 243
        }
244
        return null;
245
    }
2359 ankur.sing 246
 
1961 ankur.sing 247
    @Override
1992 ankur.sing 248
    public boolean updateItem(Item item) {
3354 chandransh 249
        logger.info("Updating item with Id: " + item.getId());
1961 ankur.sing 250
        try{
3129 rajveer 251
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 252
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 253
 
1992 ankur.sing 254
            in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());
2126 ankur.sing 255
            setThriftItemParams(tItem, item);
2359 ankur.sing 256
 
2105 ankur.sing 257
            long rItemId;
258
            if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {
3354 chandransh 259
                logger.error("Error updating item, returned Item Id: " + rItemId);
2105 ankur.sing 260
                return false;
261
            }
3354 chandransh 262
            logger.info("Successfully updated item with id: " + item.getId());
2359 ankur.sing 263
 
264
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
2119 ankur.sing 265
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
266
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
2359 ankur.sing 267
 
268
                for(Entry<String, VendorItemMapping> e : vendorMappings.entrySet()) {
2119 ankur.sing 269
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
2359 ankur.sing 270
                    VendorItemMapping v = e.getValue();
2119 ankur.sing 271
                    tVendorMapping.setVendorId(v.getVendorId());
272
                    tVendorMapping.setItemKey(v.getItemKey());
273
                    tVendorMapping.setItemId(item.getId());
274
                    tVendorMapping.setVendorCategory(item.getVendorCategory());
2359 ankur.sing 275
                    catalogClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
3354 chandransh 276
                    logger.info("Updates VendorItemMapping: " + tVendorMapping.toString());
2119 ankur.sing 277
                }
278
            }
2359 ankur.sing 279
 
2119 ankur.sing 280
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
281
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
2105 ankur.sing 282
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
2119 ankur.sing 283
                for(VendorPricings v : vendorPricings.values()) {
284
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
2105 ankur.sing 285
                    tVendorPricing.setVendorId(v.getVendorId());
286
                    tVendorPricing.setItemId(item.getId());
287
                    tVendorPricing.setMop(v.getMop());
288
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
289
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
290
                    catalogClient.addVendorItemPricing(tVendorPricing);
3354 chandransh 291
                    logger.info("Updated VendorItemPricing: " + tVendorPricing.toString());
2105 ankur.sing 292
                }
293
            }
3558 rajveer 294
 
295
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
296
            if(sourcePricings != null && !sourcePricings.isEmpty()) {
297
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricings;
298
                for(SourcePricings s : sourcePricings.values()) {
299
                	tSourcePricings = new in.shop2020.model.v1.catalog.SourceItemPricing();
300
                	tSourcePricings.setSourceId(s.getSourceId());
301
                    tSourcePricings.setItemId(item.getId());
302
                    tSourcePricings.setMrp(s.getMrp());
303
                    tSourcePricings.setSellingPrice(s.getSellingPrice());
304
                    catalogClient.addSourceItemPricing(tSourcePricings);
305
                    logger.info("Updated SourceItemPricing: " + tSourcePricings.toString());
306
                }
307
            }
308
 
2105 ankur.sing 309
        }catch(Exception e){
3354 chandransh 310
            logger.error("Error while updating item: ", e);
2427 ankur.sing 311
            return false;
2105 ankur.sing 312
        }
313
        return true;
1961 ankur.sing 314
    }
2359 ankur.sing 315
 
316
 
2066 ankur.sing 317
    @Override
318
    public Map<Long, String> getAllVendors() {
319
        Map<Long, String> vendorMap = new HashMap<Long, String>();
320
        try {
3129 rajveer 321
            CatalogClient catalogServiceClient = new CatalogClient();
2066 ankur.sing 322
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 323
 
2066 ankur.sing 324
            List<in.shop2020.model.v1.catalog.Vendor> vendors = catalogClient.getAllVendors();
2359 ankur.sing 325
 
2066 ankur.sing 326
            for(in.shop2020.model.v1.catalog.Vendor v : vendors) {
327
                vendorMap.put(v.getId(), v.getName());
328
            }
329
        } catch (Exception e) {
3354 chandransh 330
            logger.error("Error while getting all the vendors: ", e);
2066 ankur.sing 331
        }
332
        return vendorMap;
333
    }
334
 
335
    @Override
3558 rajveer 336
    public Map<Long, String> getAllSources() {
337
        Map<Long, String> sourceMap = new HashMap<Long, String>();
338
        try {
339
            CatalogClient catalogServiceClient = new CatalogClient();
340
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
341
 
342
            List<in.shop2020.model.v1.catalog.Source> sources = catalogClient.getAllSources();
343
 
344
            for(in.shop2020.model.v1.catalog.Source s : sources) {
345
            	sourceMap.put(s.getId(), s.getName());
346
            }
347
        } catch (Exception e) {
348
            logger.error("Error while getting all the vendors: ", e);
349
        }
350
        return sourceMap;
351
    }
352
 
353
    @Override
2066 ankur.sing 354
    public Map<Long, String> getAllWarehouses() {
355
        Map<Long, String> warehouseMap = new HashMap<Long, String>();
356
        try {
3129 rajveer 357
            CatalogClient catalogServiceClient = new CatalogClient();
2066 ankur.sing 358
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 359
 
2066 ankur.sing 360
            List<in.shop2020.model.v1.catalog.Warehouse> warehouses = catalogClient.getAllWarehouses(true);
2359 ankur.sing 361
 
2066 ankur.sing 362
            for(in.shop2020.model.v1.catalog.Warehouse w : warehouses) {
363
                warehouseMap.put(w.getId(), w.getDisplayName());
364
            }
365
        } catch (Exception e) {
3354 chandransh 366
            logger.error("Error while getting all the warehouses:", e );
2066 ankur.sing 367
        }
368
        return warehouseMap;
369
    }
2105 ankur.sing 370
 
371
    @Override
372
    public long addItem(Item item) {
373
        long itemId = 0;
374
        try {
3129 rajveer 375
            CatalogClient catalogServiceClient = new CatalogClient();
2105 ankur.sing 376
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2126 ankur.sing 377
 
2359 ankur.sing 378
 
2105 ankur.sing 379
            in.shop2020.model.v1.catalog.Item tItem = new in.shop2020.model.v1.catalog.Item();
2126 ankur.sing 380
            setThriftItemParams(tItem, item);
2105 ankur.sing 381
            itemId = catalogClient.addItem(tItem);
382
 
2119 ankur.sing 383
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
384
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
2105 ankur.sing 385
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
2119 ankur.sing 386
                for(VendorPricings v : vendorPricings.values()) {
387
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
388
                    tVendorPricing.setVendorId(v.getVendorId());
389
                    tVendorPricing.setItemId(itemId);
390
                    tVendorPricing.setMop(v.getMop());
391
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
392
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
393
                    catalogClient.addVendorItemPricing(tVendorPricing);
394
                }
395
            }
2359 ankur.sing 396
 
397
            Map<String, VendorItemMapping> vendorKeysMap = item.getVendorKeysMap();
2119 ankur.sing 398
            if(vendorKeysMap != null && !vendorKeysMap.isEmpty()) {
2105 ankur.sing 399
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
2359 ankur.sing 400
                for(Entry<String, VendorItemMapping> e : vendorKeysMap.entrySet()) {
2119 ankur.sing 401
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
2359 ankur.sing 402
                    VendorItemMapping v = e.getValue();
2105 ankur.sing 403
                    tVendorMapping.setVendorId(v.getVendorId());
404
                    tVendorMapping.setItemKey(v.getItemKey());
405
                    tVendorMapping.setItemId(itemId);
406
                    tVendorMapping.setVendorCategory(item.getVendorCategory());
2359 ankur.sing 407
                    catalogClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
2105 ankur.sing 408
                }
409
            }
410
        } catch (Exception e) {
3354 chandransh 411
            logger.error("Error while adding an item: ", e);
2105 ankur.sing 412
        }
413
        return itemId;
414
    }
2119 ankur.sing 415
 
416
    @Override
4725 phani.kuma 417
    public long checkSimilarItem(String brand, String modelNumber, String modelName, String color) {
2359 ankur.sing 418
 
2119 ankur.sing 419
        try {
3129 rajveer 420
            CatalogClient catalogServiceClient = new CatalogClient();
2119 ankur.sing 421
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
4725 phani.kuma 422
            return catalogClient.checkSimilarItem(brand, modelNumber, modelName, color);
2119 ankur.sing 423
        } catch (Exception e) {
3354 chandransh 424
            logger.error("Error while checking for a similar item: ", e);
2119 ankur.sing 425
        }
426
        return 0;
427
    }
2359 ankur.sing 428
 
2126 ankur.sing 429
    private void setThriftItemParams(in.shop2020.model.v1.catalog.Item tItem, Item item) {
430
        tItem.setId(tItem.getId());
431
        tItem.setProductGroup(item.getProductGroup());
432
        tItem.setBrand(item.getBrand());
433
        tItem.setModelName(item.getModelName());
434
        tItem.setModelNumber(item.getModelNumber());
435
        tItem.setColor(item.getColor());
2359 ankur.sing 436
 
437
        tItem.setStatus_description(item.getItemStatusDesc());
2126 ankur.sing 438
        tItem.setComments(item.getComments());
2359 ankur.sing 439
 
2489 ankur.sing 440
        if(item.getMrp() != null) {
2126 ankur.sing 441
            tItem.setMrp(item.getMrp());
442
        }
3354 chandransh 443
 
2489 ankur.sing 444
        if(item.getSellingPrice() != null) {
2126 ankur.sing 445
            tItem.setSellingPrice(item.getSellingPrice());
446
        }
3354 chandransh 447
 
2489 ankur.sing 448
        if(item.getWeight() != null) {
2126 ankur.sing 449
            tItem.setWeight(item.getWeight());
450
        }
3354 chandransh 451
 
3359 chandransh 452
        if(item.getExpectedDelay() != null) {
453
            tItem.setExpectedDelay(item.getExpectedDelay());
2489 ankur.sing 454
        }
3354 chandransh 455
 
3359 chandransh 456
        if(item.getPreferredWarehouse() != null) {
457
            tItem.setPreferredWarehouse(item.getPreferredWarehouse());
2489 ankur.sing 458
        }
3354 chandransh 459
 
4413 anupam.sin 460
        if(item.getDefaultWarehouse() != null) {
461
            tItem.setDefaultWarehouse(item.getDefaultWarehouse());
462
        }
463
 
464
        tItem.setIsWarehousePreferenceSticky(item.isWarehouseStickiness());
465
 
4506 phani.kuma 466
        if(item.getPreferredVendor() != null) {
467
            tItem.setPreferredVendor(item.getPreferredVendor());
468
        }
469
 
2126 ankur.sing 470
        tItem.setBestDealText(item.getBestDealsText());
2489 ankur.sing 471
        if(item.getBestDealsValue() != null) {
2126 ankur.sing 472
            tItem.setBestDealValue(item.getBestDealsValue());
473
        }
3354 chandransh 474
 
2489 ankur.sing 475
        if(item.getBestSellingRank() != null) {
2126 ankur.sing 476
            tItem.setBestSellingRank(item.getBestSellingRank());
477
        }
3354 chandransh 478
 
2126 ankur.sing 479
        tItem.setDefaultForEntity(item.isDefaultForEntity());
2252 ankur.sing 480
        tItem.setRisky(item.isRisky());
2359 ankur.sing 481
 
2489 ankur.sing 482
        if(item.getStartDate() != null) {
483
            tItem.setStartDate(item.getStartDate());
484
        }
3354 chandransh 485
 
2489 ankur.sing 486
        if(item.getRetireDate() != null) {
487
            tItem.setRetireDate(item.getRetireDate());
488
        }
3354 chandransh 489
 
2126 ankur.sing 490
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
2489 ankur.sing 491
 
2126 ankur.sing 492
        tItem.setHotspotCategory(item.getVendorCategory());
493
    }
494
 
495
    @Override
496
    public void pauseItem(long itemId) {
497
        try {
3129 rajveer 498
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 499
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 500
 
2126 ankur.sing 501
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
502
        } catch (Exception e) {
3354 chandransh 503
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 504
        }
2359 ankur.sing 505
 
2126 ankur.sing 506
    }
507
 
508
    @Override
509
    public void markInProcess(long itemId) {
510
        try {
3129 rajveer 511
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 512
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 513
 
2126 ankur.sing 514
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
515
        } catch (Exception e) {
3354 chandransh 516
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 517
        }
518
    }
2359 ankur.sing 519
 
2126 ankur.sing 520
    @Override
521
    public void phaseoutItem(long itemId) {
522
        try {
3129 rajveer 523
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 524
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 525
 
2126 ankur.sing 526
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
527
        } catch (Exception e) {
3354 chandransh 528
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 529
        }
530
    }
2359 ankur.sing 531
 
2126 ankur.sing 532
    @Override
533
    public void activateItem(long itemId) {
534
        try {
3129 rajveer 535
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 536
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 537
 
2126 ankur.sing 538
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
539
        } catch (Exception e) {
3354 chandransh 540
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 541
        }
542
    }
2359 ankur.sing 543
 
544
    @Override
545
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
546
        try {
4354 mandeep.dh 547
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 548
            // Initialize client for staging server
3129 rajveer 549
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 550
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
551
 
552
            // Initialize client for production server
3165 chandransh 553
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 554
                    ConfigClientKeys.catalog_service_server_port.toString());
555
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
556
            catalogClient.changeItemRiskyFlag(itemId, risky);
557
 
558
            try{
559
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
560
            }catch(Exception e){
3354 chandransh 561
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 562
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
563
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
564
                return false;
565
            }
566
        } catch (Exception e) {
3354 chandransh 567
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 568
            return false;
569
        }
570
        return true;
571
    }
572
 
2427 ankur.sing 573
 
574
    /**
575
     * Retuns list of Items filtered by Vendor category (column is hotspotCategory in catalog.item table)
576
     * This list is used to generate master sheet (excel sheet)
577
     * @param category
578
     * @return
579
     */
3354 chandransh 580
    public List<Item> getItemsByVendorCategory(String category) {
2359 ankur.sing 581
        List<Item> itemList = new ArrayList<Item>();
582
        try {
3129 rajveer 583
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 584
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
585
 
586
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByVendorCategory(category);
587
 
588
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
589
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
590
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 591
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
4423 phani.kuma 592
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null));
2359 ankur.sing 593
            }
594
        } catch (Exception e) {
3354 chandransh 595
            logger.error("Error while getting items by vendor category: " + category, e);
2359 ankur.sing 596
        }
597
        Collections.sort(itemList, new ItemsComparator());
598
        return itemList;
599
    }
600
 
601
    @Override
2427 ankur.sing 602
    public String updateItemOnProduction(Item item) {
3354 chandransh 603
        logger.info("Update item on production call, Item Id: " + item.getId());
2427 ankur.sing 604
 
3922 chandransh 605
        if(!canPushToProduction()) {
606
            // If we can't push to production, then return that message to user
607
            // else move forward with update.
608
            return "Maximum push to production count reached for today";
2427 ankur.sing 609
        }
2359 ankur.sing 610
        try{
3165 chandransh 611
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
2359 ankur.sing 612
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
2498 ankur.sing 613
                    ConfigClientKeys.catalog_service_server_port.toString());
2359 ankur.sing 614
 
615
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
616
 
617
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
2427 ankur.sing 618
 
2489 ankur.sing 619
            if(item.getMrp() != null) {
2359 ankur.sing 620
                tItem.setMrp(item.getMrp());
621
            }
3354 chandransh 622
 
2489 ankur.sing 623
            if(item.getSellingPrice() != null) {
2359 ankur.sing 624
                tItem.setSellingPrice(item.getSellingPrice());
625
            }
3354 chandransh 626
 
2359 ankur.sing 627
            long rItemId;
628
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
3354 chandransh 629
                logger.error("Error updating item on production, returned Item Id: " + rItemId);
2427 ankur.sing 630
                return "Error updating item prices on production";
2359 ankur.sing 631
            }
3354 chandransh 632
            logger.info("Successfully updated item: " + item.getId());
2359 ankur.sing 633
 
3911 chandransh 634
            StringBuilder sb = new StringBuilder();
635
 
2359 ankur.sing 636
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
637
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
638
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
639
                for(VendorPricings v : vendorPricings.values()) {
640
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
641
                    tVendorPricing.setVendorId(v.getVendorId());
642
                    tVendorPricing.setItemId(item.getId());
643
                    tVendorPricing.setMop(v.getMop());
644
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
645
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
646
                    catalogClient_Prod.addVendorItemPricing(tVendorPricing);
3354 chandransh 647
                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
2489 ankur.sing 648
                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",DP=" + v.getDealerPrice() + "], ");
2359 ankur.sing 649
                }
650
            }
3911 chandransh 651
 
652
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
653
            if(sourcePricings != null && !sourcePricings.isEmpty()){
654
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
655
                for(SourcePricings s : sourcePricings.values()){
656
                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
657
                    tSourcePricing.setSourceId(s.getSourceId());
658
                    tSourcePricing.setItemId(item.getId());
659
                    tSourcePricing.setMrp(s.getMrp());
660
                    tSourcePricing.setSellingPrice(s.getSellingPrice());
661
                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
662
                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
663
                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
664
                }
665
            }
666
 
3922 chandransh 667
            pushToProdLogger.info("Id=" + item.getId() + ",MRP=" + item.getMrp() + ",SP=" + " " + item.getSellingPrice() + sb.toString());
2359 ankur.sing 668
        } catch (Exception e) {
3354 chandransh 669
            logger.error("Error while updating prices on production", e);
2427 ankur.sing 670
            return "Error while updating all prices on production. Some of the prices may have been  updated";
2359 ankur.sing 671
        }
2489 ankur.sing 672
 
3907 chandransh 673
        try {
674
            ContentClient contentServiceClient = new ContentClient();
675
            ContentService.Client contentClient = contentServiceClient.getClient();
676
 
677
            contentClient.pushContentToProduction(item.getCatalogItemId());
678
        } catch (TTransportException e) {
679
            logger.error("Error while establishing connection to the content server", e);
680
            return "Error generating content. Prices updated successfully.";
681
        } catch (TException e) {
682
            logger.error("Error while pushing content to production", e);
683
            return "Error generating content. Prices updated successfully.";
684
        } catch (ContentServiceException e) {
685
            logger.error("Error while pushing content to production", e);
686
            return "Error generating content. Prices updated successfully.";
687
        }
688
 
2427 ankur.sing 689
        return "Prices updated and content generated successfully";
2359 ankur.sing 690
    }
2427 ankur.sing 691
 
2489 ankur.sing 692
    /**
3354 chandransh 693
     * Returns list of items with a particular status.
694
     * @param st
695
     * @return
696
     */
3850 chandransh 697
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 698
        List<Item> itemList = new ArrayList<Item>();
699
        try {
700
            CatalogClient catalogServiceClient = new CatalogClient();
701
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
702
 
3850 chandransh 703
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 704
 
705
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 706
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
3354 chandransh 707
            }
708
        } catch (Exception e) {
709
            logger.error("Error while getting items by status: " + st, e);
710
        }
711
        return itemList;
712
    }
713
 
714
    /**
715
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
4423 phani.kuma 716
     * Also creates a Map each for VendorItemPricing, VendorItemMapping, SourceItemPricing and SimilarItems and adds these maps to the
3354 chandransh 717
     * new Item object.
718
     * @param thriftItem
719
     * @param tVendorPricings
720
     * @param tVendorMappings
4423 phani.kuma 721
     * @param tSourceMappings
722
     * @param tSimilarItems
3354 chandransh 723
     * @return item object with attributes copied from thrift item object.
724
     */
725
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
726
            List<in.shop2020.model.v1.catalog.VendorItemPricing> tVendorPricings, 
3558 rajveer 727
            List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings,
4423 phani.kuma 728
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings,
729
            List<in.shop2020.model.v1.catalog.Item> tSimilarItems){
3354 chandransh 730
 
731
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
732
        VendorItemMapping vItemMapping;
733
        if(tVendorMappings != null) {
734
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
735
                vItemMapping = new VendorItemMapping();
736
                vItemMapping.setVendorId(vim.getVendorId());
737
                vItemMapping.setItemKey(vim.getItemKey());
738
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
739
            }
740
        }
741
 
742
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
743
        VendorPricings vPricings;
744
        if(tVendorPricings != null) {
745
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
746
                vPricings = new VendorPricings();
747
                vPricings.setVendorId(vip.getVendorId());
748
                vPricings.setMop(vip.getMop());
749
                vPricings.setDealerPrice(vip.getDealerPrice());
750
                vPricings.setTransferPrice(vip.getTransferPrice());
751
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
752
            }
753
        }
754
 
3558 rajveer 755
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
756
        SourcePricings sPricings;
757
        if(tSourceMappings != null) {
758
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
759
                sPricings = new SourcePricings();
760
                sPricings.setSourceId(sip.getSourceId());
761
                sPricings.setMrp(sip.getMrp());
762
                sPricings.setSellingPrice(sip.getSellingPrice());
763
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
764
            }
765
        }
4423 phani.kuma 766
 
767
        Map<Long, Item> SimilarItemslist = new HashMap<Long, Item>();
768
        Item sItems;
769
        if(tSimilarItems != null) {
770
            for(in.shop2020.model.v1.catalog.Item sit : tSimilarItems) {
771
            	sItems = new Item();
772
            	sItems.setCatalogItemId(sit.getCatalogItemId());
773
            	sItems.setProductGroup(sit.getProductGroup());
774
            	sItems.setBrand(sit.getBrand());
775
            	sItems.setModelNumber(sit.getModelNumber());
776
            	sItems.setModelName(sit.getModelName());
777
            	sItems.setContentCategory(CategoryManager.getCategoryManager().getCategoryLabel(sit.getCategory()));
778
            	SimilarItemslist.put(sit.getCatalogItemId(), sItems);
779
            }
780
        }
3558 rajveer 781
 
4431 phani.kuma 782
        Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
783
        ItemInventory warehousedata;
784
        if(thriftItem.getItemInventory() != null) {
785
        	Map<Long, Long> availabilityMap = thriftItem.getItemInventory().getAvailability();
786
        	Map<Long, Long> reservedMap = thriftItem.getItemInventory().getReserved();
787
            for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
788
            	warehousedata = new ItemInventory();
789
            	warehousedata.setWarehouseId(availability.getKey());
790
            	warehousedata.setAvailability(availability.getValue());
791
            	warehousedata.setReserved(reservedMap.get(availability.getKey()));
792
            	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
793
            }
794
        }
795
        else {
796
        	itemInventoryMap = null;
797
        }
798
 
3354 chandransh 799
        Item item = new Item(thriftItem.getId(),
800
                thriftItem.getHotspotCategory(),
801
                thriftItem.getProductGroup(),
802
                thriftItem.getBrand(),
803
                thriftItem.getModelNumber(),
804
                thriftItem.getModelName(),
805
                thriftItem.getColor(),
806
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
807
                thriftItem.getCategory(),
808
                thriftItem.getComments(),
809
                thriftItem.getCatalogItemId(),
810
                thriftItem.getFeatureId(),
811
                thriftItem.getFeatureDescription(),
812
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
813
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
814
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
815
                thriftItem.getAddedOn(),
816
                thriftItem.getStartDate(),
817
                thriftItem.getRetireDate(),
818
                thriftItem.getUpdatedOn(),
819
                thriftItem.getItemStatus().name(),
820
                thriftItem.getItemStatus().getValue(),
821
                thriftItem.getStatus_description(),
822
                thriftItem.getOtherInfo(),
823
                thriftItem.getBestDealText(),
824
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
825
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
826
                thriftItem.isDefaultForEntity(),
827
                thriftItem.isRisky(),
3359 chandransh 828
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
829
                thriftItem.isSetPreferredWarehouse() ? thriftItem.getPreferredWarehouse() : null,
4413 anupam.sin 830
                thriftItem.isSetDefaultWarehouse() ? thriftItem.getDefaultWarehouse() : null,
4506 phani.kuma 831
                thriftItem.isIsWarehousePreferenceSticky(),
832
                thriftItem.isSetPreferredVendor() ? thriftItem.getPreferredVendor() : null,
4431 phani.kuma 833
                itemInventoryMap,
3354 chandransh 834
                vendorPricingMap,
3558 rajveer 835
                vItemMap,
4423 phani.kuma 836
                sourcePricingMap,
837
                SimilarItemslist);
3354 chandransh 838
        return item;
839
    }
840
 
841
    /**
2489 ankur.sing 842
     * 
3922 chandransh 843
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 844
     */
3922 chandransh 845
    private boolean canPushToProduction() {
846
        int count = getPushToProdCount(logFile);
847
        return count < maxCount;
2427 ankur.sing 848
    }
3922 chandransh 849
 
2489 ankur.sing 850
    /**
851
     * If this is the first attempt to push to production for current date, returns 0
852
     * else reads number of lines from daily rolling log file to get the count.
853
     * @return push to production count for current date
854
     */
2498 ankur.sing 855
    private int getPushToProdCount(String logfile) {
856
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
857
        // this should also be set in log4j.properties
2427 ankur.sing 858
        int lineCount = 0;
2498 ankur.sing 859
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 860
        Date currentDate = Calendar.getInstance().getTime();
861
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
862
            pushToProdDate = currentDate;
863
            return lineCount;
864
        }
865
        try {
2498 ankur.sing 866
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 867
            while (br.readLine() != null) {
868
                lineCount++;
869
            }
870
        } catch (IOException e) {
3354 chandransh 871
            logger.error("Error while reading the log file", e);
2427 ankur.sing 872
        }
873
        return lineCount;
874
    }
4423 phani.kuma 875
 
876
	@Override
877
	public boolean deleteSimilarItem(long itemId, long catalogItemId) {
878
		try{
879
            CatalogClient catalogServiceClient = new CatalogClient();
880
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
881
 
882
            return catalogClient.deleteSimilarItem(itemId, catalogItemId);
883
 
884
        }catch(Exception e){
885
            logger.error("Error while deleting the SimilarItems for: " + itemId, e);
886
        }
887
        return false;
888
	}
889
 
890
	@Override
891
	public Item addSimilarItem(long itemId, long catalogItemId) {
892
		try{
893
            CatalogClient catalogServiceClient = new CatalogClient();
894
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
895
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.addSimilarItem(itemId, catalogItemId);;
896
 
897
            return getItemFromThriftItem(thriftItem, null, null, null, null);
898
        }catch(Exception e){
899
            logger.error("Error while adding the SimilarItems for: " + itemId, e);
900
        }
901
        return null;
902
	}
4431 phani.kuma 903
 
904
	@Override
905
	public Map<Long, ItemInventory> getProdItemInventory(long itemId) {
906
		try {
907
            // Initialize client for production server
908
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
909
                    ConfigClientKeys.catalog_service_server_port.toString());
910
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
911
            in.shop2020.model.v1.catalog.ItemInventory thriftItemInventory = catalogClient_Prod.getItemInventoryByItemId(itemId);
912
 
913
            Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
914
            ItemInventory warehousedata;
915
            if(thriftItemInventory != null) {
916
            	Map<Long, Long> availabilityMap = thriftItemInventory.getAvailability();
917
            	Map<Long, Long> reservedMap = thriftItemInventory.getReserved();
918
                for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
919
                	warehousedata = new ItemInventory();
920
                	warehousedata.setWarehouseId(availability.getKey());
921
                	warehousedata.setAvailability(availability.getValue());
922
                	warehousedata.setReserved(reservedMap.get(availability.getKey()));
923
                	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
924
                }
925
            }
926
            else {
927
            	itemInventoryMap = null;
928
            }
929
            return itemInventoryMap;
930
        } catch (Exception e) {
931
            logger.error("Error while updating item's risky flag on staging", e);
932
        }
933
		return null;
934
	}
4649 phani.kuma 935
 
936
	@Override
937
	public boolean addAuthorizationLog(long itemId, String username, String message) {
938
		try{
939
            CatalogClient catalogServiceClient = new CatalogClient();
940
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
941
 
942
            return catalogClient.addAuthorizationLog(itemId, username, message);
943
 
944
        }catch(Exception e){
945
            logger.error("Error while adding the event for: " + itemId, e);
946
        }
947
		return false;
948
	}
949
 
950
	@Override
951
	public Map<String, String> getConfigdataforPriceCompare() {
952
		Map<String, String> ConfigMap = new HashMap<String, String>();
953
		try {
954
			ConfigMap.put("courier_cost_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_cost_factor.toString()));
955
            ConfigMap.put("courier_weight_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_weight_factor.toString()));
956
            ConfigMap.put("breakeven_divisor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_divisor.toString()));
957
            ConfigMap.put("breakeven_additon_factor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_additon_factor.toString()));
958
            ConfigMap.put("transfer_price_percentage", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_percentage.toString()));
959
            ConfigMap.put("transfer_price_factor", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_factor.toString()));
960
        } catch(ConfigException ce) {
961
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
962
            ConfigMap.put("courier_cost_factor", "60");
963
            ConfigMap.put("courier_weight_factor", "500");
964
            ConfigMap.put("breakeven_divisor", "0.98");
965
            ConfigMap.put("breakeven_additon_factor", "50");
966
            ConfigMap.put("transfer_price_percentage", "2");
967
            ConfigMap.put("transfer_price_factor", "50");
968
        }
969
		return ConfigMap;
970
	}
2427 ankur.sing 971
}