Subversion Repositories SmartDukaan

Rev

Rev 9155 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.services;
2
 
5838 mandeep.dh 3
import in.shop2020.logistics.PickupStore;
2306 vikas 4
import in.shop2020.logistics.Provider;
8917 kshitij.so 5
import in.shop2020.model.v1.catalog.Banner;
6
import in.shop2020.model.v1.catalog.BannerMap;
2306 vikas 7
import in.shop2020.model.v1.order.Order;
7309 rajveer 8
import in.shop2020.model.v1.order.OrderSource;
4815 phani.kuma 9
import in.shop2020.model.v1.order.OrderStatus;
10
import in.shop2020.model.v1.order.OrderStatusGroups;
2306 vikas 11
import in.shop2020.model.v1.user.Address;
12
import in.shop2020.model.v1.user.User;
13
import in.shop2020.model.v1.user.UserContextException;
3830 chandransh 14
import in.shop2020.serving.utils.FormattingUtils;
2306 vikas 15
import in.shop2020.serving.utils.Utils;
3126 rajveer 16
import in.shop2020.thrift.clients.LogisticsClient;
17
import in.shop2020.thrift.clients.TransactionClient;
18
import in.shop2020.thrift.clients.UserClient;
2306 vikas 19
 
507 rajveer 20
import java.io.BufferedReader;
21
import java.io.File;
22
import java.io.FileInputStream;
23
import java.io.FileNotFoundException;
24
import java.io.IOException;
25
import java.io.InputStreamReader;
26
import java.io.StringWriter;
517 rajveer 27
import java.text.SimpleDateFormat;
507 rajveer 28
import java.util.ArrayList;
29
import java.util.Date;
30
import java.util.HashMap;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Properties;
34
 
2949 chandransh 35
import org.apache.log4j.Logger;
507 rajveer 36
import org.apache.thrift.TException;
37
import org.apache.velocity.Template;
38
import org.apache.velocity.VelocityContext;
39
import org.apache.velocity.app.Velocity;
40
import org.apache.velocity.exception.MethodInvocationException;
41
import org.apache.velocity.exception.ParseErrorException;
42
import org.apache.velocity.exception.ResourceNotFoundException;
43
 
44
 
