Subversion Repositories SmartDukaan

Rev

Rev 1364 | Rev 1429 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.services;
2
 
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.io.StringWriter;
517 rajveer 10
import java.text.SimpleDateFormat;
507 rajveer 11
import java.util.ArrayList;
12
import java.util.Date;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Properties;
17
 
18
import org.apache.thrift.TException;
19
import org.apache.velocity.Template;
20
import org.apache.velocity.VelocityContext;
21
import org.apache.velocity.app.Velocity;
22
import org.apache.velocity.exception.MethodInvocationException;
23
import org.apache.velocity.exception.ParseErrorException;
24
import org.apache.velocity.exception.ResourceNotFoundException;
25
 
26
 
745 chandransh 27
import in.shop2020.logistics.Provider;
507 rajveer 28
import in.shop2020.model.v1.catalog.InventoryServiceException;
29
import in.shop2020.model.v1.catalog.Item;
30
import in.shop2020.model.v1.catalog.InventoryService.Client;
31
import in.shop2020.model.v1.order.Order;
32
import in.shop2020.model.v1.order.OrderStatus;
555 chandransh 33
import in.shop2020.model.v1.user.Cart;
34
import in.shop2020.model.v1.user.Line;
507 rajveer 35
import in.shop2020.model.v1.user.Address;
555 chandransh 36
import in.shop2020.model.v1.user.User;
37
import in.shop2020.model.v1.user.RatingType;
38
import in.shop2020.model.v1.user.RatingsWidget;
762 rajveer 39
import in.shop2020.model.v1.user.UserContextException;
555 chandransh 40
import in.shop2020.model.v1.user.Widget;
41
import in.shop2020.model.v1.user.WidgetItem;
42
import in.shop2020.model.v1.user.WidgetType;
507 rajveer 43
import in.shop2020.serving.utils.*;
44
import in.shop2020.thrift.clients.CatalogServiceClient;
745 chandransh 45
import in.shop2020.thrift.clients.LogisticsServiceClient;
507 rajveer 46
import in.shop2020.thrift.clients.TransactionServiceClient;
47
import in.shop2020.thrift.clients.UserContextServiceClient;
48
 
49
 
