Subversion Repositories SmartDukaan

Rev

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

Rev 1044 Rev 2578
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import in.shop2020.model.v1.catalog.InventoryServiceException;
-
 
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
-
 
5
import in.shop2020.serving.utils.FileUtils;
-
 
6
import in.shop2020.serving.utils.Utils;
-
 
7
import in.shop2020.thrift.clients.CatalogServiceClient;
3
 
8
 
-
 
9
import java.io.File;
4
import java.io.IOException;
10
import java.io.IOException;
-
 
11
import java.util.ArrayList;
-
 
12
import java.util.List;
5
 
13
 
6
import org.apache.log4j.Logger;
14
import org.apache.log4j.Logger;
7
import org.apache.struts2.convention.annotation.Action;
15
import org.apache.struts2.convention.annotation.Action;
-
 
16
import org.apache.thrift.TException;
8
 
17
 
9
/**
18
/**
10
 * 
19
 * 
11
 * @author rajveer
20
 * @author rajveer
12
 *
21
 *
13
 */
22
 */
14
 
-
 
-
 
23
@SuppressWarnings("serial")
15
public class HomeController extends BaseController {
24
public class HomeController extends BaseController {
16
 
25
 
17
	private static final long serialVersionUID = 5014930578479365580L;
26
	private static Logger logger = Logger.getLogger(Class.class);
-
 
27
	
-
 
28
	private List<String> bestDealSnippets = null;
-
 
29
	private List<String> bestSellerSnippets = null;
18
	private static Logger log = Logger.getLogger(Class.class);
30
	private List<String> latestArrivalSnippets = null;
19
	
31
	
20
	public HomeController(){
32
	public HomeController(){
21
		super();
33
		super();
22
	}
34
	}
23
 
35
 
24
	@Action("/")
36
	@Action("/")
25
    public String index() throws SecurityException, IOException {
37
    public String index() throws SecurityException, IOException {
26
    	log.info("userinfo:" + userinfo.toString());
38
    	logger.info("userinfo:" + userinfo.toString());
-
 
39
    	
-
 
40
		List<Long> bestDealCatalogIds = null;
-
 
41
		List<Long> bestSellerCatalogIds = null;
-
 
42
 		List<Long> latestArrivalCatalogIds = null;
-
 
43
 		
-
 
44
		try {
-
 
45
			CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
-
 
46
			Client client = catalogServiceClient.getClient();
-
 
47
			
-
 
48
			//Get top 4 best deals 
-
 
49
			bestDealCatalogIds = client.getBestDealsCatalogIds(0,4, null, -1);
-
 
50
			
-
 
51
			//Get top 8 best deals b'coz 4 of them may overlap with best deals.
-
 
52
			bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
-
 
53
			bestSellerCatalogIds.removeAll(bestDealCatalogIds);
-
 
54
			
-
 
55
			//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
-
 
56
			//while another 4 may overlap with best sellers.
-
 
57
			latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
-
 
58
			latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
-
 
59
			latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, 4)); //We're only considering the first 4 best sellers for removal.
-
 
60
		} catch (InventoryServiceException e) {
-
 
61
			logger.error("Error while fetching data from the catalog service", e);
-
 
62
		} catch (TException e) {
-
 
63
			logger.error("Error while fetching data from the catalog service", e);
-
 
64
		} catch (Exception e) {
-
 
65
			logger.error("Unexpected exception", e);
-
 
66
		}
27
    	
67
    	
-
 
68
		bestDealSnippets = getSnippets(bestDealCatalogIds);
-
 
69
		bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, 4));
-
 
70
		latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, 4);
-
 
71
		
28
    	htmlSnippets.put("MAIN_BANNER", pageLoader.getMainBannerHtml());
72
    	htmlSnippets.put("MAIN_BANNER", pageLoader.getMainBannerHtml());
29
    	htmlSnippets.put("BEST_DEALS", pageLoader.getBestDealsHtml());
-
 
30
    	htmlSnippets.put("BEST_SELLERS", pageLoader.getBestSellersHtml());
-
 
31
    	htmlSnippets.put("LATEST_ARRIVALS", pageLoader.getLatestArrivalsHtml());
-
 
32
    	return "index";
73
    	return "index";
33
    }
74
    }
-
 
75
 
-
 
76
	private List<String> getSnippets(List<Long> catalogIds) {
-
 
77
		List<String> snippets = new ArrayList<String>();
-
 
78
		if(catalogIds == null)
-
 
79
			return snippets;
-
 
80
		for(Long item: catalogIds){
-
 
81
			try{
-
 
82
				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
-
 
83
			}catch(IOException ioex){
-
 
84
				logger.error("Unable to get home page snippet for " + item, ioex);
-
 
85
			}
-
 
86
		}
-
 
87
		return snippets;
-
 
88
	}
34
    
89
    
35
	public String getMainBannerSnippet(){
90
	public String getMainBannerSnippet(){
36
		return htmlSnippets.get("MAIN_BANNER");
91
		return htmlSnippets.get("MAIN_BANNER");
37
	}
92
	}
38
	
93
	
39
	public String getBestDealsSnippet(){
94
	public List<String> getBestDealSnippets(){
40
		return htmlSnippets.get("BEST_DEALS"); 
95
		return bestDealSnippets; 
41
	}
96
	}
42
	
97
	
43
	public String getLatestArrivalsSnippet(){
98
	public List<String> getBestSellerSnippets(){
44
		return htmlSnippets.get("LATEST_ARRIVALS");
99
		return bestSellerSnippets; 
45
	}
100
	}
46
	
101
	
47
	public String getBestSellersSnippet(){
102
	public List<String> getLatestArrivalSnippets(){
48
		return htmlSnippets.get("BEST_SELLERS");
103
		return latestArrivalSnippets;
49
	}
104
	}
50
}
105
}