Subversion Repositories SmartDukaan

Rev

Rev 5427 | Rev 5504 | 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());
5460 phani.kuma 514
        tItem.setClearance(item.isClearance());
5384 phani.kuma 515
 
516
        if(item.isItemType()) {
517
        	tItem.setType(ItemType.SERIALIZED);
518
        }
519
        else {
520
        	tItem.setType(ItemType.NON_SERIALIZED);
521
        }
522
 
2489 ankur.sing 523
        if(item.getStartDate() != null) {
524
            tItem.setStartDate(item.getStartDate());
525
        }
3354 chandransh 526
 
5217 amit.gupta 527
        if(item.getExpectedArrivalDate() != null) {
528
        	tItem.setExpectedArrivalDate(item.getExpectedArrivalDate());
529
        } else {
530
        	tItem.unsetComingSoonStartDate();
531
        	tItem.unsetExpectedArrivalDate();
532
        	if(status.COMING_SOON.equals(tItem.getItemStatus())){
533
        		tItem.setItemStatus(status.IN_PROCESS);
534
        	}
535
        }
536
 
537
        if(item.getComingSoonStartDate() != null) {
538
        	tItem.setComingSoonStartDate(item.getComingSoonStartDate());
539
        } else {
540
        	tItem.unsetComingSoonStartDate();
541
        }
542
 
2489 ankur.sing 543
        if(item.getRetireDate() != null) {
544
            tItem.setRetireDate(item.getRetireDate());
545
        }
3354 chandransh 546
 
2126 ankur.sing 547
        tItem.setUpdatedOn(Calendar.getInstance().getTimeInMillis());
548
    }
549
 
550
    @Override
551
    public void pauseItem(long itemId) {
552
        try {
3129 rajveer 553
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 554
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 555
 
2126 ankur.sing 556
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PAUSED);
557
        } catch (Exception e) {
3354 chandransh 558
            logger.error("Error while pausing the item: " + itemId, e);
2126 ankur.sing 559
        }
2359 ankur.sing 560
 
2126 ankur.sing 561
    }
562
 
563
    @Override
564
    public void markInProcess(long itemId) {
565
        try {
3129 rajveer 566
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 567
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 568
 
2126 ankur.sing 569
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.IN_PROCESS);
570
        } catch (Exception e) {
3354 chandransh 571
            logger.error("Error while marking in-process the item: " + itemId, e);
2126 ankur.sing 572
        }
573
    }
2359 ankur.sing 574
 
2126 ankur.sing 575
    @Override
576
    public void phaseoutItem(long itemId) {
577
        try {
3129 rajveer 578
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 579
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 580
 
2126 ankur.sing 581
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.PHASED_OUT);
582
        } catch (Exception e) {
3354 chandransh 583
            logger.error("Error while phasing out the item: " + itemId, e);
2126 ankur.sing 584
        }
585
    }
2359 ankur.sing 586
 
2126 ankur.sing 587
    @Override
588
    public void activateItem(long itemId) {
589
        try {
3129 rajveer 590
            CatalogClient catalogServiceClient = new CatalogClient();
2126 ankur.sing 591
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
2359 ankur.sing 592
 
2126 ankur.sing 593
            catalogClient.changeItemStatus(itemId, Calendar.getInstance().getTimeInMillis(), status.ACTIVE);
594
        } catch (Exception e) {
3354 chandransh 595
            logger.error("Error while activating the item: " + itemId, e);
2126 ankur.sing 596
        }
597
    }
2359 ankur.sing 598
 
599
    @Override
600
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
601
        try {
4354 mandeep.dh 602
            logger.info("Updating risky flag for item id: " + itemId + " to " + risky);
2359 ankur.sing 603
            // Initialize client for staging server
3129 rajveer 604
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 605
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
606
 
607
            // Initialize client for production server
3165 chandransh 608
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
2359 ankur.sing 609
                    ConfigClientKeys.catalog_service_server_port.toString());
610
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
611
            catalogClient.changeItemRiskyFlag(itemId, risky);
612
 
