Subversion Repositories SmartDukaan

Rev

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