Subversion Repositories SmartDukaan

Rev

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