Subversion Repositories SmartDukaan

Rev

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