Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.services;
2
 
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.io.StringWriter;
517 rajveer 10
import java.text.SimpleDateFormat;
507 rajveer 11
import java.util.ArrayList;
12
import java.util.Date;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Properties;
17
 
18
import org.apache.thrift.TException;
19
import org.apache.velocity.Template;
20
import org.apache.velocity.VelocityContext;
21
import org.apache.velocity.app.Velocity;
22
import org.apache.velocity.exception.MethodInvocationException;
23
import org.apache.velocity.exception.ParseErrorException;
24
import org.apache.velocity.exception.ResourceNotFoundException;
25
 
26
 
745 chandransh 27
import in.shop2020.logistics.Provider;
507 rajveer 28
import in.shop2020.model.v1.catalog.InventoryServiceException;
29
import in.shop2020.model.v1.catalog.InventoryService.Client;
30
import in.shop2020.model.v1.order.Order;
31
import in.shop2020.model.v1.order.OrderStatus;
32
import in.shop2020.model.v1.user.Address;
555 chandransh 33
import in.shop2020.model.v1.user.User;
762 rajveer 34
import in.shop2020.model.v1.user.UserContextException;
507 rajveer 35
import in.shop2020.serving.utils.*;
36
import in.shop2020.thrift.clients.CatalogServiceClient;
745 chandransh 37
import in.shop2020.thrift.clients.LogisticsServiceClient;
507 rajveer 38
import in.shop2020.thrift.clients.TransactionServiceClient;
39
import in.shop2020.thrift.clients.UserContextServiceClient;
40
 
41
 
