Subversion Repositories SmartDukaan

Rev

Rev 5118 | Rev 5309 | 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
 
5217 amit.gupta 512
        if(item.getExpectedArrivalDate() != null) {
513
        	tItem.setExpectedArrivalDate(item.getExpectedArrivalDate());
514
        } else {
515
        	tItem.unsetComingSoonStartDate();
516
        	tItem.unsetExpectedArrivalDate();
517
        	if(status.COMING_SOON.equals(tItem.getItemStatus())){
518
        		tItem.setItemStatus(status.IN_PROCESS);
519
        	}
520
        }
521
 
522
        if(item.getComingSoonStartDate() != null) {
523
        	tItem.setComingSoonStartDate(item.getComingSoonStartDate());
524
        } else {
525
        	tItem.unsetComingSoonStartDate();
526
        }
527
 
2489 ankur.sing 528
        if(item.getRetireDate() != null) {
529
            tItem.setRetireDate(item.getRetireDate());
530
        }
3354 chandransh 531
 
2126 ankur.sing 532
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
533
    }
534
 
535
    @Override
536
    public void pauseItem(long itemId) {
537
        try {
3129 rajveer 538
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 539
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 540
 
2126 ankur.sing 541
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
542
        } catch (Exception e) {
3354 chandransh 543
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 544
        }
2359 ankur.sing 545
 
2126 ankur.sing 546
    }
547
 
548
    @Override
549
    public void markInProcess(long itemId) {
550
        try {
3129 rajveer 551
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 552
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 553
 
2126 ankur.sing 554
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
555
        } catch (Exception e) {
3354 chandransh 556
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 557
        }
558
    }
2359 ankur.sing 559
 
2126 ankur.sing 560
    @Override
561
    public void phaseoutItem(long itemId) {
562
        try {
3129 rajveer 563
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 564
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 565
 
2126 ankur.sing 566
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
567
        } catch (Exception e) {
3354 chandransh 568
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 569
        }
570
    }
2359 ankur.sing 571
 
2126 ankur.sing 572
    @Override
573
    public void activateItem(long itemId) {
574
        try {
3129 rajveer 575
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 576
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 577
 
2126 ankur.sing 578
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
579
        } catch (Exception e) {
3354 chandransh 580
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 581
        }
582
    }
2359 ankur.sing 583
 
584
    @Override
585
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
586
        try {
4354 mandeep.dh 587
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 588
            // Initialize client for staging server
3129 rajveer 589
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 590
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
591
 
592
            // Initialize client for production server
3165 chandransh 593
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 594
                    ConfigClientKeys.catalog_service_server_port.toString());
595
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
596
            catalogClient.changeItemRiskyFlag(itemId, risky);
597
 
598
            try{
599
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
600
            }catch(Exception e){
3354 chandransh 601
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 602
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
603
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
604
                return false;
605
            }
606
        } catch (Exception e) {
3354 chandransh 607
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 608
            return false;
609
        }
610
        return true;
611
    }
612
 
2427 ankur.sing 613
 
614
    /**
4762 phani.kuma 615
     * Retuns list of Items filtered by category (column is product_group in catalog.item table)
2427 ankur.sing 616
     * This list is used to generate master sheet (excel sheet)
617
     * @param category
4957 phani.kuma 618
     * @param brand
2427 ankur.sing 619
     * @return
620
     */
4957 phani.kuma 621
    public List<Item> getItemsForMasterSheet(String category, String brand) {
2359 ankur.sing 622
        List<Item> itemList = new ArrayList<Item>();
623
        try {
3129 rajveer 624
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 625
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
626
 
4957 phani.kuma 627
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsForMasterSheet(category, brand);
2359 ankur.sing 628
 
629
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
630
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
631
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 632
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
4423 phani.kuma 633
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null));
2359 ankur.sing 634
            }
635
        } catch (Exception e) {
4762 phani.kuma 636
            logger.error("Error while getting items by category: " + category, e);
2359 ankur.sing 637
        }
638
        Collections.sort(itemList, new ItemsComparator());
639
        return itemList;
640
    }
641
 
642
    @Override
