Subversion Repositories SmartDukaan

Rev

Rev 3922 | Rev 4413 | 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) {
3558 rajveer 93
                itemList.add(getItemFromThriftItem(thriftItem, 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) {
3558 rajveer 133
                itemList.add(getItemFromThriftItem(thriftItem, 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) {
3558 rajveer 151
                itemList.add(getItemFromThriftItem(thriftItem, 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) {
3558 rajveer 169
                itemList.add(getItemFromThriftItem(thriftItem, 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());
3558 rajveer 188
                itemList.add(getItemFromThriftItem(thriftItem, 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) {
206
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null));
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());
238
            return getItemFromThriftItem(thriftItem, vip, vim, sip);
1961 ankur.sing 239
        }catch(Exception e){
3354 chandransh 240
            logger.error("Error while getting the item from the catalog service", e);
1961 ankur.sing 241
        }
242
        return null;
243
    }
2359 ankur.sing 244
 
1961 ankur.sing 245
    @Override
1992 ankur.sing 246
    public boolean updateItem(Item item) {
3354 chandransh 247
        logger.info("Updating item with Id: " + item.getId());
1961 ankur.sing 248
        try{
3129 rajveer 249
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 250
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 251
 
1992 ankur.sing 252
            in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());
2126 ankur.sing 253
            setThriftItemParams(tItem, item);
2359 ankur.sing 254
 
2105 ankur.sing 255
            long rItemId;
256
            if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {
3354 chandransh 257
                logger.error("Error updating item, returned Item Id: " + rItemId);
2105 ankur.sing 258
                return false;
259
            }
3354 chandransh 260
            logger.info("Successfully updated item with id: " + item.getId());
2359 ankur.sing 261
 
262
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
2119 ankur.sing 263
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
264
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
2359 ankur.sing 265
 
266
                for(Entry<String, VendorItemMapping> e : vendorMappings.entrySet()) {
2119 ankur.sing 267
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
2359 ankur.sing 268
                    VendorItemMapping v = e.getValue();
2119 ankur.sing 269
                    tVendorMapping.setVendorId(v.getVendorId());
270
                    tVendorMapping.setItemKey(v.getItemKey());
271
                    tVendorMapping.setItemId(item.getId());
272
                    tVendorMapping.setVendorCategory(item.getVendorCategory());
2359 ankur.sing 273
                    catalogClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
3354 chandransh 274
                    logger.info("Updates VendorItemMapping: " + tVendorMapping.toString());
2119 ankur.sing 275
                }
276
            }
2359 ankur.sing 277
 
2119 ankur.sing 278
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
279
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
2105 ankur.sing 280
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
2119 ankur.sing 281
                for(VendorPricings v : vendorPricings.values()) {
282
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
2105 ankur.sing 283
                    tVendorPricing.setVendorId(v.getVendorId());
284
                    tVendorPricing.setItemId(item.getId());
285
                    tVendorPricing.setMop(v.getMop());
286
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
287
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
288
                    catalogClient.addVendorItemPricing(tVendorPricing);
3354 chandransh 289
                    logger.info("Updated VendorItemPricing: " + tVendorPricing.toString());
2105 ankur.sing 290
                }
291
            }
3558 rajveer 292
 
293
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
294
            if(sourcePricings != null && !sourcePricings.isEmpty()) {
295
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricings;
296
                for(SourcePricings s : sourcePricings.values()) {
297
                	tSourcePricings = new in.shop2020.model.v1.catalog.SourceItemPricing();
298
                	tSourcePricings.setSourceId(s.getSourceId());
299
                    tSourcePricings.setItemId(item.getId());
300
                    tSourcePricings.setMrp(s.getMrp());
301
                    tSourcePricings.setSellingPrice(s.getSellingPrice());
302
                    catalogClient.addSourceItemPricing(tSourcePricings);
303
                    logger.info("Updated SourceItemPricing: " + tSourcePricings.toString());
304
                }
305
            }
306
 
2105 ankur.sing 307
        }catch(Exception e){
3354 chandransh 308
            logger.error("Error while updating item: ", e);
2427 ankur.sing 309
            return false;
2105 ankur.sing 310
        }
311
        return true;
1961 ankur.sing 312
    }
2359 ankur.sing 313
 
314
 
2066 ankur.sing 315
    @Override
316
    public Map<Long, String> getAllVendors() {
317
        Map<Long, String> vendorMap = new HashMap<Long, String>();
318
        try {
3129 rajveer 319
            CatalogClient catalogServiceClient = new CatalogClient();
2066 ankur.sing 320
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 321
 
2066 ankur.sing 322
            List<in.shop2020.model.v1.catalog.Vendor> vendors = catalogClient.getAllVendors();
2359 ankur.sing 323
 
2066 ankur.sing 324
            for(in.shop2020.model.v1.catalog.Vendor v : vendors) {
325
                vendorMap.put(v.getId(), v.getName());
326
            }
327
        } catch (Exception e) {
3354 chandransh 328
            logger.error("Error while getting all the vendors: ", e);
2066 ankur.sing 329
        }
330
        return vendorMap;
331
    }
332
 
333
    @Override
3558 rajveer 334
    public Map<Long, String> getAllSources() {
335
        Map<Long, String> sourceMap = new HashMap<Long, String>();
336
        try {
337
            CatalogClient catalogServiceClient = new CatalogClient();
338
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
339
 
340
            List<in.shop2020.model.v1.catalog.Source> sources = catalogClient.getAllSources();
341
 
342
            for(in.shop2020.model.v1.catalog.Source s : sources) {
343
            	sourceMap.put(s.getId(), s.getName());
344
            }
345
        } catch (Exception e) {
346
            logger.error("Error while getting all the vendors: ", e);
347
        }
348
        return sourceMap;
349
    }
350
 
351
    @Override
2066 ankur.sing 352
    public Map<Long, String> getAllWarehouses() {
353
        Map<Long, String> warehouseMap = new HashMap<Long, String>();
354
        try {
3129 rajveer 355
            CatalogClient catalogServiceClient = new CatalogClient();
2066 ankur.sing 356
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 357
 
2066 ankur.sing 358
            List<in.shop2020.model.v1.catalog.Warehouse> warehouses = catalogClient.getAllWarehouses(true);
2359 ankur.sing 359
 
2066 ankur.sing 360
            for(in.shop2020.model.v1.catalog.Warehouse w : warehouses) {
361
                warehouseMap.put(w.getId(), w.getDisplayName());
362
            }
363
        } catch (Exception e) {
3354 chandransh 364
            logger.error("Error while getting all the warehouses:", e );
2066 ankur.sing 365
        }
366
        return warehouseMap;
367
    }
2105 ankur.sing 368
 
369
    @Override
370
    public long addItem(Item item) {
371
        long itemId = 0;
372
        try {
3129 rajveer 373
            CatalogClient catalogServiceClient = new CatalogClient();
2105 ankur.sing 374
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2126 ankur.sing 375
 
2359 ankur.sing 376
 
2105 ankur.sing 377
            in.shop2020.model.v1.catalog.Item tItem = new in.shop2020.model.v1.catalog.Item();
2126 ankur.sing 378
            setThriftItemParams(tItem, item);
2105 ankur.sing 379
            itemId = catalogClient.addItem(tItem);
380
 
2119 ankur.sing 381
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
382
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
2105 ankur.sing 383
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
2119 ankur.sing 384
                for(VendorPricings v : vendorPricings.values()) {
385
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
386
                    tVendorPricing.setVendorId(v.getVendorId());
387
                    tVendorPricing.setItemId(itemId);
388
                    tVendorPricing.setMop(v.getMop());
389
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
390
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
391
                    catalogClient.addVendorItemPricing(tVendorPricing);
392
                }
393
            }
2359 ankur.sing 394
 
395
            Map<String, VendorItemMapping> vendorKeysMap = item.getVendorKeysMap();
2119 ankur.sing 396
            if(vendorKeysMap != null && !vendorKeysMap.isEmpty()) {
2105 ankur.sing 397
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
2359 ankur.sing 398
                for(Entry<String, VendorItemMapping> e : vendorKeysMap.entrySet()) {
2119 ankur.sing 399
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
2359 ankur.sing 400
                    VendorItemMapping v = e.getValue();
2105 ankur.sing 401
                    tVendorMapping.setVendorId(v.getVendorId());
402
                    tVendorMapping.setItemKey(v.getItemKey());
403
                    tVendorMapping.setItemId(itemId);
404
                    tVendorMapping.setVendorCategory(item.getVendorCategory());
2359 ankur.sing 405
                    catalogClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
2105 ankur.sing 406
                }
407
            }
408
        } catch (Exception e) {
3354 chandransh 409
            logger.error("Error while adding an item: ", e);
2105 ankur.sing 410
        }
411
        return itemId;
412
    }
2119 ankur.sing 413
 
414
    @Override
415
    public long checkSimilarItem(String productGroup, String brand, String modelNumber, String color) {
2359 ankur.sing 416
 
2119 ankur.sing 417
        try {
3129 rajveer 418
            CatalogClient catalogServiceClient = new CatalogClient();
2119 ankur.sing 419
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
420
            return catalogClient.checkSimilarItem(productGroup, brand, modelNumber, color);
421
        } catch (Exception e) {
3354 chandransh 422
            logger.error("Error while checking for a similar item: ", e);
2119 ankur.sing 423
        }
424
        return 0;
425
    }
2359 ankur.sing 426
 
2126 ankur.sing 427
    private void setThriftItemParams(in.shop2020.model.v1.catalog.Item tItem, Item item) {
428
        tItem.setId(tItem.getId());
429
        tItem.setProductGroup(item.getProductGroup());
430
        tItem.setBrand(item.getBrand());
431
        tItem.setModelName(item.getModelName());
432
        tItem.setModelNumber(item.getModelNumber());
433
        tItem.setColor(item.getColor());
2359 ankur.sing 434
 
435
        tItem.setStatus_description(item.getItemStatusDesc());
2126 ankur.sing 436
        tItem.setComments(item.getComments());
2359 ankur.sing 437
 
2489 ankur.sing 438
        if(item.getMrp() != null) {
2126 ankur.sing 439
            tItem.setMrp(item.getMrp());
440
        }
3354 chandransh 441
 
2489 ankur.sing 442
        if(item.getSellingPrice() != null) {
2126 ankur.sing 443
            tItem.setSellingPrice(item.getSellingPrice());
444
        }
3354 chandransh 445
 
2489 ankur.sing 446
        if(item.getWeight() != null) {
2126 ankur.sing 447
            tItem.setWeight(item.getWeight());
448
        }
3354 chandransh 449
 
3359 chandransh 450
        if(item.getExpectedDelay() != null) {
451
            tItem.setExpectedDelay(item.getExpectedDelay());
2489 ankur.sing 452
        }
3354 chandransh 453
 
3359 chandransh 454
        if(item.getPreferredWarehouse() != null) {
455
            tItem.setPreferredWarehouse(item.getPreferredWarehouse());
2489 ankur.sing 456
        }
3354 chandransh 457
 
2126 ankur.sing 458
        tItem.setBestDealText(item.getBestDealsText());
2489 ankur.sing 459
        if(item.getBestDealsValue() != null) {
2126 ankur.sing 460
            tItem.setBestDealValue(item.getBestDealsValue());
461
        }
3354 chandransh 462
 
2489 ankur.sing 463
        if(item.getBestSellingRank() != null) {
2126 ankur.sing 464
            tItem.setBestSellingRank(item.getBestSellingRank());
465
        }
3354 chandransh 466
 
2126 ankur.sing 467
        tItem.setDefaultForEntity(item.isDefaultForEntity());
2252 ankur.sing 468
        tItem.setRisky(item.isRisky());
2359 ankur.sing 469
 
2489 ankur.sing 470
        if(item.getStartDate() != null) {
471
            tItem.setStartDate(item.getStartDate());
472
        }
3354 chandransh 473
 
2489 ankur.sing 474
        if(item.getRetireDate() != null) {
475
            tItem.setRetireDate(item.getRetireDate());
476
        }
3354 chandransh 477
 
2126 ankur.sing 478
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
2489 ankur.sing 479
 
2126 ankur.sing 480
        tItem.setHotspotCategory(item.getVendorCategory());
481
    }
482
 
483
    @Override
484
    public void pauseItem(long itemId) {
485
        try {
3129 rajveer 486
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 487
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 488
 
2126 ankur.sing 489
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
490
        } catch (Exception e) {
3354 chandransh 491
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 492
        }
2359 ankur.sing 493
 
2126 ankur.sing 494
    }
495
 
496
    @Override
497
    public void markInProcess(long itemId) {
498
        try {
3129 rajveer 499
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 500
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 501
 
2126 ankur.sing 502
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
503
        } catch (Exception e) {
3354 chandransh 504
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 505
        }
506
    }
2359 ankur.sing 507
 
2126 ankur.sing 508
    @Override
509
    public void phaseoutItem(long itemId) {
510
        try {
3129 rajveer 511
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 512
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 513
 
2126 ankur.sing 514
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
515
        } catch (Exception e) {
3354 chandransh 516
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 517
        }
518
    }
2359 ankur.sing 519
 
2126 ankur.sing 520
    @Override
521
    public void activateItem(long itemId) {
522
        try {
3129 rajveer 523
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 524
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 525
 
2126 ankur.sing 526
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
527
        } catch (Exception e) {
3354 chandransh 528
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 529
        }
530
    }
2359 ankur.sing 531
 
532
    @Override
533
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
534
        try {
4354 mandeep.dh 535
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 536
            // Initialize client for staging server
3129 rajveer 537
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 538
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
539
 
540
            // Initialize client for production server
3165 chandransh 541
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 542
                    ConfigClientKeys.catalog_service_server_port.toString());
543
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
544
            catalogClient.changeItemRiskyFlag(itemId, risky);
545
 
546
            try{
547
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
548
            }catch(Exception e){
3354 chandransh 549
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 550
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
551
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
552
                return false;
553
            }
554
        } catch (Exception e) {
3354 chandransh 555
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 556
            return false;
557
        }
