Subversion Repositories SmartDukaan

Rev

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