Subversion Repositories SmartDukaan

Rev

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