Rev 1034 | Rev 2453 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.serving.controllers;import java.io.File;import java.util.ArrayList;import java.util.List;import in.shop2020.model.v1.catalog.InventoryServiceException;import in.shop2020.serving.utils.FileUtils;import in.shop2020.serving.utils.Utils;import in.shop2020.thrift.clients.CatalogServiceClient;import org.apache.log4j.Logger;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.apache.thrift.TException;/*** get best deals items from the catalog service* @author rajveer**/public class BestDealsController extends BaseController {private static final long serialVersionUID = -6862524269260661024L;private static Logger log = Logger.getLogger(Class.class);private final int windowSize = 20;private long beginIndex = 0;private long totalItems;private String id;List<Long> items = null;CatalogServiceClient catalogClientService = null;public BestDealsController() {super();try {catalogClientService = new CatalogServiceClient();} catch (Exception e) {log.error("Unable to get catalog service client.");e.printStackTrace();}}// GET /indexpublic HttpHeaders index() throws Exception {long categoryId = Long.parseLong(request.getParameter("categoryid"));in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, categoryId);return new DefaultHttpHeaders("index");}// GET /showpublic HttpHeaders show() {in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();try {this.setTotalItems(client.getBestDealsCount());this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, -1);} catch (InventoryServiceException e) {log.error("Unable to get best deals from inventory service.");log.error("Exception id is:" + e.getId() + " Exception message is:" + e.getMessage());e.printStackTrace();} catch (TException e) {log.error("Unable to get best deals from inventory service.");log.error(" Exception message is:" + e.getMessage());e.printStackTrace();}return new DefaultHttpHeaders("show");}public List<String> getItems() throws Exception{ArrayList<String> snippets = new ArrayList<String>();if(items != null){for(long item: items){snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));}}return snippets;}public void setId(String id) {this.id = id;this.beginIndex = this.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/windowSize;}public long getCurrentPage() {return Long.parseLong(getId());}}