Subversion Repositories SmartDukaan

Rev

Rev 2378 | Rev 2489 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2378 Rev 2427
Line 11... Line 11...
11
import in.shop2020.thrift.clients.CatalogServiceClient;
11
import in.shop2020.thrift.clients.CatalogServiceClient;
12
import in.shop2020.thrift.clients.config.ConfigClient;
12
import in.shop2020.thrift.clients.config.ConfigClient;
13
import in.shop2020.utils.CategoryManager;
13
import in.shop2020.utils.CategoryManager;
14
import in.shop2020.utils.ConfigClientKeys;
14
import in.shop2020.utils.ConfigClientKeys;
15
 
15
 
16
import java.io.ByteArrayOutputStream;
16
import java.io.BufferedReader;
-
 
17
import java.io.FileReader;
17
import java.io.IOException;
18
import java.io.IOException;
-
 
19
import java.text.DateFormat;
-
 
20
import java.text.SimpleDateFormat;
18
import java.util.ArrayList;
21
import java.util.ArrayList;
19
import java.util.Calendar;
22
import java.util.Calendar;
20
import java.util.Collections;
23
import java.util.Collections;
-
 
24
import java.util.Date;
21
import java.util.HashMap;
25
import java.util.HashMap;
22
import java.util.List;
26
import java.util.List;
23
import java.util.Map;
27
import java.util.Map;
24
import java.util.Map.Entry;
28
import java.util.Map.Entry;
25
 
29
 
26
import javax.servlet.ServletOutputStream;
30
import org.apache.log4j.Logger;
27
import javax.servlet.http.HttpServletResponse;
-
 
28
 
31
 
29
import com.google.gwt.core.client.GWT;
32
import com.google.gwt.core.client.GWT;
30
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
33
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
31
 
34
 
32
@SuppressWarnings("serial")
35
@SuppressWarnings("serial")
33
public class CatalogServiceImpl extends RemoteServiceServlet implements CatalogService {
36
public class CatalogServiceImpl extends RemoteServiceServlet implements CatalogService {
34
 
37
 
-
 
38
    private static Date pushToProdDate;
-
 
39
    
35
    public List<Item> getAllItems(){
40
    public List<Item> getAllItems(){
36
        List<Item> itemList = new ArrayList<Item>();
41
        List<Item> itemList = new ArrayList<Item>();
37
 
42
 
38
        try {
43
        try {
39
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
44
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
Line 59... Line 64...
59
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
64
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
60
 
65
 
61
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItems(true);
66
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getAllItems(true);
62
 
67
 
63
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
68
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
64
                List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
-
 
65
                List<in.shop2020.model.v1.catalog.VendorItemMapping> vim = catalogClient.getVendorItemMappings(thriftItem.getId());
-
 
66
                itemList.add(getItemFromThriftItem(thriftItem, vip, vim));
69
                itemList.add(getItemFromThriftItem(thriftItem, null, null));
67
            }
70
            }
68
        } catch (Exception e) {
71
        } catch (Exception e) {
69
            e.printStackTrace();
72
            e.printStackTrace();
70
        }
73
        }
71
        Collections.sort(itemList, new ItemsComparator());
74
        Collections.sort(itemList, new ItemsComparator());
Line 134... Line 137...
134
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
137
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
135
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
138
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
136
 
139
 
137
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();
140
            List<in.shop2020.model.v1.catalog.Item> thriftItems = catalogClient.getBestSellers();
138
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
141
            for(in.shop2020.model.v1.catalog.Item thriftItem : thriftItems) {
139
                //List<in.shop2020.model.v1.catalog.VendorItemPricing> vip = catalogClient.getAllItemPricing(thriftItem.getId());
-
 
140
                itemList.add(getItemFromThriftItem(thriftItem, null, null));
142
                itemList.add(getItemFromThriftItem(thriftItem, null, null));
141
            }
143
            }
142
        } catch(Exception e){
144
        } catch(Exception e){
143
            e.printStackTrace();
145
            e.printStackTrace();
144
        }
146
        }
Line 219... Line 221...
219
                thriftItem.getProductGroup(),
221
                thriftItem.getProductGroup(),
220
                thriftItem.getBrand(),
222
                thriftItem.getBrand(),
221
                thriftItem.getModelNumber(),
223
                thriftItem.getModelNumber(),
222
                thriftItem.getModelName(),
224
                thriftItem.getModelName(),
223
                thriftItem.getColor(),
225
                thriftItem.getColor(),
