Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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
	}
75
 
637 rajveer 76
	public String getLoginHeaderHtml() {
77
		String htmlString = "";
78
		VelocityContext context = new VelocityContext();
79
		String templateFile = "templates/loginheader.vm";
80
		htmlString = getHtmlFromVelocity(templateFile, context);
81
		return htmlString;
82
	}
83
 
507 rajveer 84
 
650 rajveer 85
	public String getMainBannerHtml() {
507 rajveer 86
		String htmlString = "";
87
		VelocityContext context = new VelocityContext();
88
		String templateFile = "templates/mainbanner.vm";
89
		htmlString = getHtmlFromVelocity(templateFile, context);
90
		return htmlString;
91
	}
92
 
93
 
650 rajveer 94
	public String getBestSellersHtml() {
507 rajveer 95
		String htmlString = "";
96
		VelocityContext context = new VelocityContext();
97
		String templateFile = "templates/bestsellers.vm";
98
 
99
		CatalogServiceClient catalogServiceClient = null;
100
		Client client = null;
101
 
102
		try {
103
			catalogServiceClient = new CatalogServiceClient();
104
			client = catalogServiceClient.getClient();
627 rajveer 105
			List<Long> items = client.getBestSellersCatalogIds(1, 4, -1);
507 rajveer 106
			List<String> itemList = new ArrayList<String>();
107
			for(Long item: items){
517 rajveer 108
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 109
			}
110
			context.put("itemList", itemList);
111
 
112
		} catch(Exception e){
113
 
114
		}
762 rajveer 115
		/*
116
		finally{
117
			catalogServiceClient.closeConnection();
118
		}
119
		*/
507 rajveer 120
 
121
 
122
		htmlString = getHtmlFromVelocity(templateFile, context);
517 rajveer 123
		return htmlString;	
124
	}
507 rajveer 125
 
126
 
650 rajveer 127
	public String getLatestArrivalsHtml() {
507 rajveer 128
		String htmlString = "";
129
		VelocityContext context = new VelocityContext();
130
		String templateFile = "templates/latestarrivals.vm";
131
 
132
		CatalogServiceClient catalogServiceClient = null;
133
		Client client = null;
134
 
135
		try {
136
			catalogServiceClient = new CatalogServiceClient();
137
			client = catalogServiceClient.getClient();
627 rajveer 138
			List<Long> items = client.getLatestArrivalsCatalogIds(1,4,-1);
507 rajveer 139
			List<String> itemList = new ArrayList<String>();
140
			for(Long item: items){
517 rajveer 141
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 142
			}
143
			context.put("itemList", itemList);
144
 
145
		} catch(Exception e){
146
 
762 rajveer 147
		}finally{
148
			catalogServiceClient.closeConnection();
507 rajveer 149
		}
150
 
151
		htmlString = getHtmlFromVelocity(templateFile, context);
152
		return htmlString;
153
	}
154
 
155
 
650 rajveer 156
	public String getBestDealsHtml() {
507 rajveer 157
		String htmlString = "";
158
		VelocityContext context = new VelocityContext();
159
		String templateFile = "templates/bestdeals.vm";
160
 
161
		CatalogServiceClient catalogServiceClient = null;
162
		Client client = null;
163
 
164
		try {
165
			catalogServiceClient = new CatalogServiceClient();
166
			client = catalogServiceClient.getClient();
627 rajveer 167
			List<Long> items = client.getBestDealsCatalogIds(1,4,-1);
507 rajveer 168
			List<String> itemList = new ArrayList<String>();
169
			for(Long item: items){
517 rajveer 170
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 171
			}
172
			context.put("itemList", itemList);
173
 
174
		} catch(Exception e){
175
 
762 rajveer 176
		}finally{
177
			catalogServiceClient.closeConnection();
507 rajveer 178
		}
179
 
180
		htmlString = getHtmlFromVelocity(templateFile, context);
181
		return htmlString;
182
	}
183
 
184
 
