Subversion Repositories SmartDukaan

Rev

Rev 4815 | Rev 6152 | 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
 
153
	public String getHeaderHtml(boolean isLoggedIn, String email, 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);
167
 
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());
196
			deliveryEstimate = new Date(order.getExpected_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);
517 rajveer 242
		context.put("orderedOn", dateformat.format(orderedOn));
583 rajveer 243
		context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
4325 mandeep.dh 244
		context.put("addresses", addresses);
245
		context.put("defaultAddressId", defaultAddressId);
4815 phani.kuma 246
		context.put("userinfo", userinfo);
247
		context.put("initiateOrderCancelation", initiateOrderCancelation);
248
		context.put("requestOrderCancelation", requestOrderCancelation);
4325 mandeep.dh 249
 
843 chandransh 250
		if(provider!=null){
251
			context.put("providerName", provider.getName());
252
		}
253
 
507 rajveer 254
		htmlString = getHtmlFromVelocity(templateFile, context);
255
		return htmlString;
256
	}
257
 
650 rajveer 258
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 259
		String htmlString = "";
260
		VelocityContext context = new VelocityContext();
261
		String templateFile = "templates/myaccount.vm";
262
		List<Order> orders = null;
745 chandransh 263
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 264
		try{
3126 rajveer 265
			TransactionClient transactionServiceClient = new TransactionClient();
745 chandransh 266
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 267
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
268
 
3126 rajveer 269
			LogisticsClient logisticsServiceClient = new LogisticsClient();
745 chandransh 270
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
271
			List<Provider> providers = logisticsClient.getAllProviders();
272
			for(Provider provider: providers)
273
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 274
		}catch (Exception e){
2949 chandransh 275
			logger.error("Unable to get order or provider details", e);
507 rajveer 276
		}
741 rajveer 277
		List<String> orderDate = new ArrayList<String>();
278
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 279
		if(orders!=null && !orders.isEmpty()){
762 rajveer 280
			for(Order order: orders){
281
				Date orderedOn = new Date(order.getCreated_timestamp());
282
				orderDate.add(dateformat.format(orderedOn));
283
			}
741 rajveer 284
		}
507 rajveer 285
		context.put("orders", orders);
741 rajveer 286
		context.put("orderDate", orderDate);
745 chandransh 287
		context.put("providerNames", providerNames);
507 rajveer 288
		htmlString = getHtmlFromVelocity(templateFile, context);
289
		return htmlString;
290
	}
291
 
292
 
650 rajveer 293
	public String getLoginDetailsHtml(long userId) {
507 rajveer 294
		String htmlString = "";
295
		VelocityContext context = new VelocityContext();
296
		String templateFile = "templates/logindetails.vm";
297
		String email = "";
298
		try{
762 rajveer 299
			email = getEmailId(userId);
507 rajveer 300
		}catch (Exception e){
301
 
302
		}
303
		context.put("email", email);
304
		htmlString = getHtmlFromVelocity(templateFile, context);
305
		return htmlString;
306
	}
307
 
762 rajveer 308
	public String getEmailId(long userId){
309
		String email = " ";
310
 
311
		try {
3126 rajveer 312
			UserClient userContextServiceClient = new UserClient();
762 rajveer 313
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
2949 chandransh 314
			String userEmail = userClient.getUserById(userId).getEmail(); 
315
			if( userEmail != null){
316
				email = userEmail;
762 rajveer 317
			}
318
		} catch (UserContextException e) {
2949 chandransh 319
			logger.error("Unable to get the user for " + userId, e);
762 rajveer 320
		} catch (TException e) {
2949 chandransh 321
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 322
		} catch (Exception e) {
2949 chandransh 323
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 324
		}
325
		return email; 
326
	}
327
 
328
 
595 rajveer 329
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 330
		String htmlString = "";
331
		VelocityContext context = new VelocityContext();
332
		String templateFile = "templates/personaldetails.vm";
333
		String email = "";
517 rajveer 334
		String name = "";
569 rajveer 335
		String dateOfBirth = "";
517 rajveer 336
		String sex = "";
337
		String subscribe = "false";
3126 rajveer 338
		UserClient userContextServiceClient = null;