613
            try{
614
                catalogClient_Prod.changeItemRiskyFlag(itemId, risky);
615
            }catch(Exception e){
3354 chandransh 616
                logger.error("Error while updating item's risky flag on production", e);
2359 ankur.sing 617
                // If not able to change risky flag on production, revert on staging and return false (to show error to user).
618
                catalogClient.changeItemRiskyFlag(itemId, !risky);        
619
                return false;
620
            }
621
        } catch (Exception e) {
3354 chandransh 622
            logger.error("Error while updating item's risky flag on staging", e);
2359 ankur.sing 623
            return false;
624
        }
625
        return true;
626
    }
627
 
2427 ankur.sing 628
 
629
    /**
4762 phani.kuma 630
     * Retuns list of Items filtered by category (column is product_group in catalog.item table)
2427 ankur.sing 631
     * This list is used to generate master sheet (excel sheet)
632
     * @param category
4957 phani.kuma 633
     * @param brand
2427 ankur.sing 634
     * @return
635
     */
4957 phani.kuma 636
    public List<Item> getItemsForMasterSheet(String category, String brand) {
2359 ankur.sing 637
        List<Item> itemList = new ArrayList<Item>();
638
        try {
3129 rajveer 639
            CatalogClient catalogServiceClient = new CatalogClient();
2359 ankur.sing 640
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
641
 
4957 phani.kuma 642
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getItemsForMasterSheet(category, brand);
2359 ankur.sing 643
 
644
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
645
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
646
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
3558 rajveer 647
                List<in.shop2020.model.v1.catalog.SourceItemPricing> sip = catalogClient.getAllSourcePricing(thriftItem.getId());
5309 rajveer 648
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim, sip, null, null));
2359 ankur.sing 649
            }
650
        } catch (Exception e) {
4762 phani.kuma 651
            logger.error("Error while getting items by category: " + category, e);
2359 ankur.sing 652
        }
653
        Collections.sort(itemList, new ItemsComparator());
654
        return itemList;
655
    }
656
 
657
    @Override
2427 ankur.sing 658
    public String updateItemOnProduction(Item item) {
3354 chandransh 659
        logger.info("Update item on production call, Item Id: " + item.getId());
2427 ankur.sing 660
 
3922 chandransh 661
        if(!canPushToProduction()) {
662
            // If we can't push to production, then return that message to user
663
            // else move forward with update.
664
            return "Maximum push to production count reached for today";
2427 ankur.sing 665
        }
2359 ankur.sing 666
        try{
3165 chandransh 667
            CatalogClient catalogServiceClient_Prod = new CatalogClient( 
2359 ankur.sing 668
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
2498 ankur.sing 669
                    ConfigClientKeys.catalog_service_server_port.toString());
2359 ankur.sing 670
 
671
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
672
 
673
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
2427 ankur.sing 674
 
2489 ankur.sing 675
            if(item.getMrp() != null) {
2359 ankur.sing 676
                tItem.setMrp(item.getMrp());
677
            }
3354 chandransh 678
 
2489 ankur.sing 679
            if(item.getSellingPrice() != null) {
2359 ankur.sing 680
                tItem.setSellingPrice(item.getSellingPrice());
681
            }
3354 chandransh 682
 
2359 ankur.sing 683
            long rItemId;
684
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
3354 chandransh 685
                logger.error("Error updating item on production, returned Item Id: " + rItemId);
2427 ankur.sing 686
                return "Error updating item prices on production";
2359 ankur.sing 687
            }
3354 chandransh 688
            logger.info("Successfully updated item: " + item.getId());
2359 ankur.sing 689
 
3911 chandransh 690
            StringBuilder sb = new StringBuilder();
691
 
2359 ankur.sing 692
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
693
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
694
                in.shop2020.model.v1.catalog.VendorItemPricing tVendorPricing;
695
                for(VendorPricings v : vendorPricings.values()) {
696
                    tVendorPricing = new in.shop2020.model.v1.catalog.VendorItemPricing();
697
                    tVendorPricing.setVendorId(v.getVendorId());
698
                    tVendorPricing.setItemId(item.getId());
699
                    tVendorPricing.setMop(v.getMop());
700
                    tVendorPricing.setTransferPrice(v.getTransferPrice());
701
                    tVendorPricing.setDealerPrice(v.getDealerPrice());
702
                    catalogClient_Prod.addVendorItemPricing(tVendorPricing);
3354 chandransh 703
                    logger.info("VendorItemPricing updated. " + tVendorPricing.toString());
2489 ankur.sing 704
                    sb.append("[VId:" + v.getVendorId() + ",MOP=" + v.getMop() + ",TP=" + v.getTransferPrice() + ",DP=" + v.getDealerPrice() + "], ");
2359 ankur.sing 705
                }
706
            }