224
                //thriftItem.getCategory(),
-
 
225
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
226
                CategoryManager.getCategoryManager().getCategoryLabel(thriftItem.getCategory()),
226
                thriftItem.getCategory(),
227
                thriftItem.getCategory(),
227
                thriftItem.getComments(),
228
                thriftItem.getComments(),
228
                thriftItem.getCatalogItemId(),
229
                thriftItem.getCatalogItemId(),
229
                thriftItem.getFeatureId(),
230
                thriftItem.getFeatureId(),
Line 259... Line 260...
259
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
260
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
260
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
261
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
261
 
262
 
262
            in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());
263
            in.shop2020.model.v1.catalog.Item tItem = catalogClient.getItem(item.getId());
263
            setThriftItemParams(tItem, item);
264
            setThriftItemParams(tItem, item);
264
            //          tItem.setAddedOn(item.getAddedOn());
-
 
265
 
-
 
266
            //          tItem.setCatalogItemId(item.getCatalogItemId());
-
 
267
            //          tItem.setCategory(item.getCategory());
-
 
268
 
-
 
269
            //          tItem.setFeatureId(item.getFeatureId());
-
 
270
            //          tItem.setFeatureDescription(item.getFeatureDescription());
-
 
271
 
265
 
272
            long rItemId;
266
            long rItemId;
273
            if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {
267
            if((rItemId = catalogClient.updateItem(tItem)) != item.getId()) {
274
                GWT.log("Error updating item, returned Item Id: " + rItemId);
268
                GWT.log("Error updating item, returned Item Id: " + rItemId);
275
                return false;
269
                return false;
Line 309... Line 303...
309
                }
303
                }
310
            }
304
            }
311
 
305
 
312
        }catch(Exception e){
306
        }catch(Exception e){
313
            e.printStackTrace();
307
            e.printStackTrace();
-
 
308
            return false;
314
        }
309
        }
315
        return true;
310
        return true;
316
    }
311
    }
317
 
312
 
318
 
313
 
Line 403... Line 398...
403
        try {
398
        try {
404
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
399
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
405
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
400
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
406
            return catalogClient.checkSimilarItem(productGroup, brand, modelNumber, color);
401
            return catalogClient.checkSimilarItem(productGroup, brand, modelNumber, color);
407
        } catch (Exception e) {
402
        } catch (Exception e) {
408
            // TODO Auto-generated catch block
-
 
409
            e.printStackTrace();
403
            e.printStackTrace();
410
        }
404
        }
411
        return 0;
405
        return 0;
412
    }
406
    }
413
 
407
 
Line 509... Line 503...
509
        } catch (Exception e) {
503
        } catch (Exception e) {
510
            e.printStackTrace();
504
            e.printStackTrace();
511
        }
505
        }
512
    }
506
    }
513
 
507
 
514
    /*@Override
-
 
515
    public void uploadItems(String filename, long vendorId, String category, boolean fullUpdate, boolean dryRun, String suppliedProductGroup) {
-
 
516
        try {
-
 
517
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
-
 
518
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
-
 
519
 
-
 
520
            catalogClient.runItemLoader(filename, vendorId, category, fullUpdate, dryRun, suppliedProductGroup);
-
 
521
        } catch (Exception e) {
-
 
522
            e.printStackTrace();
-
 
523
        }
-
 
524
 
-
 
525
    }*/
-
 
526
 
-
 
527
    @Override
508
    @Override
528
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
509
    public boolean changeItemRiskyFlag(long itemId, boolean risky) {
529
        try {
510
        try {
530
            // Initialize client for staging server
511
            // Initialize client for staging server
531
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
512
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
Line 551... Line 532...
551
            return false;
532
            return false;
552
        }
533
        }
553
        return true;
534
        return true;
554
    }
535
    }
555
 
536
 
-
 
537
    
-
 
538
    /**
-
 
539
     * Retuns list of Items filtered by Vendor category (column is hotspotCategory in catalog.item table)
-
 
540
     * This list is used to generate master sheet (excel sheet)
-
 
541
     * @param category
-
 
542
     * @return
-
 
543
     */
556
    List<Item> getItemsByVendorCategory(String category) {
544
    List<Item> getItemsByVendorCategory(String category) {
557
        List<Item> itemList = new ArrayList<Item>();
545
        List<Item> itemList = new ArrayList<Item>();
558
        try {
546
        try {
559
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
547
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
560
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
548
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
Line 571... Line 559...
571
        }
559
        }
572
        Collections.sort(itemList, new ItemsComparator());
560
        Collections.sort(itemList, new ItemsComparator());
573
        return itemList;
561
        return itemList;
574
    }
562
    }
