Subversion Repositories SmartDukaan

Rev

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