Rev 5945 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.serving.cache.EhcacheWrapper;import in.shop2020.serving.services.ContentServingService;import in.shop2020.serving.utils.SnippetType;import in.shop2020.thrift.clients.CatalogClient;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import net.sf.ehcache.CacheManager;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.apache.thrift.TException;/*** get latest arrivals items from the catalog service* @author rajveer**/@Results({@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})})@SuppressWarnings("serial")public class LatestArrivalsController extends BaseController {private static Logger log = Logger.getLogger(Class.class);private static final int windowSize = 20;private static final String SHOWCASE_LATEST_ARRIVALS_CACHE_KEY = "SHOWCASE_LATEST_ARRIVALS";private static final String SHOWCASE_LATEST_ARRIVALS_COUNT_CACHE_KEY = "SHOWCASE_LATEST_ARRIVALS_COUNT";private long beginIndex = 0;private Long totalItems = 0l;private String id;private String redirectUrl;List<Long> items = null;private List<String> snippets = null;CatalogClient catalogClientService = null;public LatestArrivalsController() {super();try {catalogClientService = new CatalogClient();} catch (Exception e) {log.error("Unable to get catalog service client.", e);}}// GET /indexpublic HttpHeaders index() throws Exception {long categoryId = Long.parseLong(request.getParameter("categoryid"));String brandName = request.getParameter("brand");List<Long> latestArrivalCategories = new ArrayList<Long>();//Right now if we have brand name, we can just send back for all categoriesif(brandName==null){latestArrivalCategories.add(categoryId);}in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, brandName, latestArrivalCategories);setSnippets();return new DefaultHttpHeaders("index");}// GET /show@SuppressWarnings("unchecked")public HttpHeaders show() {if(getCurrentPage() > 1) {this.redirectUrl="/best-sellers/1";return new DefaultHttpHeaders("redirect");}EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());if(sourceId == -1){this.snippets = (List<String>)showcasePageSnippetsCache.get(SHOWCASE_LATEST_ARRIVALS_CACHE_KEY + beginIndex);this.totalItems = (Long)showcasePageSnippetsCache.get(SHOWCASE_LATEST_ARRIVALS_COUNT_CACHE_KEY);if(this.snippets != null && !this.snippets.isEmpty() && this.totalItems > 0) {log.info("Using Cache.");return new DefaultHttpHeaders("show");}}log.info("Getting latest arrivals from snippets.");in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();try {long count = client.getLatestArrivalsCount();this.setTotalItems(count > 20 ? 20 : count);if(sourceId == -1){showcasePageSnippetsCache.put(SHOWCASE_LATEST_ARRIVALS_COUNT_CACHE_KEY, this.totalItems);}this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, null, Arrays.asList(new Long[]{10006L, 10010L, 10050L}));setSnippets();if(sourceId == -1){showcasePageSnippetsCache.put(SHOWCASE_LATEST_ARRIVALS_CACHE_KEY + beginIndex, this.snippets);}} catch (CatalogServiceException e) {log.error("Unable to get latest arrivals from inventory service.", e);} catch (TException e) {log.error("Unable to get latest arrivals from inventory service.", e);}return new DefaultHttpHeaders("show");}public void setSnippets(){snippets = new ArrayList<String>();if(items != null){for(long item: items){snippets.add(ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, item+"", sourceId));}}}public List<String> getSnippets() {return snippets;}public void setId(String id) {this.id = id;this.beginIndex = windowSize * (Long.parseLong(id)-1);}public String getId() {return id;}public long getBeginIndex(){if(totalItems>0)return beginIndex+1;return beginIndex;}public void setTotalItems(long totalItems) {this.totalItems = totalItems;}public long getTotalItems() {return totalItems;}public long getTotalPages() {return 1 + (totalItems-1)/windowSize;}public long getCurrentPage() {return Long.parseLong(getId());}public String getRedirectUrl(){return this.redirectUrl;}}