45
public class PageLoaderHandler {
46
 
2949 chandransh 47
    private static Logger logger = Logger.getLogger(PageLoaderHandler.class);
48
 
620 rajveer 49
	public String getSlideGuideHtml(long productId) {
507 rajveer 50
		StringBuilder htmlString = new StringBuilder();
517 rajveer 51
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideGuide.html";
507 rajveer 52
		File f = new File(filename);
53
 
54
 
55
		FileInputStream fis = null;
56
		try {
57
			fis = new FileInputStream(f);
58
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
59
			String line;
60
			while((line = br.readLine()) != null){
61
				htmlString.append(line+"\n");
62
			}
63
		} catch (FileNotFoundException e) {
2949 chandransh 64
			logger.error("Unable to find the slide guide for " + productId, e);
507 rajveer 65
		} catch (IOException e) {
2949 chandransh 66
		    logger.error("Unable to read the slide guide for " + productId, e);
507 rajveer 67
		}
68
		finally {
69
			if(fis != null) {
70
				try {
71
					fis.close();
72
				} catch (IOException e) {
2949 chandransh 73
				    logger.warn("Unable to close the slide guide for " + productId, e);
507 rajveer 74
				}
75
			}
76
		}
77
 
78
		return htmlString.toString();
79
	}
80
 
517 rajveer 81
	public String getProductSummaryHtml(long productId) {
507 rajveer 82
		StringBuilder htmlString = new StringBuilder();
517 rajveer 83
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductDetail.html";
507 rajveer 84
		File f = new File(filename);
85
 
86
 
87
		FileInputStream fis = null;
88
		try {
89
			fis = new FileInputStream(f);
90
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
91
			String line;
92
			while((line = br.readLine()) != null){
93
				htmlString.append(line+"\n");
94
			}
95
		} catch (FileNotFoundException e) {
2949 chandransh 96
			logger.error("Unable to find the product summary file", e);
507 rajveer 97
		} catch (IOException e) {
2949 chandransh 98
			logger.error("Unable to read the product summary file", e);
507 rajveer 99
		}
100
		finally {
101
			if(fis != null) {
102
				try {
103
					fis.close();
104
				} catch (IOException e) {
2949 chandransh 105
					logger.error("Unable to close the product summary file", e);
507 rajveer 106
				}
107
			}
108
		}
109
 
110
		return htmlString.toString();
111
	}
112
 
2306 vikas 113
	public String getProductPropertiesHtml(long productId) {
974 vikas 114
		StringBuilder htmlString = new StringBuilder();
2306 vikas 115
		String filename = Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "ProductPropertiesSnippet.html";
974 vikas 116
		File f = new File(filename);
117
 
118
 
119
		FileInputStream fis = null;
120
		try {
121
			fis = new FileInputStream(f);
122
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
123
			String line;
124
			while((line = br.readLine()) != null){
125
				htmlString.append(line+"\n");
126
			}
127
		} catch (FileNotFoundException e) {
2949 chandransh 128
		    logger.error("Unable to find the product properties file", e);
974 vikas 129
		} catch (IOException e) {
2949 chandransh 130
			logger.error("Unable to read the product properties file", e);
131
		} finally {
974 vikas 132
			if(fis != null) {
133
				try {
134
					fis.close();
135
				} catch (IOException e) {
2949 chandransh 136
					logger.error("Unable to close the product properties file", e);
974 vikas 137
				}
138
			}
139
		}
140
 
141
		return htmlString.toString();
142
	}
143
 
517 rajveer 144
	public String getSearchBarHtml(long itemCounts, long categoryId) {
507 rajveer 145
		String htmlString = "";
146
		VelocityContext context = new VelocityContext();
147
		String templateFile = "templates/searchbar.vm";
550 rajveer 148
 
149
		context.put("itemCount", itemCounts+"");
150
		context.put("categoryId", categoryId+"");
151
 
507 rajveer 152
		htmlString = getHtmlFromVelocity(templateFile, context);
153
		return htmlString;
154
	}
4453 varun.gupt 155
 
6152 amit.gupta 156
	public String getHeaderHtml(boolean isLoggedIn, String email, int totalItems, String url, long catId, boolean displayBestDealsImg)	{
550 rajveer 157
		VelocityContext context = new VelocityContext();
4453 varun.gupt 158
 
159
		if (isLoggedIn)	{
555 chandransh 160
			context.put("LOGGED_IN", "TRUE");
4453 varun.gupt 161
			context.put("WELCOME_MESSAGE", "Hi " + email.split("@")[0]);
162
 
163
		} else	{
801 rajveer 164
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
924 vikas 165
			context.put("REDIRECT_URL", url);
4453 varun.gupt 166
		}
507 rajveer 167
 
3903 varun.gupt 168
		context.put("BEST_DEALS_BADGE", displayBestDealsImg);
3830 chandransh 169
		context.put("CAT_ID", catId);
6152 amit.gupta 170
		context.put("TOTAL_ITEMS", totalItems);
507 rajveer 171
		String templateFile = "templates/header.vm";
550 rajveer 172
 
590 chandransh 173
		return getHtmlFromVelocity(templateFile, context);
507 rajveer 174
	}
6903 anupam.sin 175
 
11826 amit.gupta 176
	public String getHeaderHtml(boolean isLoggedIn, String email, int totalItems, String url, long catId, boolean displayBestDealsImg, boolean isPrivateDealUser)	{
177
		VelocityContext context = new VelocityContext();
178
 
179
		if (isLoggedIn)	{
180
			context.put("LOGGED_IN", "TRUE");
181
			context.put("WELCOME_MESSAGE", "Hi " + email.split("@")[0]);
182
			if(isPrivateDealUser) {
183
				context.put("PRIVATE_DEAL_USER", "TRUE");
184
			}
185
 
186
		} else	{
187
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
188
			context.put("REDIRECT_URL", url);
189
		}
190
 
191
		context.put("BEST_DEALS_BADGE", displayBestDealsImg);
192
		context.put("CAT_ID", catId);
193
		context.put("TOTAL_ITEMS", totalItems);
194
		String templateFile = "templates/header.vm";
195
 
196
		return getHtmlFromVelocity(templateFile, context);
197
	}
198
 
6903 anupam.sin 199
	public String getThinHeaderHtml(boolean isLoggedIn, String email, int totalItems, String url, long catId, boolean displayBestDealsImg) {
200
        VelocityContext context = new VelocityContext();
201
 
202
        if (isLoggedIn) {
203
            context.put("LOGGED_IN", "TRUE");
204
            context.put("WELCOME_MESSAGE", "Hi " + email.split("@")[0]);
205
 
206
        } else  {
207
            context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
208
            context.put("REDIRECT_URL", url);
209
        }
210
 
211
        context.put("BEST_DEALS_BADGE", displayBestDealsImg);
212
        context.put("CAT_ID", catId);
213
        context.put("TOTAL_ITEMS", totalItems);
214
        String templateFile = "templates/thinheader.vm";
215
 
216
        return getHtmlFromVelocity(templateFile, context);
217
    }
507 rajveer 218
 
219
 
4815 phani.kuma 220
	public String getOrderDetailsHtml(long orderId, UserSessionInfo userinfo) {
221
		long userId = userinfo.getUserId();
507 rajveer 222
		String htmlString = "";
223
		VelocityContext context = new VelocityContext();
224
		String templateFile = "templates/orderdetails.vm";
225
		Order order = null;
517 rajveer 226
		Date orderedOn = null, deliveryEstimate = null;
843 chandransh 227
		Provider provider = null;
4325 mandeep.dh 228
        List<Address> addresses = null;
229
        Long defaultAddressId = null;
4815 phani.kuma 230
        boolean initiateOrderCancelation = false;
231
        boolean requestOrderCancelation = false;
232
        OrderStatusGroups Groups = new OrderStatusGroups();
233
        List<OrderStatus> codCancellable = Groups.getCodCancellable();
234
        List<OrderStatus> prepaidCancellableBeforeBilled = Groups.getPrepaidCancellableBeforeBilled();
235
        List<OrderStatus> prepaidCancellableAfterBilled = Groups.getPrepaidCancellableAfterBilled();
236
 
4325 mandeep.dh 237
        try{
3126 rajveer 238
			TransactionClient transactionServiceClient = new TransactionClient();
843 chandransh 239
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
1527 ankur.sing 240
			order = orderClient.getOrderForCustomer(orderId, userId);
517 rajveer 241
			orderedOn = new Date(order.getCreated_timestamp());
6672 amit.gupta 242
			deliveryEstimate = new Date(order.getPromised_delivery_time());
4815 phani.kuma 243
 
7309 rajveer 244
			if(order.getSource() == OrderSource.WEBSITE.getValue()){
245
				if(order.isCod()){
246
					if(codCancellable.contains(order.getStatus())){
247
						initiateOrderCancelation = true;
248
					}
4815 phani.kuma 249
				}
7309 rajveer 250
				else {
251
					if(prepaidCancellableBeforeBilled.contains(order.getStatus())){
252
						initiateOrderCancelation = true;
253
					}
254
					else if(prepaidCancellableAfterBilled.contains(order.getStatus())){
255
						requestOrderCancelation = true;
256
					}
4815 phani.kuma 257
				}
258
			}
843 chandransh 259
 
4325 mandeep.dh 260
			in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
261
			addresses = userClient.getAllAddressesForUser(userId);
262
			defaultAddressId = userClient.getDefaultAddressId(userId);
263
			logger.info("Found addresses: " + addresses + " for userId: " + userId);
264
 
843 chandransh 265
			if(order.getLogistics_provider_id() != 0){
3126 rajveer 266
				LogisticsClient logisticsServiceClient = new LogisticsClient();
843 chandransh 267
				in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
268
				provider = logisticsClient.getProvider(order.getLogistics_provider_id());
269
			}
5838 mandeep.dh 270
 
271
			if (order.getPickupStoreId() != 0) {
272
			    in.shop2020.logistics.LogisticsService.Client logisticsServiceClient = new LogisticsClient().getClient();
273
			    PickupStore store = logisticsServiceClient.getPickupStore(order.getPickupStoreId());
274
			    order.setCustomer_name(store.getName());
275
			    order.setCustomer_address1(store.getLine1());
276
			    order.setCustomer_address2(store.getLine2());
277
			    order.setCustomer_city(store.getCity());
278
			    order.setCustomer_pincode(store.getPin());
279
			    order.setCustomer_state(store.getState());
280
			    order.setCustomer_mobilenumber(store.getPhone());
281
			}
4453 varun.gupt 282
		} catch (Exception e){
507 rajveer 283
 
284
		}
517 rajveer 285
 
286
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 287
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
4325 mandeep.dh 288
 
507 rajveer 289
		context.put("order", order);
6561 amit.gupta 290
		context.put("isShipped", Groups.getShippedOrders().contains(order.getStatus()));
291
		context.put("isDelivered", order.getStatus().equals(OrderStatus.DELIVERY_SUCCESS));
292
		context.put("isOpen", Groups.getShippedOrders().contains(order.getStatus()));
293
		context.put("order", order);
517 rajveer 294
		context.put("orderedOn", dateformat.format(orderedOn));
6561 amit.gupta 295
		context.put("fdaOn", dateformat1.format(new Date(order.getFirst_attempt_timestamp())));
6672 amit.gupta 296
		context.put("promisedDeliveryDate", dateformat1.format(deliveryEstimate));
6561 amit.gupta 297
		context.put("deliveredOn", dateformat1.format(new Date(order.getDelivery_timestamp())));
4325 mandeep.dh 298
		context.put("addresses", addresses);
299
		context.put("defaultAddressId", defaultAddressId);
4815 phani.kuma 300
		context.put("userinfo", userinfo);
301
		context.put("initiateOrderCancelation", initiateOrderCancelation);
302
		context.put("requestOrderCancelation", requestOrderCancelation);
4325 mandeep.dh 303
 
843 chandransh 304
		if(provider!=null){
305
			context.put("providerName", provider.getName());
306
		}
307
 
507 rajveer 308
		htmlString = getHtmlFromVelocity(templateFile, context);
309
		return htmlString;
310
	}
311
 
650 rajveer 312
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 313
		String htmlString = "";
314
		VelocityContext context = new VelocityContext();
315
		String templateFile = "templates/myaccount.vm";
316
		List<Order> orders = null;
745 chandransh 317
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 318
		try{
3126 rajveer 319
			TransactionClient transactionServiceClient = new TransactionClient();
745 chandransh 320
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 321
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
322
 
3126 rajveer 323
			LogisticsClient logisticsServiceClient = new LogisticsClient();
745 chandransh 324
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
325
			List<Provider> providers = logisticsClient.getAllProviders();
326
			for(Provider provider: providers)
327
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 328
		}catch (Exception e){
2949 chandransh 329
			logger.error("Unable to get order or provider details", e);
507 rajveer 330
		}
741 rajveer 331
		List<String> orderDate = new ArrayList<String>();
332
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 333
		if(orders!=null && !orders.isEmpty()){
762 rajveer 334
			for(Order order: orders){
335
				Date orderedOn = new Date(order.getCreated_timestamp());
336
				orderDate.add(dateformat.format(orderedOn));
337
			}
741 rajveer 338
		}
507 rajveer 339
		context.put("orders", orders);
741 rajveer 340
		context.put("orderDate", orderDate);
745 chandransh 341
		context.put("providerNames", providerNames);
507 rajveer 342
		htmlString = getHtmlFromVelocity(templateFile, context);
343
		return htmlString;
344
	}
