Subversion Repositories SmartDukaan

Rev

Rev 5217 | Rev 5384 | 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;
4431 phani.kuma 5
import in.shop2020.catalog.dashboard.shared.ItemInventory;
3850 chandransh 6
import in.shop2020.catalog.dashboard.shared.ItemStatus;
2068 ankur.sing 7
import in.shop2020.catalog.dashboard.shared.ItemsComparator;
3558 rajveer 8
import in.shop2020.catalog.dashboard.shared.SourcePricings;
2119 ankur.sing 9
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
10
import in.shop2020.catalog.dashboard.shared.VendorPricings;
2378 ankur.sing 11
import in.shop2020.config.ConfigException;
3907 chandransh 12
import in.shop2020.content.ContentService;
13
import in.shop2020.content.ContentServiceException;
1961 ankur.sing 14
import in.shop2020.model.v1.catalog.InventoryService;
3911 chandransh 15
import in.shop2020.model.v1.catalog.SourceItemPricing;
2105 ankur.sing 16
import in.shop2020.model.v1.catalog.status;
3129 rajveer 17
import in.shop2020.thrift.clients.CatalogClient;
3907 chandransh 18
import in.shop2020.thrift.clients.ContentClient;
2378 ankur.sing 19
import in.shop2020.thrift.clients.config.ConfigClient;
2119 ankur.sing 20
import in.shop2020.utils.CategoryManager;
2359 ankur.sing 21
import in.shop2020.utils.ConfigClientKeys;
1961 ankur.sing 22
 
2427 ankur.sing 23
import java.io.BufferedReader;
24
import java.io.FileReader;
2359 ankur.sing 25
import java.io.IOException;
2427 ankur.sing 26
import java.text.DateFormat;
27
import java.text.SimpleDateFormat;
1961 ankur.sing 28
import java.util.ArrayList;
4957 phani.kuma 29
import java.util.Arrays;
2105 ankur.sing 30
import java.util.Calendar;
2068 ankur.sing 31
import java.util.Collections;
2427 ankur.sing 32
import java.util.Date;
1992 ankur.sing 33
import java.util.HashMap;
5118 mandeep.dh 34
import java.util.HashSet;
1961 ankur.sing 35
import java.util.List;
1992 ankur.sing 36
import java.util.Map;
2359 ankur.sing 37
import java.util.Map.Entry;
5118 mandeep.dh 38
import java.util.Set;
1961 ankur.sing 39
 
2427 ankur.sing 40
import org.apache.log4j.Logger;
3907 chandransh 41
import org.apache.thrift.TException;
42
import org.apache.thrift.transport.TTransportException;
2359 ankur.sing 43
 
1961 ankur.sing 44
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
45
 
46
@SuppressWarnings("serial")
47
public class CatalogServiceImpl extends RemoteServiceServlet implements CatalogService {
48
 
3354 chandransh 49
    private static Logger logger = Logger.getLogger(CatalogServiceImpl.class);
50
 
3922 chandransh 51
    private static Logger pushToProdLogger = Logger.getLogger("pushToProdLogger");
52
 
2427 ankur.sing 53
    private static Date pushToProdDate;
54
 
3922 chandransh 55
    private static String logFile;
56
 
57
    private static int maxCount;
58
 
59
    static {
60
        try {
61
            logFile = ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_log_file.toString());
62
            maxCount = Integer.parseInt(ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_max_count.toString()));    
63
        } catch(ConfigException ce) {
64
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
65
            logFile = "/var/log/services/pushToProductionCount/pushToProd.log";
66
            maxCount = 5;
67
        }
68
 
69
    }
70
 
3850 chandransh 71
    @Override
72
    public int getItemCountByStatus(boolean useStatus, ItemStatus itemStatus){
73
        int count = 0;
74
        try{
75
            CatalogClient catalogServiceClient = new CatalogClient();
76
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
77
 
78
            status stat = status.findByValue(itemStatus.getValue());
79
            count = catalogClient.getItemCountByStatus(useStatus, stat);
80
        }catch(Exception e){
81
            logger.error("Error while getting the count of items from the catalog service", e);
82
        }
83
        return count;
84
    }
85
 
86
    @Override
87
    public List<Item> getAllItems(int start, int limit) {
1961 ankur.sing 88
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 89
 
1961 ankur.sing 90
        try {
3129 rajveer 91
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 92
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 93
 
3850 chandransh 94
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsInRange(start, limit);
2359 ankur.sing 95
 
2119 ankur.sing 96
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 97
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
2119 ankur.sing 98
            }
99
        } catch (Exception e) {
3354 chandransh 100
            logger.error("Error while getting all items from the catalog service", e);
2119 ankur.sing 101
        }
102
        return itemList;
103
    }
2208 ankur.sing 104
 
3850 chandransh 105
    public List<Item> getAllActiveItems(int start, int limit){
106
        return getItemsByStatus(status.ACTIVE, start, limit);
2208 ankur.sing 107
    }
2359 ankur.sing 108
 
109
    @Override
3850 chandransh 110
    public List<Item> getAllPhasedOutItems(int start, int limit){
111
        return getItemsByStatus(status.PHASED_OUT, start, limit);
2359 ankur.sing 112
    }
113
 
114
    @Override
3850 chandransh 115
    public List<Item> getAllPausedItems(int start, int limit){
116
        return getItemsByStatus(status.PAUSED, start, limit);
2208 ankur.sing 117
    }
2359 ankur.sing 118
 
119
    @Override
3850 chandransh 120
    public List<Item> getAllInProcessItems(int start, int limit) {
121
        return getItemsByStatus(status.IN_PROCESS, start, limit);
2359 ankur.sing 122
    }