517 rajveer 185
	public String getFooterHtml() {
507 rajveer 186
		String htmlString = "";
187
		VelocityContext context = new VelocityContext();
188
		String templateFile = "templates/footer.vm";
189
		htmlString = getHtmlFromVelocity(templateFile, context);
190
		return htmlString;
191
	}
192
 
193
 
194
 
620 rajveer 195
	public String getAccessoriesHtml(long productId) {
637 rajveer 196
		return "";
197
		//return getWidgetDiv(0, WidgetType.ACCESSORIES, "accessories.vm");
507 rajveer 198
	}
199
 
200
 
201
 
620 rajveer 202
	public String getSimilarProductsHtml(long productId) {
637 rajveer 203
		return "";
204
		//return getWidgetDiv(0, WidgetType.SIMILAR_ITEMS, "similaritems.vm");
507 rajveer 205
	}
206
 
207
 
208
 
620 rajveer 209
	public String getRecommendationsHtml() {
637 rajveer 210
		return "";
211
		//return getWidgetDiv( 0, WidgetType.RECOMMENDED_ITEMS, "recommendations.vm");
507 rajveer 212
	}
213
 
214
 
215
 
555 chandransh 216
	public String getMyResearchHtml(long userId, boolean isLoggedIn) {
217
		if(isLoggedIn)
218
			return getWidgetDiv(userId, WidgetType.MY_RESEARCH, "myresearch.vm");
219
		else
220
			return getWidgetDiv(0, WidgetType.MY_RESEARCH, "myresearch.vm");
507 rajveer 221
	}
222
 
569 rajveer 223
	public String getBrowseHistoryHtml(long userId, boolean isLoggedIn) {
768 rajveer 224
		return getWidgetDiv(userId, WidgetType.BROWSE_HISTORY, "browsehistory.vm");
507 rajveer 225
	}
226
 
517 rajveer 227
	public String getCustomerServiceHtml() {
507 rajveer 228
		String htmlString = "";
229
		VelocityContext context = new VelocityContext();
230
		String templateFile = "templates/customerservice.vm";
231
		htmlString = getHtmlFromVelocity(templateFile, context);
232
		return htmlString;
233
	}
234
 
235
 
236
 
620 rajveer 237
	public String getReviewsHtml(long productId) {
507 rajveer 238
		String htmlString = "";
239
		VelocityContext context = new VelocityContext();
240
		Map<String, String> params = new HashMap<String, String>();
241
		params.put("PRODUCT_ID", productId+"");
242
		context.put("params", params);
243
		String templateFile = "templates/reviews.vm";
244
		htmlString = getHtmlFromVelocity(templateFile, context);
245
		return htmlString;
246
	}
247
 
248
 
249
 
620 rajveer 250
	public String getLocatorHtml() {
507 rajveer 251
		String htmlString = "";
252
		VelocityContext context = new VelocityContext();
253
		String templateFile = "templates/locator.vm";
254
		htmlString = getHtmlFromVelocity(templateFile, context);
255
		return htmlString;
256
	}
257
 
258
 
620 rajveer 259
	public String getSlideGuideHtml(long productId) {
507 rajveer 260
		StringBuilder htmlString = new StringBuilder();
517 rajveer 261
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideGuide.html";
507 rajveer 262
		File f = new File(filename);
263
 
264
 
265
		FileInputStream fis = null;
266
		try {
267
			fis = new FileInputStream(f);
268
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
269
			String line;
270
			while((line = br.readLine()) != null){
271
				htmlString.append(line+"\n");
272
			}
273
		} catch (FileNotFoundException e) {
274
			// TODO Auto-generated catch block
275
			e.printStackTrace();
276
		} catch (IOException e) {
277
			// TODO Auto-generated catch block
278
			e.printStackTrace();
279
		}
280
		finally {
281
			if(fis != null) {
282
				try {
283
					fis.close();
284
				} catch (IOException e) {
285
					// TODO Auto-generated catch block
286
					e.printStackTrace();
287
				}
288
			}
289
		}
290
 
291
		return htmlString.toString();
292
	}
293
 
