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