2427 ankur.sing 643
    public String updateItemOnProduction(Item item) {
3354 chandransh 644
        logger.info("Update item on production call, Item Id: " + item.getId());
2427 ankur.sing 645
 
3922 chandransh 646
        if(!canPushToProduction()) {
647
            // If we can't push to production, then return that message to user
648
            // else move forward with update.
649
            return "Maximum push to production count reached for today";
2427 ankur.sing 650
        }
2359 ankur.sing 651
        try{
3165 chandransh 652
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
2359 ankur.sing 653
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
2498 ankur.sing 654
                    ConfigClientKeys.catalog_service_server_port.toString());
2359 ankur.sing 655
 
656
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
657
 
658
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
2427 ankur.sing 659
 
2489 ankur.sing 660
            if(item.getMrp() != null) {
2359 ankur.sing 661
                tItem.setMrp(item.getMrp());
662
            }
3354 chandransh 663
 
2489 ankur.sing 664
            if(item.getSellingPrice() != null) {
2359 ankur.sing 665
                tItem.setSellingPrice(item.getSellingPrice());
666
            }
3354 chandransh 667
 
2359 ankur.sing 668
            long rItemId;
669
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
3354 chandransh 670
                logger.error("Error updating item on production, returned Item Id: " + rItemId);
2427 ankur.sing 671
                return "Error updating item prices on production";
2359 ankur.sing 672
            }
3354 chandransh 673
            logger.info("Successfully updated item: " + item.getId());
2359 ankur.sing 674
 
3911 chandransh 675
            StringBuilder sb = new StringBuilder();
676
 
2359 ankur.sing 677
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
678
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
679
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
680
                for(VendorPricings v : vendorPricings.values()) {
681
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
682
                    tVendorPricing.setVendorId(v.getVendorId());
683
                    tVendorPricing.setItemId(item.getId());
684
                    tVendorPricing.setMop(v.getMop());
685
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
686
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
687
                    catalogClient_Prod.addVendorItemPricing(tVendorPricing);
3354 chandransh 688
                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
2489 ankur.sing 689
                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",DP=" + v.getDealerPrice() + "], ");
2359 ankur.sing 690
                }
691
            }
3911 chandransh 692
 
4993 mandeep.dh 693
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
694
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
695
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
696
                for(VendorItemMapping vendorItemMapping : vendorMappings.values()) {
697
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
698
                    tVendorMapping.setVendorId(vendorItemMapping.getVendorId());
699
                    tVendorMapping.setItemId(item.getId());
700
                    tVendorMapping.setItemKey(vendorItemMapping.getItemKey());
701
                    catalogClient_Prod.addVendorItemMapping(vendorItemMapping.getItemKey(), tVendorMapping);
702
                    logger.info("VendorItemMapping updated. " + tVendorMapping.toString());
703
                    sb.append("[VId:" + vendorItemMapping.getVendorId() + ",ItemKey=" + vendorItemMapping.getItemKey() + "], ");
704
                }
705
            }
706
 
3911 chandransh 707
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
708
            if(sourcePricings != null && !sourcePricings.isEmpty()){
709
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
710
                for(SourcePricings s : sourcePricings.values()){
711
                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
712
                    tSourcePricing.setSourceId(s.getSourceId());
713
                    tSourcePricing.setItemId(item.getId());
714
                    tSourcePricing.setMrp(s.getMrp());
715
                    tSourcePricing.setSellingPrice(s.getSellingPrice());
716
                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
717
                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
718
                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
719
                }
720
            }
721
 
3922 chandransh 722
            pushToProdLogger.info("Id=" + item.getId() + ",MRP=" + item.getMrp() + ",SP=" + " " + item.getSellingPrice() + sb.toString());
2359 ankur.sing 723
        } catch (Exception e) {
3354 chandransh 724
            logger.error("Error while updating prices on production", e);
2427 ankur.sing 725
            return "Error while updating all prices on production. Some of the prices may have been  updated";
2359 ankur.sing 726
        }
2489 ankur.sing 727
 
3907 chandransh 728
        try {
729
            ContentClient contentServiceClient = new ContentClient();
730
            ContentService.Client contentClient = contentServiceClient.getClient();
731
 
732
            contentClient.pushContentToProduction(item.getCatalogItemId());
733
        } catch (TTransportException e) {
734
            logger.error("Error while establishing connection to the content server", e);
735
            return "Error generating content. Prices updated successfully.";
736
        } catch (TException e) {
737
            logger.error("Error while pushing content to production", e);
738
            return "Error generating content. Prices updated successfully.";
739
        } catch (ContentServiceException e) {
740
            logger.error("Error while pushing content to production", e);
741
            return "Error generating content. Prices updated successfully.";
742
        }
