Subversion Repositories SmartDukaan

Rev

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