Subversion Repositories SmartDukaan

Rev

Rev 5275 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5228 amit.gupta 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.model.v1.catalog.InventoryServiceException;
7
import in.shop2020.serving.cache.EhcacheWrapper;
8
import in.shop2020.serving.services.ContentServingService;
9
import in.shop2020.serving.utils.SnippetType;
10
import in.shop2020.thrift.clients.CatalogClient;
11
 
12
import java.util.ArrayList;
13
import java.util.List;
14
 
15
import net.sf.ehcache.CacheManager;
16
 
17
import org.apache.log4j.Logger;
18
import org.apache.struts2.rest.DefaultHttpHeaders;
19
import org.apache.struts2.rest.HttpHeaders;
20
import org.apache.thrift.TException;
21
 
22
/**
23
 * get Coming Soon items from the catalog service
24
 * @author amit
25
 *	
26
 */
27
public class ComingSoonController extends BaseController {
28
 
29
	private static final long serialVersionUID = -6862524269260661024L;
30
 
31
	private static Logger log = Logger.getLogger(ComingSoonController.class);
32
	private static final int windowSize = 20;
33
 
34
    private static final String COMING_SOON_CACHE_KEY = "COMING_SOON";
35
    private static final String COMING_SOON_COUNT_CACHE_KEY = "COMING_SOON_COUNT";
36
 
37
	private long beginIndex = 0;
38
 
39
	private Long totalItems = 0l;
40
 
41
	private String id;
42
 
43
	List<Long> items = null;
44
 
45
	private List<String> snippets = null;
46
 
47
	CatalogClient catalogClientService = null;
48
 
49
	public ComingSoonController() {
50
		super();
51
		try {
52
			catalogClientService = new CatalogClient();
53
		} catch (Exception e) {
54
			log.error("Unable to get catalog service client.", e);
55
		}
56
	}
57
 
58
    // GET /index
59
    public HttpHeaders index() throws Exception {
60
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
61
        String brandName = request.getParameter("brand");
62
        //Right now if we have brand name, we can just send back data for all categories
63
        if(brandName != null){
64
            categoryId = -1;
65
        }
66
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
67
    	this.items = client.getComingSoonCatalogIds(beginIndex, windowSize, brandName, categoryId);
68
    	setSnippets();
69
    	return new DefaultHttpHeaders("index");
70
    }
71
 
72
    // GET /show
73
    @SuppressWarnings("unchecked")
74
    public HttpHeaders show() {
75
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
76
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
77
        if(sourceId == -1){
78
	        this.snippets = (List<String>)showcasePageSnippetsCache.get(COMING_SOON_CACHE_KEY + beginIndex);
79
	        this.totalItems = (Long)showcasePageSnippetsCache.get(COMING_SOON_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
        }
85
        log.info("Getting coming soon products snippets.");
86
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
87
    	try {
88
			this.setTotalItems(client.getComingSoonCount());
89
			if(sourceId == -1){
90
				showcasePageSnippetsCache.put(COMING_SOON_COUNT_CACHE_KEY, this.totalItems);
91
			}
92
			this.items = client.getComingSoonCatalogIds(beginIndex, windowSize, null, -1);
93
			setSnippets();
94
			if(sourceId == -1){
95
				showcasePageSnippetsCache.put(COMING_SOON_CACHE_KEY + beginIndex, this.snippets);
96
			}
97
		} catch (InventoryServiceException e) {
98
			log.error("Unable to get coming soon products from inventory service.", e);
99
		} catch (TException e) {
100
			log.error("Unable to get coming soon products from inventory service.", e);
101
		}
102
    	return new DefaultHttpHeaders("show");
103
    }
104
 
105
    private void setSnippets(){
106
    	this.snippets = new ArrayList<String>();
107
    	if(items != null){
108
    		for(long item: items){
109
    			snippets.add(ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, item+"", sourceId));
110
			}
111
    	}
112
    }
113
 
114
    public List<String> getSnippets() {
115
    	return snippets;
116
    }
117
 
118
	public void setId(String id) {
119
		this.id = id;
120
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
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() {
142
	    return 1 + (totalItems-1)/windowSize;
143
	}
144
 
145
	public long getCurrentPage() {
146
		return Long.parseLong(getId());
147
	}
148
}