Subversion Repositories SmartDukaan

Rev

Rev 4183 | Rev 4325 | 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
	}
149
 
150
 
151
 
3903 varun.gupt 152
	public String getHeaderHtml(boolean isLoggedIn, String  userName, String url, long catId, boolean displayBestDealsImg) {
550 rajveer 153
		VelocityContext context = new VelocityContext();
555 chandransh 154
		if (isLoggedIn) {
155
			context.put("LOGGED_IN", "TRUE");
4185 rajveer 156
			context.put("WELCOME_MESSAGE", "Welcome " + userName);
555 chandransh 157
		} else {
801 rajveer 158
			context.put("WELCOME_MESSAGE", "Hi, Welcome to Saholic");
924 vikas 159
			context.put("REDIRECT_URL", url);
555 chandransh 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;
507 rajveer 178
		try{
3126 rajveer 179
			TransactionClient transactionServiceClient = new TransactionClient();
843 chandransh 180
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
1527 ankur.sing 181
			order = orderClient.getOrderForCustomer(orderId, userId);
517 rajveer 182
			orderedOn = new Date(order.getCreated_timestamp());
183
			deliveryEstimate = new Date(order.getExpected_delivery_time());
843 chandransh 184
 
185
			if(order.getLogistics_provider_id() != 0){
3126 rajveer 186
				LogisticsClient logisticsServiceClient = new LogisticsClient();
843 chandransh 187
				in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
188
				provider = logisticsClient.getProvider(order.getLogistics_provider_id());
189
			}
507 rajveer 190
		}catch (Exception e){
191
 
192
		}
517 rajveer 193
 
194
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
583 rajveer 195
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy");
507 rajveer 196
		context.put("order", order);
517 rajveer 197
		context.put("orderedOn", dateformat.format(orderedOn));
583 rajveer 198
		context.put("deliveryEstimate", dateformat1.format(deliveryEstimate));
843 chandransh 199
		if(provider!=null){
200
			context.put("providerName", provider.getName());
201
		}
202
 
507 rajveer 203
		htmlString = getHtmlFromVelocity(templateFile, context);
204
		return htmlString;
205
	}
206
 
650 rajveer 207
	public String getMyaccountDetailsHtml(long userId) {
507 rajveer 208
		String htmlString = "";
209
		VelocityContext context = new VelocityContext();
210
		String templateFile = "templates/myaccount.vm";
211
		List<Order> orders = null;
745 chandransh 212
		Map<Long, String> providerNames = new HashMap<Long, String>();
507 rajveer 213
		try{
3126 rajveer 214
			TransactionClient transactionServiceClient = new TransactionClient();
745 chandransh 215
			in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
507 rajveer 216
			orders = orderClient.getOrdersForCustomer(userId, 0, (new Date()).getTime(), null);
217
 
3126 rajveer 218
			LogisticsClient logisticsServiceClient = new LogisticsClient();
745 chandransh 219
			in.shop2020.logistics.LogisticsService.Client logisticsClient = logisticsServiceClient.getClient();
220
			List<Provider> providers = logisticsClient.getAllProviders();
221
			for(Provider provider: providers)
222
				providerNames.put(provider.getId(), provider.getName());
507 rajveer 223
		}catch (Exception e){
2949 chandransh 224
			logger.error("Unable to get order or provider details", e);
507 rajveer 225
		}
741 rajveer 226
		List<String> orderDate = new ArrayList<String>();
227
		SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
888 chandransh 228
		if(orders!=null && !orders.isEmpty()){
762 rajveer 229
			for(Order order: orders){
230
				Date orderedOn = new Date(order.getCreated_timestamp());
231
				orderDate.add(dateformat.format(orderedOn));
232
			}
741 rajveer 233
		}
507 rajveer 234
		context.put("orders", orders);
741 rajveer 235
		context.put("orderDate", orderDate);
745 chandransh 236
		context.put("providerNames", providerNames);
507 rajveer 237
		htmlString = getHtmlFromVelocity(templateFile, context);
238
		return htmlString;
239
	}
240
 
241
 
650 rajveer 242
	public String getLoginDetailsHtml(long userId) {
507 rajveer 243
		String htmlString = "";
244
		VelocityContext context = new VelocityContext();
245
		String templateFile = "templates/logindetails.vm";
246
		String email = "";
247
		try{
762 rajveer 248
			email = getEmailId(userId);
507 rajveer 249
		}catch (Exception e){
250
 
251
		}
252
		context.put("email", email);
253
		htmlString = getHtmlFromVelocity(templateFile, context);
254
		return htmlString;
255
	}
256
 
762 rajveer 257
	public String getEmailId(long userId){
258
		String email = " ";
259
 
260
		try {
3126 rajveer 261
			UserClient userContextServiceClient = new UserClient();
762 rajveer 262
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
2949 chandransh 263
			String userEmail = userClient.getUserById(userId).getEmail(); 
264
			if( userEmail != null){
265
				email = userEmail;
762 rajveer 266
			}
267
		} catch (UserContextException e) {
2949 chandransh 268
			logger.error("Unable to get the user for " + userId, e);
762 rajveer 269
		} catch (TException e) {
2949 chandransh 270
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 271
		} catch (Exception e) {
2949 chandransh 272
		    logger.error("Unable to get the user for " + userId, e);
762 rajveer 273
		}
274
		return email; 
275
	}
276
 
277
 
595 rajveer 278
	public String getPersonalDetailsHtml(long userId) {
507 rajveer 279
		String htmlString = "";
280
		VelocityContext context = new VelocityContext();
281
		String templateFile = "templates/personaldetails.vm";
282
		String email = "";
517 rajveer 283
		String name = "";
569 rajveer 284
		String dateOfBirth = "";
517 rajveer 285
		String sex = "";
286
		String subscribe = "false";
3126 rajveer 287
		UserClient userContextServiceClient = null;
507 rajveer 288
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
289
		try{
555 chandransh 290
			User user = null;
3126 rajveer 291
			userContextServiceClient = new UserClient();
507 rajveer 292
			userClient = userContextServiceClient.getClient();
555 chandransh 293
			user = userClient.getUserById(userId);
507 rajveer 294
 
555 chandransh 295
			email = user.getCommunicationEmail();
296
			name = user.getName();
517 rajveer 297
 
569 rajveer 298
			dateOfBirth = user.getDateOfBirth();
507 rajveer 299
		}catch (Exception e){
2949 chandransh 300
		    logger.error("Unable to get the user for " + userId, e);
507 rajveer 301
		}
517 rajveer 302
		context.put("name", name);
303
		context.put("email", email);
569 rajveer 304
		context.put("dateOfBirth", dateOfBirth+"");
517 rajveer 305
		context.put("subscribe", subscribe);
306
		context.put("sex", sex);
507 rajveer 307
		htmlString = getHtmlFromVelocity(templateFile, context);
308
		return htmlString;
309
	}
310
 
595 rajveer 311
	public String getMyaccountHeaderHtml() {
507 rajveer 312
		String htmlString = "";
313
		VelocityContext context = new VelocityContext();
314
		String templateFile = "templates/myaccountheader.vm";
315
		htmlString = getHtmlFromVelocity(templateFile, context);
316
		return htmlString;
317
	}
318
 
822 vikas 319
	public String getShippingAddressDetailsHtml(long userId, String errorMsg){
507 rajveer 320
		String htmlString = "";
321
		VelocityContext context = new VelocityContext();
322
		String templateFile = "templates/shippingaddressdetails.vm";
323
		long defaultAddressId = 0;
517 rajveer 324
		List<Address> addresses = null;
507 rajveer 325
 
3126 rajveer 326
		UserClient userContextServiceClient = null;
507 rajveer 327
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
328
		try {
3126 rajveer 329
			userContextServiceClient = new UserClient();
507 rajveer 330
			userClient = userContextServiceClient.getClient();
620 rajveer 331
 
332
			addresses = userClient.getAllAddressesForUser(userId);
333
			defaultAddressId = userClient.getDefaultAddressId(userId);
507 rajveer 334
		} catch (Exception e) {
2949 chandransh 335
		    logger.error("Unable to get either the user for " + userId + " or his address", e);
507 rajveer 336
		}
517 rajveer 337
		context.put("defaultAddressId", defaultAddressId+"");
507 rajveer 338
		context.put("addresses", addresses);
822 vikas 339
		context.put("errorMsg", errorMsg);
507 rajveer 340
 
341
		htmlString = getHtmlFromVelocity(templateFile, context);
342
		return htmlString;
343
	}
344
 
345
	public String getHtmlFromVelocity(String templateFile, VelocityContext context){
346
		Properties p = new Properties();
347
		p.setProperty("resource.loader", "class");
348
		p.setProperty("class.resource.loader.class",
349
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
832 rajveer 350
		p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
351
 
2949 chandransh 352
        try {
353
            Velocity.init(p);
354
            // Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, value)
355
            Template template = Velocity.getTemplate(templateFile);
356
            if (template != null) {
357
                StringWriter writer = new StringWriter();
358
                template.merge(context, writer);
359
                writer.flush();
360
                writer.close();
361
                return writer.toString();
362
            }
363
 
364
        } catch (ResourceNotFoundException e) {
365
            logger.error("Unable to find the template file " + templateFile, e);
366
        } catch (ParseErrorException e) {
367
            logger.error("Unable to parse the template file " + templateFile, e);
368
        } catch (MethodInvocationException e) {
369
            logger.error("Unable to invoke methods for the velocity template file " + templateFile, e);
370
        } catch (IOException e) {
371
            logger.error("Unable to read the template file " + templateFile, e);
372
        } catch (Exception e) {
373
            logger.error("Unable to generate the HTML from the template file " + templateFile, e);
374
        }
507 rajveer 375
 
376
		return null;
377
	}
3830 chandransh 378
 
379
	public String getCartWidgetSnippet(int totalItems, double totalAmount) {
380
		String htmlString = "";
381
		VelocityContext context = new VelocityContext();
382
		String templateFile = "templates/cartwidget.vm";
383
		FormattingUtils formattingUtils = new FormattingUtils(0);
384
		context.put("itemCount", totalItems);
385
		context.put("totalAmount", formattingUtils.formatPrice(totalAmount));
386
 
387
		htmlString = getHtmlFromVelocity(templateFile, context);
388
		return htmlString;
389
	}
390
 
1549 rajveer 391
}