Subversion Repositories SmartDukaan

Rev

Rev 6561 | Rev 6710 | 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
	}
172
 
173
 
4815 phani.kuma 174
	public String getOrderDetailsHtml(long orderId, UserSessionInfo userinfo) {
175
		long userId = userinfo.getUserId();
507 rajveer 176
		String htmlString = "";
177
		VelocityContext context = new VelocityContext();
178
		String templateFile = "templates/orderdetails.vm";
179
		Order order = null;
517 rajveer 180
		Date orderedOn = null, deliveryEstimate = null;
843 chandransh 181
		Provider provider = null;
4325 mandeep.dh 182
        List<Address> addresses = null;
183
        Long defaultAddressId = null;
4815 phani.kuma 184
        boolean initiateOrderCancelation = false;
185
        boolean requestOrderCancelation = false;
186
        OrderStatusGroups Groups = new OrderStatusGroups();
187
        List<OrderStatus> codCancellable = Groups.getCodCancellable();
188
        List<OrderStatus> prepaidCancellableBeforeBilled = Groups.getPrepaidCancellableBeforeBilled();
189
        List<OrderStatus> prepaidCancellableAfterBilled = Groups.getPrepaidCancellableAfterBilled();
190
 
4325 mandeep.dh 191
        try{
3126 rajveer 192
			TransactionClient transactionServiceClient = new TransactionClient();
843 chandransh 193
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
1527 ankur.sing 194
			order = orderClient.getOrderForCustomer(orderId, userId);
517 rajveer 195
			orderedOn = new Date(order.getCreated_timestamp());
6672 amit.gupta 196
			deliveryEstimate = new Date(order.getPromised_delivery_time());
4815 phani.kuma 197
 
198
			if(order.isCod()){
199
				if(codCancellable.contains(order.getStatus())){
200
					initiateOrderCancelation = true;
201
				}
202
			}
203
			else {
204
				if(prepaidCancellableBeforeBilled.contains(order.getStatus())){
205
					initiateOrderCancelation = true;
206
				}
207
				else if(prepaidCancellableAfterBilled.contains(order.getStatus())){
208
					requestOrderCancelation = true;
209
				}
210
			}
843 chandransh 211
 
4325 mandeep.dh 212
			in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
213
			addresses = userClient.getAllAddressesForUser(userId);
214
			defaultAddressId = userClient.getDefaultAddressId(userId);
215
			logger.info("Found addresses: " + addresses + " for userId: " + userId);
216
 
843 chandransh 217
			if(order.getLogistics_provider_id() != 0){
3126 rajveer 218
				LogisticsClient logisticsServiceClient = new LogisticsClient();
843 chandransh 219
				in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
220
				provider = logisticsClient.getProvider(order.getLogistics_provider_id());
221
			}
5838 mandeep.dh 222
 
223
			if (order.getPickupStoreId() != 0) {
224
			    in.shop2020.logistics.LogisticsService.Client logisticsServiceClient = new LogisticsClient().getClient();
225
			    PickupStore store = logisticsServiceClient.getPickupStore(order.getPickupStoreId());
226
			    order.setCustomer_name(store.getName());
227
			    order.setCustomer_address1(store.getLine1());
228
			    order.setCustomer_address2(store.getLine2());
229
			    order.setCustomer_city(store.getCity());
230
			    order.setCustomer_pincode(store.getPin());
231
			    order.setCustomer_state(store.getState());
232
			    order.setCustomer_mobilenumber(store.getPhone());
233
			}
4453 varun.gupt 234
		} catch (Exception e){
507 rajveer 235
 
236
		}
517 rajveer 237
 
238
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 239
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
4325 mandeep.dh 240
 
507 rajveer 241
		context.put("order", order);
6561 amit.gupta 242
		context.put("isShipped", Groups.getShippedOrders().contains(order.getStatus()));
243
		context.put("isDelivered", order.getStatus().equals(OrderStatus.DELIVERY_SUCCESS));
244
		context.put("isOpen", Groups.getShippedOrders().contains(order.getStatus()));
245
		context.put("order", order);
517 rajveer 246
		context.put("orderedOn", dateformat.format(orderedOn));
6561 amit.gupta 247
		context.put("fdaOn", dateformat1.format(new Date(order.getFirst_attempt_timestamp())));
6672 amit.gupta 248
		context.put("promisedDeliveryDate", dateformat1.format(deliveryEstimate));
6561 amit.gupta 249
		context.put("deliveredOn", dateformat1.format(new Date(order.getDelivery_timestamp())));
4325 mandeep.dh 250
		context.put("addresses", addresses);
251
		context.put("defaultAddressId", defaultAddressId);
4815 phani.kuma 252
		context.put("userinfo", userinfo);
253
		context.put("initiateOrderCancelation", initiateOrderCancelation);
254
		context.put("requestOrderCancelation", requestOrderCancelation);
4325 mandeep.dh 255
 
843 chandransh 256
		if(provider!=null){
257
			context.put("providerName", provider.getName());
258
		}
259
 
507 rajveer 260
		htmlString = getHtmlFromVelocity(templateFile, context);
261
		return htmlString;
262
	}
