Subversion Repositories SmartDukaan

Rev

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