Commit 11d159bd by Jan Hrabal

wip

parent 31bd2fe9
...@@ -6,12 +6,11 @@ import org.springframework.context.annotation.Bean; ...@@ -6,12 +6,11 @@ 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.HibernateTemplateLoader;
import com.jh.boot.template.HibernateLoaderTemplateService;
import com.jh.boot.template.TemplateEngine; import com.jh.boot.template.TemplateEngine;
import com.jh.boot.template.TemplateLoader; import com.jh.boot.template.TemplateLoader;
import com.jh.boot.template.TemplateService; 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; import com.jh.boot.template.handlebars.HandlebarsTemplateEngine;
public class JhBootApplication { public class JhBootApplication {
...@@ -36,19 +35,16 @@ public class JhBootApplication { ...@@ -36,19 +35,16 @@ public class JhBootApplication {
} }
@Bean @Bean
@ConditionalOnMissingBean(TemplateService.class) @ConditionalOnMissingBean(TemplateLoader.class)
public TemplateService templateService() { public TemplateLoader templateLoader() {
return new CustomizableTemplateService(); return new HibernateTemplateLoader();
} }
@Bean @Bean
public CustomizableLabelsService labelsService() { @ConditionalOnMissingBean(TemplateService.class)
return new CustomizableLabelsService(); public TemplateService templateService() {
return new HibernateLoaderTemplateService();
} }
@Bean
public TemplateLoader templateLoader() {
return new CustomizableTemplateLoader();
}
} }
package com.jh.boot.template;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
public class HibernateLoaderTemplateService implements TemplateService {
private static final Logger LOG = LoggerFactory.getLogger(HibernateLoaderTemplateService.class);
private TemplateLoader loader;
private TemplateEngine engine;
private TemplateValuesConfig valuesConfig;
@Override
@Transactional
public String evaluate(String templateCode, Map<String, Object> data, Locale locale) {
Template t = loader.loadTemplate(templateCode, locale);
if (t == null) {
LOG.warn("Cannot find template {}", templateCode);
return null;
}
Map<String, Object> values = new HashMap<String, Object>();
values.putAll(data);
if (valuesConfig != null) {
values.putAll(valuesConfig.getValues());
}
String content = t.getTemplate();
StringWriter sw = new StringWriter();
engine.evaluate(sw, content, values);
return sw.toString();
}
@Autowired
public void setLoader(TemplateLoader loader) {
this.loader = loader;
}
@Autowired
public void setEngine(TemplateEngine engine) {
this.engine = engine;
}
@Autowired
public void setValuesConfig(TemplateValuesConfig valuesConfig) {
this.valuesConfig = valuesConfig;
}
}
package com.jh.boot.template;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
public class HibernateTemplateLoader implements TemplateLoader {
@Autowired
private HibernateTemplateRepository repository;
@Override
@Transactional
public Template loadTemplate(String code, Locale locale) {
// TODO Auto-generated method stub
return null;
}
}
package com.jh.boot.template;
import org.springframework.stereotype.Repository;
import com.jh.boot.jpa.AbstractHibernateRepository;
@Repository
public class HibernateTemplateRepository extends AbstractHibernateRepository {
}
...@@ -13,12 +13,15 @@ public class Template extends AbstractIdEntity { ...@@ -13,12 +13,15 @@ public class Template extends AbstractIdEntity {
@Column(name = "CODE") @Column(name = "CODE")
private String code; private String code;
@Column(name = "NAME") @Column(name = "LAYOUT")
private String name; private String layout;
@Column(name = "TEMPLATE") @Column(name = "TEMPLATE")
private String template; private String template;
@Column(name = "LOCALE")
private String locale;
public Template() { public Template() {
super(); super();
} }
...@@ -36,12 +39,12 @@ public class Template extends AbstractIdEntity { ...@@ -36,12 +39,12 @@ public class Template extends AbstractIdEntity {
this.code = code; this.code = code;
} }
public String getName() { public String getLayout() {
return name; return layout;
} }
public void setName(String name) { public void setLayout(String name) {
this.name = name; this.layout = name;
} }
public String getTemplate() { public String getTemplate() {
......
...@@ -5,7 +5,5 @@ import java.util.Map; ...@@ -5,7 +5,5 @@ import java.util.Map;
public interface TemplateEngine { public interface TemplateEngine {
void evaluate(Writer writer, String templateCode, Map<String, Object> data); void evaluate(Writer writer, String templateContent, Map<String, Object> data);
void evaluateInline(Writer writer, String template, Map<String, Object> data);
} }
\ No newline at end of file
package com.jh.boot.template; package com.jh.boot.template;
import java.util.Locale;
public interface TemplateLoader { public interface TemplateLoader {
Template loadTemplate(String code); Template loadTemplate(String code, Locale locale);
} }
package com.jh.boot.template;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "template")
public class TemplateValuesConfig {
private Map<String, String> values;
public Map<String, String> getValues() {
return values;
}
public void setValues(Map<String, String> values) {
this.values = values;
}
}
...@@ -72,8 +72,6 @@ public class HandlebarsTemplateEngine implements TemplateEngine { ...@@ -72,8 +72,6 @@ public class HandlebarsTemplateEngine implements TemplateEngine {
private Handlebars handlebars; private Handlebars handlebars;
private TemplateLoader templateLoader;
/** /**
* Initializes instance. * Initializes instance.
...@@ -151,33 +149,7 @@ public class HandlebarsTemplateEngine implements TemplateEngine { ...@@ -151,33 +149,7 @@ public class HandlebarsTemplateEngine implements TemplateEngine {
@Override @Override
public void evaluate(Writer writer, String templateCode, Map<String, Object> data) { public void evaluate(Writer writer, String template, Map<String, Object> data) {
String name = "template://" + templateCode;
Template template = useCache ? templateCache.get(name) : null;
if (template == null) {
com.jh.boot.template.Template t = templateLoader.loadTemplate(templateCode);
if (t == null) {
// throw new RuntimeException(String.format("Template %s not found", templateCode));
return; //TODO log warning
}
try {
template = handlebars.compileInline(t.getTemplate());
} catch (IOException e) {
throw new RuntimeException("Cannot evaluate template", e);
}
if (useCache) {
templateCache.put(name, template);
}
}
evaluateTemplate(template, data, writer);
}
@Override
public void evaluateInline(Writer writer, String template, Map<String, Object> data) {
String name = "inline://" + template; String name = "inline://" + template;
Template t = useCache ? templateCache.get(name) : null; Template t = useCache ? templateCache.get(name) : null;
...@@ -221,12 +193,5 @@ public class HandlebarsTemplateEngine implements TemplateEngine { ...@@ -221,12 +193,5 @@ public class HandlebarsTemplateEngine implements TemplateEngine {
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