123
 
124
    @Override
3850 chandransh 125
    public List<Item> getAllContentCompleteItems(int start, int limit) {
126
        return getItemsByStatus(status.CONTENT_COMPLETE, start, limit);
2359 ankur.sing 127
    }
128
 
129
    public List<Item> getBestDeals(){
2119 ankur.sing 130
        List<Item> itemList = new ArrayList<Item>();
131
        try {
3129 rajveer 132
            CatalogClient catalogServiceClient = new CatalogClient();
2119 ankur.sing 133
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 134
 
135
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestDeals();
1992 ankur.sing 136
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 137
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
1992 ankur.sing 138
            }
2359 ankur.sing 139
        } catch(Exception e){
3354 chandransh 140
            logger.error("Error while getting the best deals from the catalog service", e);
1961 ankur.sing 141
        }
2562 chandransh 142
        //Collections.sort(itemList, new ItemsComparator());
1961 ankur.sing 143
        return itemList;
144
    }
145
 
2359 ankur.sing 146
    @Override
147
    public List<Item> getRiskyItems() {
1961 ankur.sing 148
        List<Item> itemList = new ArrayList<Item>();
149
        try {
3129 rajveer 150
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 151
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 152
 
153
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsByRiskyFlag();
1992 ankur.sing 154
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 155
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
1992 ankur.sing 156
            }
1961 ankur.sing 157
        } catch(Exception e){
3354 chandransh 158
            logger.error("Error while getting the risky items from the catalog service", e);
1961 ankur.sing 159
        }
2068 ankur.sing 160
        Collections.sort(itemList, new ItemsComparator());
1961 ankur.sing 161
        return itemList;
162
    }
2359 ankur.sing 163
 
1961 ankur.sing 164
    public List<Item> getBestSellers(){
165
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 166
 
1961 ankur.sing 167
        try {
3129 rajveer 168
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 169
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 170
 
1961 ankur.sing 171
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();
1992 ankur.sing 172
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 173
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
1992 ankur.sing 174
            }
1961 ankur.sing 175
        } catch(Exception e){
3354 chandransh 176
            logger.error("Error while getting the best sellers from the catalog service", e);
1961 ankur.sing 177
        }
178
        return itemList;        
179
    }
2359 ankur.sing 180
 
3872 chandransh 181
    @Override
1961 ankur.sing 182
    public List<Item> getLatestArrivals(){
183
        List<Item> itemList = new ArrayList<Item>();
2359 ankur.sing 184
 
1961 ankur.sing 185
        try {
3129 rajveer 186
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 187
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 188
 
1961 ankur.sing 189
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getLatestArrivals();
1992 ankur.sing 190
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
2105 ankur.sing 191
                //List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
5309 rajveer 192
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
1992 ankur.sing 193
            }
1961 ankur.sing 194
        } catch(Exception e){
3354 chandransh 195
            logger.error("Error while getting the latest arrivals from the catalog service", e);
1961 ankur.sing 196
        }
197
        return itemList;
198
    }
2359 ankur.sing 199
 
3872 chandransh 200
    @Override
201
    public List<Item> searchItems(int start, int limit, List<String> searchTerms) {
202
        List<Item> itemList = new ArrayList<Item>();
203
 
204
        try {
205
            CatalogClient catalogServiceClient = new CatalogClient();
206
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
207
 
208
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.searchItemsInRange(searchTerms, start, limit);
209
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 210
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
3872 chandransh 211
            }
212
        } catch(Exception e){
213
            logger.error("Error while getting the search results from the catalog service", e);
214
        }
215
        return itemList;
216
    }
217
 
218
    @Override
219
    public int getSearchResultCount(List<String> searchTerms){
220
        int count = 0;
221
        try {
222
            CatalogClient catalogServiceClient = new CatalogClient();
223
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
224
 
225
            count = catalogClient.getSearchResultCount(searchTerms);
226
        } catch(Exception e){
227
            logger.error("Error while getting the search results from the catalog service", e);
228
        }
229
 
230
        return count;
231
    }
232
 
1961 ankur.sing 233
    public Item getItem(long itemId){
234
        try{
3129 rajveer 235
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 236
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
237
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.getItem(itemId);
2359 ankur.sing 238
 
1992 ankur.sing 239
            List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
2119 ankur.sing 240
            List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 241
            List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
4423 phani.kuma 242
            List<in.shop2020.model.v1.catalog.Item> sit = catalogClient.getAllSimilarItems(thriftItem.getId());
5309 rajveer 243
            in.shop2020.model.v1.catalog.ItemInventory itemInventory = catalogClient.getItemInventoryByItemId(thriftItem.getId());
244
            return getItemFromThriftItem(thriftItem, vip, vim, sip, sit, itemInventory);
1961 ankur.sing 245
        }catch(Exception e){
3354 chandransh 246
            logger.error("Error while getting the item from the catalog service", e);
1961 ankur.sing 247
        }
248
        return null;
249
    }
2359 ankur.sing 250
 
1961 ankur.sing 251
    @Override
