Subversion Repositories SmartDukaan

Rev

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