558
        return true;
559
    }
560
 
2427 ankur.sing 561
 
562
    /**
563
     * Retuns list of Items filtered by Vendor category (column is hotspotCategory in catalog.item table)
564
     * This list is used to generate master sheet (excel sheet)
565
     * @param category
566
     * @return
567
     */
3354 chandransh 568
    public List<Item> getItemsByVendorCategory(String category) {
2359 ankur.sing 569
        List<Item> itemList = new ArrayList<Item>();
570
        try {
3129 rajveer 571
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 572
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
573
 
574
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByVendorCategory(category);
575
 
576
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
577
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
578
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 579
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
580
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip));
2359 ankur.sing 581
            }
582
        } catch (Exception e) {
3354 chandransh 583
            logger.error("Error while getting items by vendor category: " + category, e);
2359 ankur.sing 584
        }
585
        Collections.sort(itemList, new ItemsComparator());
586
        return itemList;
587
    }
588
 
589
    @Override
2427 ankur.sing 590
    public String updateItemOnProduction(Item item) {
3354 chandransh 591
        logger.info("Update item on production call, Item Id: " + item.getId());
2427 ankur.sing 592
 
3922 chandransh 593
        if(!canPushToProduction()) {
594
            // If we can't push to production, then return that message to user
595
            // else move forward with update.
596
            return "Maximum push to production count reached for today";
2427 ankur.sing 597
        }
2359 ankur.sing 598
        try{
3165 chandransh 599
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
2359 ankur.sing 600
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
2498 ankur.sing 601
                    ConfigClientKeys.catalog_service_server_port.toString());
2359 ankur.sing 602
 
603
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
604
 
605
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
2427 ankur.sing 606
 
2489 ankur.sing 607
            if(item.getMrp() != null) {
2359 ankur.sing 608
                tItem.setMrp(item.getMrp());
609
            }
3354 chandransh 610
 
2489 ankur.sing 611
            if(item.getSellingPrice() != null) {
2359 ankur.sing 612
                tItem.setSellingPrice(item.getSellingPrice());
613
            }
3354 chandransh 614
 
2359 ankur.sing 615
            long rItemId;
616
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
3354 chandransh 617
                logger.error("Error updating item on production, returned Item Id: " + rItemId);
2427 ankur.sing 618
                return "Error updating item prices on production";
2359 ankur.sing 619
            }
