Subversion Repositories SmartDukaan

Rev

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