Subversion Repositories SmartDukaan

Rev

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