Subversion Repositories SmartDukaan

Rev

Rev 11645 | Rev 11682 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 11645 Rev 11680
Line 2... Line 2...
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.serving.controllers;
4
package in.shop2020.serving.controllers;
5
 
5
 
6
import in.shop2020.model.v1.catalog.CatalogServiceException;
6
import in.shop2020.model.v1.catalog.CatalogServiceException;
-
 
7
import in.shop2020.model.v1.user.UserContextService.Client;
7
import in.shop2020.serving.cache.EhcacheWrapper;
8
import in.shop2020.serving.cache.EhcacheWrapper;
8
import in.shop2020.serving.services.ContentServingService;
9
import in.shop2020.serving.services.ContentServingService;
9
import in.shop2020.serving.utils.SnippetType;
10
import in.shop2020.serving.utils.SnippetType;
10
import in.shop2020.thrift.clients.CatalogClient;
11
import in.shop2020.thrift.clients.CatalogClient;
-
 
12
import in.shop2020.thrift.clients.UserClient;
11
 
13
 
12
import java.util.ArrayList;
14
import java.util.ArrayList;
13
import java.util.List;
15
import java.util.List;
14
 
16
 
-
 
17
import net.sf.ehcache.CacheException;
15
import net.sf.ehcache.CacheManager;
18
import net.sf.ehcache.CacheManager;
16
 
19
 
17
import org.apache.log4j.Logger;
20
import org.apache.log4j.Logger;
-
 
21
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
22
import org.apache.struts2.convention.annotation.InterceptorRefs;
18
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Result;
19
import org.apache.struts2.convention.annotation.Results;
24
import org.apache.struts2.convention.annotation.Results;
20
import org.apache.struts2.rest.DefaultHttpHeaders;
25
import org.apache.struts2.rest.DefaultHttpHeaders;
21
import org.apache.struts2.rest.HttpHeaders;
26
import org.apache.struts2.rest.HttpHeaders;
22
import org.apache.thrift.TException;
27
import org.apache.thrift.TException;
-
 
28
import org.apache.thrift.transport.TTransportException;
23
 
29
 
24
/**
30
/**
25
 * get private deals items from the catalog service
31
 * get private deals items from the catalog service
26
 * @author rajveer
32
 * @author rajveer
27
 *	
33
 *	
28
 */
34
 */
-
 
35
@InterceptorRefs({
-
 
36
	@InterceptorRef("defaultStack"),
-
 
37
	@InterceptorRef("login")
-
 
38
})
29
@Results({
39
@Results({
30
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
40
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
31
})
41
})
32
 
42
 
