Subversion Repositories SmartDukaan

Rev

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