50
public class PageLoaderHandler {
51
 
637 rajveer 52
	public String getRegistrationFormHtml() {
507 rajveer 53
		String htmlString = "";
54
		VelocityContext context = new VelocityContext();
55
		String templateFile = "templates/registrationform.vm";
56
		htmlString = getHtmlFromVelocity(templateFile, context);
57
		return htmlString;
58
	}
59
 
637 rajveer 60
	public String getLoginFormHtml() {
61
		String htmlString = "";
62
		VelocityContext context = new VelocityContext();
63
		String templateFile = "templates/loginform.vm";
64
		htmlString = getHtmlFromVelocity(templateFile, context);
65
		return htmlString;
66
	}
507 rajveer 67
 
637 rajveer 68
	public String getRegistrationHeaderHtml() {
507 rajveer 69
		String htmlString = "";
70
		VelocityContext context = new VelocityContext();
71
		String templateFile = "templates/registrationheader.vm";
72
		htmlString = getHtmlFromVelocity(templateFile, context);
73
		return htmlString;
74
	}
1184 varun.gupt 75
 
76
	public String getSigninSignupHeaderHtml()	{
77
		String htmlString = "";
78
		VelocityContext context = new VelocityContext();
79
		String templateFile = "templates/signinsignupheader.vm";
80
		htmlString = getHtmlFromVelocity(templateFile, context);
81
		return htmlString;
82
	}
507 rajveer 83
 
637 rajveer 84
	public String getLoginHeaderHtml() {
85
		String htmlString = "";
86
		VelocityContext context = new VelocityContext();
87
		String templateFile = "templates/loginheader.vm";
88
		htmlString = getHtmlFromVelocity(templateFile, context);
89
		return htmlString;
90
	}
91
 
507 rajveer 92
 
650 rajveer 93
	public String getMainBannerHtml() {
507 rajveer 94
		String htmlString = "";
95
		VelocityContext context = new VelocityContext();
96
		String templateFile = "templates/mainbanner.vm";
97
		htmlString = getHtmlFromVelocity(templateFile, context);
98
		return htmlString;
99
	}
100
 
101
 
650 rajveer 102
	public String getBestSellersHtml() {
507 rajveer 103
		String htmlString = "";
104
		VelocityContext context = new VelocityContext();
105
		String templateFile = "templates/bestsellers.vm";
106
 
107
		CatalogServiceClient catalogServiceClient = null;
108
		Client client = null;
109
 
110
		try {
111
			catalogServiceClient = new CatalogServiceClient();
112
			client = catalogServiceClient.getClient();
627 rajveer 113
			List<Long> items = client.getBestSellersCatalogIds(1, 4, -1);
507 rajveer 114
			List<String> itemList = new ArrayList<String>();
115
			for(Long item: items){
517 rajveer 116
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 117
			}
118
			context.put("itemList", itemList);
119
 
120
		} catch(Exception e){
121
 
122
		}
762 rajveer 123
		/*
124
		finally{
125
			catalogServiceClient.closeConnection();
126
		}
127
		*/
507 rajveer 128
 
129
 
130
		htmlString = getHtmlFromVelocity(templateFile, context);
517 rajveer 131
		return htmlString;	
132
	}
507 rajveer 133
 
134
 
650 rajveer 135
	public String getLatestArrivalsHtml() {
507 rajveer 136
		String htmlString = "";
137
		VelocityContext context = new VelocityContext();
138
		String templateFile = "templates/latestarrivals.vm";
139
 
140
		CatalogServiceClient catalogServiceClient = null;
141
		Client client = null;
142
 
143
		try {
144
			catalogServiceClient = new CatalogServiceClient();
145
			client = catalogServiceClient.getClient();
1002 chandransh 146
			List<Long> items = client.getLatestArrivalsCatalogIds(1, 4, 10003);
507 rajveer 147
			List<String> itemList = new ArrayList<String>();
148
			for(Long item: items){
968 chandransh 149
				try{
150
					itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
151
				}catch(IOException ioex){
152
					ioex.printStackTrace();
153
				}
507 rajveer 154
			}
155
			context.put("itemList", itemList);
156
 
157
		} catch(Exception e){
968 chandransh 158
			e.printStackTrace();
762 rajveer 159
		}finally{
160
			catalogServiceClient.closeConnection();
507 rajveer 161
		}
162
 
163
		htmlString = getHtmlFromVelocity(templateFile, context);
164
		return htmlString;
165
	}
166
 
167
 
650 rajveer 168
	public String getBestDealsHtml() {
507 rajveer 169
		String htmlString = "";
170
		VelocityContext context = new VelocityContext();
171
		String templateFile = "templates/bestdeals.vm";
172
 
173
		CatalogServiceClient catalogServiceClient = null;
174
		Client client = null;
175
 
176
		try {
177
			catalogServiceClient = new CatalogServiceClient();
178
			client = catalogServiceClient.getClient();
627 rajveer 179
			List<Long> items = client.getBestDealsCatalogIds(1,4,-1);
507 rajveer 180
			List<String> itemList = new ArrayList<String>();
181
			for(Long item: items){
517 rajveer 182
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 183
			}
184
			context.put("itemList", itemList);
185
 
786 rajveer 186
		} catch (InventoryServiceException e) {
187
			// TODO Auto-generated catch block
188
			e.printStackTrace();
189
		} catch (TException e) {
190
			// TODO Auto-generated catch block
191
			e.printStackTrace();
192
		} catch (Exception e) {
193
			// TODO Auto-generated catch block
194
			e.printStackTrace();
195
		} finally{
762 rajveer 196
			catalogServiceClient.closeConnection();
507 rajveer 197
		}
198
 
199
		htmlString = getHtmlFromVelocity(templateFile, context);
200
		return htmlString;
201
	}
202
 
203
 
517 rajveer 204
	public String getFooterHtml() {
507 rajveer 205
		String htmlString = "";
206
		VelocityContext context = new VelocityContext();
207
		String templateFile = "templates/footer.vm";
208
		htmlString = getHtmlFromVelocity(templateFile, context);
209
		return htmlString;
210
	}
211
 
212
 
213
 
620 rajveer 214
	public String getAccessoriesHtml(long productId) {
637 rajveer 215
		return "";
216
		//return getWidgetDiv(0, WidgetType.ACCESSORIES, "accessories.vm");
507 rajveer 217
	}
218
 
219
 
220
 
620 rajveer 221
	public String getSimilarProductsHtml(long productId) {
637 rajveer 222
		return "";
223
		//return getWidgetDiv(0, WidgetType.SIMILAR_ITEMS, "similaritems.vm");
507 rajveer 224
	}
225
 
226
 
227
 
620 rajveer 228
	public String getRecommendationsHtml() {
637 rajveer 229
		return "";
230
		//return getWidgetDiv( 0, WidgetType.RECOMMENDED_ITEMS, "recommendations.vm");
507 rajveer 231
	}
232
 
233
 
234
 
555 chandransh 235
	public String getMyResearchHtml(long userId, boolean isLoggedIn) {
236
		if(isLoggedIn)
237
			return getWidgetDiv(userId, WidgetType.MY_RESEARCH, "myresearch.vm");
238
		else
239
			return getWidgetDiv(0, WidgetType.MY_RESEARCH, "myresearch.vm");
507 rajveer 240
	}
241
 
569 rajveer 242
	public String getBrowseHistoryHtml(long userId, boolean isLoggedIn) {
768 rajveer 243
		return getWidgetDiv(userId, WidgetType.BROWSE_HISTORY, "browsehistory.vm");
507 rajveer 244
	}
245
 
517 rajveer 246
	public String getCustomerServiceHtml() {
507 rajveer 247
		String htmlString = "";
248
		VelocityContext context = new VelocityContext();
249
		String templateFile = "templates/customerservice.vm";
250
		htmlString = getHtmlFromVelocity(templateFile, context);
251
		return htmlString;
252
	}
253
 
254
 
255
 
620 rajveer 256
	public String getReviewsHtml(long productId) {
507 rajveer 257
		String htmlString = "";
258
		VelocityContext context = new VelocityContext();
259
		Map<String, String> params = new HashMap<String, String>();
260
		params.put("PRODUCT_ID", productId+"");
261
		context.put("params", params);
262
		String templateFile = "templates/reviews.vm";
263
		htmlString = getHtmlFromVelocity(templateFile, context);
264
		return htmlString;
265
	}
266
 
267
 
268
 
620 rajveer 269
	public String getLocatorHtml() {
507 rajveer 270
		String htmlString = "";
271
		VelocityContext context = new VelocityContext();
272
		String templateFile = "templates/locator.vm";
273
		htmlString = getHtmlFromVelocity(templateFile, context);
274
		return htmlString;
275
	}
276
 
277
 
620 rajveer 278
	public String getSlideGuideHtml(long productId) {
507 rajveer 279
		StringBuilder htmlString = new StringBuilder();
517 rajveer 280
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideGuide.html";
507 rajveer 281
		File f = new File(filename);
282
 
283
 
284
		FileInputStream fis = null;
285
		try {
286
			fis = new FileInputStream(f);
287
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
288
			String line;
289
			while((line = br.readLine()) != null){
290
				htmlString.append(line+"\n");
291
			}
292
		} catch (FileNotFoundException e) {
293
			// TODO Auto-generated catch block
294
			e.printStackTrace();
295
		} catch (IOException e) {
296
			// TODO Auto-generated catch block
297
			e.printStackTrace();
298
		}
299
		finally {
300
			if(fis != null) {
301
				try {
302
					fis.close();
303
				} catch (IOException e) {
304
					// TODO Auto-generated catch block
305
					e.printStackTrace();
306
				}
307
			}
308
		}
309
 
310
		return htmlString.toString();
311
	}
312
 
517 rajveer 313
	public String getProductSummaryHtml(long productId) {
507 rajveer 314
		StringBuilder htmlString = new StringBuilder();
517 rajveer 315
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductDetail.html";
507 rajveer 316
		File f = new File(filename);
317
 
318
 
319
		FileInputStream fis = null;
320
		try {
321
			fis = new FileInputStream(f);
322
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
323
			String line;
324
			while((line = br.readLine()) != null){
325
				htmlString.append(line+"\n");
326
			}
327
		} catch (FileNotFoundException e) {
328
			// TODO Auto-generated catch block
329
			e.printStackTrace();
330
		} catch (IOException e) {
331
			// TODO Auto-generated catch block
332
			e.printStackTrace();
333
		}
334
		finally {
335
			if(fis != null) {
336
				try {
337
					fis.close();
338
				} catch (IOException e) {
339
					// TODO Auto-generated catch block
340
					e.printStackTrace();
341
				}
342
			}
343
		}
344
 
345
		return htmlString.toString();
346
	}
347
 
974 vikas 348
	public String getPageTitleHtml(long productId) {
349
		StringBuilder htmlString = new StringBuilder();
989 vikas 350
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "TitleSnippet.html";
974 vikas 351
		File f = new File(filename);
352
 
353
 
354
		FileInputStream fis = null;
355
		try {
356
			fis = new FileInputStream(f);
357
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
358
			String line;
359
			while((line = br.readLine()) != null){
360
				htmlString.append(line+"\n");
361
			}
362
		} catch (FileNotFoundException e) {
363
			// TODO Auto-generated catch block
364
			e.printStackTrace();
365
		} catch (IOException e) {
366
			// TODO Auto-generated catch block
367
			e.printStackTrace();
368
		}
369
		finally {
370
			if(fis != null) {
371
				try {
372
					fis.close();
373
				} catch (IOException e) {
374
					// TODO Auto-generated catch block
375
					e.printStackTrace();
376
				}
377
			}
378
		}
379
 
380
		return htmlString.toString();
381
	}
382
 
383
	public String getPageMetaDescriptionHtml(long productId) {
384
		StringBuilder htmlString = new StringBuilder();
989 vikas 385
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "MetaDescriptionSnippet.html";
974 vikas 386
		File f = new File(filename);
387
 
388
 
389
		FileInputStream fis = null;
390
		try {
391
			fis = new FileInputStream(f);
392
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
393
			String line;
394
			while((line = br.readLine()) != null){
395
				htmlString.append(line+"\n");
396
			}
397
		} catch (FileNotFoundException e) {
398
			// TODO Auto-generated catch block
399
			e.printStackTrace();
400
		} catch (IOException e) {
401
			// TODO Auto-generated catch block
402
			e.printStackTrace();
403
		}
404
		finally {
405
			if(fis != null) {
406
				try {
407
					fis.close();
408
				} catch (IOException e) {
409
					// TODO Auto-generated catch block
410
					e.printStackTrace();
411
				}
412
			}
413
		}
414
 
415
		return htmlString.toString();
416
	}
417
 
418
	public String getPageMetaKeywordsHtml(long productId) {
419
		StringBuilder htmlString = new StringBuilder();
989 vikas 420
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "MetaKeywordsSnippet.html";
974 vikas 421
		File f = new File(filename);
422
 
423
 
424
		FileInputStream fis = null;
425
		try {
426
			fis = new FileInputStream(f);
427
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
428
			String line;
429
			while((line = br.readLine()) != null){
430
				htmlString.append(line+"\n");
431
			}
432
		} catch (FileNotFoundException e) {
433
			// TODO Auto-generated catch block
434
			e.printStackTrace();
435
		} catch (IOException e) {
436
			// TODO Auto-generated catch block
437
			e.printStackTrace();
438
		}
439
		finally {
440
			if(fis != null) {
441
				try {
442
					fis.close();
443
				} catch (IOException e) {
444
					// TODO Auto-generated catch block
445
					e.printStackTrace();
446
				}
447
			}
448
		}
449
 
450
		return htmlString.toString();
451
	}
507 rajveer 452
 
517 rajveer 453
	public String getMainMenuHtml() {
507 rajveer 454
		String htmlString = "";
455
		VelocityContext context = new VelocityContext();
456
		String templateFile = "templates/mainmenu.vm";
457
		htmlString = getHtmlFromVelocity(templateFile, context);
458
		return htmlString;
459
	}
460
 
461
 
517 rajveer 462
	public String getSearchBarHtml(long itemCounts, long categoryId) {
507 rajveer 463
		String htmlString = "";
464
		VelocityContext context = new VelocityContext();
465
		String templateFile = "templates/searchbar.vm";
550 rajveer 466
 
467
		context.put("itemCount", itemCounts+"");
468
		context.put("categoryId", categoryId+"");
469
 
507 rajveer 470
		htmlString = getHtmlFromVelocity(templateFile, context);
471
		return htmlString;
472
	}
473
 
474
 
475
 
924 vikas 476
	public String getHeaderHtml(boolean isLoggedIn, String  userName, String url) {
550 rajveer 477
		VelocityContext context = new VelocityContext();
555 chandransh 478
		if (isLoggedIn) {
479
			context.put("LOGGED_IN", "TRUE");
480
			context.put("WELCOME_MESSAGE", "Hi, " + userName);
481
		} else {
801 rajveer 482
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
924 vikas 483
			context.put("REDIRECT_URL", url);
555 chandransh 484
		}		
507 rajveer 485
 
486
		String templateFile = "templates/header.vm";
550 rajveer 487
 
590 chandransh 488
		return getHtmlFromVelocity(templateFile, context);
507 rajveer 489
	}
490
 
491
 
492
 
650 rajveer 493
	public	String getWidgetDiv(long userId, WidgetType widgetType, String templateFile){
507 rajveer 494
 
555 chandransh 495
		UserContextServiceClient userServiceClient = null;
496
		in.shop2020.model.v1.user.UserContextService.Client client = null;
507 rajveer 497
		Widget widget = null;
498
		try {
555 chandransh 499
			userServiceClient = new UserContextServiceClient();
500
			client = userServiceClient.getClient();
773 rajveer 501
			if(widgetType == WidgetType.MY_RESEARCH && userId != 0){
502
				widget = client.getMyResearch(userId);
503
			}
504
			if(widgetType == WidgetType.BROWSE_HISTORY){
505
				widget = client.getBrowseHistory(userId);
506
			}
507
 
507 rajveer 508
		} catch (Exception e) {
509
			e.printStackTrace();
762 rajveer 510
		}finally{
1386 vikas 511
			if (userServiceClient != null) {
512
				userServiceClient.closeConnection();
513
			}
507 rajveer 514
		}
515
 
516
 
517
		List<Map<String, String>> itemDetails = null;
518
 
519
		if(widget != null){
520
			List<WidgetItem> items = widget.getItems();
521
			itemDetails = new ArrayList<Map<String, String>>();
522
 
523
			for(WidgetItem item: items){
524
				Map<String, String> itemDetail = new HashMap<String, String>();
1110 rajveer 525
				String itemSnippet;
507 rajveer 526
 
1110 rajveer 527
					try {
528
						itemSnippet = FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item.getItem_id() + File.separator + "WidgetSnippet.html");
529
					} catch (IOException e) {
530
						e.printStackTrace();
531
						continue;
532
					}
533
 
507 rajveer 534
				itemDetail.put("ITEM_ID", item.getItem_id()+"");
1110 rajveer 535
				itemDetail.put("ITEM_SNIPPET", itemSnippet);
507 rajveer 536
				itemDetails.add(itemDetail);
537
			}
538
		}else{
539
			System.out.println("widget not found");
540
		}
541
 
542
		VelocityContext context = new VelocityContext();
620 rajveer 543
		context.put("userId", userId);
507 rajveer 544
		context.put("itemDetails", itemDetails);
545
 
546
		return getHtmlFromVelocity("templates/"+templateFile, context);
547
	}