1992 ankur.sing 252
    public boolean updateItem(Item item) {
3354 chandransh 253
        logger.info("Updating item with Id: " + item.getId());
1961 ankur.sing 254
        try{
3129 rajveer 255
            CatalogClient catalogServiceClient = new CatalogClient();
1961 ankur.sing 256
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 257
 
1992 ankur.sing 258
            in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());
2126 ankur.sing 259
            setThriftItemParams(tItem, item);
2359 ankur.sing 260
 
2105 ankur.sing 261
            long rItemId;
262
            if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {
3354 chandransh 263
                logger.error("Error updating item, returned Item Id: " + rItemId);
2105 ankur.sing 264
                return false;
265
            }
3354 chandransh 266
            logger.info("Successfully updated item with id: " + item.getId());
2359 ankur.sing 267
 
268
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
2119 ankur.sing 269
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
270
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
2359 ankur.sing 271
 
272
                for(Entry<String, VendorItemMapping> e : vendorMappings.entrySet()) {
2119 ankur.sing 273
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
2359 ankur.sing 274
                    VendorItemMapping v = e.getValue();
2119 ankur.sing 275
                    tVendorMapping.setVendorId(v.getVendorId());
276
                    tVendorMapping.setItemKey(v.getItemKey());
277
                    tVendorMapping.setItemId(item.getId());
2359 ankur.sing 278
                    catalogClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
3354 chandransh 279
                    logger.info("Updates VendorItemMapping: " + tVendorMapping.toString());
2119 ankur.sing 280
                }
281
            }
2359 ankur.sing 282
 
2119 ankur.sing 283
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
284
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
2105 ankur.sing 285
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
2119 ankur.sing 286
                for(VendorPricings v : vendorPricings.values()) {
287
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
2105 ankur.sing 288
                    tVendorPricing.setVendorId(v.getVendorId());
289
                    tVendorPricing.setItemId(item.getId());
290
                    tVendorPricing.setMop(v.getMop());
291
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
292
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
293
                    catalogClient.addVendorItemPricing(tVendorPricing);
3354 chandransh 294
                    logger.info("Updated VendorItemPricing: " + tVendorPricing.toString());
2105 ankur.sing 295
                }
296
            }
3558 rajveer 297
 
298
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
299
            if(sourcePricings != null && !sourcePricings.isEmpty()) {
300
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricings;
301
                for(SourcePricings s : sourcePricings.values()) {
302
                	tSourcePricings = new in.shop2020.model.v1.catalog.SourceItemPricing();
303
                	tSourcePricings.setSourceId(s.getSourceId());
304
                    tSourcePricings.setItemId(item.getId());
305
                    tSourcePricings.setMrp(s.getMrp());
306
                    tSourcePricings.setSellingPrice(s.getSellingPrice());
307
                    catalogClient.addSourceItemPricing(tSourcePricings);
308
                    logger.info("Updated SourceItemPricing: " + tSourcePricings.toString());
309
                }
310
            }
311
 
2105 ankur.sing 312
        }catch(Exception e){
3354 chandransh 313
            logger.error("Error while updating item: ", e);
2427 ankur.sing 314
            return false;
2105 ankur.sing 315
        }
316
        return true;
1961 ankur.sing 317
    }
2359 ankur.sing 318
 
319
 
2066 ankur.sing 320
    @Override
321
    public Map<Long, String> getAllVendors() {
322
        Map<Long, String> vendorMap = new HashMap<Long, String>();
323
        try {
3129 rajveer 324
            CatalogClient catalogServiceClient = new CatalogClient();
2066 ankur.sing 325
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 326
 
2066 ankur.sing 327
            List<in.shop2020.model.v1.catalog.Vendor> vendors = catalogClient.getAllVendors();
2359 ankur.sing 328
 
2066 ankur.sing 329
            for(in.shop2020.model.v1.catalog.Vendor v : vendors) {
330
                vendorMap.put(v.getId(), v.getName());
331
            }
332
        } catch (Exception e) {
3354 chandransh 333
            logger.error("Error while getting all the vendors: ", e);
2066 ankur.sing 334
        }
335
        return vendorMap;
336
    }
337
 
338
    @Override
3558 rajveer 339
    public Map<Long, String> getAllSources() {
340
        Map<Long, String> sourceMap = new HashMap<Long, String>();
341
        try {
342
            CatalogClient catalogServiceClient = new CatalogClient();
343
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
344
 
345
            List<in.shop2020.model.v1.catalog.Source> sources = catalogClient.getAllSources();
346
 
347
            for(in.shop2020.model.v1.catalog.Source s : sources) {
348
            	sourceMap.put(s.getId(), s.getName());
349
            }
350
        } catch (Exception e) {
351
            logger.error("Error while getting all the vendors: ", e);
352
        }
353
        return sourceMap;
354
    }
355
 
356
    @Override
5118 mandeep.dh 357
    public Map<Long, String> getShippingWarehouses() {
358
        Map<Long, String> warehouseMap = new HashMap<Long, String>();
359
        Set<Long> shippingLocations = new HashSet<Long>();
360
        try {
361
            CatalogClient catalogServiceClient = new CatalogClient();
362
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
363
 
364
            List<in.shop2020.model.v1.catalog.Warehouse> warehouses = catalogClient.getAllWarehouses(true);
365
 
366
            for(in.shop2020.model.v1.catalog.Warehouse warehouse : warehouses) {
367
                shippingLocations.add(warehouse.getShippingWarehouseId());
368
            }
369
 
370
            for(in.shop2020.model.v1.catalog.Warehouse warehouse : warehouses) {
371
                if (shippingLocations.contains(warehouse.getId())) {
372
                    warehouseMap.put(warehouse.getId(), warehouse.getDisplayName());
373
                }                
374
            }
375
        } catch (Exception e) {
376
            logger.error("Error while getting all the warehouses:", e );
377
        }
378
        return warehouseMap;
379
    }
380
 
381
    @Override
