Commit 45b9c4f9 by jhrabal

WIP

parents 9fd83237 9f7ec9c0
...@@ -5,6 +5,13 @@ import org.springframework.context.annotation.Bean; ...@@ -5,6 +5,13 @@ import org.springframework.context.annotation.Bean;
import com.jh.boot.email.LocalEmailService; import com.jh.boot.email.LocalEmailService;
import com.jh.boot.security.utils.TemplateEmailAuthServiceListener; import com.jh.boot.security.utils.TemplateEmailAuthServiceListener;
import com.jh.boot.template.TemplateEngine;
import com.jh.boot.template.TemplateLoader;
import com.jh.boot.template.TemplateService;
import com.jh.boot.template.customizable.CustomizableLabelsService;
import com.jh.boot.template.customizable.CustomizableTemplateLoader;
import com.jh.boot.template.customizable.CustomizableTemplateService;
import com.jh.boot.template.handlebars.HandlebarsTemplateEngine;
public class JhBootApplication { public class JhBootApplication {
...@@ -21,6 +28,24 @@ public class JhBootApplication { ...@@ -21,6 +28,24 @@ public class JhBootApplication {
return new TemplateEmailAuthServiceListener(); return new TemplateEmailAuthServiceListener();
} }
@Bean
public TemplateEngine templateEngine() {
return new HandlebarsTemplateEngine();
}
@Bean
public TemplateService templateService() {
return new CustomizableTemplateService();
}
@Bean
public CustomizableLabelsService labelsService() {
return new CustomizableLabelsService();
}
@Bean
public TemplateLoader templateLoader() {
return new CustomizableTemplateLoader();
}
} }
...@@ -2,16 +2,20 @@ package com.jh.boot.template; ...@@ -2,16 +2,20 @@ package com.jh.boot.template;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.jh.common.template.handlebars.HandlebarsTemplateService;
public class JhTemplateConfig { public class JhTemplateConfig {
@Bean @Bean
public TemplateEngine templateEngine() { public TemplateLoader templateLoader() {
return null; return null;
} }
@Bean @Bean
public TemplateService templateService() { public TemplateService templateService() {
return null; HandlebarsTemplateService service = new HandlebarsTemplateService();
service.setTemplateLoader(templateLoader());
return service;
} }
......
...@@ -3,6 +3,7 @@ package com.jh.boot.template; ...@@ -3,6 +3,7 @@ package com.jh.boot.template;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient;
import com.jh.boot.jpa.AbstractIdEntity; import com.jh.boot.jpa.AbstractIdEntity;
...@@ -19,7 +20,8 @@ public class Template extends AbstractIdEntity { ...@@ -19,7 +20,8 @@ public class Template extends AbstractIdEntity {
@Column(name = "TEMPLATE") @Column(name = "TEMPLATE")
private String template; private String template;
@Column(name = "TRANSLATE_NAME") // @Column(name = "TRANSLATE_NAME")
@Transient
private Boolean translateName; private Boolean translateName;
public Template() { public Template() {
......
...@@ -25,7 +25,7 @@ public class CustomizableLabelsRepository extends AbstractHibernateRepository { ...@@ -25,7 +25,7 @@ public class CustomizableLabelsRepository extends AbstractHibernateRepository {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<CustomizableTemplateLabel> findCustomizedTemplateLabels(String templateCode, Long unitId) { public List<TemplateLabel> findCustomizedTemplateLabels(String templateCode, Long unitId) {
Query q = entityManager.createQuery("select tl from TemplateLabel tl left join tl.template t where t.code = :templateCode and tl.unitId = :unitId order by tl.position asc"); Query q = entityManager.createQuery("select tl from TemplateLabel tl left join tl.template t where t.code = :templateCode and tl.unitId = :unitId order by tl.position asc");
q.setParameter("templateCode", templateCode); q.setParameter("templateCode", templateCode);
...@@ -36,14 +36,14 @@ public class CustomizableLabelsRepository extends AbstractHibernateRepository { ...@@ -36,14 +36,14 @@ public class CustomizableLabelsRepository extends AbstractHibernateRepository {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<CustomizableTemplateLabel> findDefaultTemplateLabels(String templateCode) { public List<TemplateLabel> findDefaultTemplateLabels(String templateCode) {
Query q = entityManager.createQuery("select tl from TemplateLabel tl left join tl.template t where t.code = :templateCode and tl.unitId is null"); Query q = entityManager.createQuery("select tl from TemplateLabel tl left join tl.template t where t.code = :templateCode and tl.unitId is null");
q.setParameter("templateCode", templateCode); q.setParameter("templateCode", templateCode);
return q.getResultList(); return q.getResultList();
} }
public void saveCustomizedLabels(Collection<CustomizableTemplateLabel> labels) { public void saveCustomizedLabels(Collection<TemplateLabel> labels) {
if (labels == null) { if (labels == null) {
return; return;
} }
......
...@@ -25,11 +25,11 @@ public class CustomizableLabelsService { ...@@ -25,11 +25,11 @@ public class CustomizableLabelsService {
@Transactional @Transactional
public Map<String, CustomizableTemplateLabel> getCustomizationTemplateLabels(String templateCode, Long unitId, String defaultLanguage) { public Map<String, TemplateLabel> getCustomizationTemplateLabels(String templateCode, Long unitId, String defaultLanguage) {
TemplateLabels templateLabels = getTemplateDefaultLabels(templateCode); TemplateLabels templateLabels = getTemplateDefaultLabels(templateCode);
List<CustomizableTemplateLabel> labels = repo.findCustomizedTemplateLabels(templateCode, unitId); List<TemplateLabel> labels = repo.findCustomizedTemplateLabels(templateCode, unitId);
Map<String, CustomizableTemplateLabel> defaultMap = templateLabels.getLanguages().get(defaultLanguage); Map<String, TemplateLabel> defaultMap = templateLabels.getLanguages().get(defaultLanguage);
if (defaultMap == null) { if (defaultMap == null) {
defaultMap = templateLabels.getFallbackLabels(); defaultMap = templateLabels.getFallbackLabels();
} }
...@@ -37,11 +37,11 @@ public class CustomizableLabelsService { ...@@ -37,11 +37,11 @@ public class CustomizableLabelsService {
return null; return null;
} }
Map<String, CustomizableTemplateLabel> result = new HashMap<>(); Map<String, TemplateLabel> result = new HashMap<>();
for (String code : templateLabels.getCodes()) { for (String code : templateLabels.getCodes()) {
CustomizableTemplateLabel label = defaultMap.get(code); TemplateLabel label = defaultMap.get(code);
CustomizableTemplateLabel newLabel = new CustomizableTemplateLabel(); TemplateLabel newLabel = new TemplateLabel();
newLabel.setCode(code); newLabel.setCode(code);
newLabel.setLabel(label.getLabel()); newLabel.setLabel(label.getLabel());
newLabel.setDefaultLabel(label.getLabel()); newLabel.setDefaultLabel(label.getLabel());
...@@ -50,8 +50,8 @@ public class CustomizableLabelsService { ...@@ -50,8 +50,8 @@ public class CustomizableLabelsService {
result.put(code, newLabel); result.put(code, newLabel);
} }
for (CustomizableTemplateLabel label : labels) { for (TemplateLabel label : labels) {
CustomizableTemplateLabel defaultLabel = result.get(label.getCode()); TemplateLabel defaultLabel = result.get(label.getCode());
if (defaultLabel != null) { if (defaultLabel != null) {
label.setDefaultLabel(defaultLabel.getLabel()); label.setDefaultLabel(defaultLabel.getLabel());
} }
...@@ -64,9 +64,9 @@ public class CustomizableLabelsService { ...@@ -64,9 +64,9 @@ public class CustomizableLabelsService {
@Transactional @Transactional
public Map<String, String> getTranslatedTemplateLabels(String templateCode, Long unitId, String defaultLanguage) { public Map<String, String> getTranslatedTemplateLabels(String templateCode, Long unitId, String defaultLanguage) {
TemplateLabels templateLabels = getTemplateDefaultLabels(templateCode); TemplateLabels templateLabels = getTemplateDefaultLabels(templateCode);
List<CustomizableTemplateLabel> labels = repo.findCustomizedTemplateLabels(templateCode, unitId); List<TemplateLabel> labels = repo.findCustomizedTemplateLabels(templateCode, unitId);
Map<String, CustomizableTemplateLabel> defaultMap = templateLabels.getLanguages().get(defaultLanguage); Map<String, TemplateLabel> defaultMap = templateLabels.getLanguages().get(defaultLanguage);
if (defaultMap == null) { if (defaultMap == null) {
defaultMap = templateLabels.getFallbackLabels(); defaultMap = templateLabels.getFallbackLabels();
} }
...@@ -76,13 +76,13 @@ public class CustomizableLabelsService { ...@@ -76,13 +76,13 @@ public class CustomizableLabelsService {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
for (String code : templateLabels.getCodes()) { for (String code : templateLabels.getCodes()) {
CustomizableTemplateLabel label = defaultMap.get(code); TemplateLabel label = defaultMap.get(code);
if (label != null) { if (label != null) {
result.put(code, label.getLabel()); result.put(code, label.getLabel());
} }
} }
for (CustomizableTemplateLabel label : labels) { for (TemplateLabel label : labels) {
result.put(label.getCode(), label.getLabel()); result.put(label.getCode(), label.getLabel());
} }
...@@ -90,7 +90,7 @@ public class CustomizableLabelsService { ...@@ -90,7 +90,7 @@ public class CustomizableLabelsService {
} }
@Transactional @Transactional
public List<CustomizableTemplateLabel> fetchLabels(String templateCode, Long unitId) { public List<TemplateLabel> fetchLabels(String templateCode, Long unitId) {
return repo.findCustomizedTemplateLabels(templateCode, unitId); return repo.findCustomizedTemplateLabels(templateCode, unitId);
} }
...@@ -104,20 +104,20 @@ public class CustomizableLabelsService { ...@@ -104,20 +104,20 @@ public class CustomizableLabelsService {
Set<String> codes = new LinkedHashSet<>(); Set<String> codes = new LinkedHashSet<>();
Map<String, Map<String, CustomizableTemplateLabel>> maps = new HashMap<>(); Map<String, Map<String, TemplateLabel>> maps = new HashMap<>();
Map<String, CustomizableTemplateLabel> fallbackMap = new HashMap<>(); Map<String, TemplateLabel> fallbackMap = new HashMap<>();
List<CustomizableTemplateLabel> defaultLabels = repo.findDefaultTemplateLabels(templateCode); List<TemplateLabel> defaultLabels = repo.findDefaultTemplateLabels(templateCode);
//keep order //keep order
for (CustomizableTemplateLabel ctl : defaultLabels) { for (TemplateLabel ctl : defaultLabels) {
codes.add(ctl.getCode()); codes.add(ctl.getCode());
} }
//map labels without order //map labels without order
for (CustomizableTemplateLabel ctl : defaultLabels) { for (TemplateLabel ctl : defaultLabels) {
Map<String, CustomizableTemplateLabel> map = maps.get(ctl.getLanguageCode()); Map<String, TemplateLabel> map = maps.get(ctl.getLanguageCode());
if (map == null) { if (map == null) {
maps.put(ctl.getLanguageCode(), map = new HashMap<>()); maps.put(ctl.getLanguageCode(), map = new HashMap<>());
} }
...@@ -136,10 +136,10 @@ public class CustomizableLabelsService { ...@@ -136,10 +136,10 @@ public class CustomizableLabelsService {
} }
// align maps // align maps
for (Entry<String, Map<String, CustomizableTemplateLabel>> entry : maps.entrySet()) { for (Entry<String, Map<String, TemplateLabel>> entry : maps.entrySet()) {
Map<String, CustomizableTemplateLabel> langMap = labels.languages.get(entry.getKey()); Map<String, TemplateLabel> langMap = labels.languages.get(entry.getKey());
for (String code : codes) { for (String code : codes) {
CustomizableTemplateLabel ctl = entry.getValue().get(code); TemplateLabel ctl = entry.getValue().get(code);
if (ctl == null) { if (ctl == null) {
ctl = fallbackMap.get(code); ctl = fallbackMap.get(code);
} }
...@@ -159,7 +159,7 @@ public class CustomizableLabelsService { ...@@ -159,7 +159,7 @@ public class CustomizableLabelsService {
@Transactional @Transactional
public void saveCustomizedLabels(Collection<CustomizableTemplateLabel> labels) { public void saveCustomizedLabels(Collection<TemplateLabel> labels) {
repo.saveCustomizedLabels(labels); repo.saveCustomizedLabels(labels);
} }
...@@ -207,9 +207,9 @@ public class CustomizableLabelsService { ...@@ -207,9 +207,9 @@ public class CustomizableLabelsService {
Set<String> codes = new LinkedHashSet<>(); Set<String> codes = new LinkedHashSet<>();
Map<String, Map<String, CustomizableTemplateLabel>> languages = new HashMap<>(); Map<String, Map<String, TemplateLabel>> languages = new HashMap<>();
Map<String, CustomizableTemplateLabel> fallbackLabels = new HashMap<>(); Map<String, TemplateLabel> fallbackLabels = new HashMap<>();
@Override @Override
public Set<String> getCodes() { public Set<String> getCodes() {
...@@ -217,12 +217,12 @@ public class CustomizableLabelsService { ...@@ -217,12 +217,12 @@ public class CustomizableLabelsService {
} }
@Override @Override
public Map<String, Map<String, CustomizableTemplateLabel>> getLanguages() { public Map<String, Map<String, TemplateLabel>> getLanguages() {
return languages; return languages;
} }
@Override @Override
public Map<String, CustomizableTemplateLabel> getFallbackLabels() { public Map<String, TemplateLabel> getFallbackLabels() {
return fallbackLabels; return fallbackLabels;
} }
......
package com.jh.boot.template.customizable;
import org.springframework.beans.factory.annotation.Autowired;
import com.jh.boot.template.Template;
import com.jh.boot.template.TemplateLoader;
public class CustomizableTemplateLoader implements TemplateLoader {
@Autowired
private CustomizableLabelsRepository repository;
@Override
public String loadTemplate(String code) {
Template t = repository.fetchTemplate(code);
if (t == null) {
return null;
}
return t.getTemplate();
}
}
...@@ -29,7 +29,7 @@ public class CustomizableTemplateService implements TemplateService { ...@@ -29,7 +29,7 @@ public class CustomizableTemplateService implements TemplateService {
Map<String, String> labels = labelsService.getTranslatedTemplateLabels(templateCode, null, locale.toString()); Map<String, String> labels = labelsService.getTranslatedTemplateLabels(templateCode, null, locale.toString());
data = new HashMap<String, Object>(data); data = new HashMap<String, Object>(data);
data.put("labels", locale); data.put("labels", labels);
engine.evaluate(writer, templateCode, data); engine.evaluate(writer, templateCode, data);
} }
......
...@@ -14,7 +14,7 @@ import com.jh.boot.template.Template; ...@@ -14,7 +14,7 @@ import com.jh.boot.template.Template;
@Entity @Entity
@Table(name = "CUSTOMIZABLE_TEMPLATE_LABEL") @Table(name = "CUSTOMIZABLE_TEMPLATE_LABEL")
public class CustomizableTemplateLabel extends AbstractIdEntity { public class TemplateLabel extends AbstractIdEntity {
@ManyToOne @ManyToOne
@JoinColumn(name="TEMPLATE_ID", nullable=false, updatable=false) @JoinColumn(name="TEMPLATE_ID", nullable=false, updatable=false)
......
...@@ -7,9 +7,9 @@ public interface TemplateLabels { ...@@ -7,9 +7,9 @@ public interface TemplateLabels {
Set<String> getCodes(); Set<String> getCodes();
Map<String, Map<String, CustomizableTemplateLabel>> getLanguages(); Map<String, Map<String, TemplateLabel>> getLanguages();
Map<String, CustomizableTemplateLabel> getFallbackLabels(); Map<String, TemplateLabel> getFallbackLabels();
} }
\ No newline at end of file
...@@ -11,6 +11,7 @@ import java.util.Objects; ...@@ -11,6 +11,7 @@ import java.util.Objects;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import com.github.jknack.handlebars.Context; import com.github.jknack.handlebars.Context;
...@@ -26,6 +27,7 @@ import com.jh.boot.template.TemplateEngine; ...@@ -26,6 +27,7 @@ import com.jh.boot.template.TemplateEngine;
import com.jh.boot.template.TemplateLoader; import com.jh.boot.template.TemplateLoader;
/** /**
* *
* *
...@@ -216,7 +218,14 @@ public class HandlebarsTemplateEngine implements TemplateEngine { ...@@ -216,7 +218,14 @@ public class HandlebarsTemplateEngine implements TemplateEngine {
@Value("${global.caching:true}") @Value("${global.caching:true}")
public void setUseCache(boolean useCache) { public void setUseCache(boolean useCache) {
this.useCache = useCache; this.useCache = useCache;
}
@Autowired
public void setTemplateLoader(TemplateLoader templateLoader) {
this.templateLoader = templateLoader;
} }
} }
package com.jh.common.template;
public interface TemplateLoader {
String loadTemplate(String templateName);
}
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