Subversion Repositories SmartDukaan

Rev

Rev 2511 | Rev 2949 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
650 rajveer 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
2419 vikas 4
import in.shop2020.model.v1.user.UserContextService.Client;
1761 vikas 5
import in.shop2020.model.v1.user.Widget;
762 rajveer 6
import in.shop2020.model.v1.user.WidgetException;
1761 vikas 7
import in.shop2020.model.v1.user.WidgetItem;
651 rajveer 8
import in.shop2020.serving.utils.FileUtils;
650 rajveer 9
import in.shop2020.serving.utils.Utils;
651 rajveer 10
import in.shop2020.thrift.clients.UserContextServiceClient;
2511 vikas 11
import in.shop2020.utils.DataLogger;
650 rajveer 12
 
651 rajveer 13
import java.io.File;
1761 vikas 14
import java.io.FileNotFoundException;
786 rajveer 15
import java.io.IOException;
1761 vikas 16
import java.util.ArrayList;
17
import java.util.List;
650 rajveer 18
 
1957 vikas 19
import org.apache.commons.lang.StringUtils;
832 rajveer 20
import org.apache.log4j.Logger;
1614 rajveer 21
import org.apache.struts2.convention.annotation.Action;
22
import org.apache.struts2.convention.annotation.InterceptorRef;
23
import org.apache.struts2.convention.annotation.Result;
24
import org.apache.struts2.convention.annotation.Results;
762 rajveer 25
import org.apache.thrift.TException;
1761 vikas 26
import org.json.JSONArray;
27
import org.json.JSONException;
650 rajveer 28
 
2145 chandransh 29
@SuppressWarnings("serial")
1614 rajveer 30
@Results({
31
	@Result(name="success", location="myresearch-success.vm")
32
})
1761 vikas 33
public class MyresearchController extends BaseController {
651 rajveer 34
 
1761 vikas 35
    private UserContextServiceClient userServiceClient = null;
651 rajveer 36
	private Client client;
1761 vikas 37
    private static Logger log = Logger.getLogger(MyresearchController.class);
2718 varun.gupt 38
    private static final String htmlSnippetFileName = "MyResearchSnippet.html";
1761 vikas 39
    private String htmlSnippet = "";
40
    private String snippets="";
41
    private List<String> items;
42
    private String itemsInJson;
43
    private int totalItems = 0;
44
 
651 rajveer 45
	public MyresearchController(){
46
		super();
47
		try {
48
			userServiceClient = new UserContextServiceClient();
1386 vikas 49
			client = userServiceClient.getClient();
651 rajveer 50
		} catch (Exception e) {
51
			e.printStackTrace();
52
		}
53
	}
54
 
1614 rajveer 55
	@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})
1372 vikas 56
	public String index() {
1761 vikas 57
	    try {
58
	        JSONArray itemsJson =  new JSONArray();
59
            Widget resWidget = client.getMyResearch(userinfo.getUserId());
60
            List<WidgetItem> widgetItems = resWidget.getItems();
61
            for (WidgetItem wItem : widgetItems) {
62
                itemsJson.put(wItem.getItem_id());
63
            }
64
            itemsInJson = itemsJson.toString();
65
        } catch (WidgetException e) {
66
            e.printStackTrace();
67
        } catch (TException e) {
68
            e.printStackTrace();
69
        }
1372 vikas 70
		return "index";
71
	}
72
 
1761 vikas 73
	//myresearch/[1,2,3]
74
	@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})
75
    public String show() {
76
	    if(items != null){
77
            for(String itemId: items){
78
                try {
2718 varun.gupt 79
                    snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 80
                    totalItems++;
81
                }
82
                catch (FileNotFoundException e) {
2718 varun.gupt 83
                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 84
                }
85
                catch (IOException e) {
2718 varun.gupt 86
                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 87
                }
88
            }
89
        }
90
        return "show";
91
    }
92
 
93
	//Handle /addtoresearch/{id}
1614 rajveer 94
	@Action(value="addtoresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
786 rajveer 95
	public String create() throws WidgetException, TException, IOException {
1761 vikas 96
	    if(userinfo.getUserId() != -1){
97
			for (String item : items){
98
	            long itemId = Long.parseLong(item);
99
	            boolean isNew = client.updateMyResearch(userinfo.getUserId(), itemId);
100
	            if(isNew){
101
	                try {
2718 varun.gupt 102
	                    htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 103
	                }
104
	                catch (FileNotFoundException e) {
2718 varun.gupt 105
	                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 106
	                }
107
	                catch (IOException e) {
2718 varun.gupt 108
	                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 109
	                }
2419 vikas 110
                    DataLogger.logData(EventType.RESEARCH_ADD, session.getId(), userinfo.getUserId(), userinfo.getEmail(), StringUtils.join(items, "_"));
1761 vikas 111
                }
112
	        }
650 rajveer 113
		}
651 rajveer 114
		return "success";
650 rajveer 115
	}
116
 
1614 rajveer 117
	@Action(value="deletefromresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
651 rajveer 118
	public String destroy(){
1761 vikas 119
		for (String item : items){
120
			long itemId = Long.parseLong(item);
1957 vikas 121
			deleteFromMyResearch(userinfo.getUserId(), itemId);
2419 vikas 122
			DataLogger.logData(EventType.RESEARCH_DELETE, session.getId(), userinfo.getUserId(), userinfo.getEmail(), StringUtils.join(items, "_"));
650 rajveer 123
		}
651 rajveer 124
		return "success";
650 rajveer 125
	}
126
 
762 rajveer 127
	private void deleteFromMyResearch(long userId, long itemId) {
128
 
129
		try {
130
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
131
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
132
			userClient.deleteItemFromMyResearch(userId, itemId);
133
		} catch (WidgetException e) {
134
			e.printStackTrace();
135
		} catch (TException e) {
136
			e.printStackTrace();
137
		} catch (Exception e) {
138
			e.printStackTrace();
139
		}
140
	}
1033 varun.gupt 141
 
650 rajveer 142
	public String getHtmlSnippet(){
1032 varun.gupt 143
		if(htmlSnippet == "")	{
1643 rajveer 144
			htmlSnippet = "0";
650 rajveer 145
		}
146
		return htmlSnippet;
1761 vikas 147
	}
148
 
149
	public String getSnippets() {
150
        return snippets;
151
    }
152
 
153
	public int getTotalItems() {
154
        return totalItems;
155
    }
156
 
157
	public void setProductid(String itemsString) {
158
        setId(itemsString);
159
    }
160
 
161
	public void setId(String itemsString) {
162
        JSONArray itemJson = null;
163
        items = new ArrayList<String>();
164
        try {
165
            itemJson = new JSONArray(itemsString);
166
        } catch (JSONException e) {
167
            log.error("Bad json : " + itemsString);
168
            return;
169
        }
170
        for (int i=0; i<itemJson.length(); i++) {
171
            try {
172
                items.add(itemJson.getString(i));
173
            } catch (JSONException e) {
174
                log.error("Bad item at index : " + i);
175
            }
176
        }
177
    }
178
 
179
    public String getItemsInJson() {
180
        return this.itemsInJson;
181
    }
1032 varun.gupt 182
}