Subversion Repositories SmartDukaan

Rev

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

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