Subversion Repositories SmartDukaan

Rev

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