33
public class PrivateDealsController extends BaseController {
43
public class PrivateDealsController extends BaseController {
34
	
44
 
35
	private static final long serialVersionUID = -6862524269260661024L;
45
	private static final long serialVersionUID = -6862524269260661024L;
36
	
46
 
37
	private static Logger log = Logger.getLogger(Class.class);
47
	private static Logger log = Logger.getLogger(Class.class);
38
	private static final int windowSize = 20;
48
	private static final int windowSize = 20;
39
 
49
 
40
    private static final String SHOWCASE_PRIVATE_DEALS_CACHE_KEY = "SHOWCASE_PRIVATE_DEALS";
50
	private static final String SHOWCASE_PRIVATE_DEALS_CACHE_KEY = "SHOWCASE_PRIVATE_DEALS";
41
    private static final String SHOWCASE_PRIVATE_DEALS_COUNT_CACHE_KEY = "SHOWCASE_PRIVATE_DEALS_COUNT";
51
	private static final String SHOWCASE_PRIVATE_DEALS_COUNT_CACHE_KEY = "SHOWCASE_PRIVATE_DEALS_COUNT";
42
	
52
 
43
	private long beginIndex = 0;
53
	private long beginIndex = 0;
44
	
54
 
45
	private Long totalItems = 0l;
55
	private Long totalItems = 0l;
46
	
56
 
47
	private String id;
57
	private String id;
48
 
58
 
49
	private String redirectUrl;
59
	private String redirectUrl;
50
	
60
 
51
	List<Long> items = null;
61
	List<Long> items = null;
52
	
62
 
53
	private List<String> snippets = null;
63
	private List<String> snippets = null;
54
	
64
 
55
	CatalogClient catalogClientService = null;
65
	CatalogClient catalogClientService = null;
-
 
66
	Client uc = null;
56
	
67
 
57
	public PrivateDealsController() {
68
	public PrivateDealsController() {
58
		super();
69
		super();
59
		try {
70
		try {
60
			catalogClientService = new CatalogClient();
71
			catalogClientService = new CatalogClient();
61
		} catch (Exception e) {
72
		} catch (Exception e) {
62
			log.error("Unable to get catalog service client.", e);
73
			log.error("Unable to get catalog service client.", e);
63
		}
74
		}
-
 
75
		try {
-
 
76
			uc = new UserClient().getClient();
-
 
77
		} catch (TTransportException e) {
-
 
78
			log.error("Unable to get user service client.", e);	
-
 
79
		}
64
	}
80
	}
65
    
-
 
66
    // GET /index
-
 
67
    public HttpHeaders index() throws Exception {
-
 
68
    	in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
-
 
69
    	this.items = client.getPrivateDealsCatalogIds(beginIndex, windowSize);
-
 
70
    	setSnippets();
-
 
71
    	return new DefaultHttpHeaders("index");
-
 
72
    }
-
 
73
 
81
 
74
    // GET /show
82
	// GET /index
75
    @SuppressWarnings("unchecked")
-
 
76
    public HttpHeaders show() {
83
	public HttpHeaders index() throws Exception {
77
    	if(getCurrentPage() > 1){
-
 
78
    		log.info("Redirecting.");
-
 
79
    		redirectUrl = "/private-deals/1";
84
		if(isUserLoggedIn() && isPrivatedealUser()){
80
            return new DefaultHttpHeaders("redirect");
-
 
81
    	}
-
 
82
        EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
-
 
83
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
-
 
84
        if(sourceId == -1){
-
 
85
	        this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_PRIVATE_DEALS_CACHE_KEY + beginIndex);
-
 
86
	        this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_PRIVATE_DEALS_COUNT_CACHE_KEY);
-
 
87
	        if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
-
 
88
	            log.info("Using Cache.");
-
 
89
	            return new DefaultHttpHeaders("show");
-
 
90
	        }
-
 
91
        }
-
 
92
        log.info("Getting private deals from snippets.");
-
 
93
    	in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
85
			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
94
    	try {
-
 
95
    		long count = client.getPrivateDealsCount();
-
 
96
			this.setTotalItems(count > 20 ? 20 : count);
-
 
97
			if(sourceId == -1){
-
 
98
				showcasePageSnippetsCache.put(SHOWCASE_PRIVATE_DEALS_COUNT_CACHE_KEY, this.totalItems);
-
 
99
			}
-
 
100
			this.items = client.getPrivateDealsCatalogIds(beginIndex, windowSize);
86
			this.items = client.getPrivateDealsCatalogIds(beginIndex, windowSize);
101
			setSnippets();
87
			setSnippets();
-
 
88
			return new DefaultHttpHeaders("index");
-
 
89
		}
-
 
90
		else{
-
 
91
			redirectUrl = "/";
-
 
92
			return new DefaultHttpHeaders("redirect");
-
 
93
		}
-
 
94
	}
-
 
95
 
-
 
96
	// GET /show
-
 
97
	@SuppressWarnings("unchecked")
-
 
98
	public HttpHeaders show() {
-
 
99
		try {
-
 
100
			if(isUserLoggedIn() && isPrivatedealUser()){
-
 
101
 
-
 
102
				if(getCurrentPage() > 1){
-
 
103
					log.info("Redirecting.");
-
 
104
					redirectUrl = "/private-deals/1";
-
 
105
					return new DefaultHttpHeaders("redirect");
-
 
106
				}
-
 
107
				EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
-
 
108
						EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
102
			if(sourceId == -1){
109
				if(sourceId == -1){
-
 
110
					this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_PRIVATE_DEALS_CACHE_KEY + beginIndex);
-
 
111
					this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_PRIVATE_DEALS_COUNT_CACHE_KEY);
-
 
112
					if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {
-
 
113
						log.info("Using Cache.");
-
 
114
						return new DefaultHttpHeaders("show");
-
 
115
					}
-
 
116
				}
-
 
117
				log.info("Getting private deals from snippets.");
-
 
118
				in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
-
 
119
				try {
-
 
120
					int count = client.getPrivateDealsCount();
-
 
121
					this.setTotalItems(count > 20 ? 20 : count);
-
 
122
					if(sourceId == -1){
-
 
123
						showcasePageSnippetsCache.put(SHOWCASE_PRIVATE_DEALS_COUNT_CACHE_KEY, this.totalItems);
-
 
124
					}
-
 
125
					this.items = client.getPrivateDealsCatalogIds(beginIndex, windowSize);
-
 
126
					setSnippets();
-
 
127
					if(sourceId == -1){
103
				showcasePageSnippetsCache.put(SHOWCASE_PRIVATE_DEALS_CACHE_KEY + beginIndex, this.snippets);
128
						showcasePageSnippetsCache.put(SHOWCASE_PRIVATE_DEALS_CACHE_KEY + beginIndex, this.snippets);
-
 
129
					}
-
 
130
				} catch (CatalogServiceException e) {
-
 
131
					log.error("Unable to get private deals from inventory service.", e);
-
 
132
				} catch (TException e) {
-
 
133
					log.error("Unable to get private deals from inventory service.", e);
-
 
134
				}
-
 
135
				return new DefaultHttpHeaders("show");
-
 
136
			}
-
 
137
			else{
-
 
138
				redirectUrl = "/";
-
 
139
				return new DefaultHttpHeaders("redirect");
104
			}
140
			}
105
		} catch (CatalogServiceException e) {
141
		} catch (TTransportException e) {
106
			log.error("Unable to get private deals from inventory service.", e);
142
			log.error("Exception in User Service .", e);
-
 
143
			redirectUrl = "/";
-
 
144
			return new DefaultHttpHeaders("redirect");
-
 
145
		} catch (CacheException e) {
-
 
146
			log.error("Cache Exception .", e);
-
 
147
			redirectUrl = "/";
-
 
148
			return new DefaultHttpHeaders("redirect");
107
		} catch (TException e) {
149
		} catch (TException e) {
108
			log.error("Unable to get private deals from inventory service.", e);
150
			log.error("Transport Exception in User Service .", e);
-
 
151
			redirectUrl = "/";
-
 
152
			return new DefaultHttpHeaders("redirect");
109
		}
