Subversion Repositories SmartDukaan

Rev

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