575
    
563
    
-
 
564
    /**
-
 
565
     * Returns list of items with a particular status.
-
 
566
     * @param st
-
 
567
     * @return
-
 
568
     */
576
    List<Item> getItemsByStatus(status st) {
569
    List<Item> getItemsByStatus(status st) {
577
        List<Item> itemList = new ArrayList<Item>();
570
        List<Item> itemList = new ArrayList<Item>();
578
        try {
571
        try {
579
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
572
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
580
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
573
            InventoryService.Client catalogClient = catalogServiceClient.getClient();
Line 590... Line 583...
590
        Collections.sort(itemList, new ItemsComparator());
583
        Collections.sort(itemList, new ItemsComparator());
591
        return itemList;
584
        return itemList;
592
    }
585
    }
593
 
586
 
594
    @Override
587
    @Override
595
    public boolean updateItemOnProduction(Item item) {
588
    public String updateItemOnProduction(Item item) {
596
        GWT.log("Update item on production, Item Id: " + item.getId());
589
        GWT.log("Update item on production call, Item Id: " + item.getId());
-
 
590
        
-
 
591
        String errMsg = checkPushToProdCount();
-
 
592
        // If errMsg is not null, then return that message to user
-
 
593
        // else move forward with update.
-
 
594
        if(errMsg != null) {
-
 
595
            return errMsg;
-
 
596
        }
-
 
597
        log(item);
597
        try{
598
        try{
598
            CatalogServiceClient catalogServiceClient_Prod = new CatalogServiceClient(CatalogServiceClient.class.getSimpleName(), 
599
            /*CatalogServiceClient catalogServiceClient_Prod = new CatalogServiceClient(CatalogServiceClient.class.getSimpleName(), 
599
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
600
                    ConfigClientKeys.catalog_service_server_host_prod.toString(),
600
                    ConfigClientKeys.catalog_service_server_port.toString());
601
                    ConfigClientKeys.catalog_service_server_port.toString());*/
601
            
602
            
602
            //for testing, put catalog_service_server_port_test in shop2020.cfg
603
            //for testing, put catalog_service_server_port_test in shop2020.cfg
603
            /*CatalogServiceClient catalogServiceClient_Prod = new CatalogServiceClient(CatalogServiceClient.class.getSimpleName(), 
604
            CatalogServiceClient catalogServiceClient_Prod = new CatalogServiceClient(CatalogServiceClient.class.getSimpleName(), 
604
                    ConfigClientKeys.catalog_service_server_host.toString(), "catalog_service_server_port_test");*/
605
                    ConfigClientKeys.catalog_service_server_host.toString(), "catalog_service_server_port_test");
605
            
606
            
606
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
607
            InventoryService.Client catalogClient_Prod = catalogServiceClient_Prod.getClient();
607
 
608
 
608
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
609
            in.shop2020.model.v1.catalog.Item tItem = catalogClient_Prod.getItem(item.getId());
-
 
610
            
-
 
611
            // -1 is used to signify that the corresponding double/float/long/int field is NULL
-
 
612
            // It is done so because these fields cannot be set as null in Item bean and thrift object item.
609
            if(item.getMrp() != -1) {
613
            if(item.getMrp() != -1) {
610
                tItem.setMrp(item.getMrp());
614
                tItem.setMrp(item.getMrp());
611
            } else {
615
            } else {
612
                tItem.setMrpIsSet(false);
616
                tItem.setMrpIsSet(false);
613
            }
617
            }
Line 633... Line 637...
633
            }
637
            }
634
 
638
 
635
            long rItemId;
639
            long rItemId;
636
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
640
            if((rItemId = catalogClient_Prod.updateItem(tItem)) != item.getId()) {
637
                GWT.log("Error updating item, returned Item Id: " + rItemId);
641
                GWT.log("Error updating item, returned Item Id: " + rItemId);
638
                return false;
642
                return "Error updating item prices on production";
639
            }
643
            }
640
            GWT.log("Item updated successfully, Item Id: " + item.getId());
644
            GWT.log("Item updated successfully, Item Id: " + item.getId());
641
 
645
 
642
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
646
            Map<Long, VendorPricings> vendorPricings = item.getVendorPricesMap();
643
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
647
            if(vendorPricings != null && !vendorPricings.isEmpty()) {
Line 654... Line 658...
654
                            tVendorPricing.getMop() + ", " + tVendorPricing.getTransferPrice() + ", " + tVendorPricing.getDealerPrice());
658
                            tVendorPricing.getMop() + ", " + tVendorPricing.getTransferPrice() + ", " + tVendorPricing.getDealerPrice());
655
                }
659
                }