743
 
2427 ankur.sing 744
        return "Prices updated and content generated successfully";
2359 ankur.sing 745
    }
2427 ankur.sing 746
 
2489 ankur.sing 747
    /**
3354 chandransh 748
     * Returns list of items with a particular status.
749
     * @param st
750
     * @return
751
     */
3850 chandransh 752
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 753
        List<Item> itemList = new ArrayList<Item>();
754
        try {
755
            CatalogClient catalogServiceClient = new CatalogClient();
756
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
757
 
3850 chandransh 758
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 759
 
760
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
4423 phani.kuma 761
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null));
3354 chandransh 762
            }
763
        } catch (Exception e) {
764
            logger.error("Error while getting items by status: " + st, e);
765
        }
766
        return itemList;
767
    }
768
 
769
    /**
770
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
4423 phani.kuma 771
     * Also creates a Map each for VendorItemPricing, VendorItemMapping, SourceItemPricing and SimilarItems and adds these maps to the
3354 chandransh 772
     * new Item object.
773
     * @param thriftItem
774
     * @param tVendorPricings
775
     * @param tVendorMappings
4423 phani.kuma 776
     * @param tSourceMappings
777
     * @param tSimilarItems
3354 chandransh 778
     * @return item object with attributes copied from thrift item object.
779
     */
780
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
781
            List<in.shop2020.model.v1.catalog.VendorItemPricing> tVendorPricings, 
3558 rajveer 782
            List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings,
4423 phani.kuma 783
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings,
784
            List<in.shop2020.model.v1.catalog.Item> tSimilarItems){
3354 chandransh 785
 
786
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
787
        VendorItemMapping vItemMapping;
788
        if(tVendorMappings != null) {
789
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
790
                vItemMapping = new VendorItemMapping();
791
                vItemMapping.setVendorId(vim.getVendorId());
792
                vItemMapping.setItemKey(vim.getItemKey());
793
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
794
            }
795
        }
796
 
797
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
798
        VendorPricings vPricings;
799
        if(tVendorPricings != null) {
800
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
801
                vPricings = new VendorPricings();
802
                vPricings.setVendorId(vip.getVendorId());
803
                vPricings.setMop(vip.getMop());
804
                vPricings.setDealerPrice(vip.getDealerPrice());
805
                vPricings.setTransferPrice(vip.getTransferPrice());
806
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
807
            }
808
        }
809
 
3558 rajveer 810
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
811
        SourcePricings sPricings;
812
        if(tSourceMappings != null) {
813
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
814
                sPricings = new SourcePricings();
815
                sPricings.setSourceId(sip.getSourceId());
816
                sPricings.setMrp(sip.getMrp());
817
                sPricings.setSellingPrice(sip.getSellingPrice());
818
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
819
            }
820
        }
4423 phani.kuma 821
 
822
        Map<Long, Item> SimilarItemslist = new HashMap<Long, Item>();
823
        Item sItems;
824
        if(tSimilarItems != null) {
825
            for(in.shop2020.model.v1.catalog.Item sit : tSimilarItems) {
826
            	sItems = new Item();
827
            	sItems.setCatalogItemId(sit.getCatalogItemId());
828
            	sItems.setProductGroup(sit.getProductGroup());
829
            	sItems.setBrand(sit.getBrand());
830
            	sItems.setModelNumber(sit.getModelNumber());
831
            	sItems.setModelName(sit.getModelName());
832
            	sItems.setContentCategory(CategoryManager.getCategoryManager().getCategoryLabel(sit.getCategory()));
833
            	SimilarItemslist.put(sit.getCatalogItemId(), sItems);
834
            }
835
        }
3558 rajveer 836
 
4431 phani.kuma 837
        Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
838
        ItemInventory warehousedata;
839
        if(thriftItem.getItemInventory() != null) {
840
        	Map<Long, Long> availabilityMap = thriftItem.getItemInventory().getAvailability();
841
        	Map<Long, Long> reservedMap = thriftItem.getItemInventory().getReserved();
842
            for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
843
            	warehousedata = new ItemInventory();
844
            	warehousedata.setWarehouseId(availability.getKey());
845
            	warehousedata.setAvailability(availability.getValue());
846
            	warehousedata.setReserved(reservedMap.get(availability.getKey()));
847
            	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
848
            }
849
        }
850
        else {
851
        	itemInventoryMap = null;
852
        }
853
 
