Subversion Repositories SmartDukaan

Rev

Rev 462 | Blame | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.controllers;


import in.shop2020.model.v1.widgets.WidgetService.Client;
import in.shop2020.serving.pages.PageContentKeys;
import in.shop2020.serving.pages.PageEnum;
import in.shop2020.serving.pages.PageManager;
import in.shop2020.thrift.clients.WidgetServiceClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
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.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author naveen
 *
 */
@Results({
        @Result(name="success", location="/entity-show")
})
public class EntityController extends BaseController 
        implements  ModelDriven<Object>, ServletContextAware  {
                  
        private PageManager pageManager = null;
        
        /**
         * 
         */
        private static Log log = LogFactory.getLog(EntityController.class);
        
        /**
         * 
         */
        @SuppressWarnings("unused")
        private ServletContext servletContext = null;
        
        /**
         * 
         */
        private String id;
        
        private Map<String,String> htmlSnippets;
        
        
        public EntityController(){
                super();
                pageManager = PageManager.getPageManager();
        }

        // GET /entity/1000001
        @Actions({
                @Action("/mobiles"),
                @Action("/books"),
                @Action("/mobile"),
                @Action("/entity")
        })
    public HttpHeaders show() throws SecurityException, IOException {
        log.info("id=" + id);
        Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
        
        params.put(PageContentKeys.ENTITY_ID, id);
        params.put(PageContentKeys.USER_ID, new Long(userinfo.getUserId()).toString());
        params.put(PageContentKeys.CART_ID, new Long(userinfo.getCartId()).toString());
        params.put(PageContentKeys.SESSION_ID, new Long(userinfo.getSessionId()).toString());
        params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
        params.put(PageContentKeys.ITEM_COUNT, new Long(userinfo.getTotalItems()).toString());
        
        htmlSnippets = pageManager.getPageContents(PageEnum.PRODUCT_PAGE, params);
        
        
        System.out.println(htmlSnippets);
        // To update the browse history of the user
        try {
                        WidgetServiceClient widgetServiceClient = new WidgetServiceClient();
                        Client client = widgetServiceClient.getClient();
                        long item_id = Long.parseLong(id);
                        boolean isSessionId = userinfo.isSessionId();
                        long user_id = 0;
                        if(userinfo.isLoggedIn())
                                user_id = userinfo.getUserId();
                        else
                                user_id = userinfo.getSessionId();
                        client.updateBrowseHistory(user_id, item_id, isSessionId);
                        
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        
        /*
        String contextPath = this.servletContext.getContextPath();
        log.info("contextPath=" + contextPath);
        
        String realPath = this.servletContext.getRealPath("/");
        log.info("realPath=" + realPath);
        
        String path = this.servletContext.getRealPath(
                        "/WEB-INF/testdir/test.txt");
        
        log.info("path=" + path);
        
        FileReader fr = new FileReader(new File(path));
        LineNumberReader lnr = new LineNumberReader(fr);
        
        String line = lnr.readLine();
        log.info("line=" + line);
        */
        return new DefaultHttpHeaders("show");
    }
    
    /**
     * 
     * @param id
     */
    public void setId(String id) {
        StringTokenizer tokenizer = new StringTokenizer(id,"-");
        while(tokenizer.hasMoreTokens()){
                this.id = tokenizer.nextToken();
        }
    }

        /* (non-Javadoc)
         * @see com.opensymphony.xwork2.ModelDriven#getModel()
         */
        @Override
        public Object getModel() {
                System.out.println(" getModer  is called");
                return htmlSnippets;
                //return this.id;
        }
        
        /**
         * 
         */
        @Override
        public void setServletContext(ServletContext servletContext) {
                this.servletContext = servletContext;
        }
        
        public Map<String,String> getHtmlSnippets(){
                System.out.println(" getHtmlSnippets  is called");
                return htmlSnippets;
        }
        
        public String getHeaderSnippet(){
                return htmlSnippets.get("HEADER");
        }

        public String getSlideGuideSnippet(){
                return htmlSnippets.get("SLIDE_GUIDE");
        }

        public String getMainMenuSnippet(){
                return htmlSnippets.get("MAIN_MENU");
        }
        
        public String getSearchBarSnippet(){
                return htmlSnippets.get("SEARCH_BAR");
        }
                
        public String getProductSummarySnippet(){
                return htmlSnippets.get("PRODUCT_SUMMARY");
        }
        
        public String getSocialUtilsSnippet(){
                return htmlSnippets.get("SOCIAL_UTILS"); 
        }
        
        public String getLocatorSnippet(){
                return htmlSnippets.get("LOCATOR");
        }
        
        public String getReviewsSnippet(){
                return htmlSnippets.get("REVIEWS");
        }
        
        public String getCustomerServiceSnippet(){
                return htmlSnippets.get("CUSTOMER_SERVICE");
        }
        
        public String getMyResearchSnippet(){
                return htmlSnippets.get("MY_RESEARCH");
        }
        

        public String getRecommendationsSnippet(){
                return htmlSnippets.get("RECOMMENDATIONS");
        }

        public String getSimilarProductsSnippet(){
                return htmlSnippets.get("SIMILAR_PRODUCTS");
        }
                        
        public String getAccessoriesSnippet(){
                return htmlSnippets.get("ACCESSORIES");
        }
         
        public String getFooterSnippet(){
                return htmlSnippets.get("FOOTER");
        }

        public String getJsFileSnippet(){
                return htmlSnippets.get("JS_FILES");
        }
        
        public String getCssFileSnippet(){
                return htmlSnippets.get("CSS_FILES");
        }
        
}