Rev 3185 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.datalogger.EventType;import in.shop2020.model.v1.user.UserContextService.Client;import in.shop2020.model.v1.user.WidgetException;import in.shop2020.serving.utils.FileUtils;import in.shop2020.serving.utils.Utils;import in.shop2020.thrift.clients.UserClient;import in.shop2020.utils.DataLogger;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.thrift.TException;import org.json.JSONArray;import org.json.JSONException;@SuppressWarnings("serial")@Results({@Result(name="success", location="myresearch-success.vm")})public class MyresearchController extends BaseController {private UserClient userServiceClient = null;private Client client;private static Logger log = Logger.getLogger(MyresearchController.class);private static final String htmlSnippetFileName = "MyResearchSnippet.html";private String htmlSnippet = "";private String snippets="";private List<String> items;private String itemsInJson;private int totalItems = 0;public MyresearchController(){super();try {userServiceClient = new UserClient();client = userServiceClient.getClient();} catch (Exception e) {log.error("Unable to initialize the user service client", e);}}@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})public String index() {try {JSONArray itemsJson = new JSONArray();List<Long> widgetItemIds = client.getMyResearchItems(userinfo.getUserId());for (Long itemId : widgetItemIds) {itemsJson.put(itemId);}itemsInJson = itemsJson.toString();} catch (WidgetException e) {log.error("Unable to get the myresearch widget", e);} catch (TException e) {log.error("Unable to get the myresearch widget", e);}return "index";}//myresearch/[1,2,3]@Action(value="myresearch",interceptorRefs={@InterceptorRef("caching"),@InterceptorRef("myDefault")})public String show() {if(items != null){for(String itemId: items){try {snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);totalItems++;}catch (FileNotFoundException e) {log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);}catch (IOException e) {log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);}}}return "show";}//Handle /addtoresearch/{id}@Action(value="addtoresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})public String create() throws WidgetException, TException, IOException {if(userinfo.getUserId() != -1){for (String item : items){long itemId = Long.parseLong(item);boolean isNew = client.updateMyResearch(userinfo.getUserId(), itemId);String fromsrc = "";if(isNew){try {htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);}catch (FileNotFoundException e) {log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);}catch (IOException e) {log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator + htmlSnippetFileName);}if(request.getParameter("fromsrc") != null){fromsrc = request.getParameter("fromsrc");}DataLogger.logData(EventType.RESEARCH_ADD, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), StringUtils.join(items, "_"), fromsrc);}}}return "success";}@Action(value="deletefromresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})public String destroy(){for (String item : items){long itemId = Long.parseLong(item);deleteFromMyResearch(userinfo.getUserId(), itemId);DataLogger.logData(EventType.RESEARCH_DELETE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), StringUtils.join(items, "_"));}return "success";}private void deleteFromMyResearch(long userId, long itemId) {try {UserClient userServiceClient = new UserClient();in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();userClient.deleteItemFromMyResearch(userId, itemId);} catch (WidgetException e) {log.error("Unable to delete item from the myresearch widget", e);} catch (TException e) {log.error("Unable to delete item from the myresearch widget", e);} catch (Exception e) {log.error("Unable to delete item from the myresearch widget", e);}}public String getHtmlSnippet(){if(htmlSnippet == "") {htmlSnippet = "0";}return htmlSnippet;}public String getSnippets() {return snippets;}public int getTotalItems() {return totalItems;}public void setProductid(String itemsString) {setId(itemsString);}public void setId(String itemsString) {JSONArray itemJson = null;try {itemJson = new JSONArray(itemsString);} catch (JSONException e) {log.error("Bad json : " + itemsString);return;}items = new ArrayList<String>();for (int i=0; i<itemJson.length(); i++) {try {items.add(itemJson.getString(i));} catch (JSONException e) {log.error("Bad item at index : " + i);}}}public String getItemsInJson() {return this.itemsInJson;}}