3911 chandransh 707
 
4993 mandeep.dh 708
            Map<String, VendorItemMapping> vendorMappings = item.getVendorKeysMap();
709
            if(vendorMappings != null && !vendorMappings.isEmpty()) {
710
                in.shop2020.model.v1.catalog.VendorItemMapping tVendorMapping;
711
                for(VendorItemMapping vendorItemMapping : vendorMappings.values()) {
712
                    tVendorMapping = new in.shop2020.model.v1.catalog.VendorItemMapping();
713
                    tVendorMapping.setVendorId(vendorItemMapping.getVendorId());
714
                    tVendorMapping.setItemId(item.getId());
715
                    tVendorMapping.setItemKey(vendorItemMapping.getItemKey());
716
                    catalogClient_Prod.addVendorItemMapping(vendorItemMapping.getItemKey(), tVendorMapping);
717
                    logger.info("VendorItemMapping updated. " + tVendorMapping.toString());
718
                    sb.append("[VId:" + vendorItemMapping.getVendorId() + ",ItemKey=" + vendorItemMapping.getItemKey() + "], ");
719
                }
720
            }
721
 
3911 chandransh 722
            Map<Long, SourcePricings> sourcePricings = item.getSourcePricesMap();
723
            if(sourcePricings != null && !sourcePricings.isEmpty()){
724
                in.shop2020.model.v1.catalog.SourceItemPricing tSourcePricing;
725
                for(SourcePricings s : sourcePricings.values()){
726
                    tSourcePricing = new in.shop2020.model.v1.catalog.SourceItemPricing();
727
                    tSourcePricing.setSourceId(s.getSourceId());
728
                    tSourcePricing.setItemId(item.getId());
729
                    tSourcePricing.setMrp(s.getMrp());
730
                    tSourcePricing.setSellingPrice(s.getSellingPrice());
731
                    catalogClient_Prod.addSourceItemPricing(tSourcePricing);
732
                    logger.info("SourceItemPricing updated. " + tSourcePricing.toString());
733
                    sb.append("[SId:" + s.getSourceId() + ",MRP=" + s.getMrp() + ",SP=" + s.getSellingPrice() + "], ");
734
                }
735
            }
736
 
3922 chandransh 737
            pushToProdLogger.info("Id=" + item.getId() + ",MRP=" + item.getMrp() + ",SP=" + " " + item.getSellingPrice() + sb.toString());
2359 ankur.sing 738
        } catch (Exception e) {
3354 chandransh 739
            logger.error("Error while updating prices on production", e);
2427 ankur.sing 740
            return "Error while updating all prices on production. Some of the prices may have been  updated";
2359 ankur.sing 741
        }
2489 ankur.sing 742
 
3907 chandransh 743
        try {
744
            ContentClient contentServiceClient = new ContentClient();
745
            ContentService.Client contentClient = contentServiceClient.getClient();
746
 
747
            contentClient.pushContentToProduction(item.getCatalogItemId());
748
        } catch (TTransportException e) {
749
            logger.error("Error while establishing connection to the content server", e);
750
            return "Error generating content. Prices updated successfully.";
751
        } catch (TException e) {
752
            logger.error("Error while pushing content to production", e);
753
            return "Error generating content. Prices updated successfully.";
754
        } catch (ContentServiceException e) {
755
            logger.error("Error while pushing content to production", e);
756
            return "Error generating content. Prices updated successfully.";
757
        }
