Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 5552 | 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);
97
	            if(isNew){
98
	                try {
2718 varun.gupt 99
	                    htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 100
	                }
101
	                catch (FileNotFoundException e) {
2718 varun.gupt 102
	                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 103
	                }
104
	                catch (IOException e) {
2718 varun.gupt 105
	                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);
1761 vikas 106
	                }
3185 vikas 107
                    DataLogger.logData(EventType.RESEARCH_ADD, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), StringUtils.join(items, "_"));
1761 vikas 108
                }
109
	        }
650 rajveer 110
		}
651 rajveer 111
		return "success";
650 rajveer 112
	}
113
 
1614 rajveer 114
	@Action(value="deletefromresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
651 rajveer 115
	public String destroy(){
1761 vikas 116
		for (String item : items){
117
			long itemId = Long.parseLong(item);
1957 vikas 118
			deleteFromMyResearch(userinfo.getUserId(), itemId);
3185 vikas 119
			DataLogger.logData(EventType.RESEARCH_DELETE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), StringUtils.join(items, "_"));
650 rajveer 120
		}
651 rajveer 121
		return "success";
650 rajveer 122
	}
123
 
762 rajveer 124
	private void deleteFromMyResearch(long userId, long itemId) {
125
 
126
		try {
3126 rajveer 127
			UserClient userServiceClient = new UserClient();
762 rajveer 128
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
129
			userClient.deleteItemFromMyResearch(userId, itemId);
130
		} catch (WidgetException e) {
2949 chandransh 131
		    log.error("Unable to delete item from the myresearch widget", e);
762 rajveer 132
		} catch (TException e) {
2949 chandransh 133
		    log.error("Unable to delete item from the myresearch widget", e);
762 rajveer 134
		} catch (Exception e) {
2949 chandransh 135
		    log.error("Unable to delete item from the myresearch widget", e);
762 rajveer 136
		}
137
	}
1033 varun.gupt 138
 
650 rajveer 139
	public String getHtmlSnippet(){
1032 varun.gupt 140
		if(htmlSnippet == "")	{
1643 rajveer 141
			htmlSnippet = "0";
650 rajveer 142
		}
143
		return htmlSnippet;
1761 vikas 144
	}
145
 
146
	public String getSnippets() {
147
        return snippets;
148
    }
149
 
150
	public int getTotalItems() {
151
        return totalItems;
152
    }
153
 
154
	public void setProductid(String itemsString) {
155
        setId(itemsString);
156
    }
157
 
158
	public void setId(String itemsString) {
159
        JSONArray itemJson = null;
160
        try {
161
            itemJson = new JSONArray(itemsString);
162
        } catch (JSONException e) {
163
            log.error("Bad json : " + itemsString);
164
            return;
165
        }
2961 chandransh 166
 
167
        items = new ArrayList<String>();
1761 vikas 168
        for (int i=0; i<itemJson.length(); i++) {
169
            try {
170
                items.add(itemJson.getString(i));
171
            } catch (JSONException e) {
172
                log.error("Bad item at index : " + i);
173
            }
174
        }
175
    }
176
 
177
    public String getItemsInJson() {
178
        return this.itemsInJson;
179
    }
1032 varun.gupt 180
}