42
public class PageLoaderHandler {
43
 
44
 
650 rajveer 45
	public String getMainBannerHtml() {
507 rajveer 46
		String htmlString = "";
47
		VelocityContext context = new VelocityContext();
48
		String templateFile = "templates/mainbanner.vm";
49
		htmlString = getHtmlFromVelocity(templateFile, context);
50
		return htmlString;
51
	}
52
 
53
 
650 rajveer 54
	public String getBestSellersHtml() {
507 rajveer 55
		String htmlString = "";
56
		VelocityContext context = new VelocityContext();
57
		String templateFile = "templates/bestsellers.vm";
58
 
59
		CatalogServiceClient catalogServiceClient = null;
60
		Client client = null;
61
 
62
		try {
63
			catalogServiceClient = new CatalogServiceClient();
64
			client = catalogServiceClient.getClient();
2223 chandransh 65
			List<Long> items = client.getBestSellersCatalogIds(0, 4, null, -1);
507 rajveer 66
			List<String> itemList = new ArrayList<String>();
67
			for(Long item: items){
517 rajveer 68
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 69
			}
70
			context.put("itemList", itemList);
71
 
72
		} catch(Exception e){
73
 
74
		}
75
 
76
		htmlString = getHtmlFromVelocity(templateFile, context);
517 rajveer 77
		return htmlString;	
78
	}
507 rajveer 79
 
80
 
650 rajveer 81
	public String getLatestArrivalsHtml() {
507 rajveer 82
		String htmlString = "";
83
		VelocityContext context = new VelocityContext();
84
		String templateFile = "templates/latestarrivals.vm";
85
 
86
		CatalogServiceClient catalogServiceClient = null;
87
		Client client = null;
88
 
89
		try {
90
			catalogServiceClient = new CatalogServiceClient();
91
			client = catalogServiceClient.getClient();
2223 chandransh 92
			List<Long> items = client.getLatestArrivalsCatalogIds(0, 4, null, 10003);
507 rajveer 93
			List<String> itemList = new ArrayList<String>();
94
			for(Long item: items){
968 chandransh 95
				try{
96
					itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
97
				}catch(IOException ioex){
98
					ioex.printStackTrace();
99
				}
507 rajveer 100
			}
101
			context.put("itemList", itemList);
102
 
103
		} catch(Exception e){
968 chandransh 104
			e.printStackTrace();
507 rajveer 105
		}
106
 
107
		htmlString = getHtmlFromVelocity(templateFile, context);
108
		return htmlString;
109
	}
110
 
111
 
650 rajveer 112
	public String getBestDealsHtml() {
507 rajveer 113
		String htmlString = "";
114
		VelocityContext context = new VelocityContext();
115
		String templateFile = "templates/bestdeals.vm";
116
 
117
		CatalogServiceClient catalogServiceClient = null;
118
		Client client = null;
119
 
120
		try {
121
			catalogServiceClient = new CatalogServiceClient();
122
			client = catalogServiceClient.getClient();
1923 rajveer 123
			List<Long> items = client.getBestDealsCatalogIds(0,4, null, -1);
507 rajveer 124
			List<String> itemList = new ArrayList<String>();
125
			for(Long item: items){
517 rajveer 126
				itemList.add(FileUtils.read( Utils.EXPORT_ENTITIES_PATH + item + File.separator +"HomeSnippet.html"));
507 rajveer 127
			}
128
			context.put("itemList", itemList);
129
 
786 rajveer 130
		} catch (InventoryServiceException e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		} catch (TException e) {
134
			// TODO Auto-generated catch block
135
			e.printStackTrace();
136
		} catch (Exception e) {
137
			// TODO Auto-generated catch block
138
			e.printStackTrace();
1614 rajveer 139
		} 
507 rajveer 140
 
141
		htmlString = getHtmlFromVelocity(templateFile, context);
142
		return htmlString;
143
	}
144
 
145
 
517 rajveer 146
	public String getFooterHtml() {
507 rajveer 147
		String htmlString = "";
148
		VelocityContext context = new VelocityContext();
149
		String templateFile = "templates/footer.vm";
150
		htmlString = getHtmlFromVelocity(templateFile, context);
151
		return htmlString;
152
	}
153
 
154
 
1934 vikas 155
	public String getMyResearchHtml() {
156
	    String htmlString = "";
157
        VelocityContext context = new VelocityContext();
158
        String templateFile = "templates/myresearch.vm";
159
        htmlString = getHtmlFromVelocity(templateFile, context);
160
        return htmlString;
507 rajveer 161
	}
162
 
1934 vikas 163
	public String getBrowseHistoryHtml() {
164
	    String htmlString = "";
165
        VelocityContext context = new VelocityContext();
166
        String templateFile = "templates/browsehistory.vm";
167
        htmlString = getHtmlFromVelocity(templateFile, context);
168
        return htmlString;
507 rajveer 169
	}
170
 
517 rajveer 171
	public String getCustomerServiceHtml() {
507 rajveer 172
		String htmlString = "";
173
		VelocityContext context = new VelocityContext();
174
		String templateFile = "templates/customerservice.vm";
175
		htmlString = getHtmlFromVelocity(templateFile, context);
176
		return htmlString;
177
	}
178
 
179
 
620 rajveer 180
	public String getSlideGuideHtml(long productId) {
507 rajveer 181
		StringBuilder htmlString = new StringBuilder();
517 rajveer 182
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideGuide.html";
507 rajveer 183
		File f = new File(filename);
184
 
185
 
186
		FileInputStream fis = null;
187
		try {
188
			fis = new FileInputStream(f);
189
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
190
			String line;
191
			while((line = br.readLine()) != null){
192
				htmlString.append(line+"\n");
193
			}
194
		} catch (FileNotFoundException e) {
195
			// TODO Auto-generated catch block
196
			e.printStackTrace();
197
		} catch (IOException e) {
198
			// TODO Auto-generated catch block
199
			e.printStackTrace();
200
		}
201
		finally {
202
			if(fis != null) {
203
				try {
204
					fis.close();
205
				} catch (IOException e) {
206
					// TODO Auto-generated catch block
207
					e.printStackTrace();
208
				}
209
			}
210
		}
211
 
212
		return htmlString.toString();
213
	}
214
 
517 rajveer 215
	public String getProductSummaryHtml(long productId) {
507 rajveer 216
		StringBuilder htmlString = new StringBuilder();
517 rajveer 217
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductDetail.html";
507 rajveer 218
		File f = new File(filename);
219
 
220
 
221
		FileInputStream fis = null;
222
		try {
223
			fis = new FileInputStream(f);
224
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
225
			String line;
226
			while((line = br.readLine()) != null){
227
				htmlString.append(line+"\n");
228
			}
229
		} catch (FileNotFoundException e) {
230
			// TODO Auto-generated catch block
231
			e.printStackTrace();
232
		} catch (IOException e) {
233
			// TODO Auto-generated catch block
234
			e.printStackTrace();
235
		}
236
		finally {
237
			if(fis != null) {
238
				try {
239
					fis.close();
240
				} catch (IOException e) {
241
					// TODO Auto-generated catch block
242
					e.printStackTrace();
243
				}
244
			}
245
		}
246
 
247
		return htmlString.toString();
248
	}
249
 
974 vikas 250
	public String getPageTitleHtml(long productId) {
251
		StringBuilder htmlString = new StringBuilder();
989 vikas 252
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "TitleSnippet.html";
974 vikas 253
		File f = new File(filename);
254
 
255
 
256
		FileInputStream fis = null;
257
		try {
258
			fis = new FileInputStream(f);
259
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
260
			String line;
261
			while((line = br.readLine()) != null){
262
				htmlString.append(line+"\n");
263
			}
264
		} catch (FileNotFoundException e) {
265
			// TODO Auto-generated catch block
266
			e.printStackTrace();
267
		} catch (IOException e) {
268
			// TODO Auto-generated catch block
269
			e.printStackTrace();
270
		}
271
		finally {
272
			if(fis != null) {
273
				try {
274
					fis.close();
275
				} catch (IOException e) {
276
					// TODO Auto-generated catch block
277
					e.printStackTrace();
278
				}
279
			}
280
		}
281
 
282
		return htmlString.toString();
283
	}
284
 
285
	public String getPageMetaDescriptionHtml(long productId) {
286
		StringBuilder htmlString = new StringBuilder();
989 vikas 287
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "MetaDescriptionSnippet.html";
974 vikas 288
		File f = new File(filename);
289
 
290
 
291
		FileInputStream fis = null;
292
		try {
293
			fis = new FileInputStream(f);
294
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
295
			String line;
296
			while((line = br.readLine()) != null){
297
				htmlString.append(line+"\n");
298
			}
299
		} catch (FileNotFoundException e) {
300
			// TODO Auto-generated catch block
301
			e.printStackTrace();
302
		} catch (IOException e) {
303
			// TODO Auto-generated catch block
304
			e.printStackTrace();
305
		}
306
		finally {
307
			if(fis != null) {
308
				try {
309
					fis.close();
310
				} catch (IOException e) {
311
					// TODO Auto-generated catch block
312
					e.printStackTrace();
313
				}
314
			}
315
		}
316
 
317
		return htmlString.toString();
318
	}
319
 
320
	public String getPageMetaKeywordsHtml(long productId) {
321
		StringBuilder htmlString = new StringBuilder();
989 vikas 322
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "MetaKeywordsSnippet.html";
974 vikas 323
		File f = new File(filename);
324
 
325
 
326
		FileInputStream fis = null;
327
		try {
328
			fis = new FileInputStream(f);
329
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
330
			String line;
331
			while((line = br.readLine()) != null){
332
				htmlString.append(line+"\n");
333
			}
334
		} catch (FileNotFoundException e) {
335
			// TODO Auto-generated catch block
336
			e.printStackTrace();
337
		} catch (IOException e) {
338
			// TODO Auto-generated catch block
339
			e.printStackTrace();
340
		}
341
		finally {
342
			if(fis != null) {
343
				try {
344
					fis.close();
345
				} catch (IOException e) {
346
					// TODO Auto-generated catch block
347
					e.printStackTrace();
348
				}
349
			}
350
		}
351
 
352
		return htmlString.toString();
353
	}
507 rajveer 354
 
517 rajveer 355
	public String getMainMenuHtml() {
507 rajveer 356
		String htmlString = "";
357
		VelocityContext context = new VelocityContext();
358
		String templateFile = "templates/mainmenu.vm";
359
		htmlString = getHtmlFromVelocity(templateFile, context);
360
		return htmlString;
361
	}
362
 
363
 
517 rajveer 364
	public String getSearchBarHtml(long itemCounts, long categoryId) {
507 rajveer 365
		String htmlString = "";
366
		VelocityContext context = new VelocityContext();
367
		String templateFile = "templates/searchbar.vm";
550 rajveer 368
 
369
		context.put("itemCount", itemCounts+"");
370
		context.put("categoryId", categoryId+"");
371
 
507 rajveer 372
		htmlString = getHtmlFromVelocity(templateFile, context);
373
		return htmlString;
374
	}
375
 
376
 
377
 
924 vikas 378
	public String getHeaderHtml(boolean isLoggedIn, String  userName, String url) {
550 rajveer 379
		VelocityContext context = new VelocityContext();
555 chandransh 380
		if (isLoggedIn) {
381
			context.put("LOGGED_IN", "TRUE");
382
			context.put("WELCOME_MESSAGE", "Hi, " + userName);
383
		} else {
801 rajveer 384
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
924 vikas 385
			context.put("REDIRECT_URL", url);
555 chandransh 386
		}		
507 rajveer 387
 
388
		String templateFile = "templates/header.vm";
550 rajveer 389
 
590 chandransh 390
		return getHtmlFromVelocity(templateFile, context);
507 rajveer 391
	}
392
 
393
 
1527 ankur.sing 394
	public String getOrderDetailsHtml(long orderId, long userId) {
507 rajveer 395
		String htmlString = "";
396
		VelocityContext context = new VelocityContext();
397
		String templateFile = "templates/orderdetails.vm";
398
		Order order = null;
517 rajveer 399
		Date orderedOn = null, deliveryEstimate = null;
843 chandransh 400
		Provider provider = null;
507 rajveer 401
		try{
843 chandransh 402
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
403
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
1527 ankur.sing 404
			order = orderClient.getOrderForCustomer(orderId, userId);
517 rajveer 405
			orderedOn = new Date(order.getCreated_timestamp());
406
			deliveryEstimate = new Date(order.getExpected_delivery_time());
843 chandransh 407
 
408
			if(order.getLogistics_provider_id() != 0){
409
				LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
410
				in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
411
				provider = logisticsClient.getProvider(order.getLogistics_provider_id());
412
			}
507 rajveer 413
		}catch (Exception e){
414
 
415
		}
517 rajveer 416
 
417
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 418
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
507 rajveer 419
		context.put("order", order);
517 rajveer 420
		context.put("orderedOn", dateformat.format(orderedOn));
583 rajveer 421
		context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
843 chandransh 422
		if(provider!=null){
423
			context.put("providerName", provider.getName());
424
		}
425
 
507 rajveer 426
		htmlString = getHtmlFromVelocity(templateFile, context);
427
		return htmlString;
428
	}
429
 
650 rajveer 430
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 431
		String htmlString = "";
432
		VelocityContext context = new VelocityContext();
433
		String templateFile = "templates/myaccount.vm";
434
		List<Order> orders = null;
745 chandransh 435
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 436
		try{
745 chandransh 437
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
438
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 439
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
440
 
745 chandransh 441
			LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
442
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
443
			List<Provider> providers = logisticsClient.getAllProviders();
444
			for(Provider provider: providers)
445
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 446
		}catch (Exception e){
1275 varun.gupt 447
			e.printStackTrace();
507 rajveer 448
		}
741 rajveer 449
		List<String> orderDate = new ArrayList<String>();
450
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 451
		if(orders!=null && !orders.isEmpty()){
762 rajveer 452
			for(Order order: orders){
453
				Date orderedOn = new Date(order.getCreated_timestamp());
454
				orderDate.add(dateformat.format(orderedOn));
455
			}
741 rajveer 456
		}
507 rajveer 457
		context.put("orders", orders);
741 rajveer 458
		context.put("orderDate", orderDate);
745 chandransh 459
		context.put("providerNames", providerNames);
507 rajveer 460
		htmlString = getHtmlFromVelocity(templateFile, context);
461
		return htmlString;
462
	}