2066 ankur.sing 382
    public Map<Long, String> getAllWarehouses() {
383
        Map<Long, String> warehouseMap = new HashMap<Long, String>();
384
        try {
3129 rajveer 385
            CatalogClient catalogServiceClient = new CatalogClient();
2066 ankur.sing 386
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 387
 
2066 ankur.sing 388
            List<in.shop2020.model.v1.catalog.Warehouse> warehouses = catalogClient.getAllWarehouses(true);
2359 ankur.sing 389
 
2066 ankur.sing 390
            for(in.shop2020.model.v1.catalog.Warehouse w : warehouses) {
391
                warehouseMap.put(w.getId(), w.getDisplayName());
392
            }
393
        } catch (Exception e) {
3354 chandransh 394
            logger.error("Error while getting all the warehouses:", e );
2066 ankur.sing 395
        }
396
        return warehouseMap;
397
    }
2105 ankur.sing 398
 
399
    @Override
400
    public long addItem(Item item) {
401
        long itemId = 0;
402
        try {
3129 rajveer 403
            CatalogClient catalogServiceClient = new CatalogClient();
2105 ankur.sing 404
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2126 ankur.sing 405
 
2359 ankur.sing 406
 
2105 ankur.sing 407
            in.shop2020.model.v1.catalog.Item tItem = new in.shop2020.model.v1.catalog.Item();
2126 ankur.sing 408
            setThriftItemParams(tItem, item);
2105 ankur.sing 409
            itemId = catalogClient.addItem(tItem);
410
 
2119 ankur.sing 411
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
412
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
2105 ankur.sing 413
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
2119 ankur.sing 414
                for(VendorPricings v : vendorPricings.values()) {
415
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
416
                    tVendorPricing.setVendorId(v.getVendorId());
417
                    tVendorPricing.setItemId(itemId);
418
                    tVendorPricing.setMop(v.getMop());
419
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
420
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
421
                    catalogClient.addVendorItemPricing(tVendorPricing);
422
                }
423
            }
2359 ankur.sing 424
 
425
            Map<String, VendorItemMapping> vendorKeysMap = item.getVendorKeysMap();
2119 ankur.sing 426
            if(vendorKeysMap != null && !vendorKeysMap.isEmpty()) {
2105 ankur.sing 427
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
2359 ankur.sing 428
                for(Entry<String, VendorItemMapping> e : vendorKeysMap.entrySet()) {
2119 ankur.sing 429
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
2359 ankur.sing 430
                    VendorItemMapping v = e.getValue();
2105 ankur.sing 431
                    tVendorMapping.setVendorId(v.getVendorId());
432
                    tVendorMapping.setItemKey(v.getItemKey());
433
                    tVendorMapping.setItemId(itemId);
2359 ankur.sing 434
                    catalogClient.addVendorItemMapping(e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1), tVendorMapping);
2105 ankur.sing 435
                }
436
            }
437
        } catch (Exception e) {
3354 chandransh 438
            logger.error("Error while adding an item: ", e);
2105 ankur.sing 439
        }
440
        return itemId;
441
    }
2119 ankur.sing 442
 
443
    @Override
4725 phani.kuma 444
    public long checkSimilarItem(String brand, String modelNumber, String modelName, String color) {
2359 ankur.sing 445
 
2119 ankur.sing 446
        try {
3129 rajveer 447
            CatalogClient catalogServiceClient = new CatalogClient();
2119 ankur.sing 448
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
4725 phani.kuma 449
            return catalogClient.checkSimilarItem(brand, modelNumber, modelName, color);
2119 ankur.sing 450
        } catch (Exception e) {
3354 chandransh 451
            logger.error("Error while checking for a similar item: ", e);
2119 ankur.sing 452
        }
453
        return 0;
454
    }
2359 ankur.sing 455
 
2126 ankur.sing 456
    private void setThriftItemParams(in.shop2020.model.v1.catalog.Item tItem, Item item) {
457
        tItem.setId(tItem.getId());
458
        tItem.setProductGroup(item.getProductGroup());
459
        tItem.setBrand(item.getBrand());
460
        tItem.setModelName(item.getModelName());
461
        tItem.setModelNumber(item.getModelNumber());
462
        tItem.setColor(item.getColor());
2359 ankur.sing 463
 
464
        tItem.setStatus_description(item.getItemStatusDesc());
2126 ankur.sing 465
        tItem.setComments(item.getComments());
2359 ankur.sing 466
 
2489 ankur.sing 467
        if(item.getMrp() != null) {
2126 ankur.sing 468
            tItem.setMrp(item.getMrp());
469
        }
3354 chandransh 470
 
2489 ankur.sing 471
        if(item.getSellingPrice() != null) {
2126 ankur.sing 472
            tItem.setSellingPrice(item.getSellingPrice());
473
        }
3354 chandransh 474
 
2489 ankur.sing 475
        if(item.getWeight() != null) {
2126 ankur.sing 476
            tItem.setWeight(item.getWeight());
477
        }
3354 chandransh 478
 
3359 chandransh 479
        if(item.getExpectedDelay() != null) {
480
            tItem.setExpectedDelay(item.getExpectedDelay());
2489 ankur.sing 481
        }
3354 chandransh 482
 
3359 chandransh 483
        if(item.getPreferredWarehouse() != null) {
484
            tItem.setPreferredWarehouse(item.getPreferredWarehouse());
2489 ankur.sing 485
        }
3354 chandransh 486
 
4413 anupam.sin 487
        if(item.getDefaultWarehouse() != null) {
488
            tItem.setDefaultWarehouse(item.getDefaultWarehouse());
489
        }
490
 
491
        tItem.setIsWarehousePreferenceSticky(item.isWarehouseStickiness());
492
 
4506 phani.kuma 493
        if(item.getPreferredVendor() != null) {
494
            tItem.setPreferredVendor(item.getPreferredVendor());
495
        }
496
 
2126 ankur.sing 497
        tItem.setBestDealText(item.getBestDealsText());
2489 ankur.sing 498
        if(item.getBestDealsValue() != null) {
2126 ankur.sing 499
            tItem.setBestDealValue(item.getBestDealsValue());
500
        }
3354 chandransh 501
 
2489 ankur.sing 502
        if(item.getBestSellingRank() != null) {
2126 ankur.sing 503
            tItem.setBestSellingRank(item.getBestSellingRank());
504
        }
3354 chandransh 505
 
2126 ankur.sing 506
        tItem.setDefaultForEntity(item.isDefaultForEntity());
2252 ankur.sing 507
        tItem.setRisky(item.isRisky());
2359 ankur.sing 508
 
2489 ankur.sing 509
        if(item.getStartDate() != null) {
510
            tItem.setStartDate(item.getStartDate());
511
        }
3354 chandransh 512
 
5217 amit.gupta 513
        if(item.getExpectedArrivalDate() != null) {
514
        	tItem.setExpectedArrivalDate(item.getExpectedArrivalDate());
515
        } else {
516
        	tItem.unsetComingSoonStartDate();
517
        	tItem.unsetExpectedArrivalDate();
518
        	if(status.COMING_SOON.equals(tItem.getItemStatus())){
519
        		tItem.setItemStatus(status.IN_PROCESS);
520
        	}
521
        }
522
 
523
        if(item.getComingSoonStartDate() != null) {
524
        	tItem.setComingSoonStartDate(item.getComingSoonStartDate());
525
        } else {
526
        	tItem.unsetComingSoonStartDate();
527
        }
528
 
2489 ankur.sing 529
        if(item.getRetireDate() != null) {
530
            tItem.setRetireDate(item.getRetireDate());
531
        }
3354 chandransh 532
 
2126 ankur.sing 533
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
534
    }