3354 chandransh 620
            logger.info("Successfully updated item: " + item.getId());
2359 ankur.sing 621
 
3911 chandransh 622
            StringBuilder sb = new StringBuilder();
623
 
2359 ankur.sing 624
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
625
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
626
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
627
                for(VendorPricings v : vendorPricings.values()) {
628
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
629
                    tVendorPricing.setVendorId(v.getVendorId());
630
                    tVendorPricing.setItemId(item.getId());
631
                    tVendorPricing.setMop(v.getMop());
632
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
633
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
634
                    catalogClient_Prod.addVendorItemPricing(tVendorPricing);
3354 chandransh 635
                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
2489 ankur.sing 636
                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",DP=" + v.getDealerPrice() + "], ");
2359 ankur.sing 637
                }
638
            }
3911 chandransh 639
 
640
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
641
            if(sourcePricings != null && !sourcePricings.isEmpty()){
642
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
643
                for(SourcePricings s : sourcePricings.values()){
644
                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
645
                    tSourcePricing.setSourceId(s.getSourceId());
646
                    tSourcePricing.setItemId(item.getId());
647
                    tSourcePricing.setMrp(s.getMrp());
648
                    tSourcePricing.setSellingPrice(s.getSellingPrice());
649
                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
650
                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
651
                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
652
                }
653
            }
