Subversion Repositories SmartDukaan

Rev

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