345
 
346
 
650 rajveer 347
	public String getLoginDetailsHtml(long userId) {
507 rajveer 348
		String htmlString = "";
349
		VelocityContext context = new VelocityContext();
350
		String templateFile = "templates/logindetails.vm";
351
		String email = "";
352
		try{
762 rajveer 353
			email = getEmailId(userId);
507 rajveer 354
		}catch (Exception e){
355
 
356
		}
357
		context.put("email", email);
358
		htmlString = getHtmlFromVelocity(templateFile, context);
359
		return htmlString;
360
	}
361
 
762 rajveer 362
	public String getEmailId(long userId){
363
		String email = " ";
364
 
365
		try {
3126 rajveer 366
			UserClient userContextServiceClient = new UserClient();
762 rajveer 367
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
2949 chandransh 368
			String userEmail = userClient.getUserById(userId).getEmail(); 
369
			if( userEmail != null){
370
				email = userEmail;
762 rajveer 371
			}
372
		} catch (UserContextException e) {
2949 chandransh 373
			logger.error("Unable to get the user for " + userId, e);
762 rajveer 374
		} catch (TException e) {
2949 chandransh 375
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 376
		} catch (Exception e) {
2949 chandransh 377
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 378
		}
379
		return email; 
380
	}
381
 
382
 
