Subversion Repositories SmartDukaan

Rev

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