Subversion Repositories SmartDukaan

Rev

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

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