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;
2453 chandransh 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 deals items from the catalog service
23
 * @author rajveer
24
 *	
25
 */
26
public class BestDealsController extends BaseController {
27
 
1044 chandransh 28
	private static final long serialVersionUID = -6862524269260661024L;
545 rajveer 29
 
2453 chandransh 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
 
2453 chandransh 41
	private List<String> snippets = null;
42
 
545 rajveer 43
	CatalogServiceClient catalogClientService = null;
44
 
45
	public BestDealsController() {
46
		super();
47
		try {
48
			catalogClientService = new CatalogServiceClient();
49
		} catch (Exception e) {
2453 chandransh 50
			log.error("Unable to get catalog service client.", e);
545 rajveer 51
		}
52
	}
53
 
54
    // GET /index
55
    public HttpHeaders index() throws Exception {
637 rajveer 56
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
1928 rajveer 57
        String brandName = request.getParameter("brand");
58
        //Right now if we have brand name, we can just send back data for all categories
2453 chandransh 59
        if(brandName != null){
1928 rajveer 60
            categoryId = -1;
61
        }
545 rajveer 62
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
1928 rajveer 63
    	this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
2453 chandransh 64
    	setSnippets(items);
545 rajveer 65
    	return new DefaultHttpHeaders("index");
66
    }
67
 
68
    // GET /show
69
    public HttpHeaders show() {
70
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
71
    	try {
590 chandransh 72
			this.setTotalItems(client.getBestDealsCount());
1928 rajveer 73
			this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
2453 chandransh 74
			setSnippets(items);
545 rajveer 75
		} catch (InventoryServiceException e) {
2453 chandransh 76
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 77
		} catch (TException e) {
2453 chandransh 78
			log.error("Unable to get best deals from inventory service.", e);
545 rajveer 79
		}
80
    	return new DefaultHttpHeaders("show");
81
    }
82
 
2453 chandransh 83
    private void setSnippets(List<Long> items){
84
    	this.snippets = new ArrayList<String>();
741 rajveer 85
    	if(items != null){
86
    		for(long item: items){
2453 chandransh 87
    			try {
88
    				snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
89
    			} catch (IOException ioex) {
90
    				log.error("Error while getting the snippet for: " + item, ioex);
91
    			}
741 rajveer 92
			}
93
    	}
2453 chandransh 94
    }
95
 
96
    public List<String> getSnippets() {
741 rajveer 97
    	return snippets;
545 rajveer 98
    }
99
 
100
	public void setId(String id) {
101
		this.id = id;
2943 chandransh 102
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 103
	}
104
 
105
	public String getId() {
106
		return id;
107
	}
108
 
109
	public long getBeginIndex(){
110
		if(totalItems>0)
111
			return beginIndex+1;
112
		return beginIndex;
113
	}
114
 
115
	public void setTotalItems(long totalItems) {
116
		this.totalItems = totalItems;
117
	}
118
 
119
	public long getTotalItems() {
120
		return totalItems;
121
	}
122
 
123
	public long getTotalPages() {
124
		return 1 + totalItems/windowSize;
125
	}
126
 
127
	public long getCurrentPage() {
128
		return Long.parseLong(getId());
129
	}
130
}