535
 
536
    @Override
537
    public void pauseItem(long itemId) {
538
        try {
3129 rajveer 539
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 540
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 541
 
2126 ankur.sing 542
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
543
        } catch (Exception e) {
3354 chandransh 544
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 545
        }
2359 ankur.sing 546
 
2126 ankur.sing 547
    }
548
 
549
    @Override
550
    public void markInProcess(long itemId) {
551
        try {
3129 rajveer 552
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 553
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 554
 
2126 ankur.sing 555
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
556
        } catch (Exception e) {
3354 chandransh 557
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 558
        }
559
    }
2359 ankur.sing 560
 
2126 ankur.sing 561
    @Override
562
    public void phaseoutItem(long itemId) {
563
        try {
3129 rajveer 564
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 565
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 566
 
2126 ankur.sing 567
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
568
        } catch (Exception e) {
3354 chandransh 569
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 570
        }
571
    }
2359 ankur.sing 572
 
2126 ankur.sing 573
    @Override
574
    public void activateItem(long itemId) {
575
        try {
3129 rajveer 576
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 577
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 578
 
2126 ankur.sing 579
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
580
        } catch (Exception e) {
3354 chandransh 581
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 582
        }
583
    }
2359 ankur.sing 584
 
585
    @Override
586
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
587
        try {
4354 mandeep.dh 588
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 589
            // Initialize client for staging server
3129 rajveer 590
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 591
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
592
 
593
            // Initialize client for production server
3165 chandransh 594
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 595
                    ConfigClientKeys.catalog_service_server_port.toString());
596
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
597
            catalogClient.changeItemRiskyFlag(itemId, risky);
598
 
599
            try{
600
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
601
            }catch(Exception e){
3354 chandransh 602
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 603
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
604
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
605
                return false;
606
            }
607
        } catch (Exception e) {
3354 chandransh 608
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 609
            return false;
610
        }
611
        return true;
612
    }
613
 
2427 ankur.sing 614
 
615
    /**
4762 phani.kuma 616
     * Retuns list of Items filtered by category (column is product_group in catalog.item table)
2427 ankur.sing 617
     * This list is used to generate master sheet (excel sheet)
618
     * @param category
4957 phani.kuma 619
     * @param brand
2427 ankur.sing 620
     * @return
621
     */
4957 phani.kuma 622
    public List<Item> getItemsForMasterSheet(String category, String brand) {
2359 ankur.sing 623
        List<Item> itemList = new ArrayList<Item>();
624
        try {
3129 rajveer 625
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 626
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
627
 
4957 phani.kuma 628
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsForMasterSheet(category, brand);
2359 ankur.sing 629
 
630
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
631
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
632
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 633
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
5309 rajveer 634
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null, null));
2359 ankur.sing 635
            }
636
        } catch (Exception e) {
4762 phani.kuma 637
            logger.error("Error while getting items by category: " + category, e);
2359 ankur.sing 638
        }
639
        Collections.sort(itemList, new ItemsComparator());
640
        return itemList;
641
    }
642
 
643
    @Override
2427 ankur.sing 644
    public String updateItemOnProduction(Item item) {
3354 chandransh 645
        logger.info("Update item on production call, Item Id: " + item.getId());
2427 ankur.sing 646
 
3922 chandransh 647
        if(!canPushToProduction()) {
648
            // If we can't push to production, then return that message to user
649
            // else move forward with update.
650
            return "Maximum push to production count reached for today";
2427 ankur.sing 651
        }
2359 ankur.sing 652
        try{
3165 chandransh 653
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
2359 ankur.sing 654
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
2498 ankur.sing 655
                    ConfigClientKeys.catalog_service_server_port.toString());
2359 ankur.sing 656
 
657
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
658
 
659
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
2427 ankur.sing 660
 
2489 ankur.sing 661
            if(item.getMrp() != null) {
2359 ankur.sing 662
                tItem.setMrp(item.getMrp());
663
            }
3354 chandransh 664
 
2489 ankur.sing 665
            if(item.getSellingPrice() != null) {
2359 ankur.sing 666
                tItem.setSellingPrice(item.getSellingPrice());
667
            }
3354 chandransh 668
 
2359 ankur.sing 669
            long rItemId;
670
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
3354 chandransh 671
                logger.error("Error updating item on production, returned Item Id: " + rItemId);
2427 ankur.sing 672
                return "Error updating item prices on production";
2359 ankur.sing 673
            }
