Subversion Repositories SmartDukaan

Rev

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