Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


import in.shop2020.util.Utils;

import org.apache.thrift.TException;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;


import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.EntityContainer;
import in.shop2020.metamodel.util.ExpandedCategory;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.model.v1.catalog.InventoryServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.OrderStatus;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.model.v1.shoppingcart.Cart;
import in.shop2020.model.v1.shoppingcart.Line;
import in.shop2020.model.v1.shoppingcart.LineStatus;
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserPrimaryInfo;
import in.shop2020.model.v1.widgets.RatingType;
import in.shop2020.model.v1.widgets.RatingsWidget;
import in.shop2020.model.v1.widgets.Widget;
import in.shop2020.model.v1.widgets.WidgetItem;
import in.shop2020.model.v1.widgets.WidgetType;
import in.shop2020.serving.page.CategoryPage;
import in.shop2020.serving.page.MyAccountPage;
import in.shop2020.serving.page.ProductPage;
import in.shop2020.serving.page.ShoppingCartPage;
import in.shop2020.serving.utils.*;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.ShoppingCartClient;
import in.shop2020.thrift.clients.TransactionServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.thrift.clients.WidgetServiceClient;


public class PageLoaderHandler {

        public Map<String,String> getProductPage(long productId, Map<String, String> params) throws TException {

                Map<String,String> htmlSnippet = new HashMap<String, String>();

                boolean isSessionId = false;
                long userId = Long.parseLong(params.get("USER_ID"));
                if(userId == -1){
                        isSessionId = true;
                }
                long cartId = Long.parseLong(params.get("CART_ID"));
                String userName = params.get("USER_NAME");
                
                long categoryId = 100000;
                long itemCount = Long.parseLong(params.get("ITEM_COUNT"));
                //product.setCategoryId(categoryId);
                
                htmlSnippet.put("HEADER", getHeaderHtml(userId, isSessionId, userName));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, categoryId));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("PRODUCT_SUMMARY", getProductSummaryHtml(productId));
                System.out.println("PRODUCT_SUMMARY Snippet generated");
                
                htmlSnippet.put("SOCIAL_UTILS", getSocialUtilsHtml(productId));
                System.out.println("SOCIAL_UTILS Snippet generated");
                
                htmlSnippet.put("SLIDE_GUIDE", getSlideGuideHtml(productId));
                System.out.println("SLIDE GUIDE Snippet generated");
                
                htmlSnippet.put("LOCATOR", getLocatorHtml());
                System.out.println("LOCATOR Snippet generated");

                htmlSnippet.put("REVIEWS", getReviewsHtml(productId));
                System.out.println("REVIEWS Snippet generated");

                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");
                
                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, isSessionId));
                System.out.println("MY_RESEARCH Snippet generated");

                htmlSnippet.put("RECOMMENDATIONS", getRecommendationsHtml());
                System.out.println("RECOMMENDATIONS Snippet generated");

                htmlSnippet.put("SIMILAR_PRODUCTS", getSimilarProductsHtml(productId));
                System.out.println("RECOMMENDATIONS Snippet generated");
                
                htmlSnippet.put("ACCESSORIES", getAccessoriesHtml(productId));
                System.out.println("ACCESSORIES Snippet generated");

                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");

//              htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
//              System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                return htmlSnippet;
        }


        public Map<String, String> getRegisterPage(Map<String, String> parameters) {
                Map<String,String> htmlSnippet = new HashMap<String, String>();
                boolean isSessionId = false;
                long userId = Long.parseLong(parameters.get("USER_ID"));
                if(userId == -1){
                        isSessionId = true;
                }
                long cartId = Long.parseLong(parameters.get("CART_ID"));
                String userName = parameters.get("USER_NAME");
                long itemCount = Long.parseLong(parameters.get("ITEM_COUNT"));
                
                htmlSnippet.put("HEADER", getHeaderHtml(userId, isSessionId, userName));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("REGISTRATION_HEADER", getRegistrationHeaderHtml());
                System.out.println("REGISTRATION_HEADER Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");
                
                htmlSnippet.put("REGISTRATION_FORM", getRegistrationFormHtml());
                System.out.println("REGISTRATION_FORM Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");

                
                return htmlSnippet;
        }
        
        private String getRegistrationFormHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/registrationform.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        private String getRegistrationHeaderHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/registrationheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public Map<String,String> getHomePage(Map<String, String> params) throws TException {

                Map<String,String> htmlSnippet = new HashMap<String, String>();

                boolean isSessionId = false;
                long userId = Long.parseLong(params.get("USER_ID"));
                if(userId == -1){
                        isSessionId = true;
                }
                long cartId = Long.parseLong(params.get("CART_ID"));
                String userName = params.get("USER_NAME");
                
                long categoryId = 100000;
                long itemCount = Long.parseLong(params.get("ITEM_COUNT"));
                //product.setCategoryId(categoryId);
                
                htmlSnippet.put("HEADER", getHeaderHtml(userId, isSessionId, userName));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, categoryId));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("MAIN_BANNER", getMainBannerHtml());
                System.out.println("LOCATOR Snippet generated");

                htmlSnippet.put("BEST_DEALS", getBestDealsHtml());
                System.out.println("BEST_DEALS Snippet generated");
                
                htmlSnippet.put("LATEST_ARRIVALS", getLatestArrivalsHtml());
                System.out.println("LATEST_ARRIVALS Snippet generated");
                
                htmlSnippet.put("BEST_SELLERS", getBestSellersHtml());
                System.out.println("BEST_SELLERS Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");
                
                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, isSessionId));
                System.out.println("MY_RESEARCH Snippet generated");

                htmlSnippet.put("RECOMMENDATIONS", getRecommendationsHtml());
                System.out.println("RECOMMENDATIONS Snippet generated");

                htmlSnippet.put("BROWSE_HISTORY", getBrowseHistoryHtml(userId, isSessionId));
                System.out.println("RECOMMENDATIONS Snippet generated");

                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");

