Subversion Repositories SmartDukaan

Rev

Rev 2453 | Rev 2950 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
545 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
741 rajveer 6
import java.io.File;
1371 ankur.sing 7
import java.io.IOException;
741 rajveer 8
import java.util.ArrayList;
545 rajveer 9
import java.util.List;
10
 
11
import in.shop2020.model.v1.catalog.InventoryServiceException;
741 rajveer 12
import in.shop2020.serving.utils.FileUtils;
13
import in.shop2020.serving.utils.Utils;
545 rajveer 14
import in.shop2020.thrift.clients.CatalogServiceClient;
15
 
832 rajveer 16
import org.apache.log4j.Logger;
545 rajveer 17
import org.apache.struts2.rest.DefaultHttpHeaders;
18
import org.apache.struts2.rest.HttpHeaders;
19
import org.apache.thrift.TException;
20
 
21
/**
22
 * get best sellers items from the catalog service
23
 * @author rajveer
24
 *	
25
 */
26
public class BestSellersController extends BaseController {
27
 
1044 chandransh 28
	private static final long serialVersionUID = 3380274695464543863L;
545 rajveer 29
 
832 rajveer 30
	private static Logger log = Logger.getLogger(Class.class);	
2943 chandransh 31
	private static final int windowSize = 20;
545 rajveer 32
 
33
	private long beginIndex = 0;
34
 
35
	private long totalItems;
36
 
37
	private String id;
38
 
39
	List<Long> items = null;
40
 
41
	CatalogServiceClient catalogClientService = null;
42
 
43
	public BestSellersController() {
44
		super();
45
		try {
46
			catalogClientService = new CatalogServiceClient();
47
		} catch (Exception e) {
2453 chandransh 48
			log.error("Unable to get catalog service client.", e);
545 rajveer 49
		}
50
	}
51
 
52
    // GET /index
53
    public HttpHeaders index() throws Exception {
1923 rajveer 54
        long categoryId = Long.parseLong(request.getParameter("categoryid"));
55
        String brandName = request.getParameter("brand");
56
        //Right now if we have brand name, we can just send back data for all categories
57
        if(brandName!=null){
58
            categoryId = -1;
59
        }
545 rajveer 60
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
1923 rajveer 61
    	this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, brandName, categoryId);
545 rajveer 62
    	return new DefaultHttpHeaders("index");
63
    }
64
 
65
    // GET /show
66
    public HttpHeaders show() {
67
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
68
    	try {
590 chandransh 69
			this.setTotalItems(client.getBestSellersCount());
1923 rajveer 70
			this.items = client.getBestSellersCatalogIds(beginIndex, windowSize, null, -1);
545 rajveer 71
		} catch (InventoryServiceException e) {
2453 chandransh 72
			log.error("Unable to get best sellers from inventory service.", e);
545 rajveer 73
		} catch (TException e) {
2453 chandransh 74
			log.error("Unable to get best sellers from inventory service.", e);
545 rajveer 75
		}
76
    	return new DefaultHttpHeaders("show");
77
    }
78
 
1371 ankur.sing 79
    public List<String> getItems() {
741 rajveer 80
    	ArrayList<String> snippets = new ArrayList<String>();
81
    	if(items != null){
82
    		for(long item: items){
1371 ankur.sing 83
				try {
2453 chandransh 84
					snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
1371 ankur.sing 85
				} catch (IOException ioex) {
2453 chandransh 86
					log.error("Error while getting the snippet for: " + item, ioex);
1371 ankur.sing 87
				}
741 rajveer 88
			}
89
    	}
90
 
91
    	return snippets;
1371 ankur.sing 92
    } 
93
 
545 rajveer 94
	public void setId(String id) {
95
		this.id = id;
2943 chandransh 96
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 97
	}
98
 
99
	public String getId() {
100
		return id;
101
	}
102
 
103
	public long getBeginIndex(){
104
		if(totalItems>0)
105
			return beginIndex+1;
106
		return beginIndex;
107
	}
108
 
109
	public void setTotalItems(long totalItems) {
110
		this.totalItems = totalItems;
111
	}
112
 
113
	public long getTotalItems() {
114
		return totalItems;
115
	}
116
 
117
	public long getTotalPages() {
118
		return 1 + totalItems/windowSize;
119
	}
120
 
121
	public long getCurrentPage() {
122
		return Long.parseLong(getId());
123
	}
124
}