Subversion Repositories SmartDukaan

Rev

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