Subversion Repositories SmartDukaan

Rev

Rev 2944 | Rev 3052 | Go to most recent revision | 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;
5
import in.shop2020.serving.utils.FileUtils;
6
import in.shop2020.serving.utils.Utils;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
8
 
9
import java.io.File;
1761 vikas 10
import java.io.FileNotFoundException;
1614 rajveer 11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.List;
14
 
15
import org.apache.log4j.Logger;
16
import org.apache.thrift.TException;
1761 vikas 17
import org.json.JSONArray;
18
import org.json.JSONException;
1614 rajveer 19
 
1372 vikas 20
public class BrowseHistoryController extends BaseController{
21
 
1761 vikas 22
	private static Logger log = Logger.getLogger(BrowseHistoryController.class);
1372 vikas 23
	private static final long serialVersionUID = -5508838941749125824L;
1614 rajveer 24
	private String id;
25
	private String snippets="";
26
	private List<String> items;
1761 vikas 27
	private String itemsInJson;
28
 
1372 vikas 29
	public String index() {
30
		return "index";
1614 rajveer 31
	}
32
 
1761 vikas 33
	// GET browse-history/[1,2,3]
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();
49
            UserContextServiceClient userServiceClient = new UserContextServiceClient();
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){
1761 vikas 69
	    	    try {
70
				    snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
71
	    	    }
72
	    	    catch (FileNotFoundException e) {
73
                    log.error(e.getMessage());
74
                }
75
	    	    catch (IOException e) {
76
                    log.error(e.getMessage());
77
                }
1614 rajveer 78
			}
79
    	}
80
		return snippets;
81
    }
82
 
1761 vikas 83
    public void setId(String itemsString) {
84
        JSONArray itemJson = null;
85
        items = new ArrayList<String>();
86
        try {
87
            itemJson = new JSONArray(itemsString);
88
        } catch (JSONException e) {
89
            log.error("Bad json : " + itemsString);
90
            return;
91
        }
92
        for (int i=0; i<itemJson.length(); i++) {
93
            try {
94
                items.add(itemJson.getString(i));
95
            } catch (JSONException e) {
96
                log.error("Bad item at index : " + i);
97
            }
98
        }
1614 rajveer 99
    }
100
 
101
    public String getId() {
102
    	return this.id;
103
    }
1761 vikas 104
 
105
    public String getItemsInJson() {
106
        return this.itemsInJson;
107
    }
1614 rajveer 108
 
1761 vikas 109
    private void updateHistory(List<String> historyItems) {
1614 rajveer 110
		UserContextServiceClient userServiceClient;
111
		try {
112
			userServiceClient = new UserContextServiceClient();
113
			UserContextService.Client userClient = userServiceClient.getClient();
1761 vikas 114
			for (String item : historyItems) {
115
				long itemId = Long.parseLong(item);
1614 rajveer 116
				userClient.updateBrowseHistory(userinfo.getUserId(), itemId);
117
			}	
118
		} catch (Exception e) {
2944 chandransh 119
		    log.error("Unable to update the browse history because of: ", e);
1614 rajveer 120
		}
121
	}
1372 vikas 122
}