517 rajveer 294
	public String getProductSummaryHtml(long productId) {
507 rajveer 295
		StringBuilder htmlString = new StringBuilder();
517 rajveer 296
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductDetail.html";
507 rajveer 297
		File f = new File(filename);
298
 
299
 
300
		FileInputStream fis = null;
301
		try {
302
			fis = new FileInputStream(f);
303
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
304
			String line;
305
			while((line = br.readLine()) != null){
306
				htmlString.append(line+"\n");
307
			}
308
		} catch (FileNotFoundException e) {
309
			// TODO Auto-generated catch block
310
			e.printStackTrace();
311
		} catch (IOException e) {
312
			// TODO Auto-generated catch block
313
			e.printStackTrace();
314
		}
315
		finally {
316
			if(fis != null) {
317
				try {
318
					fis.close();
319
				} catch (IOException e) {
320
					// TODO Auto-generated catch block
321
					e.printStackTrace();
322
				}
323
			}
324
		}
325
 
326
		return htmlString.toString();
327
	}
328
 
329
 
620 rajveer 330
	public String getSocialUtilsHtml(long productId) {
507 rajveer 331
		String htmlString = "";
332
		VelocityContext context = new VelocityContext();
333
		Map<String, String> params = new HashMap<String, String>();
334
		params.put("PRODUCT_ID", productId+"");
335
		String templateFile = "templates/socialutils.vm";
336
		context.put("params", params);
337
		htmlString = getHtmlFromVelocity(templateFile, context);
338
		return htmlString;
339
	}
340
 
341
 
517 rajveer 342
	public String getMainMenuHtml() {
507 rajveer 343
		String htmlString = "";
344
		VelocityContext context = new VelocityContext();
345
		String templateFile = "templates/mainmenu.vm";
346
		htmlString = getHtmlFromVelocity(templateFile, context);
347
		return htmlString;
348
	}
349
 
350
 
517 rajveer 351
	public String getSearchBarHtml(long itemCounts, long categoryId) {
507 rajveer 352
		String htmlString = "";
353
		VelocityContext context = new VelocityContext();
354
		String templateFile = "templates/searchbar.vm";
550 rajveer 355
 
356
		context.put("itemCount", itemCounts+"");
357
		context.put("categoryId", categoryId+"");
358
 
507 rajveer 359
		htmlString = getHtmlFromVelocity(templateFile, context);
360
		return htmlString;
361
	}
362
 
363
 
364
 
555 chandransh 365
	public String getHeaderHtml(boolean isLoggedIn, String  userName) {
550 rajveer 366
		VelocityContext context = new VelocityContext();
555 chandransh 367
		if (isLoggedIn) {
368
			context.put("LOGGED_IN", "TRUE");
369
			context.put("WELCOME_MESSAGE", "Hi, " + userName);
370
		} else {
550 rajveer 371
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Shop2020");
555 chandransh 372
		}		
507 rajveer 373
 
374
		String templateFile = "templates/header.vm";
550 rajveer 375
 
590 chandransh 376
		return getHtmlFromVelocity(templateFile, context);
507 rajveer 377
	}
378
 
379
 
380
 
650 rajveer 381
	public	String getWidgetDiv(long userId, WidgetType widgetType, String templateFile){
507 rajveer 382
 
555 chandransh 383
		UserContextServiceClient userServiceClient = null;
384
		in.shop2020.model.v1.user.UserContextService.Client client = null;
507 rajveer 385
		Widget widget = null;
386
		try {
555 chandransh 387
			userServiceClient = new UserContextServiceClient();
388
			client = userServiceClient.getClient();
773 rajveer 389
			if(widgetType == WidgetType.MY_RESEARCH && userId != 0){
390
				widget = client.getMyResearch(userId);
391
			}
392
			if(widgetType == WidgetType.BROWSE_HISTORY){
393
				widget = client.getBrowseHistory(userId);
394
			}
395
 
507 rajveer 396
		} catch (Exception e) {
397
			e.printStackTrace();
762 rajveer 398
		}finally{
399
			userServiceClient.closeConnection();
507 rajveer 400
		}
401
 
402
 
403
		List<Map<String, String>> itemDetails = null;
404
 
405
		if(widget != null){
406
			List<WidgetItem> items = widget.getItems();
407
			itemDetails = new ArrayList<Map<String, String>>();
408
 
409
			for(WidgetItem item: items){
410
				Map<String, String> itemDetail = new HashMap<String, String>();
411
 
412
				itemDetail.put("ITEM_ID", item.getItem_id()+"");
413
				try {
517 rajveer 414
					itemDetail.put("ITEM_SNIPPET", FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item.getItem_id() + File.separator + "WidgetSnippet.html"));
507 rajveer 415
				} catch (Exception e) {
416
					// TODO Auto-generated catch block
417
					e.printStackTrace();
418
				}
419
				itemDetails.add(itemDetail);
420
			}
421
		}else{
422
			System.out.println("widget not found");
423
		}