463
 
464
 
650 rajveer 465
	public String getLoginDetailsHtml(long userId) {
507 rajveer 466
		String htmlString = "";
467
		VelocityContext context = new VelocityContext();
468
		String templateFile = "templates/logindetails.vm";
469
		String email = "";
470
		try{
762 rajveer 471
			email = getEmailId(userId);
507 rajveer 472
		}catch (Exception e){
473
 
474
		}
475
		context.put("email", email);
476
		htmlString = getHtmlFromVelocity(templateFile, context);
477
		return htmlString;
478
	}
479
 
762 rajveer 480
	public String getEmailId(long userId){
481
		String email = " ";
482
 
483
		try {
484
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
485
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
486
			if(userClient.getUserById(userId).getEmail() != null){
487
				email = userClient.getUserById(userId).getEmail();
488
			}
489
		} catch (UserContextException e) {
490
			e.printStackTrace();
491
		} catch (TException e) {
492
			e.printStackTrace();
493
		} catch (Exception e) {
494
			e.printStackTrace();
495
		}
496
		return email; 
497
	}
498
 
499
 
595 rajveer 500
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 501
		String htmlString = "";
502
		VelocityContext context = new VelocityContext();
503
		String templateFile = "templates/personaldetails.vm";
504
		String email = "";
