Rev 2943 | Rev 2950 | 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.io.IOException;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 latest arrivals items from the catalog service* @author rajveer**/public class LatestArrivalsController extends BaseController {private static final long serialVersionUID = -2720118892178057878L;private static Logger log = Logger.getLogger(Class.class);private static final int windowSize = 20;private long beginIndex = 0;private long totalItems;private String id;List<Long> items = null;CatalogServiceClient catalogClientService = null;public LatestArrivalsController() {super();try {catalogClientService = new CatalogServiceClient();} 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");//Right now if we have brand name, we can just send back for all categoriesif(brandName!=null){categoryId = -1;}in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, brandName, categoryId);return new DefaultHttpHeaders("index");}// GET /showpublic HttpHeaders show() {in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();try {this.setTotalItems(client.getLatestArrivalsCount());this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, null, -1);} catch (InventoryServiceException 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 List<String> getItems(){ArrayList<String> snippets = new ArrayList<String>();if(items != null){for(long item: items){try{snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));}catch(IOException ioex){log.error("Unable to read snippet for " + item, ioex);}}}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/windowSize;}public long getCurrentPage() {return Long.parseLong(getId());}}