595 rajveer 383
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 384
		String htmlString = "";
385
		VelocityContext context = new VelocityContext();
386
		String templateFile = "templates/personaldetails.vm";
387
		String email = "";
517 rajveer 388
		String name = "";
569 rajveer 389
		String dateOfBirth = "";
517 rajveer 390
		String sex = "";
391
		String subscribe = "false";
3126 rajveer 392
		UserClient userContextServiceClient = null;
507 rajveer 393
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
394
		try{
555 chandransh 395
			User user = null;
3126 rajveer 396
			userContextServiceClient = new UserClient();
507 rajveer 397
			userClient = userContextServiceClient.getClient();
555 chandransh 398
			user = userClient.getUserById(userId);
507 rajveer 399
 
555 chandransh 400
			email = user.getCommunicationEmail();
401
			name = user.getName();
517 rajveer 402
 
569 rajveer 403
			dateOfBirth = user.getDateOfBirth();
507 rajveer 404
		}catch (Exception e){
2949 chandransh 405
		    logger.error("Unable to get the user for " + userId, e);
507 rajveer 406
		}
517 rajveer 407
		context.put("name", name);
408
		context.put("email", email);
569 rajveer 409
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 410
		context.put("subscribe", subscribe);
411
		context.put("sex", sex);
507 rajveer 412
		htmlString = getHtmlFromVelocity(templateFile, context);