3354 chandransh 674
            logger.info("Successfully updated item: " + item.getId());
2359 ankur.sing 675
 
3911 chandransh 676
            StringBuilder sb = new StringBuilder();
677
 
2359 ankur.sing 678
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
679
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
680
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
681
                for(VendorPricings v : vendorPricings.values()) {
682
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
683
                    tVendorPricing.setVendorId(v.getVendorId());
684
                    tVendorPricing.setItemId(item.getId());
685
                    tVendorPricing.setMop(v.getMop());
686
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
687
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
688
                    catalogClient_Prod.addVendorItemPricing(tVendorPricing);
3354 chandransh 689
                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
2489 ankur.sing 690
                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",DP=" + v.getDealerPrice() + "], ");
2359 ankur.sing 691
                }
692
            }
3911 chandransh 693
 
4993 mandeep.dh 694
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
695
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
696
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
697
                for(VendorItemMapping vendorItemMapping : vendorMappings.values()) {
698
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
699
                    tVendorMapping.setVendorId(vendorItemMapping.getVendorId());
700
                    tVendorMapping.setItemId(item.getId());
701
                    tVendorMapping.setItemKey(vendorItemMapping.getItemKey());
702
                    catalogClient_Prod.addVendorItemMapping(vendorItemMapping.getItemKey(), tVendorMapping);
703
                    logger.info("VendorItemMapping updated. " + tVendorMapping.toString());
704
                    sb.append("[VId:" + vendorItemMapping.getVendorId() + ",ItemKey=" + vendorItemMapping.getItemKey() + "], ");
705
                }
706
            }
707
 
3911 chandransh 708
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
709
            if(sourcePricings != null && !sourcePricings.isEmpty()){
710
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
711
                for(SourcePricings s : sourcePricings.values()){
712
                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
713
                    tSourcePricing.setSourceId(s.getSourceId());
714
                    tSourcePricing.setItemId(item.getId());
715
                    tSourcePricing.setMrp(s.getMrp());
716
                    tSourcePricing.setSellingPrice(s.getSellingPrice());
717
                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
718
                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
719
                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
720
                }
721
            }
722
 
3922 chandransh 723
            pushToProdLogger.info("Id=" + item.getId() + ",MRP=" + item.getMrp() + ",SP=" + " " + item.getSellingPrice() + sb.toString());
2359 ankur.sing 724
        } catch (Exception e) {
3354 chandransh 725
            logger.error("Error while updating prices on production", e);
2427 ankur.sing 726
            return "Error while updating all prices on production. Some of the prices may have been  updated";
2359 ankur.sing 727
        }
2489 ankur.sing 728
 
3907 chandransh 729
        try {
730
            ContentClient contentServiceClient = new ContentClient();
731
            ContentService.Client contentClient = contentServiceClient.getClient();
732
 
733
            contentClient.pushContentToProduction(item.getCatalogItemId());
734
        } catch (TTransportException e) {
735
            logger.error("Error while establishing connection to the content server", e);
736
            return "Error generating content. Prices updated successfully.";
737
        } catch (TException e) {
738
            logger.error("Error while pushing content to production", e);
739
            return "Error generating content. Prices updated successfully.";
740
        } catch (ContentServiceException e) {
741
            logger.error("Error while pushing content to production", e);
742
            return "Error generating content. Prices updated successfully.";
743
        }
744
 
2427 ankur.sing 745
        return "Prices updated and content generated successfully";
2359 ankur.sing 746
    }
2427 ankur.sing 747
 
2489 ankur.sing 748
    /**
3354 chandransh 749
     * Returns list of items with a particular status.
750
     * @param st
751
     * @return
752
     */
3850 chandransh 753
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 754
        List<Item> itemList = new ArrayList<Item>();
755
        try {
756
            CatalogClient catalogServiceClient = new CatalogClient();
757
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
758
 
3850 chandransh 759
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 760
 
761
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 762
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
3354 chandransh 763
            }
764
        } catch (Exception e) {
765
            logger.error("Error while getting items by status: " + st, e);
766
        }
767
        return itemList;
768
    }
769
 
770
    /**
771
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
4423 phani.kuma 772
     * Also creates a Map each for VendorItemPricing, VendorItemMapping, SourceItemPricing and SimilarItems and adds these maps to the
3354 chandransh 773
     * new Item object.
774
     * @param thriftItem
775
     * @param tVendorPricings
776
     * @param tVendorMappings
4423 phani.kuma 777
     * @param tSourceMappings
778
     * @param tSimilarItems
3354 chandransh 779
     * @return item object with attributes copied from thrift item object.
780
     */
781
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
782
            List<in.shop2020.model.v1.catalog.VendorItemPricing> tVendorPricings, 
3558 rajveer 783
            List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings,
4423 phani.kuma 784
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings,
5309 rajveer 785
            List<in.shop2020.model.v1.catalog.Item> tSimilarItems, in.shop2020.model.v1.catalog.ItemInventory tItemInventory){
3354 chandransh 786
 
787
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
788
        VendorItemMapping vItemMapping;
789
        if(tVendorMappings != null) {
790
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
791
                vItemMapping = new VendorItemMapping();
792
                vItemMapping.setVendorId(vim.getVendorId());
793
                vItemMapping.setItemKey(vim.getItemKey());
794
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
795
            }
796
        }
