Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 3268 | 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;
3112 vikas 8
import in.shop2020.serving.utils.FileUtils;
9
import in.shop2020.serving.utils.Utils;
3126 rajveer 10
import in.shop2020.thrift.clients.CatalogClient;
3112 vikas 11
 
741 rajveer 12
import java.io.File;
2453 chandransh 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 deals items from the catalog service
26
 * @author rajveer
27
 *	
28
 */
29
public class BestDealsController extends BaseController {
30
 
1044 chandransh 31
	private static final long serialVersionUID = -6862524269260661024L;
545 rajveer 32
 
2453 chandransh 33
	private static Logger log = Logger.getLogger(Class.class);
2943 chandransh 34
	private static final int windowSize = 20;
3112 vikas 35
 
36
    private static final String SHOWCASE_BEST_DEALS_CACHE_KEY = "SHOWCASE_BEST_DEALS";
37
    private static final String SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY = "SHOWCASE_BEST_DEALS_COUNT";
545 rajveer 38
 
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;
46
 
2453 chandransh 47
	private List<String> snippets = null;
48
 
3126 rajveer 49
	CatalogClient catalogClientService = null;
545 rajveer 50
 
51
	public BestDealsController() {
52
		super();
53
		try {
3126 rajveer 54
			catalogClientService = new CatalogClient();
545 rajveer 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 {
637 rajveer 62
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
1928 rajveer 63
        String brandName = request.getParameter("brand");
64
        //Right now if we have brand name, we can just send back data for all categories
2453 chandransh 65
        if(brandName != null){
1928 rajveer 66
            categoryId = -1;
67
        }
545 rajveer 68
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
1928 rajveer 69
    	this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
3112 vikas 70
    	setSnippets();
545 rajveer 71
    	return new DefaultHttpHeaders("index");
72
    }
73
 
74
    // GET /show
75
    public HttpHeaders show() {
3112 vikas 76
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
77
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
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
        }
84
        log.info("Getting best deals from snippets.");
545 rajveer 85
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
86
    	try {
590 chandransh 87
			this.setTotalItems(client.getBestDealsCount());
3112 vikas 88
			showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY, this.totalItems);
1928 rajveer 89
			this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
3112 vikas 90
			setSnippets();
91
			showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex, this.snippets);
545 rajveer 92
		} catch (InventoryServiceException e) {
2453 chandransh 93
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 94
		} catch (TException e) {
2453 chandransh 95
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 96
		}
97
    	return new DefaultHttpHeaders("show");
98
    }
99
 
3112 vikas 100
    private void setSnippets(){
2453 chandransh 101
    	this.snippets = new ArrayList<String>();
741 rajveer 102
    	if(items != null){
103
    		for(long item: items){
2453 chandransh 104
    			try {
105
    				snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
106
    			} catch (IOException ioex) {
107
    				log.error("Error while getting the snippet for: " + item, ioex);
108
    			}
741 rajveer 109
			}
110
    	}
2453 chandransh 111
    }
112
 
113
    public List<String> getSnippets() {
741 rajveer 114
    	return snippets;
545 rajveer 115
    }
116
 
117
	public void setId(String id) {
118
		this.id = id;
2943 chandransh 119
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 120
	}
121
 
122
	public String getId() {
123
		return id;
124
	}
125
 
126
	public long getBeginIndex(){
127
		if(totalItems>0)
128
			return beginIndex+1;
129
		return beginIndex;
130
	}
131
 
132
	public void setTotalItems(long totalItems) {
133
		this.totalItems = totalItems;
134
	}
135
 
136
	public long getTotalItems() {
137
		return totalItems;
138
	}
139
 
140
	public long getTotalPages() {
2950 chandransh 141
	    return 1 + (totalItems-1)/windowSize;
545 rajveer 142
	}
143
 
144
	public long getCurrentPage() {
145
		return Long.parseLong(getId());
146
	}
147
}