758
 
2427 ankur.sing 759
        return "Prices updated and content generated successfully";
2359 ankur.sing 760
    }
2427 ankur.sing 761
 
2489 ankur.sing 762
    /**
3354 chandransh 763
     * Returns list of items with a particular status.
764
     * @param st
765
     * @return
766
     */
3850 chandransh 767
    private List<Item> getItemsByStatus(status st, int start, int limit) {
3354 chandransh 768
        List<Item> itemList = new ArrayList<Item>();
769
        try {
770
            CatalogClient catalogServiceClient = new CatalogClient();
771
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
772
 
3850 chandransh 773
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItemsByStatusInRange(st, start, limit);
3354 chandransh 774
 
775
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
5309 rajveer 776
                itemList.add(getItemFromThriftItem(thriftItem, null, null, null, null, null));
3354 chandransh 777
            }
778
        } catch (Exception e) {
779
            logger.error("Error while getting items by status: " + st, e);
780
        }
781
        return itemList;
782
    }
783
 
784
    /**
785
     * Creates a new Item object and populates its attributes from thrift item passed as parameter.
4423 phani.kuma 786
     * Also creates a Map each for VendorItemPricing, VendorItemMapping, SourceItemPricing and SimilarItems and adds these maps to the
3354 chandransh 787
     * new Item object.
788
     * @param thriftItem
789
     * @param tVendorPricings
790
     * @param tVendorMappings
4423 phani.kuma 791
     * @param tSourceMappings
792
     * @param tSimilarItems
3354 chandransh 793
     * @return item object with attributes copied from thrift item object.
794
     */
795
    private Item getItemFromThriftItem(in.shop2020.model.v1.catalog.Item thriftItem, 
796
            List<in.shop2020.model.v1.catalog.VendorItemPricing> tVendorPricings, 
3558 rajveer 797
            List<in.shop2020.model.v1.catalog.VendorItemMapping> tVendorMappings,
4423 phani.kuma 798
            List<in.shop2020.model.v1.catalog.SourceItemPricing> tSourceMappings,
5309 rajveer 799
            List<in.shop2020.model.v1.catalog.Item> tSimilarItems, in.shop2020.model.v1.catalog.ItemInventory tItemInventory){
3354 chandransh 800
 
801
        Map<String, VendorItemMapping> vItemMap = new HashMap<String, VendorItemMapping>();
802
        VendorItemMapping vItemMapping;
803
        if(tVendorMappings != null) {
804
            for(in.shop2020.model.v1.catalog.VendorItemMapping vim : tVendorMappings) {
805
                vItemMapping = new VendorItemMapping();
806
                vItemMapping.setVendorId(vim.getVendorId());
807
                vItemMapping.setItemKey(vim.getItemKey());
808
                vItemMap.put(vItemMapping.getVendorId() + Item.KEY_SEPARATOR + vim.getItemKey(), vItemMapping);
809
            }
810
        }
811
 
812
        Map<Long, VendorPricings> vendorPricingMap = new HashMap<Long, VendorPricings>();
813
        VendorPricings vPricings;
814
        if(tVendorPricings != null) {
815
            for(in.shop2020.model.v1.catalog.VendorItemPricing vip : tVendorPricings) {
816
                vPricings = new VendorPricings();
817
                vPricings.setVendorId(vip.getVendorId());
818
                vPricings.setMop(vip.getMop());
819
                vPricings.setDealerPrice(vip.getDealerPrice());
820
                vPricings.setTransferPrice(vip.getTransferPrice());
821
                vendorPricingMap.put(vPricings.getVendorId(), vPricings);
822
            }
823
        }
824
 
3558 rajveer 825
        Map<Long, SourcePricings> sourcePricingMap = new HashMap<Long, SourcePricings>();
826
        SourcePricings sPricings;
827
        if(tSourceMappings != null) {
828
            for(in.shop2020.model.v1.catalog.SourceItemPricing sip : tSourceMappings) {
829
                sPricings = new SourcePricings();
830
                sPricings.setSourceId(sip.getSourceId());
831
                sPricings.setMrp(sip.getMrp());
832
                sPricings.setSellingPrice(sip.getSellingPrice());
833
                sourcePricingMap.put(sPricings.getSourceId(), sPricings);
834
            }
835
        }
4423 phani.kuma 836
 
837
        Map<Long, Item> SimilarItemslist = new HashMap<Long, Item>();
838
        Item sItems;
839
        if(tSimilarItems != null) {
840
            for(in.shop2020.model.v1.catalog.Item sit : tSimilarItems) {
841
            	sItems = new Item();
842
            	sItems.setCatalogItemId(sit.getCatalogItemId());
843
            	sItems.setProductGroup(sit.getProductGroup());
844
            	sItems.setBrand(sit.getBrand());
845
            	sItems.setModelNumber(sit.getModelNumber());
846
            	sItems.setModelName(sit.getModelName());
847
            	sItems.setContentCategory(CategoryManager.getCategoryManager().getCategoryLabel(sit.getCategory()));
848
            	SimilarItemslist.put(sit.getCatalogItemId(), sItems);
849
            }
850
        }
3558 rajveer 851
 
4431 phani.kuma 852
        Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
853
        ItemInventory warehousedata;
5309 rajveer 854
        if(tItemInventory != null) {
855
        	Map<Long, Long> availabilityMap = tItemInventory.getAvailability();
856
        	Map<Long, Long> reservedMap = tItemInventory.getReserved();
4431 phani.kuma 857
            for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
858
            	warehousedata = new ItemInventory();
859
            	warehousedata.setWarehouseId(availability.getKey());
860
            	warehousedata.setAvailability(availability.getValue());
861
            	warehousedata.setReserved(reservedMap.get(availability.getKey()));
862
            	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
863
            }
864
        }