654
 
3922 chandransh 655
            pushToProdLogger.info("Id=" + item.getId() + ",MRP=" + item.getMrp() + ",SP=" + " " + item.getSellingPrice() + sb.toString());
2359 ankur.sing 656
        } catch (Exception e) {
3354 chandransh 657
            logger.error("Error while updating prices on production", e);
2427 ankur.sing 658
            return "Error while updating all prices on production. Some of the prices may have been  updated";
2359 ankur.sing 659
        }
2489 ankur.sing 660
 
3907 chandransh 661
        try {
662
            ContentClient contentServiceClient = new ContentClient();
663
            ContentService.Client contentClient = contentServiceClient.getClient();
664
 
665
            contentClient.pushContentToProduction(item.getCatalogItemId());
666
        } catch (TTransportException e) {
667
            logger.error("Error while establishing connection to the content server", e);
668
            return "Error generating content. Prices updated successfully.";
669
        } catch (TException e) {
670
            logger.error("Error while pushing content to production", e);
671
            return "Error generating content. Prices updated successfully.";
672
        } catch (ContentServiceException e) {
673
            logger.error("Error while pushing content to production", e);
674
            return "Error generating content. Prices updated successfully.";
675
        }
676
 
2427 ankur.sing 677
        return "Prices updated and content generated successfully";
2359 ankur.sing 678
    }
2427 ankur.sing 679
 
2489 ankur.sing 680
    /**
3354 chandransh 681
     * Returns list of items with a particular status.
682
     * @param st
683
     * @return
684
     */
3850 chandransh 685
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 686
        List<Item> itemList = new ArrayList<Item>();
687
        try {
688
            CatalogClient catalogServiceClient = new CatalogClient();
689
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
690
 
3850 chandransh 691
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 692
 
693
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
3558 rajveer 694
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null));
3354 chandransh 695
            }
696
        } catch (Exception e) {
697
            logger.error("Error while getting items by status: " + st, e);
698
        }
699
        return itemList;
700
    }
701
 
702
    /**
703
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
704
     * Also creates a Map each for VendorItemPricing and VendorItemMapping and adds these maps to the
705
     * new Item object.
706
     * @param thriftItem
707
     * @param tVendorPricings
708
     * @param tVendorMappings
709
     * @return item object with attributes copied from thrift item object.
710
     */
711
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
712
            List<in.shop2020.model.v1.catalog.VendorItemPricing> tVendorPricings, 
3558 rajveer 713
            List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings,
714
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings){
3354 chandransh 715
 
716
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
717
        VendorItemMapping vItemMapping;
718
        if(tVendorMappings != null) {
719
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
720
                vItemMapping = new VendorItemMapping();
721
                vItemMapping.setVendorId(vim.getVendorId());
722
                vItemMapping.setItemKey(vim.getItemKey());
723
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
724
            }
725
        }
