Subversion Repositories SmartDukaan

Rev

Rev 3165 | Rev 3359 | 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
 
2489 ankur.sing 341
        if(item.getMop() != null) {
342
            tItem.setMop(item.getMop());
343
        }
3354 chandransh 344
 
2489 ankur.sing 345
        if(item.getDealerPrice() != null) {
346
            tItem.setDealerPrice(item.getDealerPrice());
347
        }
3354 chandransh 348
 
2489 ankur.sing 349
        if(item.getTransferPrice() != null) {
350
            tItem.setTransferPrice(item.getTransferPrice());
351
        }
3354 chandransh 352
 
2126 ankur.sing 353
        tItem.setBestDealText(item.getBestDealsText());
2489 ankur.sing 354
        if(item.getBestDealsValue() != null) {
2126 ankur.sing 355
            tItem.setBestDealValue(item.getBestDealsValue());
356
        }
3354 chandransh 357
 
2489 ankur.sing 358
        if(item.getBestSellingRank() != null) {
2126 ankur.sing 359
            tItem.setBestSellingRank(item.getBestSellingRank());
360
        }
3354 chandransh 361
 
2126 ankur.sing 362
        tItem.setDefaultForEntity(item.isDefaultForEntity());
2252 ankur.sing 363
        tItem.setRisky(item.isRisky());
2359 ankur.sing 364
 
2489 ankur.sing 365
        if(item.getStartDate() != null) {
366
            tItem.setStartDate(item.getStartDate());
367
        }
3354 chandransh 368
 
2489 ankur.sing 369
        if(item.getRetireDate() != null) {
370
            tItem.setRetireDate(item.getRetireDate());
371
        }
3354 chandransh 372
 
2126 ankur.sing 373
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
2489 ankur.sing 374
 
2126 ankur.sing 375
        tItem.setHotspotCategory(item.getVendorCategory());
376
    }
377
 
378
    @Override
379
    public void pauseItem(long itemId) {
380
        try {
3129 rajveer 381
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 382
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 383
 
2126 ankur.sing 384
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
385
        } catch (Exception e) {
3354 chandransh 386
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 387
        }
2359 ankur.sing 388
 
2126 ankur.sing 389
    }
390
 
391
    @Override
392
    public void markInProcess(long itemId) {
393
        try {
3129 rajveer 394
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 395
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 396
 
2126 ankur.sing 397
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
398
        } catch (Exception e) {
3354 chandransh 399
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 400
        }
401
    }
2359 ankur.sing 402
 
2126 ankur.sing 403
    @Override
404
    public void phaseoutItem(long itemId) {
405
        try {
3129 rajveer 406
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 407
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 408
 
2126 ankur.sing 409
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
410
        } catch (Exception e) {
3354 chandransh 411
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 412
        }
413
    }
2359 ankur.sing 414
 
2126 ankur.sing 415
    @Override
416
    public void activateItem(long itemId) {
417
        try {
3129 rajveer 418
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 419
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 420
 
2126 ankur.sing 421
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
422
        } catch (Exception e) {
3354 chandransh 423
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 424
        }
425
    }
2359 ankur.sing 426
 
427
    @Override
428
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
429
        try {
430
            // Initialize client for staging server
3129 rajveer 431
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 432
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
433
 
434
            // Initialize client for production server
3165 chandransh 435
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 436
                    ConfigClientKeys.catalog_service_server_port.toString());
437
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
438
            catalogClient.changeItemRiskyFlag(itemId, risky);
439
 
440
            try{
441
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
442
            }catch(Exception e){
3354 chandransh 443
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 444
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
445
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
446
                return false;
447
            }
448
        } catch (Exception e) {
3354 chandransh 449
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 450
            return false;
451
        }
452
        return true;
453
    }
454
 
2427 ankur.sing 455
 
456
    /**
457
     * Retuns list of Items filtered by Vendor category (column is hotspotCategory in catalog.item table)
458
     * This list is used to generate master sheet (excel sheet)
459
     * @param category
460
     * @return
461
     */
3354 chandransh 462
    public List<Item> getItemsByVendorCategory(String category) {
2359 ankur.sing 463
        List<Item> itemList = new ArrayList<Item>();
464
        try {
3129 rajveer 465
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 466
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
467
 
468
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByVendorCategory(category);
469
 
470
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
471
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
472
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
473
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim));
474
            }
475
        } catch (Exception e) {
3354 chandransh 476
            logger.error("Error while getting items by vendor category: " + category, e);
2359 ankur.sing 477
        }
478
        Collections.sort(itemList, new ItemsComparator());
479
        return itemList;
480
    }
481
 
482
    @Override
