Subversion Repositories SmartDukaan

Rev

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