ag.orbia.common.infrastructure.email.EmailBodyResolver.kt Maven / Gradle / Ivy
package ag.orbia.common.infrastructure.email
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.thymeleaf.TemplateEngine
import org.thymeleaf.context.Context
import java.util.*
@Component
class EmailBodyResolver @Autowired constructor(
private val templateEngine: TemplateEngine
) {
fun resolve(emailBody: HtmlBody): String {
return processTemplate(emailBody.template, emailBody.data)
}
private fun processTemplate(templateFile: String, data: Map): String {
val templatePath = "emails/$templateFile"
val context = Context(Locale.getDefault())
data.forEach { (key, value) -> context.setVariable(key, value) }
return templateEngine.process(templatePath, context)
}
}