Commit e4d42a34 by Jan Hrabal

tmps

parent 202efda9
......@@ -5,13 +5,13 @@ insert into APP_USER (ID, EMAIL, FIRST_NAME, LAST_NAME, PASSWORD, PASSWORD_SALT,
insert into APP_USER_ROLE (USER_ID, ROLE_ID) values (1, 1);
-- default templates
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (1, 'email.layout', '{{template_content}}');
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (2, 'email.signup.subject', 'Welcome to ...');
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (3, 'email.signup.text', 'Welcome to ...');
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (4, 'email.signup.html', 'Welcome to ...');
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (5, 'email.password.subject', 'Password subject');
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (6, 'email.password.text', 'Password text {{token}}');
insert into SIMPLE_TEMPLATE (ID, CODE, TEMPLATE) values (7, 'email.password.html', 'Password HTML');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (1, 'email.layout', '{{template_content}}');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (2, 'email.signup.subject', 'Welcome to ...');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (3, 'email.signup.text', 'Welcome to ...');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (4, 'email.signup.html', 'Welcome to ...');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (5, 'email.password.subject', 'Password subject');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (6, 'email.password.text', 'Password text {{token}}');
insert into TEMPLATE (ID, CODE, TEMPLATE) values (7, 'email.password.html', 'Password HTML');
-- legal documents
insert into LEGAL_DOCUMENT (ID, CODE, LOCALE, CONTENT) values (1, 'AUTH.TERMS', 'en', '<p>By using the Inodio service you are
......
......@@ -7,7 +7,6 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import com.jh.boot.email.Email;
import com.jh.boot.email.EmailRecipient;
......@@ -27,9 +26,6 @@ public class TemplateEmailAuthServiceListener implements AuthServiceListener {
@Autowired
private TemplateService templateService;
@Value("${template.email.layout:email.layout}")
private String layoutTemplateCode;
@Value("${template.email.signup.subject:email.signup.subject}")
private String signupSubjectCode;
......@@ -77,14 +73,11 @@ public class TemplateEmailAuthServiceListener implements AuthServiceListener {
content = templateService.evaluate(content, data, locale);
html = templateService.evaluate(html, data, locale);
//is layout defined?
String layout = templateService.evaluate(layoutTemplateCode, Collections.singletonMap("content", html), locale);
Email email = new Email();
email.setRecipients(Collections.singleton(new EmailRecipient(user.getEmail(), RecipientType.TO)));
email.setSubject(subject);
email.setText(content);
email.setHtml(StringUtils.hasText(layout) ? layout : html);
email.setHtml(html);
emailService.sendEmail(email);
}
......
......@@ -30,16 +30,27 @@ public class HibernateLoaderTemplateService implements TemplateService {
}
Map<String, Object> values = new HashMap<String, Object>();
values.putAll(data);
if (valuesConfig != null) {
values.putAll(valuesConfig.getValues());
}
values.putAll(data);
String content = t.getTemplate();
StringWriter sw = new StringWriter();
engine.evaluate(sw, content, values);
content = sw.toString();
if (t.getLayout() != null) {
t = loader.loadTemplate(t.getLayout(), locale);
if (t != null) {
values.put("__layout_content__", content);
}
sw = new StringWriter();
engine.evaluate(sw, t.getTemplate(), values);
content = sw.toString();
}
return sw.toString();
return content;
}
@Autowired
......
......@@ -14,8 +14,16 @@ public class HibernateTemplateLoader implements TemplateLoader {
@Override
@Transactional
public Template loadTemplate(String code, Locale locale) {
// TODO Auto-generated method stub
return null;
String l = locale == null ? null : (locale.getLanguage() + "-" + locale.getCountry());
Template t = repository.loadTemplate(code, l);
//TODO refactor
if (t == null) {
t = repository.loadTemplate(code, locale.getLanguage());
}
if (t == null) {
t = repository.loadTemplate(code, null);
}
return t;
}
}
package com.jh.boot.template;
import java.util.List;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.jh.boot.jpa.AbstractHibernateRepository;
@Repository
public class HibernateTemplateRepository extends AbstractHibernateRepository {
public Template loadTemplate(String code, String locale) {
if (code == null) {
return null;
}
String hql = "select t from Template t where lower(code) = :code";
if (StringUtils.hasText(locale)) {
hql += " and lower(locale) = :locale";
} else {
hql += " and locale is null";
}
TypedQuery<Template> q = entityManager.createQuery(hql, Template.class);
q.setParameter("code", code.toLowerCase());
if (StringUtils.hasText(locale)) {
q.setParameter("locale", locale.toLowerCase());
}
List<Template> list = q.getResultList();
return list == null || list.isEmpty() ? null : list.get(0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment