Subversion Repositories SmartDukaan

Rev

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