Subversion Repositories SmartDukaan

Rev

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