Subversion Repositories SmartDukaan

Rev

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