2427 ankur.sing 483
    public String updateItemOnProduction(Item item) {
3354 chandransh 484
        logger.info("Update item on production call, Item Id: " + item.getId());
2427 ankur.sing 485
 
486
        String errMsg = checkPushToProdCount();
487
        // If errMsg is not null, then return that message to user
488
        // else move forward with update.
489
        if(errMsg != null) {
490
            return errMsg;
491
        }
2359 ankur.sing 492
        try{
3165 chandransh 493
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
2359 ankur.sing 494
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
2498 ankur.sing 495
                    ConfigClientKeys.catalog_service_server_port.toString());
2359 ankur.sing 496
 
497
            //for testing, put catalog_service_server_port_test in shop2020.cfg
2498 ankur.sing 498
            /*CatalogServiceClient catalogServiceClient_Prod = new CatalogServiceClient(CatalogServiceClient.class.getSimpleName(), 
499
                    ConfigClientKeys.catalog_service_server_host.toString(), "catalog_service_server_port_test");*/
2359 ankur.sing 500
 
501
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
502
 
503
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
2427 ankur.sing 504
 
2489 ankur.sing 505
            if(item.getMrp() != null) {
2359 ankur.sing 506
                tItem.setMrp(item.getMrp());
507
            }
3354 chandransh 508
 
2489 ankur.sing 509
            if(item.getSellingPrice() != null) {
2359 ankur.sing 510
                tItem.setSellingPrice(item.getSellingPrice());
511
            }
3354 chandransh 512
 
2489 ankur.sing 513
            if(item.getMop() != null) {
2359 ankur.sing 514
                tItem.setMop(item.getMop());
515
            }
3354 chandransh 516
 
2489 ankur.sing 517
            if(item.getDealerPrice() != null) {
2359 ankur.sing 518
                tItem.setDealerPrice(item.getDealerPrice());
519
            }
3354 chandransh 520
 
2489 ankur.sing 521
            if(item.getTransferPrice() != null) {
2359 ankur.sing 522
                tItem.setTransferPrice(item.getTransferPrice());
523
            }
3354 chandransh 524
 
2359 ankur.sing 525
            long rItemId;
526
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
3354 chandransh 527
                logger.error("Error updating item on production, returned Item Id: " + rItemId);
2427 ankur.sing 528
                return "Error updating item prices on production";
2359 ankur.sing 529
            }
3354 chandransh 530
            logger.info("Successfully updated item: " + item.getId());
2359 ankur.sing 531
 
532
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
2489 ankur.sing 533
            StringBuilder sb = new StringBuilder();
2359 ankur.sing 534
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
535
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
536
                for(VendorPricings v : vendorPricings.values()) {
537
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
538
                    tVendorPricing.setVendorId(v.getVendorId());
539
                    tVendorPricing.setItemId(item.getId());
540
                    tVendorPricing.setMop(v.getMop());
541
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
542
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
543
                    catalogClient_Prod.addVendorItemPricing(tVendorPricing);
3354 chandransh 544
                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
2489 ankur.sing 545
                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",DP=" + v.getDealerPrice() + "], ");
2359 ankur.sing 546
                }
547
            }
2489 ankur.sing 548
            logItemDetails("Id=" + item.getId() + ",MRP=" + item.getMrp() + ",SP=" + " " + item.getSellingPrice() + sb.toString());
2359 ankur.sing 549
        } catch (Exception e) {
3354 chandransh 550
            logger.error("Error while updating prices on production", e);
2427 ankur.sing 551
            return "Error while updating all prices on production. Some of the prices may have been  updated";
2359 ankur.sing 552
        }
2489 ankur.sing 553
 
2427 ankur.sing 554
        /*
2372 rajveer 555
        // Push the content of one item to production 
556
        Process p;
557
		try {
2378 ankur.sing 558
		    String scriptPath = ConfigClient.getClient().get(ConfigClientKeys.PUSH_PRICES_TO_PROD_SCRIPT_PATH.toString());
559
			p = Runtime.getRuntime().exec(scriptPath + " " + item.getCatalogItemId());
2372 rajveer 560
			p.waitFor();
561
		} catch (IOException e) {
562
			GWT.log("Not able to update content on production");
563
			e.printStackTrace();
2427 ankur.sing 564
			return "Error generating content. Prices updated successfully.";
2372 rajveer 565
		} catch (InterruptedException e) {
566
			GWT.log("Not able to update content on production");
567
			e.printStackTrace();
2427 ankur.sing 568
			return "Error generating content. Prices updated successfully.";
2378 ankur.sing 569
		} catch (ConfigException e) {
570
            e.printStackTrace();
2427 ankur.sing 571
            return "Error getting content script path";
572
        }*/
573
        return "Prices updated and content generated successfully";
2359 ankur.sing 574
    }
2427 ankur.sing 575
 
2489 ankur.sing 576
    /**
3354 chandransh 577
     * Returns list of items with a particular status.
578
     * @param st
579
     * @return
580
     */
581
    private List<Item> getItemsByStatus(status st) {
582
        List<Item> itemList = new ArrayList<Item>();
583
        try {
584
            CatalogClient catalogServiceClient = new CatalogClient();
585
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
586
 
587
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatus(st);
588
 
589
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
590
                itemList.add(getItemFromThriftItem(thriftItem, null, null));
591
            }
592
        } catch (Exception e) {
593
            logger.error("Error while getting items by status: " + st, e);
594
        }
