Subversion Repositories SmartDukaan

Rev

Rev 3561 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
545 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
3112 vikas 6
import in.shop2020.model.v1.catalog.InventoryServiceException;
3242 vikas 7
import in.shop2020.serving.cache.EhcacheWrapper;
3561 rajveer 8
import in.shop2020.serving.services.ContentServingService;
9
import in.shop2020.serving.utils.SnippetType;
3126 rajveer 10
import in.shop2020.thrift.clients.CatalogClient;
3112 vikas 11
 
741 rajveer 12
import java.util.ArrayList;
545 rajveer 13
import java.util.List;
14
 
3112 vikas 15
import net.sf.ehcache.CacheManager;
545 rajveer 16
 
832 rajveer 17
import org.apache.log4j.Logger;
5529 amit.gupta 18
import org.apache.struts2.convention.annotation.Result;
19
import org.apache.struts2.convention.annotation.Results;
545 rajveer 20
import org.apache.struts2.rest.DefaultHttpHeaders;
21
import org.apache.struts2.rest.HttpHeaders;
22
import org.apache.thrift.TException;
23
 
24
/**
25
 * get best deals items from the catalog service
26
 * @author rajveer
27
 *	
28
 */
5529 amit.gupta 29
@Results({
30
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
31
})
32
 
545 rajveer 33
public class BestDealsController extends BaseController {
34
 
1044 chandransh 35
	private static final long serialVersionUID = -6862524269260661024L;
545 rajveer 36
 
2453 chandransh 37
	private static Logger log = Logger.getLogger(Class.class);
2943 chandransh 38
	private static final int windowSize = 20;
3112 vikas 39
 
40
    private static final String SHOWCASE_BEST_DEALS_CACHE_KEY = "SHOWCASE_BEST_DEALS";
41
    private static final String SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY = "SHOWCASE_BEST_DEALS_COUNT";
545 rajveer 42
 
43
	private long beginIndex = 0;
44
 
3112 vikas 45
	private Long totalItems = 0l;
545 rajveer 46
 
47
	private String id;
5529 amit.gupta 48
 
49
	private String redirectUrl;
545 rajveer 50
 
51
	List<Long> items = null;
52
 
2453 chandransh 53
	private List<String> snippets = null;
54
 
3126 rajveer 55
	CatalogClient catalogClientService = null;
545 rajveer 56
 
57
	public BestDealsController() {
58
		super();
59
		try {
3126 rajveer 60
			catalogClientService = new CatalogClient();
545 rajveer 61
		} catch (Exception e) {
2453 chandransh 62
			log.error("Unable to get catalog service client.", e);
545 rajveer 63
		}
64
	}
65
 
66
    // GET /index
67
    public HttpHeaders index() throws Exception {
637 rajveer 68
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
1928 rajveer 69
        String brandName = request.getParameter("brand");
70
        //Right now if we have brand name, we can just send back data for all categories
2453 chandransh 71
        if(brandName != null){
1928 rajveer 72
            categoryId = -1;
73
        }
545 rajveer 74
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
1928 rajveer 75
    	this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
3112 vikas 76
    	setSnippets();
545 rajveer 77
    	return new DefaultHttpHeaders("index");
78
    }
79
 
80
    // GET /show
3268 chandransh 81
    @SuppressWarnings("unchecked")
545 rajveer 82
    public HttpHeaders show() {
5529 amit.gupta 83
    	if(getCurrentPage() > 1){
84
    		log.info("Redirecting.");
85
    		redirectUrl = "/best-deals/1";
86
            return new DefaultHttpHeaders("redirect");
87
    	}
3112 vikas 88
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
89
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
3561 rajveer 90
        if(sourceId == -1){
91
	        this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex);
92
	        this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY);
93
	        if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
94
	            log.info("Using Cache.");
95
	            return new DefaultHttpHeaders("show");
96
	        }
3112 vikas 97
        }
98
        log.info("Getting best deals from snippets.");
545 rajveer 99
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
100
    	try {
5529 amit.gupta 101
    		long count = client.getBestDealsCount();
102
			this.setTotalItems(count > 20 ? 20 : count);
3561 rajveer 103
			if(sourceId == -1){
104
				showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY, this.totalItems);
105
			}
1928 rajveer 106
			this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
3112 vikas 107
			setSnippets();
3561 rajveer 108
			if(sourceId == -1){
109
				showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex, this.snippets);
110
			}
545 rajveer 111
		} catch (InventoryServiceException e) {
2453 chandransh 112
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 113
		} catch (TException e) {
2453 chandransh 114
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 115
		}
116
    	return new DefaultHttpHeaders("show");
117
    }
118
 
3112 vikas 119
    private void setSnippets(){
2453 chandransh 120
    	this.snippets = new ArrayList<String>();
741 rajveer 121
    	if(items != null){
122
    		for(long item: items){
3561 rajveer 123
    			snippets.add(ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, item+"", sourceId));
741 rajveer 124
			}
125
    	}
2453 chandransh 126
    }
127
 
128
    public List<String> getSnippets() {
741 rajveer 129
    	return snippets;
545 rajveer 130
    }
131
 
132
	public void setId(String id) {
133
		this.id = id;
2943 chandransh 134
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 135
	}
136
 
137
	public String getId() {
138
		return id;
139
	}
140
 
141
	public long getBeginIndex(){
142
		if(totalItems>0)
143
			return beginIndex+1;
144
		return beginIndex;
145
	}
146
 
147
	public void setTotalItems(long totalItems) {
148
		this.totalItems = totalItems;
149
	}
150
 
151
	public long getTotalItems() {
152
		return totalItems;
153
	}
154
 
155
	public long getTotalPages() {
2950 chandransh 156
	    return 1 + (totalItems-1)/windowSize;
545 rajveer 157
	}
158
 
159
	public long getCurrentPage() {
160
		return Long.parseLong(getId());
161
	}
5529 amit.gupta 162
 
163
	public String getRedirectUrl(){
164
		return redirectUrl;
165
    }
545 rajveer 166
}