Subversion Repositories SmartDukaan

Rev

Rev 2975 | Rev 3112 | 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
 
3017 vikas 3
import in.shop2020.model.v1.catalog.InventoryServiceException;
4
import in.shop2020.serving.utils.FileUtils;
5
import in.shop2020.serving.utils.Utils;
6
import in.shop2020.thrift.clients.CatalogServiceClient;
7
 
741 rajveer 8
import java.io.File;
968 chandransh 9
import java.io.IOException;
741 rajveer 10
import java.util.ArrayList;
545 rajveer 11
import java.util.List;
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
 */
2975 chandransh 23
@SuppressWarnings("serial")
545 rajveer 24
public class LatestArrivalsController extends BaseController {
25
 
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");
3017 vikas 55
    	List<Long> latestArrivalCategories = new ArrayList<Long>();
1923 rajveer 56
    	//Right now if we have brand name, we can just send back for all categories
3017 vikas 57
    	if(brandName==null){
58
    	    latestArrivalCategories.add(categoryId);
1923 rajveer 59
    	}
545 rajveer 60
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
3017 vikas 61
 
62
 
2975 chandransh 63
    	this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, brandName, latestArrivalCategories);
545 rajveer 64
    	return new DefaultHttpHeaders("index");
65
    }
66
 
67
    // GET /show
68
    public HttpHeaders show() {
69
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
70
    	try {
590 chandransh 71
			this.setTotalItems(client.getLatestArrivalsCount());
2975 chandransh 72
			this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, null, new ArrayList<Long>());
545 rajveer 73
		} catch (InventoryServiceException e) {
2949 chandransh 74
			log.error("Unable to get latest arrivals from inventory service.", e);
545 rajveer 75
		} catch (TException e) {
2949 chandransh 76
			log.error("Unable to get latest arrivals from inventory service.", e);
545 rajveer 77
		}
78
    	return new DefaultHttpHeaders("show");
79
    }
80
 
968 chandransh 81
    public List<String> getItems(){
741 rajveer 82
    	ArrayList<String> snippets = new ArrayList<String>();
83
    	if(items != null){
84
    		for(long item: items){
968 chandransh 85
    			try{
86
    				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
87
    			}catch(IOException ioex){
2949 chandransh 88
    				log.error("Unable to read snippet for " + item, ioex);
968 chandransh 89
    			}
741 rajveer 90
			}
91
    	}
92
 
93
    	return snippets;
545 rajveer 94
    }
95
 
96
	public void setId(String id) {
97
		this.id = id;
2943 chandransh 98
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 99
	}
100
 
101
	public String getId() {
102
		return id;
103
	}
104
 
105
	public long getBeginIndex(){
106
		if(totalItems>0)
107
			return beginIndex+1;
108
		return beginIndex;
109
	}
110
 
111
	public void setTotalItems(long totalItems) {
112
		this.totalItems = totalItems;
113
	}
114
 
115
	public long getTotalItems() {
116
		return totalItems;
117
	}
118
 
119
	public long getTotalPages() {
2950 chandransh 120
	    return 1 + (totalItems-1)/windowSize;
545 rajveer 121
	}
122
 
123
	public long getCurrentPage() {
124
		return Long.parseLong(getId());
125
	}
126
}