517 rajveer 505
		String name = "";
569 rajveer 506
		String dateOfBirth = "";
517 rajveer 507
		String sex = "";
508
		String subscribe = "false";
507 rajveer 509
		UserContextServiceClient userContextServiceClient = null;
510
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
511
		try{
555 chandransh 512
			User user = null;
507 rajveer 513
			userContextServiceClient = new UserContextServiceClient();
514
			userClient = userContextServiceClient.getClient();
555 chandransh 515
			user = userClient.getUserById(userId);
507 rajveer 516
 
555 chandransh 517
			email = user.getCommunicationEmail();
518
			name = user.getName();
517 rajveer 519
 
569 rajveer 520
			dateOfBirth = user.getDateOfBirth();
507 rajveer 521
		}catch (Exception e){
555 chandransh 522
			e.printStackTrace();
507 rajveer 523
		}
517 rajveer 524
		context.put("name", name);
525
		context.put("email", email);
569 rajveer 526
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 527
		context.put("subscribe", subscribe);
528
		context.put("sex", sex);
507 rajveer 529
		htmlString = getHtmlFromVelocity(templateFile, context);
530
		return htmlString;
531
	}
532
 
595 rajveer 533
	public String getMyaccountHeaderHtml() {
507 rajveer 534
		String htmlString = "";
535
		VelocityContext context = new VelocityContext();
536
		String templateFile = "templates/myaccountheader.vm";
537
		htmlString = getHtmlFromVelocity(templateFile, context);
538
		return htmlString;
539
	}
