Subversion Repositories SmartDukaan

Rev

Rev 3126 | 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.WidgetException;
import in.shop2020.serving.services.ContentServingService;
import in.shop2020.serving.utils.SnippetType;
import in.shop2020.thrift.clients.UserClient;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
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]
        @Action(interceptorRefs={@InterceptorRef("caching"),@InterceptorRef("myDefault")})
        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();
            UserClient userServiceClient = new UserClient();
            UserContextService.Client userClient = userServiceClient.getClient();
            List<Long> widgetItemIds = userClient.getBrowseHistoryItems(userinfo.getUserId());
            for (Long itemId : widgetItemIds) {
                itemsJson.put(itemId);
            }
            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){
                        snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, itemId, sourceId);
                        }
        }
                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) {
                UserClient userServiceClient;
                try {
                        userServiceClient = new UserClient();
                        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);
                }
        }
}