HtmlRenderingService.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.tsystems.inwo.vgl.service;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import org.springframework.stereotype.Service;
  4. import java.util.Objects;
  5. import java.util.function.Function;
  6. @Service
  7. public class HtmlRenderingService {
  8. public String buildPersonFieldHtml(JsonNode rightPerson, JsonNode leftPerson, Function<JsonNode, String> extractor, boolean hasChanges) {
  9. if (!hasChanges || rightPerson == null) {
  10. JsonNode primary = rightPerson != null ? rightPerson : leftPerson;
  11. return primary != null ? escape(extractor.apply(primary)) : "";
  12. }
  13. String rightVal = rightPerson != null ? extractor.apply(rightPerson) : "";
  14. String leftVal = leftPerson != null ? extractor.apply(leftPerson) : "";
  15. if (!Objects.equals(rightVal, leftVal)) {
  16. return cellHtml(rightVal, leftVal, true);
  17. } else {
  18. return escape(rightVal);
  19. }
  20. }
  21. public String buildItemFieldHtml(JsonNode rightItem, JsonNode leftItem, Function<JsonNode, String> extractor, boolean hasChanges) {
  22. if (!hasChanges || rightItem == null) {
  23. JsonNode primary = rightItem != null ? rightItem : leftItem;
  24. return primary != null ? escape(extractor.apply(primary)) : "";
  25. }
  26. String rightVal = rightItem != null ? extractor.apply(rightItem) : "";
  27. String leftVal = leftItem != null ? extractor.apply(leftItem) : "";
  28. if (!Objects.equals(rightVal, leftVal)) {
  29. return cellHtml(rightVal, leftVal, true);
  30. } else {
  31. return escape(rightVal);
  32. }
  33. }
  34. public String buildEinkommenFieldHtml(JsonNode rightItem, JsonNode leftItem, Function<JsonNode, String> extractor, boolean hasChanges) {
  35. if (!hasChanges || rightItem == null) {
  36. JsonNode primary = rightItem != null ? rightItem : leftItem;
  37. return primary != null ? escape(extractor.apply(primary)) : "";
  38. }
  39. String rightVal = rightItem != null ? extractor.apply(rightItem) : "";
  40. String leftVal = leftItem != null ? extractor.apply(leftItem) : "";
  41. if (!Objects.equals(rightVal, leftVal)) {
  42. return cellHtml(rightVal, leftVal, true);
  43. } else {
  44. return escape(rightVal);
  45. }
  46. }
  47. public String buildFreibetragFieldHtml(JsonNode rightItem, JsonNode leftItem, Function<JsonNode, String> extractor, boolean hasChanges) {
  48. if (!hasChanges || rightItem == null) {
  49. JsonNode primary = rightItem != null ? rightItem : leftItem;
  50. return primary != null ? escape(extractor.apply(primary)) : "";
  51. }
  52. String rightVal = rightItem != null ? extractor.apply(rightItem) : "";
  53. String leftVal = leftItem != null ? extractor.apply(leftItem) : "";
  54. if (!Objects.equals(rightVal, leftVal)) {
  55. return cellHtml(rightVal, leftVal, true);
  56. } else {
  57. return escape(rightVal);
  58. }
  59. }
  60. public String buildKitaFieldHtml(JsonNode rightItem, JsonNode leftItem, Function<JsonNode, String> extractor, boolean hasChanges) {
  61. if (!hasChanges || rightItem == null) {
  62. JsonNode primary = rightItem != null ? rightItem : leftItem;
  63. return primary != null ? escape(extractor.apply(primary)) : "";
  64. }
  65. String rightVal = rightItem != null ? extractor.apply(rightItem) : "";
  66. String leftVal = leftItem != null ? extractor.apply(leftItem) : "";
  67. if (!Objects.equals(rightVal, leftVal)) {
  68. return cellHtml(rightVal, leftVal, true);
  69. } else {
  70. return escape(rightVal);
  71. }
  72. }
  73. public String cellHtml(String newVal, String oldVal, boolean changed) {
  74. if (!changed) {
  75. return escape(newVal);
  76. }
  77. // Handle empty old values
  78. String displayOldVal = (oldVal == null || oldVal.trim().isEmpty()) ? "leer" : oldVal;
  79. // Return with cell-changed class and proper old value display
  80. return "<span class='cell-changed'>" + escape(newVal) +
  81. "<div class='old'>Alt: " + escape(displayOldVal) + "</div></span>";
  82. }
  83. public String escape(String s) {
  84. if (s == null) return "";
  85. return s.replace("&", "&")
  86. .replace("<", "<")
  87. .replace(">", ">");
  88. }
  89. }