656
            }
660
            }
657
        } catch (Exception e) {
661
        } catch (Exception e) {
658
            e.printStackTrace();
662
            e.printStackTrace();
659
            return false;
663
            return "Error while updating all prices on production. Some of the prices may have been  updated";
660
        }
664
        }
661
        
665
        /*
662
        // Push the content of one item to production 
666
        // Push the content of one item to production 
663
        Process p;
667
        Process p;
664
		try {
668
		try {
665
		    String scriptPath = ConfigClient.getClient().get(ConfigClientKeys.PUSH_PRICES_TO_PROD_SCRIPT_PATH.toString());
669
		    String scriptPath = ConfigClient.getClient().get(ConfigClientKeys.PUSH_PRICES_TO_PROD_SCRIPT_PATH.toString());
666
			p = Runtime.getRuntime().exec(scriptPath + " " + item.getCatalogItemId());
670
			p = Runtime.getRuntime().exec(scriptPath + " " + item.getCatalogItemId());
667
			p.waitFor();
671
			p.waitFor();
668
		} catch (IOException e) {
672
		} catch (IOException e) {
669
			GWT.log("Not able to update content on production");
673
			GWT.log("Not able to update content on production");
670
			e.printStackTrace();
674
			e.printStackTrace();
671
			return false;
675
			return "Error generating content. Prices updated successfully.";
672
		} catch (InterruptedException e) {
676
		} catch (InterruptedException e) {
673
			GWT.log("Not able to update content on production");
677
			GWT.log("Not able to update content on production");
674
			e.printStackTrace();
678
			e.printStackTrace();
675
			return false;
679
			return "Error generating content. Prices updated successfully.";
676
		} catch (ConfigException e) {
680
		} catch (ConfigException e) {
677
            // TODO Auto-generated catch block
681
            // TODO Auto-generated catch block
678
            e.printStackTrace();
682
            e.printStackTrace();
679
            return false;
683
            return "Error getting content script path";
680
        } 
684
        }*/
681
        
-
 
682
        return true;
685
        return "Prices updated and content generated successfully";
683
    }
686
    }
-
 
687
    
-
 
688
    private String checkPushToProdCount() {
-
 
689
        int count = 0, maxCount;
-
 
690
        try {
-
 
691
            //logfile = ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_log_file.toString());
-
 
692
            maxCount = Integer.parseInt(ConfigClient.getClient().get(ConfigClientKeys.push_prices_to_prod_max_count.toString()));
-
 
693
            count = getPushToProdCount();
-
 
694
            if(count >= maxCount) {
-
 
695
                return "Maximum push to production count reached for today";
-
 
696
            }
-
 
697
        } catch (ConfigException e1) {
-
 
698
            e1.printStackTrace();
-
 
699
            return "Error getting push to production parameters";
-
 
700
        }
-
 
701
        return null;
684
}
702
    }
-
 
703
    
-
 
704
    private void log(Item item) {
-
 
705
        Logger log = Logger.getLogger(CatalogServiceImpl.class);
-
 
706
        log.info("MRP: " + item.getMrp() + ", SP: " + item.getSellingPrice());
-
 
707
    }
685
 
708
    
-
 
709
    private int getPushToProdCount() {
-
 
710
        int lineCount = 0;
-
 
711
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
-
 
712
        Date currentDate = Calendar.getInstance().getTime();
-
 
713
        if(pushToProdDate == null ||  !dateFormat.format(pushToProdDate).equals(dateFormat.format(currentDate))) {
-
 
714
            pushToProdDate = currentDate;
-
 
715
            return lineCount;
-
 
716
        }
-
 
717
        try {
-
 
718
            BufferedReader br = new BufferedReader(new FileReader("/home/ankur/logs/pushToProd.log"));
-
 
719
            while (br.readLine() != null) {
-
 
720
                lineCount++;
-
 
721
            }
-
 
722
        } catch (IOException e) {
-
 
723
            e.printStackTrace();
-
 
724
        }
-
 
725
        return lineCount;
-
 
726
    }
686
 
727
}
687
728