548
 
549
 
650 rajveer 550
	public String getOrderDetailsHtml(long orderId) {
507 rajveer 551
		String htmlString = "";
552
		VelocityContext context = new VelocityContext();
553
		String templateFile = "templates/orderdetails.vm";
554
		Order order = null;
517 rajveer 555
		Date orderedOn = null, deliveryEstimate = null;
843 chandransh 556
		Provider provider = null;
507 rajveer 557
		try{
843 chandransh 558
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
559
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 560
			order = orderClient.getOrder(orderId);
517 rajveer 561
			orderedOn = new Date(order.getCreated_timestamp());
562
			deliveryEstimate = new Date(order.getExpected_delivery_time());
843 chandransh 563
 
564
			if(order.getLogistics_provider_id() != 0){
565
				LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
566
				in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
567
				provider = logisticsClient.getProvider(order.getLogistics_provider_id());
568
			}
507 rajveer 569
		}catch (Exception e){
570
 
571
		}
517 rajveer 572
 
573
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 574
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
507 rajveer 575
		context.put("order", order);
517 rajveer 576
		context.put("orderedOn", dateformat.format(orderedOn));
583 rajveer 577
		context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
843 chandransh 578
		if(provider!=null){
579
			context.put("providerName", provider.getName());
580
		}
581
 
507 rajveer 582
		htmlString = getHtmlFromVelocity(templateFile, context);
583
		return htmlString;
584
	}