3354 chandransh 854
        Item item = new Item(thriftItem.getId(),
855
                thriftItem.getProductGroup(),
856
                thriftItem.getBrand(),
857
                thriftItem.getModelNumber(),
858
                thriftItem.getModelName(),
859
                thriftItem.getColor(),
860
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
861
                thriftItem.getCategory(),
862
                thriftItem.getComments(),
863
                thriftItem.getCatalogItemId(),
864
                thriftItem.getFeatureId(),
865
                thriftItem.getFeatureDescription(),
866
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
867
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
868
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
869
                thriftItem.getAddedOn(),
870
                thriftItem.getStartDate(),
5217 amit.gupta 871
                thriftItem.isSetComingSoonStartDate() ? thriftItem.getComingSoonStartDate() : null ,
872
                thriftItem.isSetExpectedArrivalDate() ? thriftItem.getExpectedArrivalDate() : null ,
3354 chandransh 873
                thriftItem.getRetireDate(),
874
                thriftItem.getUpdatedOn(),
875
                thriftItem.getItemStatus().name(),
876
                thriftItem.getItemStatus().getValue(),
877
                thriftItem.getStatus_description(),
878
                thriftItem.getOtherInfo(),
879
                thriftItem.getBestDealText(),
880
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
881
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
882
                thriftItem.isDefaultForEntity(),
883
                thriftItem.isRisky(),
3359 chandransh 884
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
885
                thriftItem.isSetPreferredWarehouse() ? thriftItem.getPreferredWarehouse() : null,
4413 anupam.sin 886
                thriftItem.isSetDefaultWarehouse() ? thriftItem.getDefaultWarehouse() : null,
4506 phani.kuma 887
                thriftItem.isIsWarehousePreferenceSticky(),
888
                thriftItem.isSetPreferredVendor() ? thriftItem.getPreferredVendor() : null,
4431 phani.kuma 889
                itemInventoryMap,
3354 chandransh 890
                vendorPricingMap,
3558 rajveer 891
                vItemMap,
4423 phani.kuma 892
                sourcePricingMap,
893
                SimilarItemslist);
3354 chandransh 894
        return item;
895
    }
896
 
897
    /**
2489 ankur.sing 898
     * 
3922 chandransh 899
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 900
     */
3922 chandransh 901
    private boolean canPushToProduction() {
902
        int count = getPushToProdCount(logFile);
903
        return count < maxCount;
2427 ankur.sing 904
    }
3922 chandransh 905
 
2489 ankur.sing 906
    /**
907
     * If this is the first attempt to push to production for current date, returns 0
908
     * else reads number of lines from daily rolling log file to get the count.
909
     * @return push to production count for current date
910
     */
2498 ankur.sing 911
    private int getPushToProdCount(String logfile) {
912
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
913
        // this should also be set in log4j.properties
2427 ankur.sing 914
        int lineCount = 0;
2498 ankur.sing 915
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 916
        Date currentDate = Calendar.getInstance().getTime();
917
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
918
            pushToProdDate = currentDate;
919
            return lineCount;
920
        }
921
        try {
2498 ankur.sing 922
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 923
            while (br.readLine() != null) {
924
                lineCount++;
925
            }
926
        } catch (IOException e) {
3354 chandransh 927
            logger.error("Error while reading the log file", e);
2427 ankur.sing 928
        }
929
        return lineCount;
930
    }
4423 phani.kuma 931
 
932
	@Override
933
	public boolean deleteSimilarItem(long itemId, long catalogItemId) {
934
		try{
935
            CatalogClient catalogServiceClient = new CatalogClient();
936
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
937
 
938
            return catalogClient.deleteSimilarItem(itemId, catalogItemId);
939
 
940
        }catch(Exception e){
941
            logger.error("Error while deleting the SimilarItems for: " + itemId, e);
942
        }
943
        return false;
944
	}
945
 
946
	@Override
947
	public Item addSimilarItem(long itemId, long catalogItemId) {
948
		try{
949
            CatalogClient catalogServiceClient = new CatalogClient();
950
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
951
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.addSimilarItem(itemId, catalogItemId);;
952
 
953
            return getItemFromThriftItem(thriftItem, null, null, null, null);
954
        }catch(Exception e){
955
            logger.error("Error while adding the SimilarItems for: " + itemId, e);
956
        }
957
        return null;
958
	}
4431 phani.kuma 959
 
960
	@Override