424
 
425
		VelocityContext context = new VelocityContext();
620 rajveer 426
		context.put("userId", userId);
507 rajveer 427
		context.put("itemDetails", itemDetails);
428
 
429
		return getHtmlFromVelocity("templates/"+templateFile, context);
430
	}
431
 
432
 
433
 
434
 
650 rajveer 435
	public String getOrderDetailsHtml(long orderId) {
507 rajveer 436
		String htmlString = "";
437
		VelocityContext context = new VelocityContext();
438
		String templateFile = "templates/orderdetails.vm";
439
		TransactionServiceClient transactionServiceClient = null;
440
		in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
441
		Order order = null;
517 rajveer 442
		Date orderedOn = null, deliveryEstimate = null;
507 rajveer 443
		try{
444
			transactionServiceClient = new TransactionServiceClient();
445
			orderClient = transactionServiceClient.getClient();
446
			order = orderClient.getOrder(orderId);
517 rajveer 447
			orderedOn = new Date(order.getCreated_timestamp());
448
			deliveryEstimate = new Date(order.getExpected_delivery_time());
507 rajveer 449
		}catch (Exception e){
450
 
451
		}
517 rajveer 452
 
453
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 454
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
507 rajveer 455
		context.put("order", order);
517 rajveer 456
		context.put("orderedOn", dateformat.format(orderedOn));
583 rajveer 457
		context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
517 rajveer 458
 
459
 
507 rajveer 460
		htmlString = getHtmlFromVelocity(templateFile, context);
461
		return htmlString;
462
	}
463
 
650 rajveer 464
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 465
		String htmlString = "";
466
		VelocityContext context = new VelocityContext();
467
		String templateFile = "templates/myaccount.vm";
468
		List<Order> orders = null;
745 chandransh 469
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 470
		try{
745 chandransh 471
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
472
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 473
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
474
 
745 chandransh 475
			LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
476
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
477
			List<Provider> providers = logisticsClient.getAllProviders();
478
			for(Provider provider: providers)
479
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 480
		}catch (Exception e){
481
 
482
		}
741 rajveer 483
		List<String> orderDate = new ArrayList<String>();
484
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
762 rajveer 485
		if(orders==null || orders.isEmpty()){
486
			for(Order order: orders){
487
				Date orderedOn = new Date(order.getCreated_timestamp());
488
				orderDate.add(dateformat.format(orderedOn));
489
			}
741 rajveer 490
		}
507 rajveer 491
		context.put("orders", orders);
741 rajveer 492
		context.put("orderDate", orderDate);
745 chandransh 493
		context.put("providerNames", providerNames);
507 rajveer 494
		htmlString = getHtmlFromVelocity(templateFile, context);
495
		return htmlString;
496
	}
497
 
498
 
650 rajveer 499
	public String getLoginDetailsHtml(long userId) {
507 rajveer 500
		String htmlString = "";
501
		VelocityContext context = new VelocityContext();
502
		String templateFile = "templates/logindetails.vm";
503
		String email = "";
504
		try{
762 rajveer 505
			email = getEmailId(userId);
507 rajveer 506
		}catch (Exception e){
507
 
508
		}
509
		context.put("email", email);
510
		htmlString = getHtmlFromVelocity(templateFile, context);
511
		return htmlString;
512
	}
