Subversion Repositories SmartDukaan

Rev

Rev 1852 | Rev 2982 | 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;
1761 vikas 4
import in.shop2020.model.v1.user.Widget;
1614 rajveer 5
import in.shop2020.model.v1.user.WidgetException;
1761 vikas 6
import in.shop2020.model.v1.user.WidgetItem;
1614 rajveer 7
import in.shop2020.serving.utils.FileUtils;
8
import in.shop2020.serving.utils.Utils;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
10
 
11
import java.io.File;
1761 vikas 12
import java.io.FileNotFoundException;
1614 rajveer 13
import java.io.IOException;
14
import java.util.ArrayList;
15
import java.util.List;
16
 
17
import org.apache.log4j.Logger;
18
import org.apache.thrift.TException;
1761 vikas 19
import org.json.JSONArray;
20
import org.json.JSONException;
1614 rajveer 21
 
1372 vikas 22
public class BrowseHistoryController extends BaseController{
23
 
1761 vikas 24
	private static Logger log = Logger.getLogger(BrowseHistoryController.class);
1372 vikas 25
	private static final long serialVersionUID = -5508838941749125824L;
1614 rajveer 26
	private String id;
27
	private String snippets="";
28
	private List<String> items;
1761 vikas 29
	private String itemsInJson;
30
 
1372 vikas 31
	public String index() {
32
		return "index";
1614 rajveer 33
	}
34
 
1761 vikas 35
	// GET browse-history/[1,2,3]
1614 rajveer 36
	public String show() {
37
		return "show";
38
	}
39
 
1761 vikas 40
	//Handle POST /browse-history/{id}
1614 rajveer 41
	public String create(){
1761 vikas 42
		log.info("list of item ids is " + items);
43
 
44
		if (items != null) {
1623 rajveer 45
			if(userinfo.getUserId() != -1){
1761 vikas 46
				updateHistory(items);
1623 rajveer 47
			}
1614 rajveer 48
		}
1761 vikas 49
		try {
50
            JSONArray itemsJson =  new JSONArray();
51
            UserContextServiceClient userServiceClient = new UserContextServiceClient();
52
            UserContextService.Client userClient = userServiceClient.getClient();
53
            Widget resWidget = userClient.getBrowseHistory(userinfo.getUserId());
54
            List<WidgetItem> widgetItems = resWidget.getItems();
55
            for (WidgetItem wItem : widgetItems) {
56
                itemsJson.put(wItem.getItem_id());
57
            }
58
            itemsInJson = itemsJson.toString();
59
        } catch (WidgetException e) {
2944 chandransh 60
            log.error("Unable to create the browse history widget because of: ", e);
1761 vikas 61
        } catch (TException e) {
2944 chandransh 62
            log.error("Unable to create the browse history widget because of: ", e);
1761 vikas 63
        } catch (Exception e) {
2944 chandransh 64
            log.error("Unable to create the browse history widget because of: ", e);
1761 vikas 65
        }
66
        return "success";
1614 rajveer 67
	}
68
 
1761 vikas 69
    public String getSnippets() {
1614 rajveer 70
    	if(items != null){
71
	    	for(String itemId: items){
1761 vikas 72
	    	    try {
73
				    snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
74
	    	    }
75
	    	    catch (FileNotFoundException e) {
76
                    log.error(e.getMessage());
77
                }
78
	    	    catch (IOException e) {
79
                    log.error(e.getMessage());
80
                }
1614 rajveer 81
			}
82
    	}
83
		return snippets;
84
    }
85
 
1761 vikas 86
    public void setId(String itemsString) {
87
        JSONArray itemJson = null;
88
        items = new ArrayList<String>();
89
        try {
90
            itemJson = new JSONArray(itemsString);
91
        } catch (JSONException e) {
92
            log.error("Bad json : " + itemsString);
93
            return;
94
        }
95
        for (int i=0; i<itemJson.length(); i++) {
96
            try {
97
                items.add(itemJson.getString(i));
98
            } catch (JSONException e) {
99
                log.error("Bad item at index : " + i);
100
            }
101
        }
1614 rajveer 102
    }
103
 
104
    public String getId() {
105
    	return this.id;
106
    }
1761 vikas 107
 
108
    public String getItemsInJson() {
109
        return this.itemsInJson;
110
    }
1614 rajveer 111
 
1761 vikas 112
    private void updateHistory(List<String> historyItems) {
1614 rajveer 113
		UserContextServiceClient userServiceClient;
114
		try {
115
			userServiceClient = new UserContextServiceClient();
116
			UserContextService.Client userClient = userServiceClient.getClient();
1761 vikas 117
			for (String item : historyItems) {
118
				long itemId = Long.parseLong(item);
1614 rajveer 119
				userClient.updateBrowseHistory(userinfo.getUserId(), itemId);
120
			}	
121
		} catch (Exception e) {
2944 chandransh 122
		    log.error("Unable to update the browse history because of: ", e);
1614 rajveer 123
		}
124
	}
1372 vikas 125
}