540
 
822 vikas 541
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 542
		String htmlString = "";
543
		VelocityContext context = new VelocityContext();
544
		String templateFile = "templates/shippingaddressdetails.vm";
545
		long defaultAddressId = 0;
517 rajveer 546
		List<Address> addresses = null;
507 rajveer 547
 
548
		UserContextServiceClient userContextServiceClient = null;
549
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
550
		try {
551
			userContextServiceClient = new UserContextServiceClient();
552
			userClient = userContextServiceClient.getClient();
620 rajveer 553
 
554
			addresses = userClient.getAllAddressesForUser(userId);
555
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 556
		} catch (Exception e) {
557
			e.printStackTrace();
558
		}
517 rajveer 559
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 560
		context.put("addresses", addresses);
822 vikas 561
		context.put("errorMsg", errorMsg);
507 rajveer 562
 
563
		htmlString = getHtmlFromVelocity(templateFile, context);
564
		return htmlString;
565
	}
566
 
567
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
568
		Properties p = new Properties();
569
		p.setProperty("resource.loader", "class");
570
		p.setProperty("class.resource.loader.class",
571
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 572
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
573
 
507 rajveer 574
		try {
575
			Velocity.init(p);
762 rajveer 576
			//Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
507 rajveer 577
			Template template = Velocity.getTemplate(templateFile);
578
			if(template != null) {
579
				StringWriter writer = new StringWriter();
580
				template.merge(context, writer);
581
				writer.flush();
582
				writer.close();
583
				return writer.toString();
584
			}
585
 
586
			} catch (ResourceNotFoundException e) {
587
				// TODO Auto-generated catch block
588
				e.printStackTrace();
589
			} catch (ParseErrorException e) {
590
				// TODO Auto-generated catch block
591
				e.printStackTrace();
592
			} catch (MethodInvocationException e) {
593
				// TODO Auto-generated catch block
594
				e.printStackTrace();
595
			} catch (IOException e) {
596
				// TODO Auto-generated catch block
597
				e.printStackTrace();
598
			} catch (Exception e) {
599
				// TODO Auto-generated catch block
600
				e.printStackTrace();
601
			}
602
 
603
		return null;
604
	}
1549 rajveer 605
}