-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitTemplateService.java
More file actions
153 lines (137 loc) · 6.91 KB
/
CommitTemplateService.java
File metadata and controls
153 lines (137 loc) · 6.91 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
149
150
151
152
153
package pl.commit.craft.template;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
class CommitTemplateService {
private static final String PROPERTY_TEMPLATES = "templates";
private static final String PROPERTY_DEDICATED_TEMPLATES = "dedicated";
private static final String PATH_NAME_ELEMENT = "name";
private static final String RESOURCES_TEMPLATES_META_SCHEMA_JSON = "src/main/resources/templates/meta-schema.json";
private static final String RESOURCES_DEDICATED_META_SCHEMA_JSON = "src/main/resources/templates/dedicated-meta-schema.json";
private final ObjectMapper objectMapper;
public List<CommitCraftTemplate> getAllTemplates() throws IOException {
JsonNode rootNode = objectMapper.readTree(new File(RESOURCES_TEMPLATES_META_SCHEMA_JSON));
JsonNode templatesNode = rootNode.path(PROPERTY_TEMPLATES);
List<CommitCraftTemplate> templatesList = objectMapper.readValue(
templatesNode.toString(),
new TypeReference<>() {
}
);
List<CommitCraftTemplate> templates = new ArrayList<>(templatesList);
JsonNode rootDedicatedNode = objectMapper.readTree(new File(RESOURCES_DEDICATED_META_SCHEMA_JSON));
JsonNode dedicatedNode = rootDedicatedNode.path(PROPERTY_DEDICATED_TEMPLATES);
List<CommitCraftTemplate> dedicatedList = objectMapper.readValue(
dedicatedNode.toString(),
new TypeReference<>() {
}
);
templates.addAll(dedicatedList);
return templates;
}
public CommitCraftJson prepareJsonByModel(String name) throws IOException {
List<CommitCraftTemplate> templates = getAllTemplates();
CommitCraftTemplate selectedTemplate = templates.stream()
.filter(template -> template.getName().equals(name))
.findFirst()
.orElseThrow(() -> new RuntimeException("Template not found"));
return getCommitCraftJson(selectedTemplate, selectedTemplate.getName());
}
CommitCraftJson getCommitCraftJson(CommitCraftTemplate selectedTemplate, String selectedTemplateName) throws IOException {
Map<String, Object> model = selectedTemplate.getModel();
CommitCraftJson commitCraftJson = new CommitCraftJson();
commitCraftJson.addField(PATH_NAME_ELEMENT, selectedTemplateName);
for (Map.Entry<String, Object> entry : model.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof List<?> list) {
commitCraftJson.addField(key, list);
} else {
commitCraftJson.addField(key, value);
}
}
return commitCraftJson;
}
public List<CommitCraftTemplate> readTemplates() throws IOException {
JsonNode rootNode = objectMapper.readTree(new File(RESOURCES_DEDICATED_META_SCHEMA_JSON));
JsonNode dedicatedNode = rootNode.path(PROPERTY_DEDICATED_TEMPLATES);
return objectMapper.convertValue(dedicatedNode, new TypeReference<List<CommitCraftTemplate>>() {
});
}
/**
* Creates a dedicated template after validating it.
*
* @param template The template to create
* @return Result containing success/failure status and a message
* @throws IOException If there's an issue accessing the template storage
*/
public TemplateOperationResult createDedicatedTemplate(CommitCraftTemplate template) throws IOException {
ValidationResult validationResult =
CommitDedicatedTemplateValidator.validatePatternAndModelScopeDetailed(template);
if (!validationResult.isValid()) {
String errorMessage = validationResult.getErrorMessage();
log.warn("Template validation failed: {}", errorMessage);
return new TemplateOperationResult(false, errorMessage);
}
if (templateExists(template.getName())) {
log.warn("Template with name '{}' already exists", template.getName());
return new TemplateOperationResult(false, "Template with name '" + template.getName() + "' already exists");
}
saveTemplate(template);
log.info("Template '{}' created successfully", template.getName());
return new TemplateOperationResult(true, "Template created successfully");
}
/**
* Checks if a template with the given name already exists
*
* @param templateName Name of the template to check
* @return true if the template exists, false otherwise
* @throws IOException If there's an issue accessing the template storage
*/
private boolean templateExists(String templateName) throws IOException {
List<CommitCraftTemplate> existingTemplates = getAllTemplates();
return existingTemplates.stream()
.anyMatch(template -> template.getName().equals(templateName));
}
/**
* Saves a new template to the dedicated templates file
*
* @param template The template to save
* @throws IOException If there's an issue accessing or writing to the template storage
*/
private void saveTemplate(CommitCraftTemplate template) throws IOException {
List<CommitCraftTemplate> existingTemplates = readTemplates();
existingTemplates.add(template);
saveTemplates(existingTemplates);
log.debug("Template saved successfully: {}", template.getName());
}
public void removeDedicatedTemplate(String dedicatedTemplateName) throws IOException {
JsonNode rootNode = objectMapper.readTree(new File(RESOURCES_DEDICATED_META_SCHEMA_JSON));
ArrayNode dedicatedArray = (ArrayNode) rootNode.path(PROPERTY_DEDICATED_TEMPLATES);
for (Iterator<JsonNode> nodeIterator = dedicatedArray.elements(); nodeIterator.hasNext(); ) {
JsonNode element = nodeIterator.next();
if (dedicatedTemplateName.equals(element.path(PATH_NAME_ELEMENT).asText())) {
nodeIterator.remove();
}
}
objectMapper.writeValue(new File(RESOURCES_DEDICATED_META_SCHEMA_JSON), rootNode);
}
private void saveTemplates(List<CommitCraftTemplate> templates) throws IOException {
JsonNode rootNode = objectMapper.createObjectNode();
((ObjectNode) rootNode).putPOJO(PROPERTY_DEDICATED_TEMPLATES, templates);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File(RESOURCES_DEDICATED_META_SCHEMA_JSON), rootNode);
}
}