//              htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
//              System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                return htmlSnippet;
        }

        
        private String getMainBannerHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/mainbanner.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        private String getBestSellersHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/bestsellers.vm";
                
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        List<Long> items = client.getBestSellers(1, 20);
                        List<String> itemList = new ArrayList<String>();
                        for(Long item: items){
                                itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
                        }
                        context.put("itemList", itemList);
                        
                } catch(Exception e){
                        
                }

                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;      
        }


        private String getLatestArrivalsHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/latestarrivals.vm";
                
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        List<Long> items = client.getLatestArrivals(1,20);
                        List<String> itemList = new ArrayList<String>();
                        for(Long item: items){
                                itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
                        }
                        context.put("itemList", itemList);
                        
                } catch(Exception e){
                        
                }

                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        private String getBestDealsHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/bestdeals.vm";
                
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        List<Long> items = client.getBestDeals(1,20);
                        List<String> itemList = new ArrayList<String>();
                        for(Long item: items){
                                itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
                        }
                        context.put("itemList", itemList);
                        
                } catch(Exception e){
                        
                }
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public String getFooterHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/footer.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        private String getAccessoriesHtml(long productId) {
                return getWidgetDiv("", WidgetType.ACCESSORIES, "accessories.vm");
        }



        private String getSimilarProductsHtml(long productId) {
                return getWidgetDiv("", WidgetType.SIMILAR_ITEMS, "similaritems.vm");
        }



        private String getRecommendationsHtml() {
                return getWidgetDiv( "", WidgetType.RECOMMENDED_ITEMS, "recommendations.vm");
        }



        public String getMyResearchHtml(long userId, boolean isSessionId) {
                return getWidgetDiv(userId+"", WidgetType.MY_RESEARCH, "myresearch.vm");
        }

        private String getBrowseHistoryHtml(long userId, boolean isSessionId) {
                return getWidgetDiv(userId+"", WidgetType.BROWSE_HISTORY, "browsehistory.vm");
        }

        public String getCustomerServiceHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/customerservice.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        private String getReviewsHtml(long productId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                Map<String, String> params = new HashMap<String, String>();
                params.put("PRODUCT_ID", productId+"");
                context.put("params", params);
                String templateFile = "templates/reviews.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        private String getLocatorHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/locator.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



//      private String getSlideGuideHtml(long productId) {
//              String htmlString = "";
//              try {
//                      VelocityContext context = new VelocityContext();
//                      String templateFile = "velocity/slideguide.vm";
//                      // For an entity
//                      EntityContainer entContainer =  Catalog.getInstance().getEntityContainer();
//                      ExpandedEntity expEntity = entContainer.getExpandedEntity(productId);
//                      
//                      context.put("expentity", expEntity);
//                      htmlString = getHtmlFromVelocity(templateFile, context);
//                      } catch (Exception e) {
//                              e.printStackTrace();
//                      }
//              return htmlString;
//      }

        private String getSlideGuideHtml(long productId) {
                StringBuilder htmlString = new StringBuilder();
                String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideGuide.html";
                File f = new File(filename);
                
                
                FileInputStream fis = null;
                try {
                        fis = new FileInputStream(f);
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                        String line;
                        while((line = br.readLine()) != null){
                                htmlString.append(line+"\n");
                        }
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally {
                        if(fis != null) {
                                try {
                                        fis.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
                
                return htmlString.toString();
        }

        public String getProductSummaryHtml(long productId) {
                StringBuilder htmlString = new StringBuilder();
                String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductDetail.html";
                File f = new File(filename);
                
                
                FileInputStream fis = null;
                try {
                        fis = new FileInputStream(f);
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                        String line;
                        while((line = br.readLine()) != null){
                                htmlString.append(line+"\n");
                        }
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally {
                        if(fis != null) {
                                try {
                                        fis.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
                
                return htmlString.toString();
        }


        private String getSocialUtilsHtml(long productId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                Map<String, String> params = new HashMap<String, String>();
                params.put("PRODUCT_ID", productId+"");
                String templateFile = "templates/socialutils.vm";
                context.put("params", params);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        private String getProductSummaryHtml(long productId, boolean toBeDecided) {
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                String htmlString = "";

                WidgetServiceClient widgetServiceClient = null;
                in.shop2020.model.v1.widgets.WidgetService.Client widgetClient = null;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();

                        widgetServiceClient = new WidgetServiceClient();
                        widgetClient = widgetServiceClient.getClient();
                        
                        RatingsWidget ratingsWidget = widgetClient.getRatings(productId, 0);
                        Map<RatingType,Double> ratings = ratingsWidget.getRatings();
                        //double rating_amazon = ratings.get(RatingType.AMAZON);
                        double rating_all  = 0.0;
                        if(ratings != null){
                                if( ratings.get(RatingType.USER_ALL) != null){
                                        rating_all = ratings.get(RatingType.USER_ALL);
                                }
                        }
                        
                        rating_all = Math.round(rating_all);
                        
                        Item item = client.getItemByCatalogId(productId);
                        
                        Double itemPrice = item.getSellingPrice();
                        // For an entity
                        EntityContainer entContainer =  Catalog.getInstance().getEntityContainer();
                        ExpandedEntity expEntity = entContainer.getExpandedEntity(productId);
                        
                        String title = expEntity.getBrand() + "" + expEntity.getModelName();
                        String categoryName = expEntity.getCategory().getLabel();
                        long categoryId = expEntity.getCategory().getID();
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("TITLE", title);
                        params.put("CATEGORY_ID", categoryId+"");
                        params.put("CATEGORY_NAME", categoryName);
                        params.put("PRICE", itemPrice.toString());
                        params.put("RATING_ALL", rating_all+"");
                        params.put("PRODUCT_ID", productId+"");

                        VelocityContext context = new VelocityContext();
                        String templateFile = "templates/productsummary.vm";
                        context.put("params", params);
                        htmlString = getHtmlFromVelocity(templateFile, context);
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
        
                return htmlString;
        }



        public String getMainMenuHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/mainmenu.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        
        public String getSearchBarHtml(long itemCounts, long categoryId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/searchbar.vm";
                Map<String, String> params = new HashMap<String, String>();
                params.put("ITEM_COUNT", itemCounts+"");
                params.put("CATEGORY_ID", categoryId+"");
                context.put("params", params );
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        public String getHeaderHtml(long userId, boolean isSessionId, String  userName) {
                String htmlString = "";
                Map<String,String> params = new HashMap<String, String>();
                if(isSessionId){
                        params.put("LOGGED_IN", "FALSE");
                        params.put("WELCOME_MESSAGE", "Hi, Welcome to Shop2020");
                }else
                {
                        params.put("LOGGED_IN", "TRUE");
                        params.put("WELCOME_MESSAGE", "Hi " + userName);
                }
                
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/header.vm";
                context.put("params", params);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        public String getSlideGuideDIV(long productId){
                String htmlString = "";
                try {
                        VelocityContext context = new VelocityContext();
                        String templateFile = "templates/slideguide.vm";
                        // For an entity
                        EntityContainer entContainer =  Catalog.getInstance().getEntityContainer();
                        ExpandedEntity expEntity = entContainer.getExpandedEntity(productId);
                        
                        context.put("expentity", expEntity);
                        htmlString = getHtmlFromVelocity(templateFile, context);
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                return htmlString;
        }

        
        public CategoryPage getCategoryPage(long categoryId,
                        Map<String, String> params){
                // TODO Auto-generated method stub

                CategoryPage category = null;
                
                try {
                        //set velocity properties
                        Velocity.init("velocity/velocity.properties");
                        VelocityContext context = new VelocityContext();

                        String templateFile = "velocity/category.vm";
                        
                        Template template = null;

                        // For an category
                        DefinitionsContainer defsContainer =    Catalog.getInstance().getDefinitionsContainer();
                        
                        ExpandedCategory expCategory = defsContainer.getExpandedCategory(categoryId);

                        //Utils.logger.info("expCategory=" + expCategory);
                                
                        context.put("expcategory", expCategory);
                        template = Velocity.getTemplate(templateFile);
                        
                        if(template != null) {
                                StringWriter writer = new StringWriter();

                                template.merge(context, writer);
                                writer.flush();
                                writer.close();
                                
                                //Put in the product object
                                Map<String,String> htmlSnippet = new HashMap<String, String>();
                                htmlSnippet.put("MAIN", writer.toString());
                                
                                category = new CategoryPage();
                                category.setCategoryId(categoryId);
                                category.setParams(params);
                                category.setHtmlSnippet(htmlSnippet);
                                
                                Utils.info("Snippet generated");
                        }
                } catch (ResourceNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ParseErrorException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (MethodInvocationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                return category;
        }

        private String getPriceDiv(long productId){
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                String htmlString = new String();

                WidgetServiceClient widgetServiceClient = null;
                in.shop2020.model.v1.widgets.WidgetService.Client widgetClient = null;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();

                        widgetServiceClient = new WidgetServiceClient();
                        widgetClient = widgetServiceClient.getClient();
                        
                        RatingsWidget ratingsWidget = widgetClient.getRatings(productId, 0);
                        Map<RatingType,Double> ratings = ratingsWidget.getRatings();
                        //double rating_amazon = ratings.get(RatingType.AMAZON);
                        double rating_all  = 0.0;
                        if(ratings != null){
                                if( ratings.get(RatingType.USER_ALL) != null){
                                        rating_all = ratings.get(RatingType.USER_ALL);
                                }
                        }
                        
                        rating_all = Math.round(rating_all);
                        
                        
                        Item item = client.getItemByCatalogId(productId);
                        Double itemPrice = item.getSellingPrice();
                        
                        //set velocity properties
                        Velocity.init("velocity/velocity.properties");
                        VelocityContext context = new VelocityContext();

                        String templateFile = "velocity/pricediv.vm";
                        
                        Template template = null;

                        // For an entity
                        EntityContainer entContainer =  Catalog.getInstance().getEntityContainer();
                        
                        ExpandedEntity expEntity = entContainer.getExpandedEntity(productId);
                        
                        //Utils.logger.info("expEntity=" + expEntity);
                        
                        String title = expEntity.getBrand() + "" + expEntity.getModelName();
                        String categoryName = expEntity.getCategory().getLabel();
                        
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("TITLE", title);
                        params.put("CATEGORY", categoryName);
                        params.put("PRICE", itemPrice.toString());
                        params.put("RATING_ALL", rating_all+"");
                        params.put("PRODUCT_ID", productId+"");
                        
                        context.put("params", params);
                        template = Velocity.getTemplate(templateFile);
                        
                        if(template != null) {
                                StringWriter writer = new StringWriter();

                                template.merge(context, writer);
                                writer.flush();
                                writer.close();
                                
                                System.out.println("PRICE DIV Snippet generated");
                                
                                return writer.toString();
                                
                        }
                } catch (ResourceNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ParseErrorException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (MethodInvocationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (InventoryServiceException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        
                return "Price";
        }
        
        private String getWidgetDiv(long productId, String userId, WidgetType widgetType, String velocityFileName, Boolean tobeusedLater){
                WidgetServiceClient widgetServiceClient = null;
                in.shop2020.model.v1.widgets.WidgetService.Client client = null;
                
                String htmlString = new String();
                try {
                        widgetServiceClient = new WidgetServiceClient();
                        client = widgetServiceClient.getClient();
                        long userID;
                        if(userId.compareTo("") == 0){
                                userID = 0;
                        }else{
                                userID = Long.parseLong(userId);
                        }
                        Widget widget = client.getWidget(widgetType, userID, true);
                        
                        Utils.logger.info("widget" + widget);
                        
                        List<WidgetItem> items = widget.getItems();
                        
                        List<Map<String, String>> itemDetails = new ArrayList<Map<String, String>>();
                        
                        for(WidgetItem item: items){
                                Map<String, String> itemDetail = new HashMap<String, String>();
                                itemDetail.put("ITEM_ID", item.getItem_id()+"");
                                itemDetail.put("ITEM_SNIPPET", getItemSnippet(item.getItem_id()));
                                itemDetails.add(itemDetail);
                        }
                        
                        
                        Velocity.init("velocity/velocity.properties");
                        VelocityContext context = new VelocityContext();
                        String templateFile = "velocity/"+velocityFileName.trim();
                        Template template = null;
                                
                        
                                
                        context.put("itemDetails", itemDetails);
                        template = Velocity.getTemplate(templateFile);

                        if(template != null) {
                                StringWriter writer = new StringWriter();
                                template.merge(context, writer);
                                writer.flush();
                                writer.close();
                                
                                System.out.println("WIDGET DIV Snippet generated");
                                return writer.toString();
                                }
                } catch (ResourceNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ParseErrorException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (MethodInvocationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return "Widget";
        }
        
        private String getItemSnippet(long productId){
                        StringBuilder htmlString = new StringBuilder();
                        String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "WidgetSnippet.html";
                        try {
                                return FileUtils.read(filename);
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        return "";
                }



        private String getWidgetDiv(String userId, WidgetType widgetType, String templateFile){
                long userID;
                if(userId.compareTo("") == 0){
                        userID = 0;
                }else{
                        userID = Long.parseLong(userId);
                }

                WidgetServiceClient widgetServiceClient = null;
                in.shop2020.model.v1.widgets.WidgetService.Client client = null;
                Widget widget = null;
                try {
                        widgetServiceClient = new WidgetServiceClient();
                        client = widgetServiceClient.getClient();
                        widget = client.getWidget(widgetType, userID, true);
                } catch (Exception e) {
                        e.printStackTrace();
                }


                List<Map<String, String>> itemDetails = null;
                
                if(widget != null){
                        List<WidgetItem> items = widget.getItems();
                        itemDetails = new ArrayList<Map<String, String>>();
                        
                        for(WidgetItem item: items){
                                Map<String, String> itemDetail = new HashMap<String, String>();
                                
                                itemDetail.put("ITEM_ID", item.getItem_id()+"");
                                try {
                                        itemDetail.put("ITEM_SNIPPET", FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item.getItem_id() + File.separator + "WidgetSnippet.html"));
                                } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                itemDetails.add(itemDetail);
                        }
                }else{
                        System.out.println("widget not found");
                }
                
                VelocityContext context = new VelocityContext();
                context.put("itemDetails", itemDetails);
                
                return getHtmlFromVelocity("templates/"+templateFile, context);
        }

        /*
        private String getWidgetDiv(long productId, String userId, WidgetType widgetType, String templateFile){
                long userID;
                if(userId.compareTo("") == 0){
                        userID = 0;
                }else{
                        userID = Long.parseLong(userId);
                }

                WidgetServiceClient widgetServiceClient = null;
                in.shop2020.model.v1.widgets.WidgetService.Client client = null;
                Widget widget = null;
                try {
                        widgetServiceClient = new WidgetServiceClient();
                        client = widgetServiceClient.getClient();
                        widget = client.getWidget(widgetType, userID, true);
                } catch (Exception e) {
                        e.printStackTrace();
                }


                List<Map<String, String>> itemDetails = null;
                
                if(widget != null){
                        List<WidgetItem> items = widget.getItems();
                        itemDetails = new ArrayList<Map<String, String>>();
                        
                        for(WidgetItem item: items){
                                Map<String, String> itemDetail = new HashMap<String, String>();
                                
                                itemDetail.put("ITEM_ID", item.getItem_id()+"");
                                itemDetail.put("ITEM_NAME", "ID"+item.getItem_id());
                                itemDetail.put("ITEM_SNIPPET", item.getSnippet());
                                itemDetail.put("ITEM_PRICE", getItemPriceByCatalogId(item.getItem_id())+"");
                                itemDetails.add(itemDetail);
                        }
                }else{
                        System.out.println("widget not found");
                }
                
                VelocityContext context = new VelocityContext();
                context.put("itemDetails", itemDetails);
                
                return getHtmlFromVelocity("velocity/"+templateFile, context);
        }
*/


        public Map<String, String> getCompletedOrdersPage(long userId, long itemCount,
                        Map<String, String> params) {
                Map<String,String> htmlSnippet = new HashMap<String, String>();
                htmlSnippet.put("HEADER", getHeaderHtml(userId, false, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_HEADER", getMyaccountHeaderHtml());
                System.out.println("MYACCOUNT_HEADER Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_DETAILS", getCompletedOrdersHtml(userId));
                System.out.println("MYACCOUNT_DETAILS Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");

                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, false));
                System.out.println("MY_RESEARCH Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                
                return htmlSnippet;
        
        }

        
        public Map<String, String> getMyAccountPage(long userId, long itemCount,
                        Map<String, String> params) {
                Map<String,String> htmlSnippet = new HashMap<String, String>();
                htmlSnippet.put("HEADER", getHeaderHtml(userId, false, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_HEADER", getMyaccountHeaderHtml());
                System.out.println("MYACCOUNT_HEADER Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_DETAILS", getMyaccountDetailsHtml(userId));
                System.out.println("MYACCOUNT_DETAILS Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");

                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, false));
                System.out.println("MY_RESEARCH Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                
                return htmlSnippet;
        }


        public Map<String, String> getPersonalDetailsPage(long userId, long itemCount,
                        Map<String, String> params) {
                Map<String,String> htmlSnippet = new HashMap<String, String>();
                htmlSnippet.put("HEADER", getHeaderHtml(userId, false, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_HEADER", getMyaccountHeaderHtml());
                System.out.println("MYACCOUNT_HEADER Snippet generated");
                
                htmlSnippet.put("PERSONAL_DETAILS", getPersonalDetailsHtml(userId));
                System.out.println("PERSONAL_DETAILS Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");

                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, false));
                System.out.println("MY_RESEARCH Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                
                return htmlSnippet;
        }
        
        public Map<String, String> getLoginDetailsPage(long userId, long itemCount,
                        Map<String, String> params) {
                Map<String,String> htmlSnippet = new HashMap<String, String>();
                htmlSnippet.put("HEADER", getHeaderHtml(userId, false, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_HEADER", getMyaccountHeaderHtml());
                System.out.println("MYACCOUNT_HEADER Snippet generated");
                
                htmlSnippet.put("LOGIN_DETAILS", getLoginDetailsHtml(userId));
                System.out.println("LOGIN_DETAILS Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");

                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, false));
                System.out.println("MY_RESEARCH Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                
                return htmlSnippet;
        }


        
        public Map<String, String> getOrderDetailsPage(long userId, long itemCount, long orderId,
                        Map<String, String> params) {
                Map<String,String> htmlSnippet = new HashMap<String, String>();
                htmlSnippet.put("HEADER", getHeaderHtml(userId, false, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("MYACCOUNT_HEADER", getMyaccountHeaderHtml());
                System.out.println("MYACCOUNT_HEADER Snippet generated");
                
                htmlSnippet.put("ORDER_DETAILS", getOrderDetailsHtml(orderId));
                System.out.println("MYACCOUNT_DETAILS Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");

                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, false));
                System.out.println("MY_RESEARCH Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                
                return htmlSnippet;
        }

        
        
        private String getOrderDetailsHtml(long orderId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/orderdetails.vm";
                TransactionServiceClient transactionServiceClient = null;
                in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
                Order order = null;
                Date orderedOn = null, deliveryEstimate = null;
                try{
                        transactionServiceClient = new TransactionServiceClient();
                        orderClient = transactionServiceClient.getClient();
                        order = orderClient.getOrder(orderId);
                        orderedOn = new Date(order.getCreated_timestamp());
                        deliveryEstimate = new Date(order.getExpected_delivery_time());
                }catch (Exception e){
                        
                }
                
                SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
                context.put("order", order);
                context.put("orderedOn", dateformat.format(orderedOn));
                context.put("deliveryEstimate", dateformat.format(deliveryEstimate));
                
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        private String getMyaccountDetailsHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/myaccount.vm";
                TransactionServiceClient transactionServiceClient = null;
                in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
                List<Order> orders = null;
                try{
                        transactionServiceClient = new TransactionServiceClient();
                        orderClient = transactionServiceClient.getClient();
                        orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
                        
                }catch (Exception e){
                        
                }
                context.put("orders", orders);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        
        private String getLoginDetailsHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/logindetails.vm";
                String email = "";
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                
                try{
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                        UserPrimaryInfo primaryInfo = userClient.getPrimaryInfo(userId, false);
                        email = primaryInfo.getEmail();
                }catch (Exception e){
                        
                }
                context.put("email", email);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        private String getPersonalDetailsHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/personaldetails.vm";
                String email = "";
                String name = "";
                int year = 0, month = 0, day = 0;
                String sex = "";
                String subscribe = "false";
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                UserPrimaryInfo primaryInfo = null;
                try{
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                        primaryInfo = userClient.getPrimaryInfo(userId, false);
                        
                        email = primaryInfo.getCommunicationEmail();
                        name = primaryInfo.getFirstName();
                        
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(primaryInfo.getDateOfBirth());
                        month = calendar.get(Calendar.MONTH);
                        year = calendar.get(Calendar.YEAR);
                        day = calendar.get(Calendar.DAY_OF_MONTH);
                        
                }catch (Exception e){
                        
                }
                context.put("name", name);
                context.put("email", email);
                context.put("month", month+"");
                context.put("day", day+"");
                context.put("year", year+"");
                context.put("subscribe", subscribe);
                context.put("sex", sex);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        private String getCompletedOrdersHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/completedorders.vm";
                TransactionServiceClient transactionServiceClient = null;
                in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
                List<Order> orders = null;
                try{
                        transactionServiceClient = new TransactionServiceClient();
                        orderClient = transactionServiceClient.getClient();
                        orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), OrderStatus.DELIVERY_SUCCESS);
                        
                }catch (Exception e){
                        
                }
                context.put("orders", orders);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        private String getMyaccountHeaderHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/myaccountheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public Map<String, String> getMyAccountPage_old(long userId,
                        Map<String, String> params) {
                
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client client = null;
                String htmlString = new String();
                
                        UserPrimaryInfo userPrimaryInfo;
                        try {
                                userContextServiceClient = new UserContextServiceClient();
                                client = userContextServiceClient.getClient();
                                userPrimaryInfo = client.getPrimaryInfo(userId, false);
                                
                                String templateFile = "templates/myaccounts.vm";
                                VelocityContext context = new VelocityContext();        
                                context.put("userPrimaryInfo", userPrimaryInfo);
                                
                                htmlString = getHtmlFromVelocity(templateFile, context);
                                System.out.println("My Accounts Snippet generated" + htmlString);
                        } catch (UserContextException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        
                        Map<String,String> htmlSnippet = new HashMap<String, String>();
                        htmlSnippet.put("My_ACCOUNT", htmlString);

                        htmlSnippet.put("My_ORDERS", getMyOrdersDiv(userId));
                        
                        return htmlSnippet;
        }

        //ShippingPage
        public Map<String, String> getShippingPage(long userId, boolean isSessionId, long cartId, long itemCount, Map<String, String> params){
                Map<String,String> htmlSnippet = new HashMap<String, String>();

                htmlSnippet.put("HEADER", getHeaderHtml(userId, isSessionId, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("SHIPPING_HEADER", getShippingHeaderHtml());
                System.out.println("SHIPPING_HEADER Snippet generated");
                
                htmlSnippet.put("SHIPPING_DETAILS", getShippingDetailsHtml(cartId));
                System.out.println("SHIPPING_BAR Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");
                
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                return htmlSnippet;
        
        }

        //ShippingPage
        public Map<String, String> getShippingAddressPage(long userId, boolean isSessionId, long cartId, long itemCount, Map<String, String> params){
                Map<String,String> htmlSnippet = new HashMap<String, String>();

                htmlSnippet.put("HEADER", getHeaderHtml(userId, isSessionId, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("SHIPPING_ADDRESS_HEADER", getMyaccountHeaderHtml());
                System.out.println("SHIPPING_ADDRESS_HEADER Snippet generated");
                
                htmlSnippet.put("SHIPPING_ADDRESS_DETAILS", getShippingAddressDetailsHtml(userId));
                System.out.println("SHIPPING_ADDRESS_BAR Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");
                
                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, false));
                System.out.println("MY_RESEARCH Snippet generated");

                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                return htmlSnippet;
        
        }

        private String getShippingHeaderHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/shippingheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        private String getShippingAddressDetailsHtml(long userId){
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/shippingaddressdetails.vm";
                long defaultAddressId = 0;
                List<Address> addresses = null;
                
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                try {
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                        UserPrimaryInfo primaryInfo = userClient.getPrimaryInfo(userId, false);
                        addresses = primaryInfo.getAddresses();
                        defaultAddressId = primaryInfo.getDefaultAddressId();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                context.put("defaultAddressId", defaultAddressId+"");
                context.put("addresses", addresses);
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        private String getShippingDetailsHtml(long cartId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/shippingdetails.vm";
                List<Map<String,String>> items = null;
                double totalamount= 0.0;
                List<Address> addresses = null;
                long defaultAddressId = 0;
                ShoppingCartClient shoppingCartClient = null;
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
                CatalogServiceClient catalogServiceClient  = null;
                in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                
                try {
                        shoppingCartClient = new ShoppingCartClient();
                        cartClient = shoppingCartClient.getClient();
                        catalogServiceClient = new CatalogServiceClient();
                        catalogClient = catalogServiceClient.getClient();
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                        
                        
                        Cart cart = cartClient.getCart(cartId);
                        List<Line> lineItems = cart.getLines();
                        
                        if( ! lineItems.isEmpty())
                        {
                                items = new ArrayList<Map<String,String>>();
                                
                                for (Line line : lineItems) {
                                        Map<String, String> itemdetail = new HashMap<String, String>();
                                        Item item = catalogClient.getItemByCatalogId(line.getItemId());
                                        String itemName = ((item.getManufacturerName() != null) ? item.getManufacturerName() + " " : "")
                                                                                + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
                                                                                + (( item.getModelNumber() != null ) ? item.getModelNumber() : "" );
                                        
                                        itemdetail.put("ITEM_NAME", itemName);
                                        itemdetail.put("ITEM_ID", line.getItemId()+"");
                                        itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
                                        itemdetail.put("MRP", ((int)item.getMrp())+"");
                                        itemdetail.put("SELLING_PRICE", (int)item.getSellingPrice()+"");
                                        itemdetail.put("TOTAL_PRICE", (int)(item.getSellingPrice()*line.getQuantity())+"");
                                        itemdetail.put("SHIPPING_TIME", 48+"");
                                        totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
                                        items.add(itemdetail);                          
                                }
                        }
                        
                        UserPrimaryInfo primaryInfo = userClient.getPrimaryInfo(cart.getUserId(), false);
                        addresses = primaryInfo.getAddresses();
                        defaultAddressId = primaryInfo.getDefaultAddressId();
                        
                }catch (Exception e){
                        e.printStackTrace();
                }
                
                context.put("items", items);
                context.put("totalamount", (int)totalamount+"");
                context.put("addresses", addresses);
                context.put("defaultAddressId", defaultAddressId+"");
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        
        //ShoppingCartPage
        public Map<String, String> getShoppingCartPage(long userId, boolean isSessionId, long cartId, long itemCount, Map<String, String> params){
                Map<String,String> htmlSnippet = new HashMap<String, String>();

                htmlSnippet.put("HEADER", getHeaderHtml(userId, isSessionId, ""));
                System.out.println("HEADER Snippet generated");
                
                htmlSnippet.put("MAIN_MENU", getMainMenuHtml());
                System.out.println("MAIN_MENU Snippet generated");
                
                htmlSnippet.put("SEARCH_BAR", getSearchBarHtml(itemCount, 0));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("CART_HEADER", getCartHeaderHtml());
                System.out.println("CART_HEADER Snippet generated");
                
                htmlSnippet.put("CART_DETAILS", getCartDetailsHtml(cartId));
                System.out.println("SEARCH_BAR Snippet generated");
                
                htmlSnippet.put("CUSTOMER_SERVICE", getCustomerServiceHtml());
                System.out.println("CUSTOMER_SERVICE Snippet generated");
                
                htmlSnippet.put("MY_RESEARCH", getMyResearchHtml(userId, isSessionId));
                System.out.println("MY_RESEARCH Snippet generated");
        
                htmlSnippet.put("FOOTER", getFooterHtml());
                System.out.println("Footer Snippet generated");
        
        //      htmlSnippet.put("WIDGET_DIV", getWidgetDiv(productId,"", WidgetType.ACCESSORIES));
        //      System.out.println("ACCESSORIES Snippet generated")
                
                htmlSnippet.put("JS_FILES", "");
                htmlSnippet.put("CSS_FILES", "");
                
                System.out.println("Returning Generated Responce");
                
                return htmlSnippet;
        
        }

        private String getCartHeaderHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/cartheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        private String getCartDetailsHtml(long cartId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/cartdetails.vm";
                List<Map<String,String>> items = null;
                double totalamount= 0.0;
                
                ShoppingCartClient shoppingCartClient = null;
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
                CatalogServiceClient catalogServiceClient  = null;
                in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
                
                try {
                        shoppingCartClient = new ShoppingCartClient();
                        cartClient = shoppingCartClient.getClient();
                        catalogServiceClient = new CatalogServiceClient();
                        catalogClient = catalogServiceClient.getClient();
                        
                        Cart cart = cartClient.getCart(cartId);
                        List<Line> lineItems = cart.getLines();
                        if(lineItems.size() != 0){
                                items = new ArrayList<Map<String,String>>();
                                for (Line line : lineItems) {
        
                                        Map<String, String> itemdetail = new HashMap<String, String>();
                                        Item item = catalogClient.getItemByCatalogId(line.getItemId());
                                        
                                        String itemName = ((item.getManufacturerName() != null) ? item.getManufacturerName() + " " : "")
                                                                                + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
                                                                                + (( item.getModelNumber() != null ) ? item.getModelNumber() : "" );
                                        
                                        itemdetail.put("ITEM_NAME", itemName);
                                        System.out.println(itemdetail.get("ITEM_NAME"));
                                        itemdetail.put("ITEM_ID", line.getItemId()+"");
                                        itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
                                        itemdetail.put("MRP", ((int)item.getMrp())+"");
                                        itemdetail.put("SELLING_PRICE", ((int)item.getSellingPrice())+"");
                                        itemdetail.put("TOTAL_PRICE", ((int)((item.getSellingPrice()*line.getQuantity())))+"");
                                        itemdetail.put("SHIPPING_TIME", 48+"");
                                        totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
                                        items.add(itemdetail);                          
                                }
                        }
                        
                }catch (Exception e){
                        e.printStackTrace();
                }
                        
                context.put("items", items);
                context.put("totalamount", ((int)totalamount)+"");
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public Map<String, String> getShoppingCartPage_Old(long userId, boolean isSessionId, Map<String, String> params)
        {
                ShoppingCartClient shoppingCartClient = null;
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
                
                try {
                        shoppingCartClient = new ShoppingCartClient();
                        cartClient = shoppingCartClient.getClient();
                
                        Cart cart = cartClient.getCurrentCart(userId, isSessionId);
                
                        List<Line> lineItems = cart.getLines();
                        List<Map<String,String>> items = new ArrayList<Map<String,String>>(); 
                
                        for (Line line : lineItems) {
//                              long productId = getCatalogIdByItemId(line.getItemId());
                                long productId = line.getItemId();
                                Map<String, String> productDetails = getProductDetails(productId);
                                double mrp = getItemMrpByCatalogId(productId);
                                double sellingPrice = getItemSellingPriceByCatalogId(productId);
                                double saving = (mrp - sellingPrice)/mrp*100; 
                                saving = Math.round(saving);
                                Map<String, String> item = new HashMap<String, String>();
                                item.put("BRAND_NAME", productDetails.get("BRAND_NAME"));
                                item.put("MODEL_NAME", productDetails.get("MODEL_NAME"));
                                item.put("MODEL_NUMBER", productDetails.get("MODEL_NUMBER"));
                                item.put("ITEM_ID", line.getItemId()+"");
                                item.put("ITEM_QUANTITY", line.getQuantity()+"");
                                item.put("MRP", mrp+"");
                                item.put("SELLING_PRICE", sellingPrice+"");
                                item.put("SAVING", sellingPrice+"");
                                items.add(item);
                        }
                        
                        String templateFile = "templates/shoppingcart.vm";
                        VelocityContext context = new VelocityContext();        
                        context.put("items", items);
                        
                        String htmlString = getHtmlFromVelocity(templateFile, context);
                        System.out.println("My Cart Snippet generated" + htmlString);

                        Map<String,String> htmlSnippet = new HashMap<String, String>();
                        htmlSnippet.put("SHOPPING_CART", htmlString);

                        return htmlSnippet;

                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                return null;
        }
        
        
        public long getCatalogIdByItemId(long itemId){
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        
                        return client.getItem(itemId).getCatalogItemId();
                } catch (InventoryServiceException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return -1;
        }
        
//      private void addItemToCart(long catalogItemId, long userId) throws Exception{
//              UserContextServiceClient userContextServiceClient = null;
//              in.shop2020.model.v1.user.UserContextService.Client userClient = null;
//              userContextServiceClient = new UserContextServiceClient();
//              userClient = userContextServiceClient.getClient();
//
//              ShoppingCartClient shoppingCartClient = null;
//              in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
//              shoppingCartClient = new ShoppingCartClient();
//              cartClient = shoppingCartClient.getClient();
//              
//              Cart cart = cartClient.getCurrentCart(userId);
//              //if user is logged in create  new cart
//              //if( userClient.getState(userId, false).isIsLoggedIn()){
//              if(cart == null){
//                      cartClient.createCart(userId);
//                      cart = cartClient.getCurrentCart(userId);
//              }
//              cartClient.addItemToCart(cart.getId(), catalogItemId, 1);
//      }

                //userClient.getState(userId, isSessionId);
                
//              TransactionServiceClient transactionServiceClient = null;
//              in.shop2020.model.v1.order.TransactionService.Client client = null;
//              
//              Cart cart = cartClient.getCurrentCart(userId);
//              List<Line> lineItems = cart.getLines();
//              
//              Line ln = lineItems.get(0);
//              ln.getItemId();
//              ln.getId();
//              ln.getQuantity();
//              ln.getStatusMessage();
//              long productId = 10;
//              
//              Cart cart = cartClient.getCurrentCart(0);
//              List<Line> lineItems = cart.getLines();
//              Line ln = lineItems.get(0);
//              ln.getItemId();
//              ln.getId();
//              
//              ln.getStatusMessage();
//              
        
        private Map<String,String> getProductDetails(long productId){
                Map<String, String> productDetails = new HashMap<String, String>();
                
                EntityContainer entContainer =  Catalog.getInstance().getEntityContainer();
                ExpandedEntity expEntity;
                try {
                        expEntity = entContainer.getExpandedEntity(productId);
                        
                        productDetails.put("BRAND_NAME", expEntity.getBrand());
                        productDetails.put("MODEL_NUMBER", expEntity.getModelNumber());
                        productDetails.put("MODEL_NAME", expEntity.getModelName());
                        productDetails.put("CATEGORY_ID", expEntity.getCategoryID() + "");
                        productDetails.put("CATEGORY_NAME", expEntity.getCategory().getLabel());
                        productDetails.put("INTRODUCTION_TEXT", expEntity.getSlide(130001).getFreeformContent().getFreeformText());
                        return productDetails;
//                      expEntity.getBrand();
//                      expEntity.getModelNumber();
//                      expEntity.getModelName();
//                      expEntity.getCategoryID();
//                      expEntity.getCategory().getLabel();
//                      expEntity.getSlide(130001).getFreeformContent().getFreeformText();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                return null;
        }

        
        private String getMyOrdersDiv(long userId){
                TransactionServiceClient transactionServiceClient = null;
                in.shop2020.model.v1.order.TransactionService.Client client = null;
                String htmlString = new String();
                        try {
                                transactionServiceClient = new TransactionServiceClient();
                                client = transactionServiceClient.getClient();
                                List<Transaction> transactions = client.getTransactionsForCustomer(userId, 0, 0, null);
                                
                                String templateFile = "templates/myorders.vm";
                                VelocityContext context = new VelocityContext();        
                                context.put("transactions", transactions);
                                if(!transactions.isEmpty()){
                                        Transaction tn = transactions.get(0);
                                        java.sql.Date d = new java.sql.Date(tn.getCreatedOn());
                                        System.out.println("Date is" + d.toString());
                                }
                                htmlString = getHtmlFromVelocity(templateFile, context);
                                System.out.println("Orders Info Snippet generated" + htmlString);
                                return htmlString;

                        } catch (UserContextException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        return "MY_ORDERS";
        }
        
        public String getHtmlFromVelocity(String templateFile, VelocityContext context){
                Properties p = new Properties();
                p.setProperty("resource.loader", "class");
                p.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

                
                try {
                        Velocity.init(p);
                        Template template = Velocity.getTemplate(templateFile);
                        if(template != null) {
                                StringWriter writer = new StringWriter();
                                template.merge(context, writer);
                                writer.flush();
                                writer.close();
                                return writer.toString();
                        }
        
                        } catch (ResourceNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (ParseErrorException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (MethodInvocationException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
        
                return null;
        }
        
        public double getItemMrpByCatalogId(long productId){
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                Double itemPrice = 0.0;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        Item item = client.getItemByCatalogId(productId);
                        itemPrice = item.getMrp();
                }
                catch(Exception e){
                        e.printStackTrace();
                }
                return itemPrice;
        }

        public double getItemSellingPriceByCatalogId(long productId){
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                Double itemPrice = 0.0;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        Item item = client.getItemByCatalogId(productId);
                        itemPrice = item.getSellingPrice();
                }
                catch(Exception e){
                        e.printStackTrace();
                }
                return itemPrice;
        }


        public String getCategoryHeaderSnippet() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/categoryheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

                

}