263
 
650 rajveer 264
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 265
		String htmlString = "";
266
		VelocityContext context = new VelocityContext();
267
		String templateFile = "templates/myaccount.vm";
268
		List<Order> orders = null;
745 chandransh 269
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 270
		try{
3126 rajveer 271
			TransactionClient transactionServiceClient = new TransactionClient();
745 chandransh 272
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 273
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
274
 
3126 rajveer 275
			LogisticsClient logisticsServiceClient = new LogisticsClient();
745 chandransh 276
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
277
			List<Provider> providers = logisticsClient.getAllProviders();
278
			for(Provider provider: providers)
279
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 280
		}catch (Exception e){
2949 chandransh 281
			logger.error("Unable to get order or provider details", e);
507 rajveer 282
		}
741 rajveer 283
		List<String> orderDate = new ArrayList<String>();
284
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 285
		if(orders!=null && !orders.isEmpty()){
762 rajveer 286
			for(Order order: orders){
287
				Date orderedOn = new Date(order.getCreated_timestamp());
288
				orderDate.add(dateformat.format(orderedOn));
289
			}
741 rajveer 290
		}
507 rajveer 291
		context.put("orders", orders);
741 rajveer 292
		context.put("orderDate", orderDate);
745 chandransh 293
		context.put("providerNames", providerNames);
507 rajveer 294
		htmlString = getHtmlFromVelocity(templateFile, context);
295
		return htmlString;
296
	}
297
 
298
 
650 rajveer 299
	public String getLoginDetailsHtml(long userId) {
507 rajveer 300
		String htmlString = "";
301
		VelocityContext context = new VelocityContext();
302
		String templateFile = "templates/logindetails.vm";
303
		String email = "";
304
		try{
762 rajveer 305
			email = getEmailId(userId);
507 rajveer 306
		}catch (Exception e){
307
 
308
		}
309
		context.put("email", email);
310
		htmlString = getHtmlFromVelocity(templateFile, context);
311
		return htmlString;
312
	}
313
 
762 rajveer 314
	public String getEmailId(long userId){
315
		String email = " ";
316
 
317
		try {
3126 rajveer 318
			UserClient userContextServiceClient = new UserClient();
762 rajveer 319
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
2949 chandransh 320
			String userEmail = userClient.getUserById(userId).getEmail(); 
321
			if( userEmail != null){
322
				email = userEmail;
762 rajveer 323
			}
324
		} catch (UserContextException e) {
2949 chandransh 325
			logger.error("Unable to get the user for " + userId, e);
762 rajveer 326
		} catch (TException e) {
2949 chandransh 327
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 328
		} catch (Exception e) {
2949 chandransh 329
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 330
		}
331
		return email; 
332
	}
333
 
334
 
595 rajveer 335
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 336
		String htmlString = "";
337
		VelocityContext context = new VelocityContext();
338
		String templateFile = "templates/personaldetails.vm";
339
		String email = "";
517 rajveer 340
		String name = "";
569 rajveer 341
		String dateOfBirth = "";
517 rajveer 342
		String sex = "";
