1 package de.spring.emails.services.impl;
3 import java.util.HashMap;
4 import java.util.Locale;
7 import org.apache.velocity.app.VelocityEngine;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.context.MessageSource;
10 import org.springframework.stereotype.Service;
11 import org.springframework.ui.velocity.VelocityEngineUtils;
13 import de.spring.emails.services.EmailMakerService;
16 @Service("emailMakerService")
17 public class EmailMakerServiceImpl implements EmailMakerService {
18 private static final String TEMPLATES_DEFAULT_EXTENSION = ".vm";
19 private static final String TEMPLATES_DEFAULT_PATH = "email/";
20 private static final String EMAIL_CONTENT_ENCODING = "UTF-8";
22 private final VelocityEngine velocityEngine;
23 private final MessageSource messageSource;
26 public EmailMakerServiceImpl(VelocityEngine velocityEngine, MessageSource messageSource) {
27 this.velocityEngine = velocityEngine;
28 this.messageSource = messageSource;
32 public String emailMaker(Map<String, String> text, String templateName, Locale locale) {
33 final String templateLocation = TEMPLATES_DEFAULT_PATH + templateName + TEMPLATES_DEFAULT_EXTENSION;
34 final Map<String, Object> model = new HashMap<>();
35 model.put("text", text);
36 model.put("messageSource", messageSource);
37 model.put("locale", locale);
39 return VelocityEngineUtils.mergeTemplateIntoString(
40 velocityEngine, templateLocation, EMAIL_CONTENT_ENCODING, model);
44 public String getSubject(String code, Locale locale, Object... args) {
45 return messageSource.getMessage(code, args, locale);