Subversion Repositories SmartDukaan

Rev

Rev 2268 | Rev 2578 | 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
 
2306 vikas 3
import in.shop2020.logistics.Provider;
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
6
import in.shop2020.model.v1.order.Order;
7
import in.shop2020.model.v1.user.Address;
8
import in.shop2020.model.v1.user.User;
9
import in.shop2020.model.v1.user.UserContextException;
10
import in.shop2020.serving.utils.FileUtils;
11
import in.shop2020.serving.utils.Utils;
12
import in.shop2020.thrift.clients.CatalogServiceClient;
13
import in.shop2020.thrift.clients.LogisticsServiceClient;
14
import in.shop2020.thrift.clients.TransactionServiceClient;
15
import in.shop2020.thrift.clients.UserContextServiceClient;
16
 
507 rajveer 17
import java.io.BufferedReader;
18
import java.io.File;
19
import java.io.FileInputStream;
20
import java.io.FileNotFoundException;
21
import java.io.IOException;
22
import java.io.InputStreamReader;
23
import java.io.StringWriter;
517 rajveer 24
import java.text.SimpleDateFormat;
507 rajveer 25
import java.util.ArrayList;
26
import java.util.Date;
27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.Properties;
31
 
32
import org.apache.thrift.TException;
33
import org.apache.velocity.Template;
34
import org.apache.velocity.VelocityContext;
35
import org.apache.velocity.app.Velocity;
36
import org.apache.velocity.exception.MethodInvocationException;
37
import org.apache.velocity.exception.ParseErrorException;
38
import org.apache.velocity.exception.ResourceNotFoundException;
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
 
2306 vikas 249
	public String getProductPropertiesHtml(long productId) {
974 vikas 250
		StringBuilder htmlString = new StringBuilder();
2306 vikas 251
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductPropertiesSnippet.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
 
517 rajveer 284
	public String getMainMenuHtml() {
507 rajveer 285
		String htmlString = "";
286
		VelocityContext context = new VelocityContext();
287
		String templateFile = "templates/mainmenu.vm";
288
		htmlString = getHtmlFromVelocity(templateFile, context);
289
		return htmlString;
290
	}
291
 
292
 
517 rajveer 293
	public String getSearchBarHtml(long itemCounts, long categoryId) {
507 rajveer 294
		String htmlString = "";
295
		VelocityContext context = new VelocityContext();
296
		String templateFile = "templates/searchbar.vm";
550 rajveer 297
 
298
		context.put("itemCount", itemCounts+"");
299
		context.put("categoryId", categoryId+"");
300
 
507 rajveer 301
		htmlString = getHtmlFromVelocity(templateFile, context);
302
		return htmlString;
303
	}
304
 
305
 
306
 
924 vikas 307
	public String getHeaderHtml(boolean isLoggedIn, String  userName, String url) {
550 rajveer 308
		VelocityContext context = new VelocityContext();
555 chandransh 309
		if (isLoggedIn) {
310
			context.put("LOGGED_IN", "TRUE");
311
			context.put("WELCOME_MESSAGE", "Hi, " + userName);
312
		} else {
801 rajveer 313
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
924 vikas 314
			context.put("REDIRECT_URL", url);
555 chandransh 315
		}		
507 rajveer 316
 
317
		String templateFile = "templates/header.vm";
550 rajveer 318
 
590 chandransh 319
		return getHtmlFromVelocity(templateFile, context);
507 rajveer 320
	}
321
 
322
 
1527 ankur.sing 323
	public String getOrderDetailsHtml(long orderId, long userId) {
507 rajveer 324
		String htmlString = "";
325
		VelocityContext context = new VelocityContext();
326
		String templateFile = "templates/orderdetails.vm";
327
		Order order = null;
517 rajveer 328
		Date orderedOn = null, deliveryEstimate = null;
843 chandransh 329
		Provider provider = null;
507 rajveer 330
		try{
843 chandransh 331
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
332
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
1527 ankur.sing 333
			order = orderClient.getOrderForCustomer(orderId, userId);
517 rajveer 334
			orderedOn = new Date(order.getCreated_timestamp());
335
			deliveryEstimate = new Date(order.getExpected_delivery_time());
843 chandransh 336
 
337
			if(order.getLogistics_provider_id() != 0){
338
				LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
339
				in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
340
				provider = logisticsClient.getProvider(order.getLogistics_provider_id());
341
			}
507 rajveer 342
		}catch (Exception e){
343
 
344
		}
517 rajveer 345
 
346
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 347
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
507 rajveer 348
		context.put("order", order);
517 rajveer 349
		context.put("orderedOn", dateformat.format(orderedOn));
583 rajveer 350
		context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
843 chandransh 351
		if(provider!=null){
352
			context.put("providerName", provider.getName());
353
		}
354
 
507 rajveer 355
		htmlString = getHtmlFromVelocity(templateFile, context);
356
		return htmlString;
357
	}
358
 
650 rajveer 359
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 360
		String htmlString = "";
361
		VelocityContext context = new VelocityContext();
362
		String templateFile = "templates/myaccount.vm";
363
		List<Order> orders = null;
745 chandransh 364
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 365
		try{
745 chandransh 366
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
367
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 368
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
369
 
745 chandransh 370
			LogisticsServiceClient logisticsServiceClient = new LogisticsServiceClient();
371
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
372
			List<Provider> providers = logisticsClient.getAllProviders();
373
			for(Provider provider: providers)
374
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 375
		}catch (Exception e){
1275 varun.gupt 376
			e.printStackTrace();
507 rajveer 377
		}
741 rajveer 378
		List<String> orderDate = new ArrayList<String>();
379
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 380
		if(orders!=null && !orders.isEmpty()){
762 rajveer 381
			for(Order order: orders){
382
				Date orderedOn = new Date(order.getCreated_timestamp());
383
				orderDate.add(dateformat.format(orderedOn));
384
			}
741 rajveer 385
		}
507 rajveer 386
		context.put("orders", orders);
741 rajveer 387
		context.put("orderDate", orderDate);
745 chandransh 388
		context.put("providerNames", providerNames);
507 rajveer 389
		htmlString = getHtmlFromVelocity(templateFile, context);
390
		return htmlString;
391
	}