865
        else {
866
        	itemInventoryMap = null;
867
        }
868
 
3354 chandransh 869
        Item item = new Item(thriftItem.getId(),
870
                thriftItem.getProductGroup(),
871
                thriftItem.getBrand(),
872
                thriftItem.getModelNumber(),
873
                thriftItem.getModelName(),
874
                thriftItem.getColor(),
875
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
876
                thriftItem.getCategory(),
877
                thriftItem.getComments(),
878
                thriftItem.getCatalogItemId(),
879
                thriftItem.getFeatureId(),
880
                thriftItem.getFeatureDescription(),
881
                thriftItem.isSetMrp() ? thriftItem.getMrp() : null,
882
                thriftItem.isSetSellingPrice() ? thriftItem.getSellingPrice() : null,
883
                thriftItem.isSetWeight() ? thriftItem.getWeight() : null,
884
                thriftItem.getAddedOn(),
885
                thriftItem.getStartDate(),
5217 amit.gupta 886
                thriftItem.isSetComingSoonStartDate() ? thriftItem.getComingSoonStartDate() : null ,
887
                thriftItem.isSetExpectedArrivalDate() ? thriftItem.getExpectedArrivalDate() : null ,
3354 chandransh 888
                thriftItem.getRetireDate(),
889
                thriftItem.getUpdatedOn(),
890
                thriftItem.getItemStatus().name(),
891
                thriftItem.getItemStatus().getValue(),
892
                thriftItem.getStatus_description(),
893
                thriftItem.getBestDealText(),
894
                thriftItem.isSetBestDealValue() ? thriftItem.getBestDealValue() : null,
895
                thriftItem.isSetBestSellingRank() ? thriftItem.getBestSellingRank() : null,
896
                thriftItem.isDefaultForEntity(),
897
                thriftItem.isRisky(),
3359 chandransh 898
                thriftItem.isSetExpectedDelay() ? thriftItem.getExpectedDelay() : null,
4506 phani.kuma 899
                thriftItem.isIsWarehousePreferenceSticky(),
5384 phani.kuma 900
                thriftItem.isHasItemNo(),
901
                ItemType.SERIALIZED.equals(thriftItem.getType()),
5460 phani.kuma 902
                thriftItem.isClearance(),
4506 phani.kuma 903
                thriftItem.isSetPreferredVendor() ? thriftItem.getPreferredVendor() : null,
4431 phani.kuma 904
                itemInventoryMap,
3354 chandransh 905
                vendorPricingMap,
3558 rajveer 906
                vItemMap,
4423 phani.kuma 907
                sourcePricingMap,
908
                SimilarItemslist);