585
 
650 rajveer 586
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 587
		String htmlString = "";
588
		VelocityContext context = new VelocityContext();
589
		String templateFile = "templates/myaccount.vm";
590
		List<Order> orders = null;
745 chandransh 591
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 592
		try{
745 chandransh 593
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
594
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 595
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
596
 
745 chandransh 597
			LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
598
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
599
			List<Provider> providers = logisticsClient.getAllProviders();
600
			for(Provider provider: providers)
601
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 602
		}catch (Exception e){
1275 varun.gupt 603
			e.printStackTrace();
507 rajveer 604
		}
741 rajveer 605
		List<String> orderDate = new ArrayList<String>();
606
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 607
		if(orders!=null && !orders.isEmpty()){
762 rajveer 608
			for(Order order: orders){
609
				Date orderedOn = new Date(order.getCreated_timestamp());
610
				orderDate.add(dateformat.format(orderedOn));
611
			}
741 rajveer 612
		}
507 rajveer 613
		context.put("orders", orders);
741 rajveer 614
		context.put("orderDate", orderDate);
745 chandransh 615
		context.put("providerNames", providerNames);
507 rajveer 616
		htmlString = getHtmlFromVelocity(templateFile, context);
617
		return htmlString;
618
	}
619
 
620
 
650 rajveer 621
	public String getLoginDetailsHtml(long userId) {
507 rajveer 622
		String htmlString = "";
623
		VelocityContext context = new VelocityContext();
624
		String templateFile = "templates/logindetails.vm";
625
		String email = "";
626
		try{
762 rajveer 627
			email = getEmailId(userId);
507 rajveer 628
		}catch (Exception e){
629
 
630
		}
631
		context.put("email", email);
632
		htmlString = getHtmlFromVelocity(templateFile, context);
633
		return htmlString;
634
	}