726
 
727
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
728
        VendorPricings vPricings;
729
        if(tVendorPricings != null) {
730
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
731
                vPricings = new VendorPricings();
732
                vPricings.setVendorId(vip.getVendorId());
733
                vPricings.setMop(vip.getMop());
734
                vPricings.setDealerPrice(vip.getDealerPrice());
735
                vPricings.setTransferPrice(vip.getTransferPrice());
736
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
737
            }
738
        }
739
 
3558 rajveer 740
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
741
        SourcePricings sPricings;
742
        if(tSourceMappings != null) {
743
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
744
                sPricings = new SourcePricings();
745
                sPricings.setSourceId(sip.getSourceId());
746
                sPricings.setMrp(sip.getMrp());
747
                sPricings.setSellingPrice(sip.getSellingPrice());
748
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
749
            }
750
        }
751
 
3354 chandransh 752
        Item item = new Item(thriftItem.getId(),
753
                thriftItem.getHotspotCategory(),
754
                thriftItem.getProductGroup(),
755
                thriftItem.getBrand(),
756
                thriftItem.getModelNumber(),
757
                thriftItem.getModelName(),
758
                thriftItem.getColor(),
759
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
760
                thriftItem.getCategory(),
761
                thriftItem.getComments(),
762
                thriftItem.getCatalogItemId(),
763
                thriftItem.getFeatureId(),
764
                thriftItem.getFeatureDescription(),
765
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
766
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
767
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
768
                thriftItem.getAddedOn(),
769
                thriftItem.getStartDate(),
770
                thriftItem.getRetireDate(),
771
                thriftItem.getUpdatedOn(),
772
                thriftItem.getItemStatus().name(),
773
                thriftItem.getItemStatus().getValue(),
774
                thriftItem.getStatus_description(),
775
                thriftItem.getOtherInfo(),
776
                thriftItem.getBestDealText(),
777
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
778
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
779
                thriftItem.isDefaultForEntity(),
780
                thriftItem.isRisky(),
3359 chandransh 781
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
782
                thriftItem.isSetPreferredWarehouse() ? thriftItem.getPreferredWarehouse() : null,
3354 chandransh 783
                (thriftItem.getItemInventory() != null ? thriftItem.getItemInventory().getAvailability() : null),
784
                vendorPricingMap,
3558 rajveer 785
                vItemMap,
786
                sourcePricingMap);
3354 chandransh 787
        return item;
788
    }
789
 
790
    /**
2489 ankur.sing 791
     * 
3922 chandransh 792
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 793
     */
3922 chandransh 794
    private boolean canPushToProduction() {
795
        int count = getPushToProdCount(logFile);
796
        return count < maxCount;
2427 ankur.sing 797
    }
3922 chandransh 798
 
2489 ankur.sing 799
    /**
800
     * If this is the first attempt to push to production for current date, returns 0
801
     * else reads number of lines from daily rolling log file to get the count.
802
     * @return push to production count for current date
803
     */
2498 ankur.sing 804
    private int getPushToProdCount(String logfile) {
805
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
806
        // this should also be set in log4j.properties
2427 ankur.sing 807
        int lineCount = 0;
2498 ankur.sing 808
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 809
        Date currentDate = Calendar.getInstance().getTime();
810
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
811
            pushToProdDate = currentDate;
812
            return lineCount;
813
        }
814
        try {
2498 ankur.sing 815
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 816
            while (br.readLine() != null) {
817
                lineCount++;
818
            }
819
        } catch (IOException e) {
3354 chandransh 820
            logger.error("Error while reading the log file", e);
2427 ankur.sing 821
        }
822
        return lineCount;
823
    }
824
}