TemplateService.java
5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.ecommerce.notification.service;
import com.ecommerce.notification.model.NotificationTemplate;
import com.ecommerce.notification.repository.NotificationTemplateRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
public class TemplateService {
private final NotificationTemplateRepository templateRepository;
private final ResourceLoader resourceLoader;
public Map<String, Object> processTemplate(String templateName, String type, Map<String, Object> variables) {
try {
// First try to get template from database
NotificationTemplate template = templateRepository.findByName(templateName)
.orElseThrow(() -> new RuntimeException("Template not found: " + templateName));
if (!template.getIsActive()) {
throw new RuntimeException("Template is not active: " + templateName);
}
String content = template.getContent();
String subject = template.getSubject();
// Process variables in content and subject
if (variables != null) {
content = processVariables(content, variables);
if (subject != null) {
subject = processVariables(subject, variables);
}
}
Map<String, Object> result = new HashMap<>();
result.put("content", content);
if (subject != null) {
result.put("subject", subject);
}
return result;
} catch (Exception e) {
log.warn("Database template processing failed for {}: {}", templateName, e.getMessage());
// Fallback to file-based templates
return processFileTemplate(templateName, type, variables);
}
}
private Map<String, Object> processFileTemplate(String templateName, String type, Map<String, Object> variables) {
try {
String templatePath = String.format("classpath:templates/%s/%s",
type.toLowerCase(), templateName);
Resource resource = resourceLoader.getResource(templatePath);
String content = readResourceContent(resource);
// Process variables in content
if (variables != null) {
content = processVariables(content, variables);
}
Map<String, Object> result = new HashMap<>();
result.put("content", content);
// For email templates, set a default subject
if ("EMAIL".equalsIgnoreCase(type)) {
result.put("subject", getDefaultSubject(templateName));
}
return result;
} catch (Exception e) {
log.error("File template processing failed for {}: {}", templateName, e.getMessage());
throw new RuntimeException("Template processing failed: " + e.getMessage());
}
}
private String processVariables(String template, Map<String, Object> variables) {
String processed = template;
for (Map.Entry<String, Object> entry : variables.entrySet()) {
String placeholder = "{{" + entry.getKey() + "}}";
String value = entry.getValue() != null ? entry.getValue().toString() : "";
processed = processed.replace(placeholder, value);
}
return processed;
}
private String readResourceContent(Resource resource) throws IOException {
try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
return FileCopyUtils.copyToString(reader);
}
}
private String getDefaultSubject(String templateName) {
switch (templateName) {
case "order-confirmation":
return "Order Confirmation - Your Order has been Received";
case "payment-success":
return "Payment Successful - Thank You for Your Purchase";
case "shipping-update":
return "Shipping Update - Your Order is on the Way";
case "password-reset":
return "Password Reset Request";
default:
return "Notification from E-Commerce";
}
}
public NotificationTemplate createTemplate(NotificationTemplate template) {
if (templateRepository.findByName(template.getName()).isPresent()) {
throw new RuntimeException("Template with name already exists: " + template.getName());
}
return templateRepository.save(template);
}
public NotificationTemplate updateTemplate(Long id, NotificationTemplate templateDetails) {
NotificationTemplate template = templateRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Template not found with id: " + id));
template.setName(templateDetails.getName());
template.setType(templateDetails.getType());
template.setChannel(templateDetails.getChannel());
template.setSubject(templateDetails.getSubject());
template.setContent(templateDetails.getContent());
template.setVariables(templateDetails.getVariables());
template.setIsActive(templateDetails.getIsActive());
template.setDescription(templateDetails.getDescription());
return templateRepository.save(template);
}
}