Subversion Repositories SmartDukaan

Rev

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