3354 chandransh 909
        return item;
910
    }
911
 
912
    /**
2489 ankur.sing 913
     * 
3922 chandransh 914
     * @return True if push to production count is less than maximum allowed.
2489 ankur.sing 915
     */
3922 chandransh 916
    private boolean canPushToProduction() {
917
        int count = getPushToProdCount(logFile);
918
        return count < maxCount;
2427 ankur.sing 919
    }
3922 chandransh 920
 
2489 ankur.sing 921
    /**
922
     * If this is the first attempt to push to production for current date, returns 0
923
     * else reads number of lines from daily rolling log file to get the count.
924
     * @return push to production count for current date
925
     */
2498 ankur.sing 926
    private int getPushToProdCount(String logfile) {
927
        // logfile is "/var/log/services/pushToProductionCount/pushToProd.log"
928
        // this should also be set in log4j.properties
2427 ankur.sing 929
        int lineCount = 0;
2498 ankur.sing 930
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2427 ankur.sing 931
        Date currentDate = Calendar.getInstance().getTime();
932
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
933
            pushToProdDate = currentDate;
934
            return lineCount;
935
        }
936
        try {
2498 ankur.sing 937
            BufferedReader br = new BufferedReader(new FileReader(logfile));
2427 ankur.sing 938
            while (br.readLine() != null) {
939
                lineCount++;
940
            }
941
        } catch (IOException e) {
3354 chandransh 942
            logger.error("Error while reading the log file", e);
2427 ankur.sing 943
        }
944
        return lineCount;
945
    }
4423 phani.kuma 946
 
947
	@Override
948
	public boolean deleteSimilarItem(long itemId, long catalogItemId) {
949
		try{
950
            CatalogClient catalogServiceClient = new CatalogClient();
951
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
952
 
953
            return catalogClient.deleteSimilarItem(itemId, catalogItemId);
954
 
955
        }catch(Exception e){
956
            logger.error("Error while deleting the SimilarItems for: " + itemId, e);
957
        }
958
        return false;
959
	}
960
 
961
	@Override
962
	public Item addSimilarItem(long itemId, long catalogItemId) {
963
		try{
964
            CatalogClient catalogServiceClient = new CatalogClient();
965
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
966
            in.shop2020.model.v1.catalog.Item thriftItem = catalogClient.addSimilarItem(itemId, catalogItemId);;
967
 
5309 rajveer 968
            return getItemFromThriftItem(thriftItem, null, null, null, null, null);
4423 phani.kuma 969
        }catch(Exception e){
970
            logger.error("Error while adding the SimilarItems for: " + itemId, e);
971
        }
972
        return null;
973
	}
4431 phani.kuma 974
 
975
	@Override
976
	public Map<Long, ItemInventory> getProdItemInventory(long itemId) {
977
		try {
978
            // Initialize client for production server
979
            CatalogClient catalogServiceClient_Prod = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(),
980
                    ConfigClientKeys.catalog_service_server_port.toString());
981
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
982
            in.shop2020.model.v1.catalog.ItemInventory thriftItemInventory = catalogClient_Prod.getItemInventoryByItemId(itemId);
983
 
984
            Map<Long, ItemInventory> itemInventoryMap = new HashMap<Long, ItemInventory>();
985
            ItemInventory warehousedata;
986
            if(thriftItemInventory != null) {
987
            	Map<Long, Long> availabilityMap = thriftItemInventory.getAvailability();
988
            	Map<Long, Long> reservedMap = thriftItemInventory.getReserved();
989
                for(Entry<Long, Long> availability : availabilityMap.entrySet()) {
990
                	warehousedata = new ItemInventory();
991
                	warehousedata.setWarehouseId(availability.getKey());
992
                	warehousedata.setAvailability(availability.getValue());
993
                	warehousedata.setReserved(reservedMap.get(availability.getKey()));
994
                	itemInventoryMap.put(warehousedata.getWarehouseId(), warehousedata);
995
                }
996
            }
997
            else {
998
            	itemInventoryMap = null;
999
            }
1000
            return itemInventoryMap;
1001
        } catch (Exception e) {
1002
            logger.error("Error while updating item's risky flag on staging", e);
1003
        }