513
 
762 rajveer 514
	public String getEmailId(long userId){
515
		String email = " ";
516
 
517
		try {
518
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
519
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
520
			if(userClient.getUserById(userId).getEmail() != null){
521
				email = userClient.getUserById(userId).getEmail();
522
			}
523
		} catch (UserContextException e) {
524
			e.printStackTrace();
525
		} catch (TException e) {
526
			e.printStackTrace();
527
		} catch (Exception e) {
528
			e.printStackTrace();
529
		}
530
		return email; 
531
	}
532
 
533
 
595 rajveer 534
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 535
		String htmlString = "";
536
		VelocityContext context = new VelocityContext();
537
		String templateFile = "templates/personaldetails.vm";
538
		String email = "";
517 rajveer 539
		String name = "";
569 rajveer 540
		String dateOfBirth = "";
517 rajveer 541
		String sex = "";
542
		String subscribe = "false";
507 rajveer 543
		UserContextServiceClient userContextServiceClient = null;
544
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
545
		try{
555 chandransh 546
			User user = null;
507 rajveer 547
			userContextServiceClient = new UserContextServiceClient();
548
			userClient = userContextServiceClient.getClient();
555 chandransh 549
			user = userClient.getUserById(userId);
507 rajveer 550
 
555 chandransh 551
			email = user.getCommunicationEmail();
552
			name = user.getName();
517 rajveer 553
 
569 rajveer 554
			dateOfBirth = user.getDateOfBirth();
507 rajveer 555
		}catch (Exception e){
555 chandransh 556
			e.printStackTrace();
507 rajveer 557
		}
517 rajveer 558
		context.put("name", name);
559
		context.put("email", email);
569 rajveer 560
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 561
		context.put("subscribe", subscribe);
562
		context.put("sex", sex);
507 rajveer 563
		htmlString = getHtmlFromVelocity(templateFile, context);
564
		return htmlString;
565
	}
566
 
620 rajveer 567
	public String getCompletedOrdersHtml(long userId) {
507 rajveer 568
		String htmlString = "";
569
		VelocityContext context = new VelocityContext();
570
		String templateFile = "templates/completedorders.vm";
571
		TransactionServiceClient transactionServiceClient = null;
572
		in.shop2020.model.v1.order.TransactionService.Client orderClient = null;
573
		List<Order> orders = null;
574
		try{
575
			transactionServiceClient = new TransactionServiceClient();
576
			orderClient = transactionServiceClient.getClient();
577
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), OrderStatus.DELIVERY_SUCCESS);
578
 
579
		}catch (Exception e){
580
 
581
		}
582
		context.put("orders", orders);
583
		htmlString = getHtmlFromVelocity(templateFile, context);
584
		return htmlString;
585
	}
595 rajveer 586
	public String getMyaccountHeaderHtml() {
507 rajveer 587
		String htmlString = "";
588
		VelocityContext context = new VelocityContext();
589
		String templateFile = "templates/myaccountheader.vm";
590
		htmlString = getHtmlFromVelocity(templateFile, context);
591
		return htmlString;
592
	}
593
 
650 rajveer 594
	public String getShippingHeaderHtml() {
507 rajveer 595
		String htmlString = "";
596
		VelocityContext context = new VelocityContext();
597
		String templateFile = "templates/shippingheader.vm";
598
		htmlString = getHtmlFromVelocity(templateFile, context);
599
		return htmlString;
600
	}
601
 
650 rajveer 602
	public String getShippingAddressDetailsHtml(long userId){
507 rajveer 603
		String htmlString = "";
604
		VelocityContext context = new VelocityContext();
605
		String templateFile = "templates/shippingaddressdetails.vm";
606
		long defaultAddressId = 0;
517 rajveer 607
		List<Address> addresses = null;
507 rajveer 608
 
609
		UserContextServiceClient userContextServiceClient = null;
610
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
611
		try {
612
			userContextServiceClient = new UserContextServiceClient();
613
			userClient = userContextServiceClient.getClient();
620 rajveer 614
 
615
			addresses = userClient.getAllAddressesForUser(userId);
616
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 617
		} catch (Exception e) {
618
			e.printStackTrace();
619
		}
517 rajveer 620
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 621
		context.put("addresses", addresses);
622
 
623
		htmlString = getHtmlFromVelocity(templateFile, context);
624
		return htmlString;
625
	}