153
		}
110
    	return new DefaultHttpHeaders("show");
-
 
111
    }
154
	}
112
    
155
 
113
    private void setSnippets(){
156
	private void setSnippets(){
114
    	this.snippets = new ArrayList<String>();
157
		this.snippets = new ArrayList<String>();
115
    	if(items != null){
158
		if(items != null){
116
    		for(long item: items){
159
			for(long item: items){
117
    			snippets.add(ContentServingService.getSnippet(SnippetType.PRIVATE_DEAL_SNIPPET, item+"", sourceId));
160
				snippets.add(ContentServingService.getSnippet(SnippetType.PRIVATE_DEAL_SNIPPET, item+"", sourceId));
118
			}
161
			}
119
    	}
162
		}
120
    }
163
	}
121
    
164
 
122
    public List<String> getSnippets() {
165
	public List<String> getSnippets() {
123
    	return snippets;
166
		return snippets;
124
    }
167
	}
125
    
168
 
126
	public void setId(String id) {
169
	public void setId(String id) {
127
		this.id = id;
170
		this.id = id;
128
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
171
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
129
	}
172
	}
130
 
173
 
Line 135... Line 178...
135
	public long getBeginIndex(){
178
	public long getBeginIndex(){
136
		if(totalItems>0)
179
		if(totalItems>0)
137
			return beginIndex+1;
180
			return beginIndex+1;
138
		return beginIndex;
181
		return beginIndex;
139
	}
182
	}
140
	
183
 
141
	public void setTotalItems(long totalItems) {
184
	public void setTotalItems(long totalItems) {
142
		this.totalItems = totalItems;
185
		this.totalItems = totalItems;
143
	}
186
	}
144
 
187
 
145
	public long getTotalItems() {
188
	public long getTotalItems() {
146
		return totalItems;
189
		return totalItems;
147
	}
190
	}
148
 
191
 
149
	public long getTotalPages() {
192
	public long getTotalPages() {
150
	    return 1 + (totalItems-1)/windowSize;
193
		return 1 + (totalItems-1)/windowSize;
151
	}
194
	}
152
 
195
 
153
	public long getCurrentPage() {
196
	public long getCurrentPage() {
154
		return Long.parseLong(getId());
197
		return Long.parseLong(getId());
155
	}
198
	}
156
	
199
 
157
	public String getRedirectUrl(){
200
	public String getRedirectUrl(){
158
		return redirectUrl;
201
		return redirectUrl;
-
 
202
	}
-
 
203
	public boolean isUserLoggedIn()	{
-
 
204
		return userinfo.isLoggedIn();
159
    }
205
	}
-
 
206
	public boolean isPrivatedealUser() throws TTransportException, TException{
-
 
207
		if(!uc.isAlive())
-
 
208
			uc = new UserClient().getClient();
-
 
209
		return uc.isPrivateDealUser(super.getUserInfo().getUserId());
-
 
210
	}
160
}
211
}