635
 
762 rajveer 636
	public String getEmailId(long userId){
637
		String email = " ";
638
 
639
		try {
640
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
641
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
642
			if(userClient.getUserById(userId).getEmail() != null){
643
				email = userClient.getUserById(userId).getEmail();
644
			}
645
		} catch (UserContextException e) {
646
			e.printStackTrace();
647
		} catch (TException e) {
648
			e.printStackTrace();
649
		} catch (Exception e) {
650
			e.printStackTrace();
651
		}
652
		return email; 
653
	}
654
 
655
 
595 rajveer 656
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 657
		String htmlString = "";
658
		VelocityContext context = new VelocityContext();
659
		String templateFile = "templates/personaldetails.vm";
660
		String email = "";
517 rajveer 661
		String name = "";
569 rajveer 662
		String dateOfBirth = "";
517 rajveer 663
		String sex = "";
664
		String subscribe = "false";
507 rajveer 665
		UserContextServiceClient userContextServiceClient = null;
666
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
667
		try{
555 chandransh 668
			User user = null;
507 rajveer 669
			userContextServiceClient = new UserContextServiceClient();
670
			userClient = userContextServiceClient.getClient();
555 chandransh 671
			user = userClient.getUserById(userId);
507 rajveer 672
 
555 chandransh 673
			email = user.getCommunicationEmail();
674
			name = user.getName();
517 rajveer 675
 
569 rajveer 676
			dateOfBirth = user.getDateOfBirth();
507 rajveer 677
		}catch (Exception e){
555 chandransh 678
			e.printStackTrace();
507 rajveer 679
		}
