Subversion Repositories SmartDukaan

Rev

Rev 2306 | Rev 2426 | 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.catalog.Item;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.serving.utils.DataLogger;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.velocity.VelocityContext;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author rajveer
 *
 */

@Results({
    @Result(name = "show", location = "entity-show.vm"),
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
})
public class EntityController extends BaseController {
                
        private static final long serialVersionUID = 1L;

        private static Logger log = Logger.getLogger(Class.class);
        /**
         * 
         */
        private String id;
        private String redirectUrl;
        private long productId;

        public EntityController(){
                super();
        }

        // GET /*/1000001
        @Actions({
                @Action("/mobile-phones"),
                @Action("/mobile-accessories"),
                @Action("/entity")
        })
    public String show() throws SecurityException, IOException, JSONException {
        log.info("id=" + id);

        String entityUrl = "";
        String metaKeywords = "";
        String metaDescription = "";
        String pageTitle = "";
        try {
            JSONObject productPropertiesInJson = new JSONObject(pageLoader.getProductPropertiesHtml(productId).trim());
            entityUrl = productPropertiesInJson.getString("entityUrl");
            metaDescription = productPropertiesInJson.getString("metaDescription");
            metaKeywords = productPropertiesInJson.getString("metaKeywords");
            pageTitle = productPropertiesInJson.getString("title");
        }
        catch (JSONException e) {
            e.printStackTrace();
            try {
                CatalogServiceClient catalogClientService = new CatalogServiceClient();
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
                
                Item item = client.getItemsByCatalogId(productId).get(0);
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
            } catch (Exception e1) {
                redirectUrl = "/";
                e.printStackTrace();
            }
            log.info(redirectUrl);
            return "redirect";
       }
        String currentUrl = request.getRequestURL().toString();
        
        if (!currentUrl.contains(entityUrl)) {
            redirectUrl = entityUrl;
            return "redirect";
        }
        
        //Extracting Product name from Page Title
        String[] items = pageTitle.split("\\|");
        String productName = items[0] != null ? items[0].trim() : null;
        String rootUrl = currentUrl.split("/")[2];
        
        String productUrlEncoded = URLEncoder.encode(currentUrl, "UTF-8");
        
        htmlSnippets.put("PRODUCT_SUMMARY", pageLoader.getProductSummaryHtml(productId));
        htmlSnippets.put("PRODUCT_ID", productId + "");
        htmlSnippets.put("PRODUCT_NAME", productName);
        htmlSnippets.put("PRODUCT_URL", currentUrl);
        htmlSnippets.put("PRODUCT_URL_ENCODED", productUrlEncoded);
        htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
                htmlSnippets.put("SOCIAL_UTILS", getSocialUtilsHtml(productId, productName, productUrlEncoded));
                htmlSnippets.put("SLIDE_GUIDE", pageLoader.getSlideGuideHtml(productId));
                htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
                htmlSnippets.put("PAGE_METADESC", metaDescription);
                htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
        
                try {
                        UserContextServiceClient userServiceClient = new UserContextServiceClient();
                        Client client = userServiceClient.getClient();
                        long itemId = Long.parseLong(id);
                        long userId = userinfo.getUserId();
                        if(userId != -1){
                                client.updateBrowseHistory(userId, itemId);
                        }
                
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        
                DataLogger.logData(EventType.PRODUCT_VIEW.name(), session.getId(), Long.toString(userinfo.getUserId()), userinfo.getEmail(),
                productName);
                return "show";
    }
    
    /**
     * 
     * @param id
     */
    public void setId(String id) {
        StringTokenizer tokenizer = new StringTokenizer(id,"-");
        while(tokenizer.hasMoreTokens()){
                this.id = tokenizer.nextToken();
        }
        this.productId = Long.parseLong(this.id);
    }

        public Map<String,String> getHtmlSnippets(){
                System.out.println(" getHtmlSnippets  is called");
                return htmlSnippets;
        }
        
        public String getSlideGuideSnippet(){
                return htmlSnippets.get("SLIDE_GUIDE");
        }

        public String getRedirectUrl(){
        return redirectUrl;
    }
        
        public String getProductSummarySnippet(){
                return htmlSnippets.get("PRODUCT_SUMMARY");
        }
        
        public String getPageTitleSnippet(){
                return htmlSnippets.get("PAGE_TITLE");
        }
        
        public String getPageMetaDescSnippet(){
                return htmlSnippets.get("PAGE_METADESC");
        }
        
        public String getPageMetaKeywordsSnippet(){
                return htmlSnippets.get("PAGE_METAKEYWORDS");
        }
        
        public String getSocialUtilsSnippet(){
                return htmlSnippets.get("SOCIAL_UTILS"); 
        }
        
        public String getLocatorSnippet(){
                return htmlSnippets.get("LOCATOR");
        }
        
        public String getRecommendationsSnippet(){
                return htmlSnippets.get("RECOMMENDATIONS");
        }
        
        public String getSimilarProductsSnippet(){
                return htmlSnippets.get("SIMILAR_PRODUCTS");
        }
        
        public String getAccessoriesSnippet(){
                return htmlSnippets.get("ACCESSORIES");
        }
        
        public String getReviewsSnippet(){
                return htmlSnippets.get("REVIEWS");
        }
        
        public String getProductName()  {
                return htmlSnippets.get("PRODUCT_NAME");
        }

        public String getProductId()    {
                return htmlSnippets.get("PRODUCT_ID");
        }
        
        public String getProductUrl()   {
                return htmlSnippets.get("PRODUCT_URL");
        }
        
        public String getRootUrl()      {
                return htmlSnippets.get("ROOT_URL");
        }

        private String getSocialUtilsHtml(long productId, String productName, String productEncodedUrl) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                
                String templateFile = "templates/socialutils.vm";
                
                context.put("PRODUCT_ID", productId + "");
                context.put("PRODUCT_NAME", productName);
                context.put("PRODUCT_URL_ENCODED", productEncodedUrl);
                
                htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
}