Rev 1852 | Rev 3052 | 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.UserContextService;import in.shop2020.model.v1.user.Widget;import in.shop2020.model.v1.user.WidgetException;import in.shop2020.model.v1.user.WidgetItem;import in.shop2020.serving.utils.FileUtils;import in.shop2020.serving.utils.Utils;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.log4j.Logger;import org.apache.thrift.TException;import org.json.JSONArray;import org.json.JSONException;public class BrowseHistoryController extends BaseController{private static Logger log = Logger.getLogger(BrowseHistoryController.class);private static final long serialVersionUID = -5508838941749125824L;private String id;private String snippets="";private List<String> items;private String itemsInJson;public String index() {return "index";}// GET browse-history/[1,2,3]public String show() {return "show";}//Handle POST /browse-history/{id}public String create(){log.info("list of item ids is " + items);if (items != null) {if(userinfo.getUserId() != -1){updateHistory(items);}}try {JSONArray itemsJson = new JSONArray();UserContextServiceClient userServiceClient = new UserContextServiceClient();UserContextService.Client userClient = userServiceClient.getClient();Widget resWidget = userClient.getBrowseHistory(userinfo.getUserId());List<WidgetItem> widgetItems = resWidget.getItems();for (WidgetItem wItem : widgetItems) {itemsJson.put(wItem.getItem_id());}itemsInJson = itemsJson.toString();} catch (WidgetException e) {log.error("Unable to create the browse history widget because of: ", e);} catch (TException e) {log.error("Unable to create the browse history widget because of: ", e);} catch (Exception e) {log.error("Unable to create the browse history widget because of: ", e);}return "success";}public String getSnippets() {if(items != null){for(String itemId: items){try {snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");}catch (FileNotFoundException e) {log.error(e.getMessage());}catch (IOException e) {log.error(e.getMessage());}}}return snippets;}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 getId() {return this.id;}public String getItemsInJson() {return this.itemsInJson;}private void updateHistory(List<String> historyItems) {UserContextServiceClient userServiceClient;try {userServiceClient = new UserContextServiceClient();UserContextService.Client userClient = userServiceClient.getClient();for (String item : historyItems) {long itemId = Long.parseLong(item);userClient.updateBrowseHistory(userinfo.getUserId(), itemId);}} catch (Exception e) {log.error("Unable to update the browse history because of: ", e);}}}