Subversion Repositories SmartDukaan

Rev

Rev 5529 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.controllers;

import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.serving.cache.EhcacheWrapper;
import in.shop2020.serving.services.ContentServingService;
import in.shop2020.serving.utils.SnippetType;
import in.shop2020.thrift.clients.CatalogClient;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.CacheManager;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.thrift.TException;

/**
 * get best sellers items from the catalog service
 * @author rajveer
 *      
 */
@Results({
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
})
public class BestSellersController extends BaseController {
        
        private static final long serialVersionUID = 3380274695464543863L;
        
        private static Logger log = Logger.getLogger(Class.class);      
        private static final int windowSize = 20;
        
        private static final String SHOWCASE_BEST_SELLERS_CACHE_KEY = "SHOWCASE_BEST_SELLERS";
    private static final String SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY = "SHOWCASE_BEST_SELLERS_COUNT";
        
        private long beginIndex = 0;
        
        private Long totalItems = 0l;
        
        private String id;
        private String redirectUrl;
        
        List<Long> items = null;
    
    private List<String> snippets = null;
        
        CatalogClient catalogClientService = null;
        
        public BestSellersController() {
                super();
                try {
                        catalogClientService = new CatalogClient();
                } catch (Exception e) {
                        log.error("Unable to get catalog service client.", e);
                }
        }
    
    // GET /index
    public HttpHeaders index() throws Exception {
        long categoryId = Long.parseLong(request.getParameter("categoryid"));
        String brandName = request.getParameter("brand");
        //Right now if we have brand name, we can just send back data for all categories
        if(brandName!=null){
            categoryId = -1;
        }
        in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
        this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, brandName, categoryId);
        setSnippets(items);
        return new DefaultHttpHeaders("index");
    }

    // GET /show
    @SuppressWarnings("unchecked")
    public HttpHeaders show() {
        if(getCurrentPage() > 1) {
                log.info("Redirecting");
                this.redirectUrl = "/best-sellers/1";
                return new DefaultHttpHeaders("redirect");
        }       
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
        if(sourceId == -1){
                this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_BEST_SELLERS_CACHE_KEY + beginIndex);
                this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY);
                if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
                        log.info("Using Cache.");
                        return new DefaultHttpHeaders("show");
                }
        }
        log.info("Getting best sellers from snippets.");
        in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
        try {

                long count = client.getBestSellersCount();
                        this.setTotalItems(count > 20 ? 20 : count);
                        if(sourceId == -1){
                                showcasePageSnippetsCache.put(SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY, this.totalItems);
                        }
                        this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, null, -1);
                        setSnippets(items);
                        if(sourceId == -1){
                                showcasePageSnippetsCache.put(SHOWCASE_BEST_SELLERS_CACHE_KEY + beginIndex, this.snippets);
                        }
                } catch (CatalogServiceException e) {
                        log.error("Unable to get best sellers from inventory service.", e);
                } catch (TException e) {
                        log.error("Unable to get best sellers from inventory service.", e);
                }
        return new DefaultHttpHeaders("show");
    }
    
    public void setSnippets(List<Long> items) {
        this.snippets = new ArrayList<String>();
        if(items != null){
                for(long item: items){
                        snippets.add(ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, item+"", sourceId));
                        }
        }
    } 
    
    public List<String> getSnippets() {
        return snippets;
    }
    
        public void setId(String id) {
                this.id = id;
                this.beginIndex = windowSize * (Long.parseLong(id)-1);
        }

        public String getId() {
                return id;
        }

        public long getBeginIndex(){
                if(totalItems>0)
                        return beginIndex+1;
                return beginIndex;
        }
        
        public void setTotalItems(long totalItems) {
                this.totalItems = totalItems;
        }

        public long getTotalItems() {
                return totalItems;
        }

        public long getTotalPages() {
            return 1 + (totalItems-1)/windowSize;
        }

        public long getCurrentPage() {
                return Long.parseLong(getId());
        }
        
        public String getRedirectUrl(){
                return this.redirectUrl;
        }
}