Rev 2268 | Rev 2652 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.logistics.Provider;import in.shop2020.model.v1.catalog.InventoryService.Client;import in.shop2020.model.v1.catalog.InventoryServiceException;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.User;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.serving.utils.FileUtils;import in.shop2020.serving.utils.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;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;public class PageLoaderHandler {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(0, 4, null, -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){}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(0, 4, null, 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();}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(0,4, null, -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 blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}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 getMyResearchHtml() {String htmlString = "";VelocityContext context = new VelocityContext();String templateFile = "templates/myresearch.vm";htmlString = getHtmlFromVelocity(templateFile, context);return htmlString;}public String getBrowseHistoryHtml() {String htmlString = "";VelocityContext context = new VelocityContext();String templateFile = "templates/browsehistory.vm";htmlString = getHtmlFromVelocity(templateFile, context);return htmlString;}public String getCustomerServiceHtml() {String htmlString = "";VelocityContext context = new VelocityContext();String templateFile = "templates/customerservice.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(fis != null) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(fis != null) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return htmlString.toString();}public String getProductPropertiesHtml(long productId) {StringBuilder htmlString = new StringBuilder();String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductPropertiesSnippet.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(fis != null) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 getOrderDetailsHtml(long orderId, long userId) {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.getOrderForCustomer(orderId, userId);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 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 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 blocke.printStackTrace();} catch (ParseErrorException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MethodInvocationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}