Subversion Repositories SmartDukaan

Rev

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