797
 
798
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
799
        VendorPricings vPricings;
800
        if(tVendorPricings != null) {
801
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
802
                vPricings = new VendorPricings();
803
                vPricings.setVendorId(vip.getVendorId());
804
                vPricings.setMop(vip.getMop());
805
                vPricings.setDealerPrice(vip.getDealerPrice());
806
                vPricings.setTransferPrice(vip.getTransferPrice());
807
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
808
            }
809
        }
810
 
3558 rajveer 811
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
812
        SourcePricings sPricings;
813
        if(tSourceMappings != null) {
814
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
815
                sPricings = new SourcePricings();
816
                sPricings.setSourceId(sip.getSourceId());
817
                sPricings.setMrp(sip.getMrp());
818
                sPricings.setSellingPrice(sip.getSellingPrice());
819
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
820
            }
821
        }
4423 phani.kuma 822
 
823
        Map<Long, Item> SimilarItemslist = new HashMap<Long, Item>();
824
        Item sItems;
825
        if(tSimilarItems != null) {
826
            for(in.shop2020.model.v1.catalog.Item sit : tSimilarItems) {
827
            	sItems = new Item();
828
            	sItems.setCatalogItemId(sit.getCatalogItemId());
829
            	sItems.setProductGroup(sit.getProductGroup());
830
            	sItems.setBrand(sit.getBrand());
831
            	sItems.setModelNumber(sit.getModelNumber());
832
            	sItems.setModelName(sit.getModelName());
833
            	sItems.setContentCategory(CategoryManager.getCategoryManager().getCategoryLabel(sit.getCategory()));
834
            	SimilarItemslist.put(sit.getCatalogItemId(), sItems);
835
            }
836
        }
3558 rajveer 837
 
4431 phani.kuma 838
        Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
839
        ItemInventory warehousedata;
5309 rajveer 840
        if(tItemInventory != null) {
841
        	Map<Long, Long> availabilityMap = tItemInventory.getAvailability();
842
        	Map<Long, Long> reservedMap = tItemInventory.getReserved();
4431 phani.kuma 843
            for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
844
            	warehousedata = new ItemInventory();
845
            	warehousedata.setWarehouseId(availability.getKey());
846
            	warehousedata.setAvailability(availability.getValue());
847
            	warehousedata.setReserved(reservedMap.get(availability.getKey()));
848
            	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
849
            }
850
        }
851
        else {
852
        	itemInventoryMap = null;
853
        }
854
 
3354 chandransh 855
        Item item = new Item(thriftItem.getId(),
856
                thriftItem.getProductGroup(),
857
                thriftItem.getBrand(),
858
                thriftItem.getModelNumber(),
859
                thriftItem.getModelName(),
860
                thriftItem.getColor(),
861
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
862
                thriftItem.getCategory(),
863
                thriftItem.getComments(),
864
                thriftItem.getCatalogItemId(),
865
                thriftItem.getFeatureId(),
866
                thriftItem.getFeatureDescription(),
867
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
868
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
869
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
870
                thriftItem.getAddedOn(),
871
                thriftItem.getStartDate(),
5217 amit.gupta 872
                thriftItem.isSetComingSoonStartDate() ? thriftItem.getComingSoonStartDate() : null ,
873
                thriftItem.isSetExpectedArrivalDate() ? thriftItem.getExpectedArrivalDate() : null ,
3354 chandransh 874
                thriftItem.getRetireDate(),
875
                thriftItem.getUpdatedOn(),
876
                thriftItem.getItemStatus().name(),
877
                thriftItem.getItemStatus().getValue(),
878
                thriftItem.getStatus_description(),
879
                thriftItem.getBestDealText(),
880
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
881
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
882
                thriftItem.isDefaultForEntity(),
883
                thriftItem.isRisky(),
3359 chandransh 884
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
885
                thriftItem.isSetPreferredWarehouse() ? thriftItem.getPreferredWarehouse() : null,
4413 anupam.sin 886
                thriftItem.isSetDefaultWarehouse() ? thriftItem.getDefaultWarehouse() : null,
4506 phani.kuma 887
                thriftItem.isIsWarehousePreferenceSticky(),
888
                thriftItem.isSetPreferredVendor() ? thriftItem.getPreferredVendor() : null,
4431 phani.kuma 889
                itemInventoryMap,
3354 chandransh 890
                vendorPricingMap,
3558 rajveer 891
                vItemMap,
4423 phani.kuma 892
                sourcePricingMap,
893
                SimilarItemslist);
3354 chandransh 894
        return item;
895
    }
896
 
897
    /**
2489 ankur.sing 898
     * 
3922 chandransh 899
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 900
     */
3922 chandransh 901
    private boolean canPushToProduction() {
902
        int count = getPushToProdCount(logFile);
903
        return count < maxCount;
2427 ankur.sing 904
    }
3922 chandransh 905
 
2489 ankur.sing 906
    /**
907
     * If this is the first attempt to push to production for current date, returns 0
908
     * else reads number of lines from daily rolling log file to get the count.
909
     * @return push to production count for current date
910
     */
2498 ankur.sing 911
    private int getPushToProdCount(String logfile) {
912
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
913
        // this should also be set in log4j.properties
2427 ankur.sing 914
        int lineCount = 0;
2498 ankur.sing 915
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 916
        Date currentDate = Calendar.getInstance().getTime();
917
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
918
            pushToProdDate = currentDate;
919
            return lineCount;
920
        }
