Subversion Repositories SmartDukaan

Rev

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