1004
		return null;
1005
	}
4649 phani.kuma 1006
 
1007
	@Override
1008
	public boolean addAuthorizationLog(long itemId, String username, String message) {
1009
		try{
1010
            CatalogClient catalogServiceClient = new CatalogClient();
1011
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1012
 
1013
            return catalogClient.addAuthorizationLog(itemId, username, message);
1014
 
1015
        }catch(Exception e){
1016
            logger.error("Error while adding the event for: " + itemId, e);
1017
        }
1018
		return false;
1019
	}
1020
 
1021
	@Override
1022
	public Map<String, String> getConfigdataforPriceCompare() {
1023
		Map<String, String> ConfigMap = new HashMap<String, String>();
1024
		try {
1025
			ConfigMap.put("courier_cost_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_cost_factor.toString()));
1026
            ConfigMap.put("courier_weight_factor", ConfigClient.getClient().get(ConfigClientKeys.courier_weight_factor.toString()));
1027
            ConfigMap.put("breakeven_divisor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_divisor.toString()));
1028
            ConfigMap.put("breakeven_additon_factor", ConfigClient.getClient().get(ConfigClientKeys.breakeven_additon_factor.toString()));
1029
            ConfigMap.put("transfer_price_percentage", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_percentage.toString()));
1030
            ConfigMap.put("transfer_price_factor", ConfigClient.getClient().get(ConfigClientKeys.transfer_price_factor.toString()));
1031
        } catch(ConfigException ce) {
1032
            logger.error("Unable to connect to the config server. Setting sensible defaults.", ce);
1033
            ConfigMap.put("courier_cost_factor", "60");
1034
            ConfigMap.put("courier_weight_factor", "500");
1035
            ConfigMap.put("breakeven_divisor", "0.98");
1036
            ConfigMap.put("breakeven_additon_factor", "50");
1037
            ConfigMap.put("transfer_price_percentage", "2");
1038
            ConfigMap.put("transfer_price_factor", "50");
1039
        }
1040
		return ConfigMap;
1041
	}
4957 phani.kuma 1042
 
1043
	@Override
1044
	public List<String> getAllCategories() {
1045
		List<String> categoryList = new ArrayList<String>();
1046
		List<Long> parentCategoryIDs = Arrays.asList(new Long[] {(long) 0,(long) 10001,(long) 10009,(long) 10049});
1047
        try {
1048
            CatalogClient catalogServiceClient = new CatalogClient();
1049
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1050
 
1051
            List<in.shop2020.model.v1.catalog.Category> categories = catalogClient.getAllCategories();
1052
 
1053
            for(in.shop2020.model.v1.catalog.Category category : categories) {
1054
            	if(!parentCategoryIDs.contains(category.getParent_category_id())){
1055
            		categoryList.add(category.getDisplay_name());
1056
            	}
1057
            	else{
1058
            		continue;
1059
            	}
1060
            }
1061
        } catch (Exception e) {
1062
            logger.error("Error while getting all the categories: ", e);
1063
        }
1064
        return categoryList;
1065
	}
1066
 
1067
	@Override
1068
	public List<String> getAllBrands() {
1069
		List<String> brandsList = new ArrayList<String>();
1070
        try {
1071
            CatalogClient catalogServiceClient = new CatalogClient();
1072
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
1073
 
1074
            brandsList = catalogClient.getAllBrands();
1075
        } catch (Exception e) {
1076
            logger.error("Error while getting all the categories: ", e);
1077
        }
1078
        return brandsList;
1079
	}
5427 amit.gupta 1080
 
2427 ankur.sing 1081
}