Subversion Repositories SmartDukaan

Rev

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