Commit 45b9c4f9 by jhrabal

WIP

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