Refactor toHtml() method in Form.java

Enhanced `toHtml()` to split multiline values into paragraphs using streams and filters. This improves HTML output readability by ensuring each line is wrapped in `<p>` tags. Also updated workspace.xml with the correct description for the recent changes.
This commit is contained in:
Teriuihi 2024-08-11 18:47:36 +02:00
parent ab74360571
commit 3eb4970407
2 changed files with 22 additions and 10 deletions

View File

@ -4,11 +4,9 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Update `toHtml()` method in `Form.java`&#10;&#10;Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments.">
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/controlers/FormRequestHandler.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/controlers/FormRequestHandler.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/database/Database.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/database/Database.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/StoreFormQuery.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/StoreFormQuery.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/TestForm.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/TestForm.java" afterDir="false" />
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Add IP address tracking for form submissions&#10;&#10;Included IP and timestamp columns in the database schema to track form submissions. Updated methods to handle and store the IP information upon form submission. Adjusted related test cases to validate the new implementation.">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -395,7 +393,7 @@
<workItem from="1723242314809" duration="9799000" />
<workItem from="1723307861750" duration="2179000" />
<workItem from="1723392202182" duration="258000" />
<workItem from="1723392471077" duration="1301000" />
<workItem from="1723392471077" duration="2183000" />
</task>
<task id="LOCAL-00001" summary="Initial commit for site for forms">
<option name="closed" value="true" />
@ -685,7 +683,15 @@
<option name="project" value="LOCAL" />
<updated>1723393478027</updated>
</task>
<option name="localTasksCounter" value="37" />
<task id="LOCAL-00037" summary="Add IP address tracking for form submissions&#10;&#10;Included IP and timestamp columns in the database schema to track form submissions. Updated methods to handle and store the IP information upon form submission. Adjusted related test cases to validate the new implementation.">
<option name="closed" value="true" />
<created>1723393877624</created>
<option name="number" value="00037" />
<option name="presentableId" value="LOCAL-00037" />
<option name="project" value="LOCAL" />
<updated>1723393877624</updated>
</task>
<option name="localTasksCounter" value="38" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -703,7 +709,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Update URI to point to Discord bot endpoint&#10;&#10;Changed the URI in VerifyController to use the Discord bot's endpoint for form submission. This adjustment is part of the process to route form data correctly through the specified internal service." />
<MESSAGE value="Rename StoreFormQuery package to forms.form&#10;&#10;Updated the package name for consistency and better categorization. Adjusted imports in related test files to reflect the new package structure." />
<MESSAGE value="Refactor HTML generation in ContactFormData&#10;&#10;Extract the HTML generation logic to a reusable method in the Form class. This change reduces code duplication and enhances maintainability by centralizing the table generation functionality." />
<MESSAGE value="Add Staff Application Form handling&#10;&#10;Introduced `StaffAppController` and `StaffAppFormData` to handle staff application form submissions. The controller now stores user data, initiates email verification, and provides appropriate responses based on the verification outcome. Additionally, updated `application.properties` and cleaned up IntelliJ workspace.xml." />
@ -728,7 +733,8 @@
<MESSAGE value="Add branch condition to Discord notification stage&#10;&#10;The Discord notification stage now runs only on 'main' or 'master' branches. This helps to avoid unnecessary notifications for feature or bugfix branches." />
<MESSAGE value="Improve email handling with Mailable interface&#10;&#10;Added a new Mailable interface for extracting email-related data like receiver and subject. Updated StaffAppFormData and ContactFormData to implement this interface, and refactored MailForm to utilize these details. This enhances flexibility and decouples email details from form classes." />
<MESSAGE value="Update `toHtml()` method in `Form.java`&#10;&#10;Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments." />
<option name="LAST_COMMIT_MESSAGE" value="Update `toHtml()` method in `Form.java`&#10;&#10;Simplified the HTML structure for the `toHtml` method by switching from a table layout to paragraph tags. This removes unnecessary table formatting for improved readability and maintainability. Also updated workspace.xml with task tracking and removed outdated comments." />
<MESSAGE value="Add IP address tracking for form submissions&#10;&#10;Included IP and timestamp columns in the database schema to track form submissions. Updated methods to handle and store the IP information upon form submission. Adjusted related test cases to validate the new implementation." />
<option name="LAST_COMMIT_MESSAGE" value="Add IP address tracking for form submissions&#10;&#10;Included IP and timestamp columns in the database schema to track form submissions. Updated methods to handle and store the IP information upon form submission. Adjusted related test cases to validate the new implementation." />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />

View File

@ -4,7 +4,9 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
public abstract class Form {
@ -19,13 +21,17 @@ public abstract class Form {
public String toHtml(String[] fields, String[] values) {
StringBuilder htmlOutput = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
String paragraphs = Arrays.stream(values[i].split("\n"))
.filter(str -> !str.isBlank())
.map(str -> String.format("<p>%s</p>", str))
.collect(Collectors.joining("\n"));
htmlOutput.append(
String.format(
//language=HTML
"""
<p><strong>%s</strong></p>
<p>%s</p>
""", fields[i], values[i]));
""", fields[i], paragraphs));
}
return htmlOutput.toString();
}