Subversion Repositories SmartDukaan

Rev

Rev 3242 | 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;
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
3268 chandransh 75
    @SuppressWarnings("unchecked")
545 rajveer 76
    public HttpHeaders show() {
3112 vikas 77
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
78
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
79
        this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex);
80
        this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY);
81
        if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
82
            log.info("Using Cache.");
83
            return new DefaultHttpHeaders("show");
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());
3112 vikas 89
			showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_COUNT_CACHE_KEY, this.totalItems);
1928 rajveer 90
			this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
3112 vikas 91
			setSnippets();
92
			showcasePageSnippetsCache.put(SHOWCASE_BEST_DEALS_CACHE_KEY + beginIndex, this.snippets);
545 rajveer 93
		} catch (InventoryServiceException e) {
2453 chandransh 94
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 95
		} catch (TException e) {
2453 chandransh 96
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 97
		}
98
    	return new DefaultHttpHeaders("show");
99
    }
100
 
3112 vikas 101
    private void setSnippets(){
2453 chandransh 102
    	this.snippets = new ArrayList<String>();
741 rajveer 103
    	if(items != null){
104
    		for(long item: items){
2453 chandransh 105
    			try {
106
    				snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
107
    			} catch (IOException ioex) {
108
    				log.error("Error while getting the snippet for: " + item, ioex);
109
    			}
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
}