626
 
650 rajveer 627
	public String getShippingDetailsHtml(long cartId, String errorMsg) {
507 rajveer 628
		String htmlString = "";
629
		VelocityContext context = new VelocityContext();
630
		String templateFile = "templates/shippingdetails.vm";
517 rajveer 631
		List<Map<String,String>> items = null;
507 rajveer 632
		double totalamount= 0.0;
517 rajveer 633
		List<Address> addresses = null;
634
		long defaultAddressId = 0;
555 chandransh 635
 
507 rajveer 636
		CatalogServiceClient catalogServiceClient  = null;
637
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
638
		UserContextServiceClient userContextServiceClient = null;
639
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
640
 
641
		try {
642
			catalogServiceClient = new CatalogServiceClient();
643
			catalogClient = catalogServiceClient.getClient();
644
			userContextServiceClient = new UserContextServiceClient();
645
			userClient = userContextServiceClient.getClient();
646
 
647
 
555 chandransh 648
			Cart cart = userClient.getCart(cartId);
507 rajveer 649
			List<Line> lineItems = cart.getLines();
650
 
517 rajveer 651
			if( ! lineItems.isEmpty())
652
			{
653
				items = new ArrayList<Map<String,String>>();
507 rajveer 654
 
517 rajveer 655
				for (Line line : lineItems) {
656
					Map<String, String> itemdetail = new HashMap<String, String>();
637 rajveer 657
					Item item = catalogClient.getItem(line.getItemId());
517 rajveer 658
					String itemName = ((item.getManufacturerName() != null) ? item.getManufacturerName() + " " : "")
659
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
637 rajveer 660
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
661
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
517 rajveer 662
 
663
					itemdetail.put("ITEM_NAME", itemName);
664
					itemdetail.put("ITEM_ID", line.getItemId()+"");
665
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
666
					itemdetail.put("MRP", ((int)item.getMrp())+"");
536 rajveer 667
					itemdetail.put("SELLING_PRICE", (int)item.getSellingPrice()+"");
668
					itemdetail.put("TOTAL_PRICE", (int)(item.getSellingPrice()*line.getQuantity())+"");
615 chandransh 669
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
536 rajveer 670
					totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
517 rajveer 671
					items.add(itemdetail);				
672
				}
507 rajveer 673
			}
674
 
637 rajveer 675
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
650 rajveer 676
			if(cart.isSetAddressId()){
677
				defaultAddressId = cart.getAddressId();
678
			}else{
679
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
680
			}
507 rajveer 681
 
682
		}catch (Exception e){
683
			e.printStackTrace();
684
		}
685
 
686
		context.put("items", items);
536 rajveer 687
		context.put("totalamount", (int)totalamount+"");
507 rajveer 688
		context.put("addresses", addresses);
517 rajveer 689
		context.put("defaultAddressId", defaultAddressId+"");
572 chandransh 690
		context.put("errorMsg", errorMsg);
507 rajveer 691
		htmlString = getHtmlFromVelocity(templateFile, context);
692
		return htmlString;
693
	}
694
 
695
 
650 rajveer 696
	public String getCartHeaderHtml() {
507 rajveer 697
		String htmlString = "";
698
		VelocityContext context = new VelocityContext();
699
		String templateFile = "templates/cartheader.vm";
700
		htmlString = getHtmlFromVelocity(templateFile, context);
701
		return htmlString;
702
	}
703
 
704
 