961
	public Map<Long, ItemInventory> getProdItemInventory(long itemId) {
962
		try {
963
            // Initialize client for production server
964
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
965
                    ConfigClientKeys.catalog_service_server_port.toString());
966
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
967
            in.shop2020.model.v1.catalog.ItemInventory thriftItemInventory = catalogClient_Prod.getItemInventoryByItemId(itemId);
968
 
969
            Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
970
            ItemInventory warehousedata;
971
            if(thriftItemInventory != null) {
972
            	Map<Long, Long> availabilityMap = thriftItemInventory.getAvailability();
973
            	Map<Long, Long> reservedMap = thriftItemInventory.getReserved();
974
                for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
975
                	warehousedata = new ItemInventory();
976
                	warehousedata.setWarehouseId(availability.getKey());
977
                	warehousedata.setAvailability(availability.getValue());
978
                	warehousedata.setReserved(reservedMap.get(availability.getKey()));
979
                	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
980
                }
981
            }
982
            else {
983
            	itemInventoryMap = null;
984
            }
985
            return itemInventoryMap;
986
        } catch (Exception e) {
987
            logger.error("Error while updating item's risky flag on staging", e);
988
        }
989
		return null;
990
	}
4649 phani.kuma 991
 
992
	@Override
993
	public boolean addAuthorizationLog(long itemId, String username, String message) {
994
		try{
995
            CatalogClient catalogServiceClient = new CatalogClient();
996
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
997
 
998
            return catalogClient.addAuthorizationLog(itemId, username, message);
999
 
1000
        }catch(Exception e){
1001
            logger.error("Error while adding the event for: " + itemId, e);
1002
        }
1003
		return false;
1004
	}
1005
 
1006
	@Override
1007
	public Map<String, String> getConfigdataforPriceCompare() {
1008
		Map<String, String> ConfigMap = new HashMap<String, String>();
1009
		try {
1010
			ConfigMap.put("courier_cost_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_cost_factor.toString()));
1011
            ConfigMap.put("courier_weight_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_weight_factor.toString()));
1012
            ConfigMap.put("breakeven_divisor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_divisor.toString()));
1013
            ConfigMap.put("breakeven_additon_factor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_additon_factor.toString()));
1014
            ConfigMap.put("transfer_price_percentage", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_percentage.toString()));
1015
            ConfigMap.put("transfer_price_factor", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_factor.toString()));
1016
        } catch(ConfigException ce) {
1017
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1018
            ConfigMap.put("courier_cost_factor", "60");
1019
            ConfigMap.put("courier_weight_factor", "500");
1020
            ConfigMap.put("breakeven_divisor", "0.98");
1021
            ConfigMap.put("breakeven_additon_factor", "50");
1022
            ConfigMap.put("transfer_price_percentage", "2");
1023
            ConfigMap.put("transfer_price_factor", "50");
1024
        }
1025
		return ConfigMap;
1026
	}
4957 phani.kuma 1027
 
1028
	@Override
1029
	public List<String> getAllCategories() {
1030
		List<String> categoryList = new ArrayList<String>();
1031
		List<Long> parentCategoryIDs = Arrays.asList(new Long[] {(long) 0,(long) 10001,(long) 10009,(long) 10049});
1032
        try {
1033
            CatalogClient catalogServiceClient = new CatalogClient();
1034
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1035
 
1036
            List<in.shop2020.model.v1.catalog.Category> categories = catalogClient.getAllCategories();
1037
 
1038
            for(in.shop2020.model.v1.catalog.Category category : categories) {
1039
            	if(!parentCategoryIDs.contains(category.getParent_category_id())){
1040
            		categoryList.add(category.getDisplay_name());
1041
            	}
1042
            	else{
1043
            		continue;
1044
            	}
1045
            }
1046
        } catch (Exception e) {
1047
            logger.error("Error while getting all the categories: ", e);
1048
        }
1049
        return categoryList;
1050
	}
1051
 
1052
	@Override
1053
	public List<String> getAllBrands() {
1054
		List<String> brandsList = new ArrayList<String>();
1055
        try {
1056
            CatalogClient catalogServiceClient = new CatalogClient();
1057
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1058
 
1059
            brandsList = catalogClient.getAllBrands();
1060
        } catch (Exception e) {
1061
            logger.error("Error while getting all the categories: ", e);
1062
        }
1063
        return brandsList;
1064
	}
2427 ankur.sing 1065
}