Subversion Repositories SmartDukaan

Rev

Rev 1034 | Rev 2943 | 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 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.printStackTrace();
                }
        }
    
    // GET /index
    public HttpHeaders index() throws Exception {
        long categoryId = Long.parseLong(request.getParameter("categoryid"));
        in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
        this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, categoryId);
        return new DefaultHttpHeaders("index");
    }

    // GET /show
    public HttpHeaders show() {
        in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
        try {
                        this.setTotalItems(client.getLatestArrivalsCount());
                        this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, -1);
                } catch (InventoryServiceException e) {
                        log.error("Unable to get latest arrivals 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 latest arrivals from inventory service.");
                        log.error(" Exception message is:" + e.getMessage());
                        e.printStackTrace();
                }
        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){
                                ioex.printStackTrace();
                                log.error(ioex);
                        }
                        }
        }
                
        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());
        }
}