413
		return htmlString;
414
	}
415
 
595 rajveer 416
	public String getMyaccountHeaderHtml() {
507 rajveer 417
		String htmlString = "";
418
		VelocityContext context = new VelocityContext();
419
		String templateFile = "templates/myaccountheader.vm";
420
		htmlString = getHtmlFromVelocity(templateFile, context);
421
		return htmlString;
422
	}
423
 
822 vikas 424
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 425
		String htmlString = "";
426
		VelocityContext context = new VelocityContext();
427
		String templateFile = "templates/shippingaddressdetails.vm";
428
		long defaultAddressId = 0;
517 rajveer 429
		List<Address> addresses = null;
507 rajveer 430
 
3126 rajveer 431
		UserClient userContextServiceClient = null;
507 rajveer 432
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
433
		try {
3126 rajveer 434
			userContextServiceClient = new UserClient();
507 rajveer 435
			userClient = userContextServiceClient.getClient();
620 rajveer 436
 
437
			addresses = userClient.getAllAddressesForUser(userId);
438
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 439
		} catch (Exception e) {
2949 chandransh 440
		    logger.error("Unable to get either the user for " + userId + " or his address", e);
507 rajveer 441
		}
517 rajveer 442
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 443
		context.put("addresses", addresses);
822 vikas 444
		context.put("errorMsg", errorMsg);
507 rajveer 445
 
446
		htmlString = getHtmlFromVelocity(templateFile, context);
447
		return htmlString;
448
	}
449
 
450
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
451
		Properties p = new Properties();
452
		p.setProperty("resource.loader", "class");
453
		p.setProperty("class.resource.loader.class",
454
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 455
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
456
 
2949 chandransh 457
        try {
458
            Velocity.init(p);
459
            // Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
460
            Template template = Velocity.getTemplate(templateFile);
461
            if (template != null) {
462
                StringWriter writer = new StringWriter();
463
                template.merge(context, writer);
464
                writer.flush();
465
                writer.close();
466
                return writer.toString();
467
            }
468
 
469
        } catch (ResourceNotFoundException e) {
470
            logger.error("Unable to find the template file " + templateFile, e);
471
        } catch (ParseErrorException e) {
472
            logger.error("Unable to parse the template file " + templateFile, e);
473
        } catch (MethodInvocationException e) {
474
            logger.error("Unable to invoke methods for the velocity template file " + templateFile, e);
475
        } catch (IOException e) {
476
            logger.error("Unable to read the template file " + templateFile, e);
477
        } catch (Exception e) {
478
            logger.error("Unable to generate the HTML from the template file " + templateFile, e);
479
        }
507 rajveer 480
 
481
		return null;
482
	}
3830 chandransh 483
 
9155 kshitij.so 484
	public String getCartWidgetSnippet(int totalItems, double totalAmount, long catId, List<Banner> bannerList, Map<String, List<BannerMap>> sideBannersMap) {
3830 chandransh 485
		String htmlString = "";
486
		VelocityContext context = new VelocityContext();
487
		String templateFile = "templates/cartwidget.vm";
6710 kshitij.so 488
		context.put("CAT_ID", catId);
8917 kshitij.so 489
		context.put("BANNERS", bannerList);
9155 kshitij.so 490
		context.put("ALL_BANNER_MAP",sideBannersMap);
3830 chandransh 491
		htmlString = getHtmlFromVelocity(templateFile, context);
492
		return htmlString;
493
	}
494
 
1549 rajveer 495
}