595
        Collections.sort(itemList, new ItemsComparator());
596
        return itemList;
597
    }
598
 
599
    /**
600
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
601
     * Also creates a Map each for VendorItemPricing and VendorItemMapping and adds these maps to the
602
     * new Item object.
603
     * @param thriftItem
604
     * @param tVendorPricings
605
     * @param tVendorMappings
606
     * @return item object with attributes copied from thrift item object.
607
     */
608
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
609
            List<in.shop2020.model.v1.catalog.VendorItemPricing> tVendorPricings, 
610
            List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings){
611
 
612
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
613
        VendorItemMapping vItemMapping;
614
        if(tVendorMappings != null) {
615
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
616
                vItemMapping = new VendorItemMapping();
617
                vItemMapping.setVendorId(vim.getVendorId());
618
                vItemMapping.setItemKey(vim.getItemKey());
619
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
620
            }
621
        }
622
 
623
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
624
        VendorPricings vPricings;
625
        if(tVendorPricings != null) {
626
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
627
                vPricings = new VendorPricings();
628
                vPricings.setVendorId(vip.getVendorId());
629
                vPricings.setMop(vip.getMop());
630
                vPricings.setDealerPrice(vip.getDealerPrice());
631
                vPricings.setTransferPrice(vip.getTransferPrice());
632
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
633
            }
634
        }
635
 
636
        Item item = new Item(thriftItem.getId(),
637
                thriftItem.getHotspotCategory(),
638
                thriftItem.getProductGroup(),
639
                thriftItem.getBrand(),
640
                thriftItem.getModelNumber(),
641
                thriftItem.getModelName(),
642
                thriftItem.getColor(),
643
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
644
                thriftItem.getCategory(),
645
                thriftItem.getComments(),
646
                thriftItem.getCatalogItemId(),
647
                thriftItem.getFeatureId(),
648
                thriftItem.getFeatureDescription(),
649
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
650
                thriftItem.getMop(),
651
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
652
                thriftItem.getDealerPrice(),
653
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
654
                thriftItem.getAddedOn(),
655
                thriftItem.getStartDate(),
656
                thriftItem.getRetireDate(),
657
                thriftItem.getUpdatedOn(),
658
                thriftItem.getItemStatus().name(),
659
                thriftItem.getItemStatus().getValue(),
660
                thriftItem.getStatus_description(),
661
                thriftItem.getOtherInfo(),
662
                thriftItem.getBestDealText(),
663
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
664
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
665
                thriftItem.isDefaultForEntity(),
666
                thriftItem.isRisky(),
667
                (thriftItem.getItemInventory() != null ? thriftItem.getItemInventory().getAvailability() : null),
668
                vendorPricingMap,
669
                vItemMap);
670
        return item;
671
    }
672
 
673
    /**
2489 ankur.sing 674
     * 
675
     * @return null if push to production count is less than maximum allowed.
676
     *   <br>otherwise error message to be passed to the client side for showing to the user
677
     */
2427 ankur.sing 678
    private String checkPushToProdCount() {
679
        int count = 0, maxCount;
680
        try {
3354 chandransh 681
            //String logFile = ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_log_file.toString());
2427 ankur.sing 682
            maxCount = Integer.parseInt(ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_max_count.toString()));
3354 chandransh 683
            //count = getPushToProdCount(logFile);
2427 ankur.sing 684
            if(count >= maxCount) {
685
                return "Maximum push to production count reached for today";
686
            }
3354 chandransh 687
        } catch (ConfigException e) {
688
            logger.error("Error while getting the max count from the config server", e);
2427 ankur.sing 689
            return "Error getting push to production parameters";
690
        }
691
        return null;
692
    }
693
 
2489 ankur.sing 694
    /**
695
     * Log push to production details
696
     * @param str
697
     */
698
    private void logItemDetails(String str) {
3354 chandransh 699
        logger.info(str);
2427 ankur.sing 700
    }
701
 
2489 ankur.sing 702
    /**
703
     * If this is the first attempt to push to production for current date, returns 0
704
     * else reads number of lines from daily rolling log file to get the count.
705
     * @return push to production count for current date
706
     */
2498 ankur.sing 707
    private int getPushToProdCount(String logfile) {
708
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
709
        // this should also be set in log4j.properties
2427 ankur.sing 710
        int lineCount = 0;
2498 ankur.sing 711
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 712
        Date currentDate = Calendar.getInstance().getTime();
713
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
714
            pushToProdDate = currentDate;
715
            return lineCount;
716
        }
717
        try {
2498 ankur.sing 718
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 719
            while (br.readLine() != null) {
720
                lineCount++;
721
            }
722
        } catch (IOException e) {
3354 chandransh 723
            logger.error("Error while reading the log file", e);
2427 ankur.sing 724
        }
725
        return lineCount;
726
    }
727
}