517 rajveer 680
		context.put("name", name);
681
		context.put("email", email);
569 rajveer 682
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 683
		context.put("subscribe", subscribe);
684
		context.put("sex", sex);
507 rajveer 685
		htmlString = getHtmlFromVelocity(templateFile, context);
686
		return htmlString;
687
	}
688
 
620 rajveer 689
	public String getCompletedOrdersHtml(long userId) {
507 rajveer 690
		String htmlString = "";
691
		VelocityContext context = new VelocityContext();
692
		String templateFile = "templates/completedorders.vm";
693
		TransactionServiceClient transactionServiceClient = null;
694
		in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
1310 varun.gupt 695
 
507 rajveer 696
		List<Order> orders = null;
1310 varun.gupt 697
		List<String> orderDate = new ArrayList<String>();
1316 varun.gupt 698
		Map<Long, String> providerNames = new HashMap<Long, String>();
1310 varun.gupt 699
 
507 rajveer 700
		try{
701
			transactionServiceClient = new TransactionServiceClient();
702
			orderClient = transactionServiceClient.getClient();
703
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), OrderStatus.DELIVERY_SUCCESS);
1316 varun.gupt 704
 
705
			LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
706
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
707
			List<Provider> providers = logisticsClient.getAllProviders();
708
			for(Provider provider: providers)
709
				providerNames.put(provider.getId(), provider.getName());
710
 
1310 varun.gupt 711
			SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
712
 
713
			if(orders != null && !orders.isEmpty()){
714
				for(Order order: orders){
715
					Date orderedOn = new Date(order.getCreated_timestamp());
716
					orderDate.add(dateformat.format(orderedOn));
717
				}
718
			}
507 rajveer 719
		}catch (Exception e){
720
 
721
		}
722
		context.put("orders", orders);
1310 varun.gupt 723
		context.put("orderDate", orderDate);
1316 varun.gupt 724
		context.put("providerNames", providerNames);
1310 varun.gupt 725
 
507 rajveer 726
		htmlString = getHtmlFromVelocity(templateFile, context);
727
		return htmlString;
728
	}
