|
| 1 | +package ca.bc.gov.nrs.ilcr.homecontent; |
| 2 | + |
| 3 | +import ca.bc.gov.nrs.ilcr.exception.FieldValuesRequiredException; |
| 4 | +import ca.bc.gov.nrs.ilcr.homecontent.dto.HomeContentEntry; |
| 5 | +import ca.bc.gov.nrs.ilcr.homecontent.dto.HomeContentSaveRequest; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | + |
| 14 | +/** |
| 15 | + * Content Editing service (Story 24.2 / UC-CNT-001): reads the three role messages, serves one role's |
| 16 | + * message for the Home render, and saves all three ATOMICALLY (A-3 — the legacy per-role |
| 17 | + * non-atomic save S09 is fixed: one transaction, all-or-nothing). |
| 18 | + * |
| 19 | + * <p>Each editor is required (FLD-001) — validated before any write so a rejection saves nothing, with |
| 20 | + * ALL blank editors reported together. On save the legacy transform is applied (tabs/newlines/{@code |
| 21 | + * }, D-3). Rich text is stored raw (legacy stored the WYSIWYG HTML unsanitized); the Home render |
| 22 | + * sanitizes with DOMPurify (defence-in-depth), so no server-side HTML rewrite happens here. |
| 23 | + */ |
| 24 | +@Slf4j |
| 25 | +@Service |
| 26 | +@ConditionalOnProperty(name = "ilcr.datasource.enabled", havingValue = "true") |
| 27 | +public class HomeContentService { |
| 28 | + |
| 29 | + static final String ROLE_LICENSEE = "LICENSEE"; |
| 30 | + static final String ROLE_AUDITOR = "AUDITOR"; |
| 31 | + static final String ROLE_ADMIN = "ADMIN"; |
| 32 | + private static final int MAX_MESSAGE_LENGTH = 4000; |
| 33 | + // Field labels for the FLD-001 required-field messages — verbatim from legacy content.xhtml. |
| 34 | + private static final String LABEL_LICENSEE = "Licensee Welcome Message"; |
| 35 | + private static final String LABEL_AUDITOR = "Auditor Welcome Message"; |
| 36 | + private static final String LABEL_ADMIN = "Administrator Welcome Message"; |
| 37 | + |
| 38 | + private final HomeContentRepository repository; |
| 39 | + |
| 40 | + public HomeContentService(HomeContentRepository repository) { |
| 41 | + this.repository = repository; |
| 42 | + } |
| 43 | + |
| 44 | + /** All three role messages for the Content Editing page. */ |
| 45 | + public List<HomeContentEntry> readAll() { |
| 46 | + return repository.findAll(); |
| 47 | + } |
| 48 | + |
| 49 | + /** The message for one role — the Home render of the viewer's role (empty text when none). */ |
| 50 | + public HomeContentEntry readForRole(String role) { |
| 51 | + return repository.findByRole(role).orElse(new HomeContentEntry(role, null)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Save all three role messages in one transaction (A-3). Validate every editor first (FLD-001, all |
| 56 | + * blanks together), then transform + update each; any failure rolls the whole save back. |
| 57 | + * |
| 58 | + * @param request the three messages |
| 59 | + * @param user the acting administrator (audit) |
| 60 | + */ |
| 61 | + @Transactional |
| 62 | + public void saveAll(HomeContentSaveRequest request, String user) { |
| 63 | + List<RoleMessage> messages = List.of( |
| 64 | + new RoleMessage(ROLE_LICENSEE, LABEL_LICENSEE, request.licensee()), |
| 65 | + new RoleMessage(ROLE_AUDITOR, LABEL_AUDITOR, request.auditor()), |
| 66 | + new RoleMessage(ROLE_ADMIN, LABEL_ADMIN, request.administrator())); |
| 67 | + |
| 68 | + List<String> blankLabels = new ArrayList<>(); |
| 69 | + for (RoleMessage message : messages) { |
| 70 | + if (isBlankHtml(message.text())) { |
| 71 | + blankLabels.add(message.label()); |
| 72 | + } |
| 73 | + } |
| 74 | + if (!blankLabels.isEmpty()) { |
| 75 | + throw new FieldValuesRequiredException(blankLabels); |
| 76 | + } |
| 77 | + |
| 78 | + for (RoleMessage message : messages) { |
| 79 | + String transformed = transform(message.text()); |
| 80 | + // Cap by BYTES: MESSAGE_TEXT is VARCHAR2(4000 BYTE), so multi-byte content (smart quotes, |
| 81 | + // em dashes from paste) could pass a char-count check and then fail the insert with ORA-12899. |
| 82 | + if (transformed.getBytes(StandardCharsets.UTF_8).length > MAX_MESSAGE_LENGTH) { |
| 83 | + throw HomeContentException.tooLong(); |
| 84 | + } |
| 85 | + if (repository.updateMessage(message.role(), transformed, user) == 0) { |
| 86 | + throw HomeContentException.contentNotFound(); |
| 87 | + } |
| 88 | + } |
| 89 | + log.info("Home content updated (3 role messages) by {}", user); |
| 90 | + } |
| 91 | + |
| 92 | + /** Empty once tags, {@code } and whitespace are stripped — the required-editor check. */ |
| 93 | + private static boolean isBlankHtml(String html) { |
| 94 | + if (html == null) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + return html.replaceAll("<[^>]*>", "").replace(" ", " ").strip().isEmpty(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Save-transform after {@code CoreUtil.replaceCharsForExtractFormat}: tab → two spaces, newline |
| 102 | + * → one space. Legacy dropped {@code } entirely (CoreUtil.java:972), but the legacy |
| 103 | + * PrimeFaces editor rarely emitted it; TipTap emits {@code } for leading/consecutive spaces, |
| 104 | + * so dropping it would silently delete word breaks. We map it to a space instead (deliberate, |
| 105 | + * editor-driven deviation from legacy). |
| 106 | + */ |
| 107 | + private static String transform(String text) { |
| 108 | + return text.replace("\t", " ").replace("\n", " ").replace(" ", " "); |
| 109 | + } |
| 110 | + |
| 111 | + private record RoleMessage(String role, String label, String text) {} |
| 112 | +} |
0 commit comments