Subversion Repositories SmartDukaan

Rev

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;
3126 rajveer 7
import in.shop2020.thrift.clients.UserClient;
1614 rajveer 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;
3052 vikas 16
import org.apache.struts2.convention.annotation.Action;
17
import org.apache.struts2.convention.annotation.InterceptorRef;
1614 rajveer 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]
3052 vikas 36
	@Action(interceptorRefs={@InterceptorRef("caching"),@InterceptorRef("myDefault")})
1614 rajveer 37
	public String show() {
38
		return "show";
39
	}
40
 
1761 vikas 41
	//Handle POST /browse-history/{id}
1614 rajveer 42
	public String create(){
1761 vikas 43
		log.info("list of item ids is " + items);
44
 
45
		if (items != null) {
1623 rajveer 46
			if(userinfo.getUserId() != -1){
1761 vikas 47
				updateHistory(items);
1623 rajveer 48
			}
1614 rajveer 49
		}
1761 vikas 50
		try {
51
            JSONArray itemsJson =  new JSONArray();
3126 rajveer 52
            UserClient userServiceClient = new UserClient();
1761 vikas 53
            UserContextService.Client userClient = userServiceClient.getClient();
2982 rajveer 54
            List<Long> widgetItemIds = userClient.getBrowseHistoryItems(userinfo.getUserId());
55
            for (Long itemId : widgetItemIds) {
56
                itemsJson.put(itemId);
1761 vikas 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) {
3126 rajveer 113
		UserClient userServiceClient;
1614 rajveer 114
		try {
3126 rajveer 115
			userServiceClient = new UserClient();
1614 rajveer 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
}