595 rajveer 729
	public String getMyaccountHeaderHtml() {
507 rajveer 730
		String htmlString = "";
731
		VelocityContext context = new VelocityContext();
732
		String templateFile = "templates/myaccountheader.vm";
733
		htmlString = getHtmlFromVelocity(templateFile, context);
734
		return htmlString;
735
	}
736
 
650 rajveer 737
	public String getShippingHeaderHtml() {
507 rajveer 738
		String htmlString = "";
739
		VelocityContext context = new VelocityContext();
740
		String templateFile = "templates/shippingheader.vm";
741
		htmlString = getHtmlFromVelocity(templateFile, context);
742
		return htmlString;
743
	}
744
 
822 vikas 745
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 746
		String htmlString = "";
747
		VelocityContext context = new VelocityContext();
748
		String templateFile = "templates/shippingaddressdetails.vm";
749
		long defaultAddressId = 0;
517 rajveer 750
		List<Address> addresses = null;
507 rajveer 751
 
752
		UserContextServiceClient userContextServiceClient = null;
753
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
754
		try {
755
			userContextServiceClient = new UserContextServiceClient();
756
			userClient = userContextServiceClient.getClient();
620 rajveer 757
 
758
			addresses = userClient.getAllAddressesForUser(userId);
759
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 760
		} catch (Exception e) {
761
			e.printStackTrace();
762
		}
517 rajveer 763
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 764
		context.put("addresses", addresses);
822 vikas 765
		context.put("errorMsg", errorMsg);
507 rajveer 766
 
767
		htmlString = getHtmlFromVelocity(templateFile, context);
768
		return htmlString;
769
	}
770
 
650 rajveer 771
	public String getShippingDetailsHtml(long cartId, String errorMsg) {
507 rajveer 772
		String htmlString = "";
773
		VelocityContext context = new VelocityContext();
774
		String templateFile = "templates/shippingdetails.vm";
517 rajveer 775
		List<Map<String,String>> items = null;
507 rajveer 776
		double totalamount= 0.0;
517 rajveer 777
		List<Address> addresses = null;
778
		long defaultAddressId = 0;
555 chandransh 779
 
507 rajveer 780
		CatalogServiceClient catalogServiceClient  = null;
781
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
782
		UserContextServiceClient userContextServiceClient = null;
783
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
784
 
785
		try {
786
			catalogServiceClient = new CatalogServiceClient();
787
			catalogClient = catalogServiceClient.getClient();
788
			userContextServiceClient = new UserContextServiceClient();
789
			userClient = userContextServiceClient.getClient();
790
 
791
 
555 chandransh 792
			Cart cart = userClient.getCart(cartId);
507 rajveer 793
			List<Line> lineItems = cart.getLines();
794
 
517 rajveer 795
			if( ! lineItems.isEmpty())
796
			{
797
				items = new ArrayList<Map<String,String>>();
507 rajveer 798
 
517 rajveer 799
				for (Line line : lineItems) {
800
					Map<String, String> itemdetail = new HashMap<String, String>();
637 rajveer 801
					Item item = catalogClient.getItem(line.getItemId());
962 chandransh 802
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
517 rajveer 803
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
637 rajveer 804
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
805
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
517 rajveer 806
 
807
					itemdetail.put("ITEM_NAME", itemName);
808
					itemdetail.put("ITEM_ID", line.getItemId()+"");
809
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
810
					itemdetail.put("MRP", ((int)item.getMrp())+"");
536 rajveer 811
					itemdetail.put("SELLING_PRICE", (int)item.getSellingPrice()+"");
812
					itemdetail.put("TOTAL_PRICE", (int)(item.getSellingPrice()*line.getQuantity())+"");
615 chandransh 813
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
536 rajveer 814
					totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
517 rajveer 815
					items.add(itemdetail);				
816
				}
507 rajveer 817
			}
818
 
637 rajveer 819
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
650 rajveer 820
			if(cart.isSetAddressId()){
821
				defaultAddressId = cart.getAddressId();
822
			}else{
823
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
824
			}
507 rajveer 825
 
826
		}catch (Exception e){
827
			e.printStackTrace();
828
		}
829
 
830
		context.put("items", items);
536 rajveer 831
		context.put("totalamount", (int)totalamount+"");
507 rajveer 832
		context.put("addresses", addresses);
517 rajveer 833
		context.put("defaultAddressId", defaultAddressId+"");
572 chandransh 834
		context.put("errorMsg", errorMsg);
507 rajveer 835
		htmlString = getHtmlFromVelocity(templateFile, context);
836
		return htmlString;
837
	}
