Skip to content

Commit

Permalink
Added: Support for Thymeleaf standard processor by converting hyphena…
Browse files Browse the repository at this point in the history
…ted variables to camel case
  • Loading branch information
djuarezgf committed Dec 5, 2024
1 parent e959157 commit 42a9597
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.0.1 - 2024-12-02]
## [0.0.1 - 2024-12-05]
### Added
- First version of the project
- Spring Application
Expand Down Expand Up @@ -169,3 +169,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Decode Base64 values of email context
- Enable minus in variables of email context
- Last document variables in email context
- Support for Thymeleaf standard processor by converting hyphenated variables to camel case
2 changes: 1 addition & 1 deletion src/main/java/de/samply/email/EmailContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class EmailContext {
@PostConstruct
public void init() {
log.info("Email Context initialized: ({})", context.entrySet().size());
Map<String, String> tempContext = new HashMap<>(context);
Map<String, String> tempContext = new HashMap<>();
context.entrySet().stream().forEach(keyValue -> {
String key = replaceHyphen(keyValue.getKey());
AtomicReference<String> value = new AtomicReference<>(keyValue.getValue());
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/de/samply/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.samply.notification.NotificationService;
import de.samply.notification.OperationType;
import de.samply.user.roles.ProjectRole;
import de.samply.utils.KeyTransformer;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -128,6 +129,12 @@ private Optional<MessageSubject> createEmailMessageAndSubject(ProjectRole role,
private Context createContext(EmailKeyValues keyValues) {
Context context = new Context();
keyValues.getKeyValues().forEach((key, value) -> context.setVariable(key, value));
// Remove hyphens ("-") and convert keys to camel case to ensure Thymeleaf can process variables correctly.
// For example: "my-variable" -> "myVariable".
// In Thymeleaf templates, we can use <my-variable/> to reference the variable directly.
// However, when using the standard Thymeleaf processor, we need to use <span th:text="${myVariable}">
// because Thymeleaf does not support hyphens ("-") in variable names (e.g., ${my-variable} is invalid).
KeyTransformer.transformMapKeys(keyValues.getKeyValues()).forEach((key, value) -> context.setVariable(key, value));
return context;
}

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/de/samply/utils/KeyTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.samply.utils;

import java.util.Map;
import java.util.stream.Collectors;

public class KeyTransformer {

public static Map<String, String> transformMapKeys(Map<String, String> inputMap) {
return inputMap.entrySet().stream()
.collect(Collectors.toMap(
entry -> transformKey(entry.getKey()), // transform the key
Map.Entry::getValue // keep the value unchanged
));
}

private static String transformKey(String key) {
StringBuilder transformedKey = new StringBuilder();
boolean toUpperCase = false;

for (char c : key.toCharArray()) {
if (c == '-') {
toUpperCase = true; // next character should be uppercase
} else {
if (toUpperCase) {
transformedKey.append(Character.toUpperCase(c));
toUpperCase = false;
} else {
transformedKey.append(c);
}
}
}
return transformedKey.toString();
}

}

0 comments on commit 42a9597

Please sign in to comment.