507 rajveer 339
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
340
		try{
555 chandransh 341
			User user = null;
3126 rajveer 342
			userContextServiceClient = new UserClient();
507 rajveer 343
			userClient = userContextServiceClient.getClient();
555 chandransh 344
			user = userClient.getUserById(userId);
507 rajveer 345
 
555 chandransh 346
			email = user.getCommunicationEmail();
347
			name = user.getName();
517 rajveer 348
 
569 rajveer 349
			dateOfBirth = user.getDateOfBirth();
507 rajveer 350
		}catch (Exception e){
2949 chandransh 351
		    logger.error("Unable to get the user for " + userId, e);
507 rajveer 352
		}
517 rajveer 353
		context.put("name", name);
354
		context.put("email", email);
569 rajveer 355
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 356
		context.put("subscribe", subscribe);
357
		context.put("sex", sex);
507 rajveer 358
		htmlString = getHtmlFromVelocity(templateFile, context);
359
		return htmlString;
360
	}
361
 
595 rajveer 362
	public String getMyaccountHeaderHtml() {
507 rajveer 363
		String htmlString = "";
364
		VelocityContext context = new VelocityContext();
365
		String templateFile = "templates/myaccountheader.vm";
366
		htmlString = getHtmlFromVelocity(templateFile, context);
367
		return htmlString;
368
	}
369
 
822 vikas 370
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 371
		String htmlString = "";
372
		VelocityContext context = new VelocityContext();
373
		String templateFile = "templates/shippingaddressdetails.vm";
374
		long defaultAddressId = 0;
517 rajveer 375
		List<Address> addresses = null;
507 rajveer 376
 
3126 rajveer 377
		UserClient userContextServiceClient = null;
507 rajveer 378
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
379
		try {
3126 rajveer 380
			userContextServiceClient = new UserClient();
507 rajveer 381
			userClient = userContextServiceClient.getClient();
620 rajveer 382
 
383
			addresses = userClient.getAllAddressesForUser(userId);
384
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 385
		} catch (Exception e) {
2949 chandransh 386
		    logger.error("Unable to get either the user for " + userId + " or his address", e);
507 rajveer 387
		}
517 rajveer 388
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 389
		context.put("addresses", addresses);
822 vikas 390
		context.put("errorMsg", errorMsg);
507 rajveer 391
 
392
		htmlString = getHtmlFromVelocity(templateFile, context);
393
		return htmlString;
394
	}
395
 
396
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
397
		Properties p = new Properties();
398
		p.setProperty("resource.loader", "class");
399
		p.setProperty("class.resource.loader.class",
400
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 401
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
402
 
2949 chandransh 403
        try {
404
            Velocity.init(p);
405
            // Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
406
            Template template = Velocity.getTemplate(templateFile);
407
            if (template != null) {
408
                StringWriter writer = new StringWriter();
409
                template.merge(context, writer);
410
                writer.flush();
411
                writer.close();
412
                return writer.toString();
413
            }
414
 
415
        } catch (ResourceNotFoundException e) {
416
            logger.error("Unable to find the template file " + templateFile, e);
417
        } catch (ParseErrorException e) {
418
            logger.error("Unable to parse the template file " + templateFile, e);
419
        } catch (MethodInvocationException e) {
420
            logger.error("Unable to invoke methods for the velocity template file " + templateFile, e);
421
        } catch (IOException e) {
422
            logger.error("Unable to read the template file " + templateFile, e);
423
        } catch (Exception e) {
424
            logger.error("Unable to generate the HTML from the template file " + templateFile, e);
425
        }
507 rajveer 426
 
427
		return null;
428
	}
3830 chandransh 429
 
430
	public String getCartWidgetSnippet(int totalItems, double totalAmount) {
431
		String htmlString = "";
432
		VelocityContext context = new VelocityContext();
433
		String templateFile = "templates/cartwidget.vm";
434
		FormattingUtils formattingUtils = new FormattingUtils(0);
435
		context.put("itemCount", totalItems);
436
		context.put("totalAmount", formattingUtils.formatPrice(totalAmount));
437
 
438
		htmlString = getHtmlFromVelocity(templateFile, context);
439
		return htmlString;
440
	}
441
 
1549 rajveer 442
}