Subversion Repositories SmartDukaan

Rev

Rev 1923 | Rev 2949 | 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) {
47
			log.error("Unable to get catalog service client.");
48
			e.printStackTrace();
49
		}
50
	}
51
 
52
    // GET /index
53
    public HttpHeaders index() throws Exception {
637 rajveer 54
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
1923 rajveer 55
    	String brandName = request.getParameter("brand");
56
    	//Right now if we have brand name, we can just send back for all categories
57
    	if(brandName!=null){
58
    	    categoryId = -1;
59
    	}
545 rajveer 60
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
1923 rajveer 61
    	this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, brandName, categoryId);
545 rajveer 62
    	return new DefaultHttpHeaders("index");
63
    }
64
 
65
    // GET /show
66
    public HttpHeaders show() {
67
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
68
    	try {
590 chandransh 69
			this.setTotalItems(client.getLatestArrivalsCount());
1923 rajveer 70
			this.items = client.getLatestArrivalsCatalogIds(beginIndex, windowSize, null, -1);
545 rajveer 71
		} catch (InventoryServiceException e) {
72
			log.error("Unable to get latest arrivals from inventory service.");
73
			log.error("Exception id is:"  + e.getId() + " Exception message is:" + e.getMessage());
74
			e.printStackTrace();
75
		} catch (TException e) {
76
			log.error("Unable to get latest arrivals from inventory service.");
77
			log.error(" Exception message is:" + e.getMessage());
78
			e.printStackTrace();
79
		}
80
    	return new DefaultHttpHeaders("show");
81
    }
82
 
968 chandransh 83
    public List<String> getItems(){
741 rajveer 84
    	ArrayList<String> snippets = new ArrayList<String>();
85
    	if(items != null){
86
    		for(long item: items){
968 chandransh 87
    			try{
88
    				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
89
    			}catch(IOException ioex){
90
    				ioex.printStackTrace();
91
    				log.error(ioex);
92
    			}
741 rajveer 93
			}
94
    	}
95
 
96
    	return snippets;
545 rajveer 97
    }
98
 
99
	public void setId(String id) {
100
		this.id = id;
2943 chandransh 101
		this.beginIndex = windowSize * (Long.parseLong(id)-1);
545 rajveer 102
	}
103
 
104
	public String getId() {
105
		return id;
106
	}
107
 
108
	public long getBeginIndex(){
109
		if(totalItems>0)
110
			return beginIndex+1;
111
		return beginIndex;
112
	}
113
 
114
	public void setTotalItems(long totalItems) {
115
		this.totalItems = totalItems;
116
	}
117
 
118
	public long getTotalItems() {
119
		return totalItems;
120
	}
121
 
122
	public long getTotalPages() {
123
		return 1 + totalItems/windowSize;
124
	}
125
 
126
	public long getCurrentPage() {
127
		return Long.parseLong(getId());
128
	}
129
}