Subversion Repositories SmartDukaan

Rev

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

Rev 2867 Rev 2930
Line 15... Line 15...
15
import org.apache.struts2.convention.annotation.Action;
15
import org.apache.struts2.convention.annotation.Action;
16
import org.apache.thrift.TException;
16
import org.apache.thrift.TException;
17
 
17
 
18
/**
18
/**
19
 * 
19
 * 
20
 * @author rajveer
20
 * @author chandranshu
21
 *
21
 *
22
 */
22
 */
23
@SuppressWarnings("serial")
23
@SuppressWarnings("serial")
24
public class HomeController extends BaseController {
24
public class HomeController extends BaseController {
25
 
25
 
26
	private static Logger logger = Logger.getLogger(Class.class);
26
	private static Logger logger = Logger.getLogger(Class.class);
27
	
27
	
28
	private List<String> bestDealSnippets = null;
28
	private static List<String> bestDealSnippets = null;
29
	private List<String> bestSellerSnippets = null;
29
	private static List<String> bestSellerSnippets = null;
30
	private List<String> latestArrivalSnippets = null;
30
	private static List<String> latestArrivalSnippets = null;
31
	
31
	
32
	public HomeController(){
32
	public HomeController(){
33
		super();
33
		super();
34
	}
34
	}
35
 
35
 
-
 
36
    /**
-
 
37
     * Renders the home page. Loads the snippets for best deals, best sellers
-
 
38
     * and latest arrivals if they haven't been loaded already.
-
 
39
     * 
-
 
40
     * @return Name of the view to render.
-
 
41
     * @throws SecurityException
-
 
42
     * @throws IOException
-
 
43
     */
36
	@Action("/")
44
	@Action("/")
37
    public String index() throws SecurityException, IOException {
45
    public String index() throws SecurityException, IOException {
38
    	logger.info("userinfo:" + userinfo.toString());
46
    	logger.debug("userinfo:" + userinfo.toString());
-
 
47
    	logger.info("Rendering the home page");
39
    	
48
    	
40
		List<Long> bestDealCatalogIds = null;
-
 
41
		List<Long> bestSellerCatalogIds = null;
-
 
42
 		List<Long> latestArrivalCatalogIds = null;
-
 
43
 		
-
 
44
 		int bestSellerCount = 4;
-
 
45
 		int latestArrivalCount = 4;
-
 
46
 		
-
 
47
		try {
-
 
48
			CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
-
 
49
			Client client = catalogServiceClient.getClient();
-
 
50
			
-
 
51
			//Get top 4 best deals 
-
 
52
			bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
-
 
53
			
-
 
54
			//Get top 8 best sellers b'coz 4 of them may overlap with best deals.
-
 
55
			bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
49
    	if(bestDealSnippets == null || bestSellerSnippets == null || latestArrivalSnippets == null){
56
			bestSellerCatalogIds.removeAll(bestDealCatalogIds);
-
 
57
			if(bestSellerCatalogIds.size() < bestSellerCount)
-
 
58
				bestSellerCount = bestSellerCatalogIds.size();
-
 
59
			
-
 
60
			//Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
-
 
61
			//while another 4 may overlap with best sellers.
-
 
62
			latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
-
 
63
			latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
-
 
64
			latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
-
 
65
			if(latestArrivalCatalogIds.size() < latestArrivalCount)
-
 
66
				latestArrivalCount = latestArrivalCatalogIds.size();
-
 
67
		} catch (InventoryServiceException e) {
-
 
68
			logger.error("Error while fetching data from the catalog service", e);
50
    	    logger.info("Cache miss: Reloading best deals, best sellers and latest arrivals");
69
		} catch (TException e) {
51
            setSnippets();    	    
70
			logger.error("Error while fetching data from the catalog service", e);
-
 
71
		} catch (Exception e) {
-
 
72
			logger.error("Unexpected exception", e);
-
 
73
		}
52
    	}	
74
    	
53
    	
75
		bestDealSnippets = getSnippets(bestDealCatalogIds);
-
 
76
		bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
-
 
77
		latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
-
 
78
		
-
 
79
    	return "index";
54
    	return "index";
80
    }
55
    }
81
 
56
 
82
	/**
57
    /**
83
	 * Returns a list of snippets to be used on the home page corresponding to
-
 
84
	 * the given catalog ids. The snippets are in the same order as the ids in
58
     * Renders the home page but always loads the snippets for best deals, best
85
	 * the input list. In case a snippet is not found for an entity, this method
59
     * sellers and latest arrivals unconditionally. Should be used to reset the
86
	 * simply logs the problem and moves on.
60
     * snippets.
87
	 * 
61
     * 
88
	 * @param catalogIds
62
     * @return Name of the view to render.
89
	 *            Ids of the entities which we want to show.
63
     * @throws SecurityException
90
	 * @return The snippet corresponding to each catalog entity
64
     * @throws IOException
91
	 */
65
     */
92
	private List<String> getSnippets(List<Long> catalogIds) {
66
	@Action("/refresh-home")
93
		List<String> snippets = new ArrayList<String>();
67
	public String destroy() throws SecurityException, IOException{
94
		if(catalogIds == null)
68
	    logger.info("Reloading best deals, best sellers and latest arrivals");
95
			return snippets;
69
	    setSnippets();
96
		for(Long item: catalogIds){
-
 
97
			try{
-
 
98
				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
-
 
99
			}catch(IOException ioex){
-
 
100
				logger.error("Unable to get home page snippet for " + item, ioex);
-
 
101
			}
-
 
102
		}
-
 
103
		return snippets;
70
	    return "index";
104
	}
71
	}
105
    
72
	
106
	public List<String> getBestDealSnippets(){
73
	public List<String> getBestDealSnippets(){
107
		return bestDealSnippets; 
74
		return bestDealSnippets; 
108
	}
75
	}
109
	
76
	
110
	public List<String> getBestSellerSnippets(){
77
	public List<String> getBestSellerSnippets(){
Line 112... Line 79...
112
	}
79
	}
113
	
80
	
114
	public List<String> getLatestArrivalSnippets(){
81
	public List<String> getLatestArrivalSnippets(){
115
		return latestArrivalSnippets;
82
		return latestArrivalSnippets;
116
	}
83
	}
-
 
84
 
-
 
85
	/**
-
 
86
	 * Loads the snippets for best deals, best sellers and latest arrivals
-
 
87
	 * into memory.
-
 
88
	 */
-
 
89
    private void setSnippets() {
-
 
90
        List<Long> bestDealCatalogIds = null;
-
 
91
        List<Long> bestSellerCatalogIds = null;
-
 
92
        List<Long> latestArrivalCatalogIds = null;
-
 
93
        
-
 
94
        int bestSellerCount = 4;
-
 
95
        int latestArrivalCount = 4;
-
 
96
        
-
 
97
        try {
-
 
98
            CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
-
 
99
            Client client = catalogServiceClient.getClient();
-
 
100
            
-
 
101
            //Get top 4 best deals 
-
 
102
            bestDealCatalogIds = client.getBestDealsCatalogIds(0, 4, null, -1);
-
 
103
            
-
 
104
            //Get top 8 best sellers b'coz 4 of them may overlap with best deals.
-
 
105
            bestSellerCatalogIds = client.getBestSellersCatalogIds(0, 8, null, -1);
-
 
106
            bestSellerCatalogIds.removeAll(bestDealCatalogIds);
-
 
107
            if(bestSellerCatalogIds.size() < bestSellerCount)
-
 
108
                bestSellerCount = bestSellerCatalogIds.size();
-
 
109
            
-
 
110
            //Get top 12 latest arrivals b'coz 4 of them may overlap with best deals
-
 
111
            //while another 4 may overlap with best sellers.
-
 
112
            latestArrivalCatalogIds = client.getLatestArrivalsCatalogIds(0, 12, null, 10003);
-
 
113
            latestArrivalCatalogIds.removeAll(bestDealCatalogIds);
-
 
114
            latestArrivalCatalogIds.removeAll(bestSellerCatalogIds.subList(0, bestSellerCount)); //We're only considering the first 4 best sellers for removal.
-
 
115
            if(latestArrivalCatalogIds.size() < latestArrivalCount)
-
 
116
                latestArrivalCount = latestArrivalCatalogIds.size();
-
 
117
        } catch (InventoryServiceException e) {
-
 
118
            logger.error("Error while fetching data from the catalog service", e);
-
 
119
        } catch (TException e) {
-
 
120
            logger.error("Error while fetching data from the catalog service", e);
-
 
121
        } catch (Exception e) {
-
 
122
            logger.error("Unexpected exception", e);
-
 
123
        }
-
 
124
        
-
 
125
        bestDealSnippets = getSnippets(bestDealCatalogIds);
-
 
126
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
-
 
127
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
-
 
128
    }
-
 
129
	
-
 
130
    /**
-
 
131
     * Returns a list of snippets to be used on the home page corresponding to
-
 
132
     * the given catalog ids. The snippets are in the same order as the ids in
-
 
133
     * the input list. In case a snippet is not found for an entity, this method
-
 
134
     * simply logs the problem and moves on.
-
 
135
     * 
-
 
136
     * @param catalogIds
-
 
137
     *            Ids of the entities which we want to show.
-
 
138
     * @return The snippet corresponding to each catalog entity
-
 
139
     */
-
 
140
    private List<String> getSnippets(List<Long> catalogIds) {
-
 
141
        List<String> snippets = new ArrayList<String>();
-
 
142
        if(catalogIds == null)
-
 
143
            return snippets;
-
 
144
        for(Long item: catalogIds){
-
 
145
            try{
-
 
146
                snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
-
 
147
            }catch(IOException ioex){
-
 
148
                logger.error("Unable to get home page snippet for " + item, ioex);
-
 
149
            }
-
 
150
        }
-
 
151
        return snippets;
-
 
152
    }
117
}
153
}