838
 
839
 
650 rajveer 840
	public String getCartHeaderHtml() {
507 rajveer 841
		String htmlString = "";
842
		VelocityContext context = new VelocityContext();
843
		String templateFile = "templates/cartheader.vm";
844
		htmlString = getHtmlFromVelocity(templateFile, context);
845
		return htmlString;
846
	}
847
 
848
 
786 rajveer 849
	public String getCartDetailsHtml(long userId, long cartId, String errorMsg) {
507 rajveer 850
		String htmlString = "";
851
		VelocityContext context = new VelocityContext();
852
		String templateFile = "templates/cartdetails.vm";
517 rajveer 853
		List<Map<String,String>> items = null;
507 rajveer 854
		double totalamount= 0.0;
855
 
555 chandransh 856
		UserContextServiceClient userServiceClient = null;
857
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
507 rajveer 858
		CatalogServiceClient catalogServiceClient  = null;
859
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
786 rajveer 860
		String pincode = "110001";
507 rajveer 861
		try {
862
			catalogServiceClient = new CatalogServiceClient();
863
			catalogClient = catalogServiceClient.getClient();
555 chandransh 864
			userServiceClient = new UserContextServiceClient();
865
			userClient = userServiceClient.getClient();
507 rajveer 866
 
786 rajveer 867
			pincode = userClient.getDefaultPincode(userId);
555 chandransh 868
			Cart cart = userClient.getCart(cartId);
507 rajveer 869
			List<Line> lineItems = cart.getLines();
517 rajveer 870
			if(lineItems.size() != 0){
871
				items = new ArrayList<Map<String,String>>();
872
				for (Line line : lineItems) {
873
 
874
					Map<String, String> itemdetail = new HashMap<String, String>();
637 rajveer 875
					Item item = catalogClient.getItem(line.getItemId());
517 rajveer 876
 
962 chandransh 877
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
517 rajveer 878
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
637 rajveer 879
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
880
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
517 rajveer 881
 
882
					itemdetail.put("ITEM_NAME", itemName);
883
					System.out.println(itemdetail.get("ITEM_NAME"));
884
					itemdetail.put("ITEM_ID", line.getItemId()+"");
805 rajveer 885
					itemdetail.put("CATALOG_ID", item.getCatalogItemId()+"");
517 rajveer 886
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
887
					itemdetail.put("MRP", ((int)item.getMrp())+"");
888
					itemdetail.put("SELLING_PRICE", ((int)item.getSellingPrice())+"");
889
					itemdetail.put("TOTAL_PRICE", ((int)((item.getSellingPrice()*line.getQuantity())))+"");
615 chandransh 890
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
517 rajveer 891
					totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
892
					items.add(itemdetail);				
507 rajveer 893
				}
894
			}
895
 
896
		}catch (Exception e){
897
			e.printStackTrace();
898
		}
899
 
900
		context.put("items", items);
517 rajveer 901
		context.put("totalamount", ((int)totalamount)+"");
572 chandransh 902
		context.put("errorMsg", errorMsg);
786 rajveer 903
		context.put("pincode", pincode);
507 rajveer 904
		htmlString = getHtmlFromVelocity(templateFile, context);
905
		return htmlString;
906
	}
907
 
908
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
909
		Properties p = new Properties();
910
		p.setProperty("resource.loader", "class");
911
		p.setProperty("class.resource.loader.class",
912
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 913
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
914
 
507 rajveer 915
		try {
916
			Velocity.init(p);
762 rajveer 917
			//Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
507 rajveer 918
			Template template = Velocity.getTemplate(templateFile);
919
			if(template != null) {
920
				StringWriter writer = new StringWriter();
921
				template.merge(context, writer);
922
				writer.flush();
923
				writer.close();
924
				return writer.toString();
925
			}
926
 
927
			} catch (ResourceNotFoundException e) {
928
				// TODO Auto-generated catch block
929
				e.printStackTrace();
930
			} catch (ParseErrorException e) {
931
				// TODO Auto-generated catch block
932
				e.printStackTrace();
933
			} catch (MethodInvocationException e) {
934
				// TODO Auto-generated catch block
935
				e.printStackTrace();
936
			} catch (IOException e) {
937
				// TODO Auto-generated catch block
938
				e.printStackTrace();
939
			} catch (Exception e) {
940
				// TODO Auto-generated catch block
941
				e.printStackTrace();
942
			}
943
 
944
		return null;
945
	}
946
}
947