392
 
393
 
650 rajveer 394
	public String getLoginDetailsHtml(long userId) {
507 rajveer 395
		String htmlString = "";
396
		VelocityContext context = new VelocityContext();
397
		String templateFile = "templates/logindetails.vm";
398
		String email = "";
399
		try{
762 rajveer 400
			email = getEmailId(userId);
507 rajveer 401
		}catch (Exception e){
402
 
403
		}
404
		context.put("email", email);
405
		htmlString = getHtmlFromVelocity(templateFile, context);
406
		return htmlString;
407
	}
408
 
762 rajveer 409
	public String getEmailId(long userId){
410
		String email = " ";
411
 
412
		try {
413
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
414
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
415
			if(userClient.getUserById(userId).getEmail() != null){
416
				email = userClient.getUserById(userId).getEmail();
417
			}
418
		} catch (UserContextException e) {
419
			e.printStackTrace();
420
		} catch (TException e) {
421
			e.printStackTrace();
422
		} catch (Exception e) {
423
			e.printStackTrace();
424
		}
425
		return email; 
426
	}
427
 
428
 
595 rajveer 429
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 430
		String htmlString = "";
431
		VelocityContext context = new VelocityContext();
432
		String templateFile = "templates/personaldetails.vm";
433
		String email = "";
517 rajveer 434
		String name = "";
569 rajveer 435
		String dateOfBirth = "";
517 rajveer 436
		String sex = "";
437
		String subscribe = "false";
507 rajveer 438
		UserContextServiceClient userContextServiceClient = null;
439
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
440
		try{
555 chandransh 441
			User user = null;
507 rajveer 442
			userContextServiceClient = new UserContextServiceClient();
443
			userClient = userContextServiceClient.getClient();
555 chandransh 444
			user = userClient.getUserById(userId);
507 rajveer 445
 
555 chandransh 446
			email = user.getCommunicationEmail();
447
			name = user.getName();
517 rajveer 448
 
569 rajveer 449
			dateOfBirth = user.getDateOfBirth();
507 rajveer 450
		}catch (Exception e){
555 chandransh 451
			e.printStackTrace();
507 rajveer 452
		}
517 rajveer 453
		context.put("name", name);
454
		context.put("email", email);
569 rajveer 455
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 456
		context.put("subscribe", subscribe);
457
		context.put("sex", sex);
507 rajveer 458
		htmlString = getHtmlFromVelocity(templateFile, context);
459
		return htmlString;
460
	}
461
 
595 rajveer 462
	public String getMyaccountHeaderHtml() {
507 rajveer 463
		String htmlString = "";
464
		VelocityContext context = new VelocityContext();
465
		String templateFile = "templates/myaccountheader.vm";
466
		htmlString = getHtmlFromVelocity(templateFile, context);
467
		return htmlString;
468
	}
469
 
822 vikas 470
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 471
		String htmlString = "";
472
		VelocityContext context = new VelocityContext();
473
		String templateFile = "templates/shippingaddressdetails.vm";
474
		long defaultAddressId = 0;
517 rajveer 475
		List<Address> addresses = null;
507 rajveer 476
 
477
		UserContextServiceClient userContextServiceClient = null;
478
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
479
		try {
480
			userContextServiceClient = new UserContextServiceClient();
481
			userClient = userContextServiceClient.getClient();
620 rajveer 482
 
483
			addresses = userClient.getAllAddressesForUser(userId);
484
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 485
		} catch (Exception e) {
486
			e.printStackTrace();
487
		}
517 rajveer 488
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 489
		context.put("addresses", addresses);
822 vikas 490
		context.put("errorMsg", errorMsg);
507 rajveer 491
 
492
		htmlString = getHtmlFromVelocity(templateFile, context);
493
		return htmlString;
494
	}
495
 
496
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
497
		Properties p = new Properties();
498
		p.setProperty("resource.loader", "class");
499
		p.setProperty("class.resource.loader.class",
500
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 501
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
502
 
507 rajveer 503
		try {
504
			Velocity.init(p);
762 rajveer 505
			//Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
507 rajveer 506
			Template template = Velocity.getTemplate(templateFile);
507
			if(template != null) {
508
				StringWriter writer = new StringWriter();
509
				template.merge(context, writer);
510
				writer.flush();
511
				writer.close();
512
				return writer.toString();
513
			}
514
 
515
			} catch (ResourceNotFoundException e) {
516
				// TODO Auto-generated catch block
517
				e.printStackTrace();
518
			} catch (ParseErrorException e) {
519
				// TODO Auto-generated catch block
520
				e.printStackTrace();
521
			} catch (MethodInvocationException e) {
522
				// TODO Auto-generated catch block
523
				e.printStackTrace();
524
			} catch (IOException e) {
525
				// TODO Auto-generated catch block
526
				e.printStackTrace();
527
			} catch (Exception e) {
528
				// TODO Auto-generated catch block
529
				e.printStackTrace();
530
			}
531
 
532
		return null;
533
	}
1549 rajveer 534
}