Rev 1853 | Rev 2157 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.model.v1.user.Widget;import in.shop2020.model.v1.user.WidgetException;import in.shop2020.model.v1.user.UserContextService.Client;import in.shop2020.model.v1.user.WidgetItem;import in.shop2020.serving.utils.DataLogger;import in.shop2020.serving.utils.FileUtils;import in.shop2020.serving.utils.Utils;import in.shop2020.serving.utils.DataLogger.Event;import in.shop2020.thrift.clients.UserContextServiceClient;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;@Results({@Result(name="success", location="myresearch-success.vm")})public class MyresearchController extends BaseController {private UserContextServiceClient userServiceClient = null;private Client client;private static Logger log = Logger.getLogger(MyresearchController.class);private static Logger dataLog = DataLogger.getLogger();private String htmlSnippet = "";private String snippets="";private List<String> items;private String itemsInJson;private int totalItems = 0;public MyresearchController(){super();try {userServiceClient = new UserContextServiceClient();client = userServiceClient.getClient();} catch (Exception e) {e.printStackTrace();}}@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})public String index() {try {JSONArray itemsJson = new JSONArray();Widget resWidget = client.getMyResearch(userinfo.getUserId());List<WidgetItem> widgetItems = resWidget.getItems();for (WidgetItem wItem : widgetItems) {itemsJson.put(wItem.getItem_id());}itemsInJson = itemsJson.toString();} catch (WidgetException e) {e.printStackTrace();} catch (TException e) {e.printStackTrace();}return "index";}//myresearch/[1,2,3]@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})public String show() {if(items != null){for(String itemId: items){try {snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");totalItems++;}catch (FileNotFoundException e) {log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");}catch (IOException e) {log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");}}}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);if(isNew){try {htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");}catch (FileNotFoundException e) {log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");}catch (IOException e) {log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");}dataLog.info(StringUtils.join(new String[] { Event.RESEARCH_ADD.name(),StringUtils.join(items, "_") }, ", "));}}}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);dataLog.info(StringUtils.join(new String[] { Event.RESEARCH_DELETE.name(),StringUtils.join(items, "_") }, ", "));}return "success";}private void deleteFromMyResearch(long userId, long itemId) {try {UserContextServiceClient userServiceClient = new UserContextServiceClient();in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();userClient.deleteItemFromMyResearch(userId, itemId);} catch (WidgetException e) {e.printStackTrace();} catch (TException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}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;items = new ArrayList<String>();try {itemJson = new JSONArray(itemsString);} catch (JSONException e) {log.error("Bad json : " + itemsString);return;}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;}}