Subversion Repositories SmartDukaan

Rev

Rev 3126 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1372 vikas 1
package in.shop2020.serving.controllers;
2
 
1614 rajveer 3
import in.shop2020.model.v1.user.UserContextService;
4
import in.shop2020.model.v1.user.WidgetException;
3599 rajveer 5
import in.shop2020.serving.services.ContentServingService;
6
import in.shop2020.serving.utils.SnippetType;
3126 rajveer 7
import in.shop2020.thrift.clients.UserClient;
1614 rajveer 8
 
9
import java.util.ArrayList;
10
import java.util.List;
11
 
12
import org.apache.log4j.Logger;
3052 vikas 13
import org.apache.struts2.convention.annotation.Action;
14
import org.apache.struts2.convention.annotation.InterceptorRef;
1614 rajveer 15
import org.apache.thrift.TException;
1761 vikas 16
import org.json.JSONArray;
17
import org.json.JSONException;
1614 rajveer 18
 
1372 vikas 19
public class BrowseHistoryController extends BaseController{
20
 
1761 vikas 21
	private static Logger log = Logger.getLogger(BrowseHistoryController.class);
1372 vikas 22
	private static final long serialVersionUID = -5508838941749125824L;
1614 rajveer 23
	private String id;
24
	private String snippets="";
25
	private List<String> items;
1761 vikas 26
	private String itemsInJson;
27
 
1372 vikas 28
	public String index() {
29
		return "index";
1614 rajveer 30
	}
31
 
1761 vikas 32
	// GET browse-history/[1,2,3]
3052 vikas 33
	@Action(interceptorRefs={@InterceptorRef("caching"),@InterceptorRef("myDefault")})
1614 rajveer 34
	public String show() {
35
		return "show";
36
	}
37
 
1761 vikas 38
	//Handle POST /browse-history/{id}
1614 rajveer 39
	public String create(){
1761 vikas 40
		log.info("list of item ids is " + items);
41
 
42
		if (items != null) {
1623 rajveer 43
			if(userinfo.getUserId() != -1){
1761 vikas 44
				updateHistory(items);
1623 rajveer 45
			}
1614 rajveer 46
		}
1761 vikas 47
		try {
48
            JSONArray itemsJson =  new JSONArray();
3126 rajveer 49
            UserClient userServiceClient = new UserClient();
1761 vikas 50
            UserContextService.Client userClient = userServiceClient.getClient();
2982 rajveer 51
            List<Long> widgetItemIds = userClient.getBrowseHistoryItems(userinfo.getUserId());
52
            for (Long itemId : widgetItemIds) {
53
                itemsJson.put(itemId);
1761 vikas 54
            }
55
            itemsInJson = itemsJson.toString();
56
        } catch (WidgetException e) {
2944 chandransh 57
            log.error("Unable to create the browse history widget because of: ", e);
1761 vikas 58
        } catch (TException e) {
2944 chandransh 59
            log.error("Unable to create the browse history widget because of: ", e);
1761 vikas 60
        } catch (Exception e) {
2944 chandransh 61
            log.error("Unable to create the browse history widget because of: ", e);
1761 vikas 62
        }
63
        return "success";
1614 rajveer 64
	}
65
 
1761 vikas 66
    public String getSnippets() {
1614 rajveer 67
    	if(items != null){
68
	    	for(String itemId: items){
3599 rajveer 69
	    		snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, itemId, sourceId);
1614 rajveer 70
			}
71
    	}
72
		return snippets;
73
    }
74
 
1761 vikas 75
    public void setId(String itemsString) {
76
        JSONArray itemJson = null;
77
        items = new ArrayList<String>();
78
        try {
79
            itemJson = new JSONArray(itemsString);
80
        } catch (JSONException e) {
81
            log.error("Bad json : " + itemsString);
82
            return;
83
        }
84
        for (int i=0; i<itemJson.length(); i++) {
85
            try {
86
                items.add(itemJson.getString(i));
87
            } catch (JSONException e) {
88
                log.error("Bad item at index : " + i);
89
            }
90
        }
1614 rajveer 91
    }
92
 
93
    public String getId() {
94
    	return this.id;
95
    }
1761 vikas 96
 
97
    public String getItemsInJson() {
98
        return this.itemsInJson;
99
    }
1614 rajveer 100
 
1761 vikas 101
    private void updateHistory(List<String> historyItems) {
3126 rajveer 102
		UserClient userServiceClient;
1614 rajveer 103
		try {
3126 rajveer 104
			userServiceClient = new UserClient();
1614 rajveer 105
			UserContextService.Client userClient = userServiceClient.getClient();
1761 vikas 106
			for (String item : historyItems) {
107
				long itemId = Long.parseLong(item);
1614 rajveer 108
				userClient.updateBrowseHistory(userinfo.getUserId(), itemId);
109
			}	
110
		} catch (Exception e) {
2944 chandransh 111
		    log.error("Unable to update the browse history because of: ", e);
1614 rajveer 112
		}
113
	}
1372 vikas 114
}