Subversion Repositories SmartDukaan

Rev

Rev 1957 | Rev 2157 | 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
 
1761 vikas 3
import in.shop2020.model.v1.user.Widget;
762 rajveer 4
import in.shop2020.model.v1.user.WidgetException;
651 rajveer 5
import in.shop2020.model.v1.user.UserContextService.Client;
1761 vikas 6
import in.shop2020.model.v1.user.WidgetItem;
1957 vikas 7
import in.shop2020.serving.utils.DataLogger;
651 rajveer 8
import in.shop2020.serving.utils.FileUtils;
650 rajveer 9
import in.shop2020.serving.utils.Utils;
1957 vikas 10
import in.shop2020.serving.utils.DataLogger.Event;
651 rajveer 11
import in.shop2020.thrift.clients.UserContextServiceClient;
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);
1957 vikas 38
    private static Logger dataLog = DataLogger.getLogger();
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 {
79
                    snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
80
                    totalItems++;
81
                }
82
                catch (FileNotFoundException e) {
83
                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
84
                }
85
                catch (IOException e) {
86
                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
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 {
102
	                    htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
103
	                }
104
	                catch (FileNotFoundException e) {
105
	                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
106
	                }
107
	                catch (IOException e) {
108
	                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
109
	                }
1957 vikas 110
                    dataLog.info(StringUtils.join(
111
                            new String[] { Event.RESEARCH_ADD.name(),
112
                                    StringUtils.join(items, "_") }, ", "));
1761 vikas 113
                }
114
	        }
650 rajveer 115
		}
651 rajveer 116
		return "success";
650 rajveer 117
	}
118
 
1614 rajveer 119
	@Action(value="deletefromresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
651 rajveer 120
	public String destroy(){
1761 vikas 121
		for (String item : items){
122
			long itemId = Long.parseLong(item);
1957 vikas 123
			deleteFromMyResearch(userinfo.getUserId(), itemId);
124
			dataLog.info(StringUtils.join(
125
                    new String[] { Event.RESEARCH_DELETE.name(),
126
                            StringUtils.join(items, "_") }, ", "));
650 rajveer 127
		}
651 rajveer 128
		return "success";
650 rajveer 129
	}
130
 
762 rajveer 131
	private void deleteFromMyResearch(long userId, long itemId) {
132
 
133
		try {
134
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
135
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
136
			userClient.deleteItemFromMyResearch(userId, itemId);
137
		} catch (WidgetException e) {
138
			e.printStackTrace();
139
		} catch (TException e) {
140
			e.printStackTrace();
141
		} catch (Exception e) {
142
			e.printStackTrace();
143
		}
144
	}
1033 varun.gupt 145
 
650 rajveer 146
	public String getHtmlSnippet(){
1032 varun.gupt 147
		if(htmlSnippet == "")	{
1643 rajveer 148
			htmlSnippet = "0";
650 rajveer 149
		}
150
		return htmlSnippet;
1761 vikas 151
	}
152
 
153
	public String getSnippets() {
154
        return snippets;
155
    }
156
 
157
	public int getTotalItems() {
158
        return totalItems;
159
    }
160
 
161
	public void setProductid(String itemsString) {
162
        setId(itemsString);
163
    }
164
 
165
	public void setId(String itemsString) {
166
        JSONArray itemJson = null;
167
        items = new ArrayList<String>();
168
        try {
169
            itemJson = new JSONArray(itemsString);
170
        } catch (JSONException e) {
171
            log.error("Bad json : " + itemsString);
172
            return;
173
        }
174
        for (int i=0; i<itemJson.length(); i++) {
175
            try {
176
                items.add(itemJson.getString(i));
177
            } catch (JSONException e) {
178
                log.error("Bad item at index : " + i);
179
            }
180
        }
181
    }
182
 
183
    public String getItemsInJson() {
184
        return this.itemsInJson;
185
    }
1032 varun.gupt 186
}