Subversion Repositories SmartDukaan

Rev

Rev 1928 | 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 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;
        
        private List<String> snippets = null;
        
        CatalogServiceClient catalogClientService = null;
        
        public BestDealsController() {
                super();
                try {
                        catalogClientService = new CatalogServiceClient();
                } catch (Exception e) {
                        log.error("Unable to get catalog service client.", e);
                }
        }
    
    // GET /index
    public 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 data for all categories
        if(brandName != null){
            categoryId = -1;
        }
        in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
        this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
        setSnippets(items);
        return new DefaultHttpHeaders("index");
    }

    // GET /show
    public HttpHeaders show() {
        in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
        try {
                        this.setTotalItems(client.getBestDealsCount());
                        this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
                        setSnippets(items);
                } catch (InventoryServiceException e) {
                        log.error("Unable to get best deals from inventory service.", e);
                } catch (TException e) {
                        log.error("Unable to get best deals from inventory service.", e);
                }
        return new DefaultHttpHeaders("show");
    }
    
    private void setSnippets(List<Long> items){
        this.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("Error while getting the snippet for: " + item, ioex);
                        }
                        }
        }
    }
    
    public List<String> getSnippets() {
        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());
        }
}