Subversion Repositories SmartDukaan

Rev

Rev 2950 | Rev 3017 | 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
 */
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");
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();
2975 chandransh 60
    	List<Long> latestArrivalCategories = new ArrayList<Long>();
61
        latestArrivalCategories.add(categoryId);
62
    	this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, brandName, latestArrivalCategories);
545 rajveer 63
    	return new DefaultHttpHeaders("index");
64
    }
65
 
66
    // GET /show
67
    public HttpHeaders show() {
68
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
69
    	try {
590 chandransh 70
			this.setTotalItems(client.getLatestArrivalsCount());
2975 chandransh 71
			this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, null, new ArrayList<Long>());
545 rajveer 72
		} catch (InventoryServiceException e) {
2949 chandransh 73
			log.error("Unable to get latest arrivals from inventory service.", e);
545 rajveer 74
		} catch (TException e) {
2949 chandransh 75
			log.error("Unable to get latest arrivals from inventory service.", e);
545 rajveer 76
		}
77
    	return new DefaultHttpHeaders("show");
78
    }
79
 
968 chandransh 80
    public List<String> getItems(){
741 rajveer 81
    	ArrayList<String> snippets = new ArrayList<String>();
82
    	if(items != null){
83
    		for(long item: items){
968 chandransh 84
    			try{
85
    				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
86
    			}catch(IOException ioex){
2949 chandransh 87
    				log.error("Unable to read snippet for " + item, ioex);
968 chandransh 88
    			}
741 rajveer 89
			}
90
    	}
91
 
92
    	return snippets;
545 rajveer 93
    }
94
 
95
	public void setId(String id) {
96
		this.id = id;
2943 chandransh 97
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 98
	}
99
 
100
	public String getId() {
101
		return id;
102
	}
103
 
104
	public long getBeginIndex(){
105
		if(totalItems>0)
106
			return beginIndex+1;
107
		return beginIndex;
108
	}
109
 
110
	public void setTotalItems(long totalItems) {
111
		this.totalItems = totalItems;
112
	}
113
 
114
	public long getTotalItems() {
115
		return totalItems;
116
	}
117
 
118
	public long getTotalPages() {
2950 chandransh 119
	    return 1 + (totalItems-1)/windowSize;
545 rajveer 120
	}
121
 
122
	public long getCurrentPage() {
123
		return Long.parseLong(getId());
124
	}
125
}