Subversion Repositories SmartDukaan

Rev

Rev 1386 | Rev 1549 | Go to most recent revision | Blame | Compare with Previous | 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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

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.logistics.Provider;
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.user.Cart;
import in.shop2020.model.v1.user.Line;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.RatingType;
import in.shop2020.model.v1.user.RatingsWidget;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.Widget;
import in.shop2020.model.v1.user.WidgetItem;
import in.shop2020.model.v1.user.WidgetType;
import in.shop2020.serving.utils.*;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.LogisticsServiceClient;
import in.shop2020.thrift.clients.TransactionServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;


public class PageLoaderHandler {

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

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

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

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

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


        public 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.getBestSellersCatalogIds(1, 4, -1);
                        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){
                        
                }
                /*
                finally{
                        catalogServiceClient.closeConnection();
                }
                */

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


        public 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.getLatestArrivalsCatalogIds(1, 4, 10003);
                        List<String> itemList = new ArrayList<String>();
                        for(Long item: items){
                                try{
                                        itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
                                }catch(IOException ioex){
                                        ioex.printStackTrace();
                                }
                        }
                        context.put("itemList", itemList);
                        
                } catch(Exception e){
                        e.printStackTrace();
                }finally{
                        catalogServiceClient.closeConnection();
                }
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public 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.getBestDealsCatalogIds(1,4,-1);
                        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 (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();
                } finally{
                        catalogServiceClient.closeConnection();
                }
                
                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;
        }



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



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



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



        public String getMyResearchHtml(long userId, boolean isLoggedIn) {
                if(isLoggedIn)
                        return getWidgetDiv(userId, WidgetType.MY_RESEARCH, "myresearch.vm");
                else
                        return getWidgetDiv(0, WidgetType.MY_RESEARCH, "myresearch.vm");
        }

        public String getBrowseHistoryHtml(long userId, boolean isLoggedIn) {
                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;
        }



        public 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;
        }



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


        public 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();
        }

        public String getPageTitleHtml(long productId) {
                StringBuilder htmlString = new StringBuilder();
                String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "TitleSnippet.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 getPageMetaDescriptionHtml(long productId) {
                StringBuilder htmlString = new StringBuilder();
                String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "MetaDescriptionSnippet.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 getPageMetaKeywordsHtml(long productId) {
                StringBuilder htmlString = new StringBuilder();
                String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "MetaKeywordsSnippet.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 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";
                
                context.put("itemCount", itemCounts+"");
                context.put("categoryId", categoryId+"");
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }



        public String getHeaderHtml(boolean isLoggedIn, String  userName, String url) {
                VelocityContext context = new VelocityContext();
                if (isLoggedIn) {
                        context.put("LOGGED_IN", "TRUE");
                        context.put("WELCOME_MESSAGE", "Hi, " + userName);
                } else {
                        context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
                        context.put("REDIRECT_URL", url);
                }               
                
                String templateFile = "templates/header.vm";
                
                return getHtmlFromVelocity(templateFile, context);
        }


        
        public  String getWidgetDiv(long userId, WidgetType widgetType, String templateFile){

                UserContextServiceClient userServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client client = null;
                Widget widget = null;
                try {
                        userServiceClient = new UserContextServiceClient();
                        client = userServiceClient.getClient();
                        if(widgetType == WidgetType.MY_RESEARCH && userId != 0){
                                widget = client.getMyResearch(userId);
                        }
                        if(widgetType == WidgetType.BROWSE_HISTORY){
                                widget = client.getBrowseHistory(userId);
                        }
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }finally{
                        if (userServiceClient != null) {
                                userServiceClient.closeConnection();
                        }
                }


                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>();
                                String itemSnippet;
                                
                                        try {
                                                itemSnippet = FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item.getItem_id() + File.separator + "WidgetSnippet.html");
                                        } catch (IOException e) {
                                                e.printStackTrace();
                                                continue;
                                        }
                                        
                                itemDetail.put("ITEM_ID", item.getItem_id()+"");
                                itemDetail.put("ITEM_SNIPPET", itemSnippet);
                                itemDetails.add(itemDetail);
                        }
                }else{
                        System.out.println("widget not found");
                }
                
                VelocityContext context = new VelocityContext();
                context.put("userId", userId);
                context.put("itemDetails", itemDetails);
                
                return getHtmlFromVelocity("templates/"+templateFile, context);
        }

        
        public String getOrderDetailsHtml(long orderId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/orderdetails.vm";
                Order order = null;
                Date orderedOn = null, deliveryEstimate = null;
                Provider provider = null;
                try{
                        TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
                        in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
                        order = orderClient.getOrder(orderId);
                        orderedOn = new Date(order.getCreated_timestamp());
                        deliveryEstimate = new Date(order.getExpected_delivery_time());

                        if(order.getLogistics_provider_id() != 0){
                                LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
                                in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
                                provider = logisticsClient.getProvider(order.getLogistics_provider_id());
                        }
                }catch (Exception e){
                        
                }
                
                SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
                SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
                context.put("order", order);
                context.put("orderedOn", dateformat.format(orderedOn));
                context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
                if(provider!=null){
                        context.put("providerName", provider.getName());
                }

                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        public String getMyaccountDetailsHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/myaccount.vm";
                List<Order> orders = null;
                Map<Long, String> providerNames = new HashMap<Long, String>();
                try{
                        TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
                        in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
                        orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
                        
                        LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
                        in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
                        List<Provider> providers = logisticsClient.getAllProviders();
                        for(Provider provider: providers)
                                providerNames.put(provider.getId(), provider.getName());
                }catch (Exception e){
                        e.printStackTrace();
                }
                List<String> orderDate = new ArrayList<String>();
                SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
                if(orders!=null && !orders.isEmpty()){
                        for(Order order: orders){
                                Date orderedOn = new Date(order.getCreated_timestamp());
                                orderDate.add(dateformat.format(orderedOn));
                        }
                }
                context.put("orders", orders);
                context.put("orderDate", orderDate);
                context.put("providerNames", providerNames);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        
        public String getLoginDetailsHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/logindetails.vm";
                String email = "";
                try{
                        email = getEmailId(userId);
                }catch (Exception e){
                        
                }
                context.put("email", email);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        public String getEmailId(long userId){
                String email = " ";
                
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        if(userClient.getUserById(userId).getEmail() != null){
                                email = userClient.getUserById(userId).getEmail();
                        }
                } catch (UserContextException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return email; 
        }

        
        public String getPersonalDetailsHtml(long userId) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/personaldetails.vm";
                String email = "";
                String name = "";
                String dateOfBirth = "";
                String sex = "";
                String subscribe = "false";
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                try{
                        User user = null;
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                        user = userClient.getUserById(userId);
                        
                        email = user.getCommunicationEmail();
                        name = user.getName();
                        
                        dateOfBirth = user.getDateOfBirth();
                }catch (Exception e){
                        e.printStackTrace();
                }
                context.put("name", name);
                context.put("email", email);
                context.put("dateOfBirth", dateOfBirth+"");
                context.put("subscribe", subscribe);
                context.put("sex", sex);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        public 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;
                List<String> orderDate = new ArrayList<String>();
                Map<Long, String> providerNames = new HashMap<Long, String>();
                
                try{
                        transactionServiceClient = new TransactionServiceClient();
                        orderClient = transactionServiceClient.getClient();
                        orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), OrderStatus.DELIVERY_SUCCESS);

                        LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
                        in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
                        List<Provider> providers = logisticsClient.getAllProviders();
                        for(Provider provider: providers)
                                providerNames.put(provider.getId(), provider.getName());

                        SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
                        
                        if(orders != null && !orders.isEmpty()){
                                for(Order order: orders){
                                        Date orderedOn = new Date(order.getCreated_timestamp());
                                        orderDate.add(dateformat.format(orderedOn));
                                }
                        }
                }catch (Exception e){
                        
                }
                context.put("orders", orders);
                context.put("orderDate", orderDate);
                context.put("providerNames", providerNames);
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        public String getMyaccountHeaderHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/myaccountheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public String getShippingAddressDetailsHtml(long userId, String errorMsg){
                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();
                        
                        addresses = userClient.getAllAddressesForUser(userId);
                        defaultAddressId = userClient.getDefaultAddressId(userId);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                context.put("defaultAddressId", defaultAddressId+"");
                context.put("addresses", addresses);
                context.put("errorMsg", errorMsg);
                
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }
        
        public String getCartHeaderHtml() {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/cartheader.vm";
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }


        public String getCartDetailsHtml(long userId, long cartId, String errorMsg) {
                String htmlString = "";
                VelocityContext context = new VelocityContext();
                String templateFile = "templates/cartdetails.vm";
                List<Map<String,String>> items = null;
                double totalamount= 0.0;
                
                UserContextServiceClient userServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                CatalogServiceClient catalogServiceClient  = null;
                in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
                String pincode = "110001";
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        catalogClient = catalogServiceClient.getClient();
                        userServiceClient = new UserContextServiceClient();
                        userClient = userServiceClient.getClient();
                        
                        pincode = userClient.getDefaultPincode(userId);
                        Cart cart = userClient.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.getItem(line.getItemId());
                                        
                                        String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
                                                                                + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
                                                                                + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
                                                                                + (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
                                        
                                        itemdetail.put("ITEM_NAME", itemName);
                                        System.out.println(itemdetail.get("ITEM_NAME"));
                                        itemdetail.put("ITEM_ID", line.getItemId()+"");
                                        itemdetail.put("CATALOG_ID", item.getCatalogItemId()+"");
                                        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", line.getEstimate()+"");
                                        totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
                                        items.add(itemdetail);                          
                                }
                        }
                        
                }catch (Exception e){
                        e.printStackTrace();
                }
                        
                context.put("items", items);
                context.put("totalamount", ((int)totalamount)+"");
                context.put("errorMsg", errorMsg);
                context.put("pincode", pincode);
                htmlString = getHtmlFromVelocity(templateFile, context);
                return htmlString;
        }

        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");
                p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
                
                try {
                        Velocity.init(p);
                        //Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
                        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;
        }
}