343
		String subscribe = "false";
3126 rajveer 344
		UserClient userContextServiceClient = null;
507 rajveer 345
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
346
		try{
555 chandransh 347
			User user = null;
3126 rajveer 348
			userContextServiceClient = new UserClient();
507 rajveer 349
			userClient = userContextServiceClient.getClient();
555 chandransh 350
			user = userClient.getUserById(userId);
507 rajveer 351
 
555 chandransh 352
			email = user.getCommunicationEmail();
353
			name = user.getName();
517 rajveer 354
 
569 rajveer 355
			dateOfBirth = user.getDateOfBirth();
507 rajveer 356
		}catch (Exception e){
2949 chandransh 357
		    logger.error("Unable to get the user for " + userId, e);
507 rajveer 358
		}
517 rajveer 359
		context.put("name", name);
360
		context.put("email", email);
569 rajveer 361
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 362
		context.put("subscribe", subscribe);
363
		context.put("sex", sex);
507 rajveer 364
		htmlString = getHtmlFromVelocity(templateFile, context);
365
		return htmlString;
366
	}
367
 
595 rajveer 368
	public String getMyaccountHeaderHtml() {
507 rajveer 369
		String htmlString = "";
370
		VelocityContext context = new VelocityContext();
371
		String templateFile = "templates/myaccountheader.vm";
372
		htmlString = getHtmlFromVelocity(templateFile, context);
373
		return htmlString;
374
	}
375
 
822 vikas 376
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 377
		String htmlString = "";
378
		VelocityContext context = new VelocityContext();
379
		String templateFile = "templates/shippingaddressdetails.vm";
380
		long defaultAddressId = 0;
517 rajveer 381
		List<Address> addresses = null;
507 rajveer 382
 
3126 rajveer 383
		UserClient userContextServiceClient = null;
507 rajveer 384
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
385
		try {
3126 rajveer 386
			userContextServiceClient = new UserClient();
507 rajveer 387
			userClient = userContextServiceClient.getClient();
620 rajveer 388
 
389
			addresses = userClient.getAllAddressesForUser(userId);
390
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 391
		} catch (Exception e) {
2949 chandransh 392
		    logger.error("Unable to get either the user for " + userId + " or his address", e);
507 rajveer 393
		}
517 rajveer 394
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 395
		context.put("addresses", addresses);
822 vikas 396
		context.put("errorMsg", errorMsg);
507 rajveer 397
 
398
		htmlString = getHtmlFromVelocity(templateFile, context);
399
		return htmlString;
400
	}
401
 
402
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
403
		Properties p = new Properties();
404
		p.setProperty("resource.loader", "class");
405
		p.setProperty("class.resource.loader.class",
406
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 407
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
408
 
2949 chandransh 409
        try {
410
            Velocity.init(p);
411
            // Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
412
            Template template = Velocity.getTemplate(templateFile);
413
            if (template != null) {
414
                StringWriter writer = new StringWriter();
415
                template.merge(context, writer);
416
                writer.flush();
417
                writer.close();
418
                return writer.toString();
419
            }
420
 
421
        } catch (ResourceNotFoundException e) {
422
            logger.error("Unable to find the template file " + templateFile, e);
423
        } catch (ParseErrorException e) {
424
            logger.error("Unable to parse the template file " + templateFile, e);
425
        } catch (MethodInvocationException e) {
426
            logger.error("Unable to invoke methods for the velocity template file " + templateFile, e);
427
        } catch (IOException e) {
428
            logger.error("Unable to read the template file " + templateFile, e);
429
        } catch (Exception e) {
430
            logger.error("Unable to generate the HTML from the template file " + templateFile, e);
431
        }
507 rajveer 432
 
433
		return null;
434
	}
3830 chandransh 435
 
436
	public String getCartWidgetSnippet(int totalItems, double totalAmount) {
437
		String htmlString = "";
438
		VelocityContext context = new VelocityContext();
439
		String templateFile = "templates/cartwidget.vm";
440
		htmlString = getHtmlFromVelocity(templateFile, context);
441
		return htmlString;
442
	}
443
 
1549 rajveer 444
}