Subversion Repositories SmartDukaan

Rev

Rev 1928 | Rev 2943 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1928 Rev 2453
Line 2... Line 2...
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.serving.controllers;
4
package in.shop2020.serving.controllers;
5
 
5
 
6
import java.io.File;
6
import java.io.File;
-
 
7
import java.io.IOException;
7
import java.util.ArrayList;
8
import java.util.ArrayList;
8
import java.util.List;
9
import java.util.List;
9
 
10
 
10
import in.shop2020.model.v1.catalog.InventoryServiceException;
11
import in.shop2020.model.v1.catalog.InventoryServiceException;
11
import in.shop2020.serving.utils.FileUtils;
12
import in.shop2020.serving.utils.FileUtils;
Line 24... Line 25...
24
 */
25
 */
25
public class BestDealsController extends BaseController {
26
public class BestDealsController extends BaseController {
26
	
27
	
27
	private static final long serialVersionUID = -6862524269260661024L;
28
	private static final long serialVersionUID = -6862524269260661024L;
28
	
29
	
29
	private static Logger log = Logger.getLogger(Class.class);	
30
	private static Logger log = Logger.getLogger(Class.class);
30
	private final int windowSize = 20;
31
	private final int windowSize = 20;
31
	
32
	
32
	private long beginIndex = 0;
33
	private long beginIndex = 0;
33
	
34
	
34
	private long totalItems;
35
	private long totalItems;
35
	
36
	
36
	private String id;
37
	private String id;
37
	
38
	
38
	List<Long> items = null;
39
	List<Long> items = null;
39
	
40
	
40
	CatalogServiceClient catalogClientService = null;
41
	private List<String> snippets = null;
41
	
-
 
42
	
42
	
-
 
43
	CatalogServiceClient catalogClientService = null;
43
	
44
	
44
	public BestDealsController() {
45
	public BestDealsController() {
45
		super();
46
		super();
46
		try {
47
		try {
47
			catalogClientService = new CatalogServiceClient();
48
			catalogClientService = new CatalogServiceClient();
48
		} catch (Exception e) {
49
		} catch (Exception e) {
49
			log.error("Unable to get catalog service client.");
50
			log.error("Unable to get catalog service client.", e);
50
			e.printStackTrace();
-
 
51
		}
51
		}
52
	}
52
	}
53
    
53
    
54
    // GET /index
54
    // GET /index
55
    public HttpHeaders index() throws Exception {
55
    public HttpHeaders index() throws Exception {
56
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
56
    	long categoryId = Long.parseLong(request.getParameter("categoryid"));
57
        String brandName = request.getParameter("brand");
57
        String brandName = request.getParameter("brand");
58
        //Right now if we have brand name, we can just send back data for all categories
58
        //Right now if we have brand name, we can just send back data for all categories
59
        if(brandName!=null){
59
        if(brandName != null){
60
            categoryId = -1;
60
            categoryId = -1;
61
        }
61
        }
62
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
62
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
63
    	this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
63
    	this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, brandName, categoryId);
-
 
64
    	setSnippets(items);
64
    	return new DefaultHttpHeaders("index");
65
    	return new DefaultHttpHeaders("index");
65
    }
66
    }
66
 
67
 
67
    // GET /show
68
    // GET /show
68
    public HttpHeaders show() {
69
    public HttpHeaders show() {
69
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
70
    	in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
70
    	try {
71
    	try {
71
			this.setTotalItems(client.getBestDealsCount());
72
			this.setTotalItems(client.getBestDealsCount());
72
			this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
73
			this.items = client.getBestDealsCatalogIds(beginIndex, windowSize, null, -1);
-
 
74
			setSnippets(items);
73
		} catch (InventoryServiceException e) {
75
		} catch (InventoryServiceException e) {
74
			log.error("Unable to get best deals from inventory service.");
76
			log.error("Unable to get best deals from inventory service.", e);
75
			log.error("Exception id is:"  + e.getId() + " Exception message is:" + e.getMessage());
-
 
76
			e.printStackTrace();
-
 
77
		} catch (TException e) {
77
		} catch (TException e) {
78
			log.error("Unable to get best deals from inventory service.");
78
			log.error("Unable to get best deals from inventory service.", e);
79
			log.error(" Exception message is:" + e.getMessage());
-
 
80
			e.printStackTrace();
-
 
81
		}
79
		}
82
    	return new DefaultHttpHeaders("show");
80
    	return new DefaultHttpHeaders("show");
83
    }
81
    }
84
    
82
    
85
    public List<String> getItems() throws Exception{
83
    private void setSnippets(List<Long> items){
86
    	ArrayList<String> snippets = new ArrayList<String>();
84
    	this.snippets = new ArrayList<String>();
87
    	if(items != null){
85
    	if(items != null){
88
    		for(long item: items){
86
    		for(long item: items){
-
 
87
    			try {
89
				snippets.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
88
    				snippets.add(FileUtils.read(Utils.EXPORT_ENTITIES_PATH + item + File.separator +"CategorySnippet.html"));
-
 
89
    			} catch (IOException ioex) {
-
 
90
    				log.error("Error while getting the snippet for: " + item, ioex);
-
 
91
    			}
90
			}
92
			}
91
    	}
93
    	}
-
 
94
    }
92
		
95
    
-
 
96
    public List<String> getSnippets() {
93
    	return snippets;
97
    	return snippets;
94
    }
98
    }
95
    
99
    
96
	public void setId(String id) {
100
	public void setId(String id) {
97
		this.id = id;
101
		this.id = id;