Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
545 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import java.util.List;
7
 
8
import in.shop2020.model.v1.catalog.InventoryServiceException;
637 rajveer 9
import in.shop2020.serving.services.PageLoaderHandler;
545 rajveer 10
import in.shop2020.thrift.clients.CatalogServiceClient;
11
 
12
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
14
import org.apache.struts2.rest.DefaultHttpHeaders;
15
import org.apache.struts2.rest.HttpHeaders;
16
import org.apache.thrift.TException;
17
 
18
/**
19
 * get latest arrivals items from the catalog service
20
 * @author rajveer
21
 *	
22
 */
23
public class LatestArrivalsController extends BaseController {
24
 
25
	/**
26
	 * 
27
	 */
28
 
29
	private static Log log = LogFactory.getLog(LatestArrivalsController.class);
30
 
31
	private final int windowSize = 20;
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
 
44
 
45
	public LatestArrivalsController() {
46
		super();
47
		try {
48
			catalogClientService = new CatalogServiceClient();
49
		} catch (Exception e) {
50
			log.error("Unable to get catalog service client.");
51
			e.printStackTrace();
52
		}
53
	}
54
 
55
    // GET /index
56
    public HttpHeaders index() throws Exception {
637 rajveer 57
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
545 rajveer 58
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
637 rajveer 59
    	this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, categoryId);
545 rajveer 60
    	return new DefaultHttpHeaders("index");
61
    }
62
 
63
    // GET /show
64
    public HttpHeaders show() {
65
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
66
    	try {
590 chandransh 67
			this.setTotalItems(client.getLatestArrivalsCount());
624 chandransh 68
			this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, -1);
545 rajveer 69
		} catch (InventoryServiceException e) {
70
			log.error("Unable to get latest arrivals from inventory service.");
71
			log.error("Exception id is:"  + e.getId() + " Exception message is:" + e.getMessage());
72
			e.printStackTrace();
73
		} catch (TException e) {
74
			log.error("Unable to get latest arrivals from inventory service.");
75
			log.error(" Exception message is:" + e.getMessage());
76
			e.printStackTrace();
77
		}
78
    	return new DefaultHttpHeaders("show");
79
    }
80
 
81
    public List<Long> getItems(){
82
    	return items;
83
    }
84
 
85
	public void setId(String id) {
86
		this.id = id;
87
		this.beginIndex = this.windowSize * (Long.parseLong(id)-1);
88
	}
89
 
90
	public String getId() {
91
		return id;
92
	}
93
 
94
	public long getBeginIndex(){
95
		if(totalItems>0)
96
			return beginIndex+1;
97
		return beginIndex;
98
	}
99
 
100
	public void setTotalItems(long totalItems) {
101
		this.totalItems = totalItems;
102
	}
103
 
104
	public long getTotalItems() {
105
		return totalItems;
106
	}
107
 
108
	public long getTotalPages() {
109
		return 1 + totalItems/windowSize;
110
	}
111
 
112
	public long getCurrentPage() {
113
		return Long.parseLong(getId());
114
	}
115
}