921
        try {
2498 ankur.sing 922
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 923
            while (br.readLine() != null) {
924
                lineCount++;
925
            }
926
        } catch (IOException e) {
3354 chandransh 927
            logger.error("Error while reading the log file", e);
2427 ankur.sing 928
        }
929
        return lineCount;
930
    }
4423 phani.kuma 931
 
932
	@Override
933
	public boolean deleteSimilarItem(long itemId, long catalogItemId) {
934
		try{
935
            CatalogClient catalogServiceClient = new CatalogClient();
936
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
937
 
938
            return catalogClient.deleteSimilarItem(itemId, catalogItemId);
939
 
940
        }catch(Exception e){
941
            logger.error("Error while deleting the SimilarItems for: " + itemId, e);
942
        }
943
        return false;
944
	}
945
 
946
	@Override
947
	public Item addSimilarItem(long itemId, long catalogItemId) {
948
		try{
949
            CatalogClient catalogServiceClient = new CatalogClient();
950
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
951
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.addSimilarItem(itemId, catalogItemId);;
952
 
5309 rajveer 953
            return getItemFromThriftItem(thriftItem, null, null, null, null, null);
4423 phani.kuma 954
        }catch(Exception e){
955
            logger.error("Error while adding the SimilarItems for: " + itemId, e);
956
        }
957
        return null;
958
	}
4431 phani.kuma 959
 
960
	@Override
961
	public Map<Long, ItemInventory> getProdItemInventory(long itemId) {
962
		try {
963
            // Initialize client for production server
964
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
965
                    ConfigClientKeys.catalog_service_server_port.toString());
966
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
967
            in.shop2020.model.v1.catalog.ItemInventory thriftItemInventory = catalogClient_Prod.getItemInventoryByItemId(itemId);
968
 
969
            Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
970
            ItemInventory warehousedata;
971
            if(thriftItemInventory != null) {
972
            	Map<Long, Long> availabilityMap = thriftItemInventory.getAvailability();
973
            	Map<Long, Long> reservedMap = thriftItemInventory.getReserved();
974
                for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
975
                	warehousedata = new ItemInventory();
976
                	warehousedata.setWarehouseId(availability.getKey());
977
                	warehousedata.setAvailability(availability.getValue());
978
                	warehousedata.setReserved(reservedMap.get(availability.getKey()));
979
                	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
980
                }
981
            }
982
            else {
983
            	itemInventoryMap = null;
984
            }
985
            return itemInventoryMap;
986
        } catch (Exception e) {
987
            logger.error("Error while updating item's risky flag on staging", e);
988
        }
989
		return null;
990
	}
4649 phani.kuma 991
 
992
	@Override
993
	public boolean addAuthorizationLog(long itemId, String username, String message) {
994
		try{
995
            CatalogClient catalogServiceClient = new CatalogClient();
996
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
997
 
998
            return catalogClient.addAuthorizationLog(itemId, username, message);
999
 
1000
        }catch(Exception e){
1001
            logger.error("Error while adding the event for: " + itemId, e);
1002
        }
1003
		return false;
1004
	}
1005
 
1006
	@Override
1007
	public Map<String, String> getConfigdataforPriceCompare() {
1008
		Map<String, String> ConfigMap = new HashMap<String, String>();
1009
		try {
1010
			ConfigMap.put("courier_cost_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_cost_factor.toString()));
1011
            ConfigMap.put("courier_weight_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_weight_factor.toString()));
1012
            ConfigMap.put("breakeven_divisor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_divisor.toString()));
1013
            ConfigMap.put("breakeven_additon_factor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_additon_factor.toString()));
1014
            ConfigMap.put("transfer_price_percentage", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_percentage.toString()));
1015
            ConfigMap.put("transfer_price_factor", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_factor.toString()));
1016
        } catch(ConfigException ce) {
1017
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1018
            ConfigMap.put("courier_cost_factor", "60");
1019
            ConfigMap.put("courier_weight_factor", "500");
1020
            ConfigMap.put("breakeven_divisor", "0.98");
1021
            ConfigMap.put("breakeven_additon_factor", "50");
1022
            ConfigMap.put("transfer_price_percentage", "2");
1023
            ConfigMap.put("transfer_price_factor", "50");
1024
        }
1025
		return ConfigMap;
1026
	}
4957 phani.kuma 1027
 
1028
	@Override
1029
	public List<String> getAllCategories() {
1030
		List<String> categoryList = new ArrayList<String>();
1031
		List<Long> parentCategoryIDs = Arrays.asList(new Long[] {(long) 0,(long) 10001,(long) 10009,(long) 10049});
1032
        try {
1033
            CatalogClient catalogServiceClient = new CatalogClient();
1034
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1035
 
1036
            List<in.shop2020.model.v1.catalog.Category> categories = catalogClient.getAllCategories();
1037
 
1038
            for(in.shop2020.model.v1.catalog.Category category : categories) {
1039
            	if(!parentCategoryIDs.contains(category.getParent_category_id())){
1040
            		categoryList.add(category.getDisplay_name());
1041
            	}
1042
            	else{
1043
            		continue;
1044
            	}
1045
            }
1046
        } catch (Exception e) {
1047
            logger.error("Error while getting all the categories: ", e);
1048
        }
1049
        return categoryList;
1050
	}
1051
 
1052
	@Override
1053
	public List<String> getAllBrands() {
1054
		List<String> brandsList = new ArrayList<String>();
1055
        try {
1056
            CatalogClient catalogServiceClient = new CatalogClient();
1057
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1058
 
1059
            brandsList = catalogClient.getAllBrands();
1060
        } catch (Exception e) {
1061
            logger.error("Error while getting all the categories: ", e);
1062
        }
1063
        return brandsList;
1064
	}
2427 ankur.sing 1065
}