650 rajveer 705
	public String getCartDetailsHtml(long cartId, String errorMsg) {
507 rajveer 706
		String htmlString = "";
707
		VelocityContext context = new VelocityContext();
708
		String templateFile = "templates/cartdetails.vm";
517 rajveer 709
		List<Map<String,String>> items = null;
507 rajveer 710
		double totalamount= 0.0;
711
 
555 chandransh 712
		UserContextServiceClient userServiceClient = null;
713
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
507 rajveer 714
		CatalogServiceClient catalogServiceClient  = null;
715
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
716
 
717
		try {
718
			catalogServiceClient = new CatalogServiceClient();
719
			catalogClient = catalogServiceClient.getClient();
555 chandransh 720
			userServiceClient = new UserContextServiceClient();
721
			userClient = userServiceClient.getClient();
507 rajveer 722
 
555 chandransh 723
			Cart cart = userClient.getCart(cartId);
507 rajveer 724
			List<Line> lineItems = cart.getLines();
517 rajveer 725
			if(lineItems.size() != 0){
726
				items = new ArrayList<Map<String,String>>();
727
				for (Line line : lineItems) {
728
 
729
					Map<String, String> itemdetail = new HashMap<String, String>();
637 rajveer 730
					Item item = catalogClient.getItem(line.getItemId());
517 rajveer 731
 
732
					String itemName = ((item.getManufacturerName() != null) ? item.getManufacturerName() + " " : "")
733
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
637 rajveer 734
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
735
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
517 rajveer 736
 
737
					itemdetail.put("ITEM_NAME", itemName);
738
					System.out.println(itemdetail.get("ITEM_NAME"));
739
					itemdetail.put("ITEM_ID", line.getItemId()+"");
740
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
741
					itemdetail.put("MRP", ((int)item.getMrp())+"");
742
					itemdetail.put("SELLING_PRICE", ((int)item.getSellingPrice())+"");
743
					itemdetail.put("TOTAL_PRICE", ((int)((item.getSellingPrice()*line.getQuantity())))+"");
615 chandransh 744
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
517 rajveer 745
					totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
746
					items.add(itemdetail);				
507 rajveer 747
				}
748
			}
749
 
750
		}catch (Exception e){
751
			e.printStackTrace();
752
		}
753
 
754
		context.put("items", items);
517 rajveer 755
		context.put("totalamount", ((int)totalamount)+"");
572 chandransh 756
		context.put("errorMsg", errorMsg);
507 rajveer 757
		htmlString = getHtmlFromVelocity(templateFile, context);
758
		return htmlString;
759
	}
760
 
761
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
762
		Properties p = new Properties();
763
		p.setProperty("resource.loader", "class");
764
		p.setProperty("class.resource.loader.class",
765
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
762 rajveer 766
		p.setProperty("file.resource.loader.path",
767
		"/home/FTP-shared/upload/");
768
 
507 rajveer 769
		try {
770
			Velocity.init(p);
762 rajveer 771
			//Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
507 rajveer 772
			Template template = Velocity.getTemplate(templateFile);
773
			if(template != null) {
774
				StringWriter writer = new StringWriter();
775
				template.merge(context, writer);
776
				writer.flush();
777
				writer.close();
778
				return writer.toString();
779
			}
780
 
781
			} catch (ResourceNotFoundException e) {
782
				// TODO Auto-generated catch block
783
				e.printStackTrace();
784
			} catch (ParseErrorException e) {
785
				// TODO Auto-generated catch block
786
				e.printStackTrace();
787
			} catch (MethodInvocationException e) {
788
				// TODO Auto-generated catch block
789
				e.printStackTrace();
790
			} catch (IOException e) {
791
				// TODO Auto-generated catch block
792
				e.printStackTrace();
793
			} catch (Exception e) {
794
				// TODO Auto-generated catch block
795
				e.printStackTrace();
796
			}
797
 
798
		return null;
799
	}
800
 
801
 
517 rajveer 802
	public String getCategoryHeaderSnippet() {
803
		String htmlString = "";
804
		VelocityContext context = new VelocityContext();
805
		String templateFile = "templates/categoryheader.vm";
806
		htmlString = getHtmlFromVelocity(templateFile, context);
807
		return htmlString;
808
	}
809
 
507 rajveer 810
 
811
 
812
}
813