TemplatingServiceImpl.java

  1. /*
  2.  * Copyright 2020 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gringlobal.service.impl;

  17. import java.io.StringReader;
  18. import java.io.StringWriter;
  19. import java.time.Instant;
  20. import java.time.LocalDateTime;
  21. import java.time.ZoneOffset;
  22. import java.time.ZonedDateTime;
  23. import java.time.format.DateTimeFormatter;
  24. import java.time.format.DateTimeFormatterBuilder;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.concurrent.ExecutionException;
  29. import java.util.concurrent.TimeUnit;
  30. import java.util.function.Function;

  31. import lombok.extern.slf4j.Slf4j;
  32. import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
  33. import com.vladsch.flexmark.ext.tables.TablesExtension;
  34. import com.vladsch.flexmark.html.HtmlRenderer;
  35. import com.vladsch.flexmark.parser.Parser;
  36. import com.vladsch.flexmark.util.ast.KeepType;
  37. import com.vladsch.flexmark.util.ast.Node;
  38. import com.vladsch.flexmark.util.data.MutableDataHolder;
  39. import com.vladsch.flexmark.util.data.MutableDataSet;
  40. import org.apache.commons.lang3.StringUtils;
  41. import org.genesys.blocks.util.StringToJavaTimeConverter;
  42. import org.gringlobal.service.CodeValueTranslationService;
  43. import org.gringlobal.service.TemplatingService;
  44. import org.springframework.beans.factory.annotation.Autowired;
  45. import org.springframework.context.MessageSource;
  46. import org.springframework.context.i18n.LocaleContextHolder;
  47. import org.springframework.stereotype.Service;
  48. import org.springframework.web.util.UriUtils;

  49. import com.github.mustachejava.DefaultMustacheFactory;
  50. import com.github.mustachejava.Mustache;
  51. import com.github.mustachejava.MustacheFactory;
  52. import com.google.common.base.Charsets;
  53. import com.google.common.cache.Cache;
  54. import com.google.common.cache.CacheBuilder;

  55. /**
  56.  * The TemplatingServiceImpl using Mustache https://github.com/spullara/mustache.java
  57.  *
  58.  * See http://mustache.github.io/mustache.5.html for manual.
  59.  *
  60.  * - All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.
  61.  */
  62. @Service
  63. @Slf4j
  64. public class TemplatingServiceImpl implements TemplatingService {
  65.    
  66.     /** The Constant flexmark OPTIONS. */
  67.     private static final MutableDataHolder OPTIONS = new MutableDataSet()
  68.         .set(Parser.REFERENCES_KEEP, KeepType.LAST)
  69.         .set(Parser.HTML_BLOCK_PARSER, false)
  70. //          .set(Parser.HTML_BLOCK_DEEP_PARSER, true)
  71. //          .set(Parser.HTML_BLOCK_START_ONLY_ON_BLOCK_TAGS, true)
  72.         .set(HtmlRenderer.INDENT_SIZE, 2)
  73.         .set(HtmlRenderer.PERCENT_ENCODE_URLS, true)
  74. //          .set(HtmlRenderer.ESCAPE_HTML, false)

  75.         // for full GFM table compatibility add the following table extension options:
  76.         .set(TablesExtension.COLUMN_SPANS, false)
  77.         .set(TablesExtension.APPEND_MISSING_COLUMNS, true)
  78.         .set(TablesExtension.DISCARD_EXTRA_COLUMNS, true)
  79.         .set(TablesExtension.HEADER_SEPARATOR_COLUMN_MATCH, true)
  80.         .set(Parser.EXTENSIONS, List.of(
  81.             TablesExtension.create(),
  82.             StrikethroughExtension.create()
  83.         ));

  84.     private static final Parser parser = Parser.builder(OPTIONS).build();
  85.     private static final HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS).build();

  86.     /**
  87.      * Extra functions available in templates:
  88.      *
  89.      * - <code>URL</code>: URL encode the string
  90.      * - <code>URLPATH</code>: URL encodes path
  91.      * - <code>MARKDOWN</code> converts Markdown input to HTML
  92.      */
  93.     private static final Map<String, Object> templateFunctions = Map.of(
  94.         // URL encode
  95.         "_url", (Function<String, String>) (input) -> StringUtils.isBlank(input) ? "" : UriUtils.encode(input, Charsets.UTF_8),
  96.         "URL", (Function<String, String>) (input) -> StringUtils.isBlank(input) ? "" : UriUtils.encode(input, Charsets.UTF_8),

  97.         "_urlpath", (Function<String, String>) (input) -> StringUtils.isBlank(input) ? "" : UriUtils.encodePath(input, Charsets.UTF_8),
  98.         "URLPATH", (Function<String, String>) (input) -> StringUtils.isBlank(input) ? "" : UriUtils.encodePath(input, Charsets.UTF_8),

  99.         "year", (Function<String, String>) TemplatingServiceImpl::formatToYear,
  100.         "YEAR", (Function<String, String>) TemplatingServiceImpl::formatToYear,

  101.         "formatDate", (Function<String, String>) TemplatingServiceImpl::formatDate,
  102.         "FORMATDATE", (Function<String, String>) TemplatingServiceImpl::formatDate,

  103.         "MARKDOWN", (Function<String, String>) TemplatingServiceImpl::formatMarkdown,

  104.         // {{#RFIDBARCODE}}{{{inventory.barcode}}}{{/RFIDBARCODE}}
  105.         "RFIDBARCODE", (Function<String, String>) TemplatingServiceImpl::rfidBarcode

  106.     );

  107.     /** Java Date.toString() produces: Fri Sep 06 05:21:48 CEST 2024 */
  108.     private static final DateTimeFormatter JAVA_DATE_PATTERN = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss v yyyy");
  109.     /** Instant seems to be formatted as: 2024-09-06 06:25:05.701 */
  110.     private static final DateTimeFormatter MUSTACHE_PATTERN = new DateTimeFormatterBuilder().append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
  111.             .parseLenient()
  112.             .appendOptional(DateTimeFormatter.ofPattern(".SSS")) // milliseconds seem to be very optional and dynamic
  113.             .toFormatter();

  114.     private final Cache<String, Mustache> templateCache = CacheBuilder.newBuilder().maximumSize(20).expireAfterAccess(10, TimeUnit.MINUTES).build();

  115.     private final MustacheFactory mf = new DefaultMustacheFactory();

  116.     @Autowired
  117.     private CodeValueTranslationService codeValueService;
  118.     @Autowired
  119.     private MessageSource messageSource;

  120.     @Override
  121.     public String fillTemplate(String template, Map<String, Object> params) {
  122.         return fillTemplate(compileTemplate(template), params);
  123.     }

  124.     @Override
  125.     public String fillTemplate(Mustache mustache, Map<String, Object> params) {
  126.         StringWriter swOut = new StringWriter();
  127.         var paramsPlus = new HashMap<>(params);
  128.         paramsPlus.putAll(templateFunctions);
  129.         paramsPlus.putAll(Map.of(
  130.             "codeValueTitle", (Function<String, String>)this::codeValueTitle,
  131.             "i18n", (Function<String, String>)this::i18n
  132.         ));
  133.         mustache.execute(swOut, paramsPlus);

  134.         if (log.isTraceEnabled()) {
  135.             log.trace(swOut.toString());
  136.         }
  137.         return swOut.toString();
  138.     }

  139.     @Override
  140.     public Mustache compileTemplate(String template) {
  141.         try {
  142.             return templateCache.get(template, () -> {
  143.                 log.debug("Compiling Mustache template");
  144.                 return mf.compile(new StringReader(template), null);
  145.             });
  146.         } catch (ExecutionException e) {
  147.             throw new RuntimeException(e);
  148.         }
  149.     }

  150.     private static String formatToYear(String input) {
  151.         if (StringUtils.isBlank(input)) return "";
  152.         input = StringUtils.trim(input);
  153.         try {
  154.             var instant = parseDate(input);
  155.             return String.valueOf(instant.atZone(LocaleContextHolder.getTimeZone().toZoneId()).getYear());
  156.         } catch (Exception e) {
  157.             var date = StringToJavaTimeConverter.StringToLocalDateConverter.INSTANCE.convert(input);
  158.             return String.valueOf(date.getYear());
  159.         }
  160.     }

  161.     private static Instant parseDate(String input) {
  162.         try {
  163.             return StringToJavaTimeConverter.StringToInstantConverter.INSTANCE.convert(input);
  164.         } catch (Exception e1) {
  165.             try {
  166.                 return MUSTACHE_PATTERN.parse(input, LocalDateTime::from).toInstant(ZoneOffset.UTC);
  167.             } catch (Exception e2) {
  168.                 return JAVA_DATE_PATTERN.parse(input, ZonedDateTime::from).toInstant();
  169.             }
  170.         }
  171.     }

  172.     private static String formatDate(String input) {
  173.         if (StringUtils.isBlank(input)) return "";
  174.         input = StringUtils.trim(input);
  175.         String[] inputParts = input.split("\\s+", 2); // Keep the timestamp togehter
  176.         String pattern = inputParts[0];
  177.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern).withZone(LocaleContextHolder.getTimeZone().toZoneId());
  178.         if (inputParts.length == 2) {
  179.             String date = inputParts[1];
  180.             if (StringUtils.isBlank(date)) return "";
  181.             // System.out.println("FORMATDATE " + input + " pattern=" + pattern + " date=" + date);
  182.             var instant = parseDate(date);
  183.             // System.out.println(" Instant: " + instant);
  184.             return formatter.format(instant);
  185.         } else {
  186.             return "";
  187.         }
  188.     }

  189.     private static String formatMarkdown(String input) {
  190.         if (StringUtils.isBlank(input)) return "";
  191.         final Node document = parser.parse(input);
  192.         return renderer.render(document);
  193.     }

  194.     private static String rfidBarcode(String input) {
  195.         return StringUtils.defaultIfBlank(input, "").replaceAll("[^a-f0-9A-F]", "").trim();
  196.     }

  197.     private String codeValueTitle(String input) {
  198.         input = StringUtils.trim(input);
  199.         String[] params = input.split(" ", 2);
  200.         if (params.length != 2) {
  201.             throw new IllegalArgumentException("Cannot find code value group and value in input: ".concat(input));
  202.         }
  203.         var cv = codeValueService.findTranslatedCodeValue(params[0], params[1], LocaleContextHolder.getLocale());
  204.         String title = params[1];
  205.         if (cv.isPresent()) {
  206.             title = cv.get().getTitle();
  207.         }
  208.         return title;
  209.     }

  210.     private String i18n(String input) {
  211.         log.trace("i18n: {}", input);
  212.         return messageSource.getMessage(input, null, input, LocaleContextHolder.getLocale());
  213.     }
  214. }