Subversion Repositories SmartDukaan

Rev

Rev 2950 | Rev 3118 | 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;
7
import in.shop2020.serving.utils.EhcacheWrapper;
8
import in.shop2020.serving.utils.FileUtils;
9
import in.shop2020.serving.utils.Utils;
10
import in.shop2020.thrift.clients.CatalogServiceClient;
11
 
741 rajveer 12
import java.io.File;
1371 ankur.sing 13
import java.io.IOException;
741 rajveer 14
import java.util.ArrayList;
545 rajveer 15
import java.util.List;
16
 
3112 vikas 17
import net.sf.ehcache.CacheManager;
545 rajveer 18
 
832 rajveer 19
import org.apache.log4j.Logger;
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 sellers items from the catalog service
26
 * @author rajveer
27
 *	
28
 */
29
public class BestSellersController extends BaseController {
30
 
1044 chandransh 31
	private static final long serialVersionUID = 3380274695464543863L;
545 rajveer 32
 
832 rajveer 33
	private static Logger log = Logger.getLogger(Class.class);	
2943 chandransh 34
	private static final int windowSize = 20;
545 rajveer 35
 
3112 vikas 36
	private static final String SHOWCASE_BEST_SELLERS_CACHE_KEY = "SHOWCASE_BEST_SELLERS";
37
    private static final String SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY = "SHOWCASE_BEST_SELLERS_COUNT";
38
 
545 rajveer 39
	private long beginIndex = 0;
40
 
3112 vikas 41
	private Long totalItems = 0l;
545 rajveer 42
 
43
	private String id;
44
 
45
	List<Long> items = null;
3112 vikas 46
 
47
    private List<String> snippets = null;
545 rajveer 48
 
49
	CatalogServiceClient catalogClientService = null;
50
 
51
	public BestSellersController() {
52
		super();
53
		try {
54
			catalogClientService = new CatalogServiceClient();
55
		} catch (Exception e) {
2453 chandransh 56
			log.error("Unable to get catalog service client.", e);
545 rajveer 57
		}
58
	}
59
 
60
    // GET /index
61
    public HttpHeaders index() throws Exception {
1923 rajveer 62
        long categoryId = Long.parseLong(request.getParameter("categoryid"));
63
        String brandName = request.getParameter("brand");
64
        //Right now if we have brand name, we can just send back data for all categories
65
        if(brandName!=null){
66
            categoryId = -1;
67
        }
545 rajveer 68
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
1923 rajveer 69
    	this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, brandName, categoryId);
545 rajveer 70
    	return new DefaultHttpHeaders("index");
71
    }
72
 
73
    // GET /show
74
    public HttpHeaders show() {
3112 vikas 75
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
76
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
77
        this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_BEST_SELLERS_CACHE_KEY + beginIndex);
78
        this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY);
79
        if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
80
            log.info("Using Cache.");
81
            return new DefaultHttpHeaders("show");
82
        }
83
        log.info("Getting best sellers from snippets.");
545 rajveer 84
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
85
    	try {
590 chandransh 86
			this.setTotalItems(client.getBestSellersCount());
3112 vikas 87
			showcasePageSnippetsCache.put(SHOWCASE_BEST_SELLERS_COUNT_CACHE_KEY, this.totalItems);
1923 rajveer 88
			this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, null, -1);
3112 vikas 89
			setSnippets(items);
90
			showcasePageSnippetsCache.put(SHOWCASE_BEST_SELLERS_CACHE_KEY + beginIndex, this.snippets);
545 rajveer 91
		} catch (InventoryServiceException e) {
2453 chandransh 92
			log.error("Unable to get best sellers from inventory service.", e);
545 rajveer 93
		} catch (TException e) {
2453 chandransh 94
			log.error("Unable to get best sellers from inventory service.", e);
545 rajveer 95
		}
96
    	return new DefaultHttpHeaders("show");
97
    }
98
 
3112 vikas 99
    public void setSnippets(List<Long> items) {
100
    	this.snippets = new ArrayList<String>();
741 rajveer 101
    	if(items != null){
102
    		for(long item: items){
1371 ankur.sing 103
				try {
2453 chandransh 104
					snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
1371 ankur.sing 105
				} catch (IOException ioex) {
2453 chandransh 106
					log.error("Error while getting the snippet for: " + item, ioex);
1371 ankur.sing 107
				}
741 rajveer 108
			}
109
    	}
1371 ankur.sing 110
    } 
111
 
3112 vikas 112
    public List<String> getSnippets() {
113
        return snippets;
114
    }
115
 
545 rajveer 116
	public void setId(String id) {
117
		this.id = id;
2943 chandransh 118
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 119
	}
120
 
121
	public String getId() {
122
		return id;
123
	}
124
 
125
	public long getBeginIndex(){
126
		if(totalItems>0)
127
			return beginIndex+1;
128
		return beginIndex;
129
	}
130
 
131
	public void setTotalItems(long totalItems) {
132
		this.totalItems = totalItems;
133
	}
134
 
135
	public long getTotalItems() {
136
		return totalItems;
137
	}
138
 
139
	public long getTotalPages() {
2950 chandransh 140
	    return 1 + (totalItems-1)/windowSize;
545 rajveer 141
	}
142
 
143
	public long getCurrentPage() {
144
		return Long.parseLong(getId());
145
	}
146
}