Content of file BluetteGalleryAdapter.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>BluetteGalleryAdapter.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">NorthernWind :: Frontend :: Spring MVC</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-frontend-components-htmltemplate</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.ui.component.gallery.htmltemplate.bluette</a> &gt; <span class="el_source">BluetteGalleryAdapter.java</span></div><h1>BluetteGalleryAdapter.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
 * %%
 * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.northernwind.frontend.ui.component.gallery.htmltemplate.bluette;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Optional;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.northernwind.core.model.ModelFactory;
import it.tidalwave.northernwind.core.model.ResourcePath;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import it.tidalwave.northernwind.core.model.Site;
import it.tidalwave.northernwind.core.model.SiteNode;
import it.tidalwave.northernwind.core.model.Template;
import it.tidalwave.northernwind.core.model.Template.Aggregate;
import it.tidalwave.northernwind.frontend.ui.RenderContext;
import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.TextHolder;
import it.tidalwave.northernwind.frontend.ui.component.gallery.GalleryView;
import it.tidalwave.northernwind.frontend.ui.component.gallery.GalleryViewController.GalleryItem;
import it.tidalwave.northernwind.frontend.ui.component.gallery.spi.GalleryAdapterSupport;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.northernwind.core.model.Template.Aggregates.toAggregates;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L55">@Slf4j</span>
public class BluetteGalleryAdapter extends GalleryAdapterSupport
  {
<span class="nc" id="L58">    private static final Key&lt;String&gt; P_COPYRIGHT = Key.of(&quot;copyright&quot;, String.class);</span>

<span class="nc" id="L60">    private static final Key&lt;ResourcePath&gt; P_BLUETTE_TEMPLATE_PATH = Key.of(&quot;bluettePath&quot;, ResourcePath.class);</span>

    /** The path to the template for fallback rendering of a single gallery page, when JavaScript is not available. */
<span class="nc" id="L63">    private static final Key&lt;ResourcePath&gt; P_BLUETTE_FALLBACK_TEMPLATE_PATH = Key.of(&quot;bluetteFallbackPath&quot;, ResourcePath.class);</span>

    /** The path to the template for fallback rendering of the light box, when JavaScript is not available. */
<span class="nc" id="L66">    private static final Key&lt;ResourcePath&gt; P_BLUETTE_LIGHTBOX_FALLBACK_TEMPLATE_PATH = Key.of(&quot;bluetteLightboxFallbackPath&quot;, ResourcePath.class);</span>

    /** The path to the template for the script that redirects a page to the dynamic counterpart (with #!). */
<span class="nc" id="L69">    private static final Key&lt;ResourcePath&gt; P_BLUETTE_REDIRECT_SCRIPT_TEMPLATE_PATH = Key.of(&quot;bluetteRedirectScriptPath&quot;, ResourcePath.class);</span>

    /** The path to the template for the image catalog. */
<span class="nc" id="L72">    private static final Key&lt;ResourcePath&gt; P_BLUETTE_CATALOG_TEMPLATE_PATH = Key.of(&quot;bluetteCatalogPath&quot;, ResourcePath.class);</span>

<span class="nc" id="L74">    private static final Key&lt;ResourcePath&gt; P_BLUETTE_VARIABLES_TEMPLATE_PATH = Key.of(&quot;bluetteVariablesPath&quot;, ResourcePath.class);</span>

<span class="nc" id="L76">    private static final Key&lt;String&gt; P_CATALOG_URL = Key.of(&quot;bluetteCatalogUrl&quot;, String.class);</span>

    @Nonnull
    private final Site site;

    @Nonnull
    private final Template defaultTemplate;

    @Nonnull
    private final Template fallbackTemplate;

    @Nonnull
    private final Template lightboxFallbackTemplate;

    @Nonnull
    private final Template redirectScriptTemplate;

    @Nonnull
    private final Template catalogTemplate;

    @Nonnull
    private final Template variablesTemplate;

    @Nonnull
    private final ResourceProperties bluetteConfiguration;

    @Nonnull
    private final ResourcePath baseUrl;

    @Nonnull
    private final String copyright;

<span class="nc" id="L108">    private final int fallbackImageSize = 1280; // FIXME: parametrise size</span>

<span class="nc" id="L110">    private final int fallbackThumbnailSize = 100; // FIXME: parametrise size</span>

<span class="nc" id="L112">    private final ResourcePath lightboxSegmentUri = ResourcePath.of(&quot;lightbox&quot;);</span>

    @Nonnull
    private final String homeUrl;

<span class="nc" id="L117">    private String rendered = &quot;&quot;;</span>

    private boolean catalog;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public BluetteGalleryAdapter (@Nonnull final SiteNode siteNode,
                                  @Nonnull final GalleryView view,
                                  @Nonnull final ModelFactory modelFactory)
      {
<span class="nc" id="L130">        super(siteNode, view, modelFactory);</span>
<span class="nc" id="L131">        this.site = siteNode.getSite();</span>
<span class="nc" id="L132">        bluetteConfiguration = siteNode.getPropertyGroup(new Id(&quot;bluetteConfiguration&quot;));</span>
<span class="nc" id="L133">        baseUrl              = siteNode.getRelativeUri().prependedWith(site.getContextPath());</span>
<span class="nc" id="L134">        homeUrl              = site.createLink(ResourcePath.of(&quot;/blog&quot;)); // FIXME</span>
<span class="nc" id="L135">        copyright            = bluetteConfiguration.getProperty(P_COPYRIGHT).orElse(&quot;&quot;);</span>

<span class="nc" id="L137">        defaultTemplate          = loadTemplate(P_BLUETTE_TEMPLATE_PATH,                   &quot;Default.st&quot;);</span>
<span class="nc" id="L138">        fallbackTemplate         = loadTemplate(P_BLUETTE_FALLBACK_TEMPLATE_PATH,          &quot;Fallback.st&quot;);</span>
<span class="nc" id="L139">        lightboxFallbackTemplate = loadTemplate(P_BLUETTE_LIGHTBOX_FALLBACK_TEMPLATE_PATH, &quot;LightBoxFallback.st&quot;);</span>
<span class="nc" id="L140">        redirectScriptTemplate   = loadTemplate(P_BLUETTE_REDIRECT_SCRIPT_TEMPLATE_PATH,   &quot;RedirectScript.st&quot;);</span>
<span class="nc" id="L141">        catalogTemplate          = loadTemplate(P_BLUETTE_CATALOG_TEMPLATE_PATH,           &quot;Catalog.st&quot;);</span>
<span class="nc" id="L142">        variablesTemplate        = loadTemplate(P_BLUETTE_VARIABLES_TEMPLATE_PATH,         &quot;Variables.st&quot;);</span>
<span class="nc" id="L143">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String getInlinedScript()
      {
<span class="nc" id="L153">        final var link = site.createLink(siteNode.getRelativeUri().appendedWith(&quot;images.xml&quot;));</span>
<span class="nc" id="L154">        final var properties = bluetteConfiguration.withProperty(P_CATALOG_URL, &quot;'&quot; + link + &quot;'&quot;);</span>
        // FIXME: since key doesn't have dynamic type, we can't properly escape strings.
<span class="nc" id="L156">        final var variables = properties.getKeys().stream()</span>
<span class="nc bnc" id="L157" title="All 4 branches missed.">                                        .filter(k -&gt; k.getName().startsWith(&quot;bluette&quot;) || &quot;logging&quot;.equals(k.getName()))</span>
<span class="nc" id="L158">                                        .flatMap(k -&gt; toAggregate(properties, k).stream())</span>
<span class="nc" id="L159">                                        .collect(toAggregates(&quot;entries&quot;));</span>

<span class="nc" id="L161">        return variablesTemplate.render(variables) + &quot;\n&quot; + redirectScriptTemplate.render();</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void prepareCatalog (@Nonnull final List&lt;? extends GalleryItem&gt; items)
      {
<span class="nc" id="L172">        final var entries = items.stream().map(this::toAggregate).collect(toAggregates(&quot;entries&quot;));</span>
<span class="nc" id="L173">        rendered = catalogTemplate.render(entries);</span>
<span class="nc" id="L174">        catalog = true;</span>
<span class="nc" id="L175">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    // TODO: Could we manage #! params here and select the proper item?
    @Override
    public void prepareGallery (@Nonnull final GalleryItem item, @Nonnull final List&lt;? extends GalleryItem&gt; items)
      {
<span class="nc" id="L186">        prepare(item, items, defaultTemplate);</span>
<span class="nc" id="L187">        rendered = defaultTemplate.render();</span>
<span class="nc" id="L188">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void prepareFallbackGallery (@Nonnull final GalleryItem item, @Nonnull final List&lt;? extends GalleryItem&gt; items)
      {
<span class="nc" id="L198">        prepare(item, items, fallbackTemplate);</span>
<span class="nc" id="L199">        rendered = fallbackTemplate.render();</span>
<span class="nc" id="L200">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void prepareFallbackLightbox (@Nonnull final List&lt;? extends GalleryItem&gt; items)
      {
<span class="nc" id="L210">        final var entries = items.stream().map(this::toAggregate).collect(toAggregates(&quot;entries&quot;));</span>
<span class="nc" id="L211">        final var redirectUrl = site.createLink(baseUrl.appendedWith(&quot;#!&quot;).appendedWith(lightboxSegmentUri)).replaceAll(&quot;/$&quot;, &quot;&quot;);</span>
<span class="nc" id="L212">        redirectScriptTemplate.addAttribute(&quot;redirectUrl&quot;, redirectUrl);</span>
<span class="nc" id="L213">        lightboxFallbackTemplate.addAttribute(&quot;copyright&quot;, copyright);</span>
<span class="nc" id="L214">        rendered = lightboxFallbackTemplate.render(entries);</span>
<span class="nc" id="L215">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void render (@Nonnull final RenderContext context)
      {
<span class="nc" id="L225">        final var textHolder = (TextHolder)view;</span>

<span class="nc bnc" id="L227" title="All 2 branches missed.">        if (!catalog)</span>
          {
<span class="nc" id="L229">            textHolder.addAttribute(&quot;content&quot;, rendered);</span>
          }
        else
          {
<span class="nc" id="L233">            textHolder.setTemplate(&quot;$content$\n&quot;);</span>
<span class="nc" id="L234">            textHolder.setContent(rendered);</span>
<span class="nc" id="L235">            textHolder.setMimeType(&quot;text/xml&quot;);</span>
          }
<span class="nc" id="L237">      }</span>

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void prepare (// final @Nonnull RenderContext context,
                          @Nonnull final GalleryItem item,
                          @Nonnull final List&lt;? extends GalleryItem&gt; items,
                          @Nonnull final Template template)
      {
<span class="nc" id="L248">        final var count          = items.size();</span>
<span class="nc" id="L249">        final var index          = items.indexOf(item);</span>
<span class="nc" id="L250">        final var prevIndex      = (index - 1 + count) % count;</span>
<span class="nc" id="L251">        final var nextIndex      = (index + 1) % count;</span>

<span class="nc" id="L253">        final var imageId     = item.getId().stringValue();</span>
<span class="nc" id="L254">        final var imageUrl    = createImageLink(imageId, fallbackImageSize);</span>

<span class="nc" id="L256">        final var redirectUrl = site.createLink(baseUrl.appendedWith(&quot;#!&quot;).appendedWith(imageId)).replaceAll(&quot;/$&quot;, &quot;&quot;);</span>
<span class="nc" id="L257">        final var previousUrl = site.createLink(baseUrl.appendedWith(items.get(prevIndex).getId().stringValue()));</span>
<span class="nc" id="L258">        final var nextUrl     = site.createLink(baseUrl.appendedWith(items.get(nextIndex).getId().stringValue()));</span>
<span class="nc" id="L259">        final var lightboxUrl = site.createLink(baseUrl.appendedWith(lightboxSegmentUri));</span>

<span class="nc" id="L261">        template.addAttribute(&quot;caption&quot;,   item.getDescription())</span>
<span class="nc" id="L262">                .addAttribute(&quot;previous&quot;,  previousUrl)</span>
<span class="nc" id="L263">                .addAttribute(&quot;next&quot;,      nextUrl)</span>
<span class="nc" id="L264">                .addAttribute(&quot;lightbox&quot;,  lightboxUrl)</span>
<span class="nc" id="L265">                .addAttribute(&quot;home&quot;,      homeUrl)</span>
<span class="nc" id="L266">                .addAttribute(&quot;imageId&quot;,   imageId)</span>
<span class="nc" id="L267">                .addAttribute(&quot;imageUrl&quot;,  imageUrl)</span>
<span class="nc" id="L268">                .addAttribute(&quot;copyright&quot;, copyright);</span>

<span class="nc bnc" id="L270" title="All 2 branches missed.">        if (template != defaultTemplate)</span>
          {
<span class="nc" id="L272">            redirectScriptTemplate.addAttribute(&quot;redirectUrl&quot;, redirectUrl);</span>

    //        context.setDynamicNodeProperty(PD_IMAGE_ID, imageId);
            // FIXME: these should be dynamic properties
these should be dynamic properties
// siteNodeProperties.getProperty().ifPresent(id -&gt; view.addAttribute(&quot;imageId&quot;, id)); // siteNodeProperties.getProperty(PD_URL).ifPresent(id -&gt; view.addAttribute(&quot;url&quot;, id)); <span class="nc" id="L278"> final var textHolder = (TextHolder)view;</span> <span class="nc" id="L279"> textHolder.addAttribute(&quot;description&quot;, item.getDescription());</span> <span class="nc" id="L280"> textHolder.addAttribute(&quot;imageId&quot;, imageId);</span> <span class="nc" id="L281"> textHolder.addAttribute(&quot;imageUrl&quot;, imageUrl);</span> } <span class="nc" id="L283"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private Aggregate toAggregate (@Nonnull final GalleryItem item) { <span class="nc" id="L291"> final var id = item.getId().stringValue();</span> <span class="nc" id="L292"> final var link = site.createLink(baseUrl.appendedWith(id));</span> <span class="nc" id="L293"> return Aggregate.of(&quot;id&quot;, id)</span> <span class="nc" id="L294"> .with(&quot;link&quot;, link)</span> <span class="nc" id="L295"> .with(&quot;url&quot;, createImageLink(id, fallbackThumbnailSize))</span> <span class="nc" id="L296"> .with(&quot;title&quot;, item.getDescription().replaceAll(&quot;\n+&quot;, &quot; &quot;));</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private static Optional&lt;Aggregate&gt; toAggregate (@Nonnull final ResourceProperties properties, @Nonnull final Key&lt;?&gt; key) { // A space in front of [ ] makes them interpreted as a string and not a list. <span class="nc" id="L307"> return properties.getProperty(key).map(v -&gt; Aggregate.of(&quot;name&quot;, key.stringValue()).with(&quot;value&quot;, v.toString().replace(&quot;[&quot;, &quot; [&quot;)));</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private String createImageLink (@Nonnull final String id, @Nonnegative final int size) { <span class="nc" id="L316"> return site.createLink(ResourcePath.of(String.format(&quot;/media/stillimages/%d/%s.jpg&quot;, size, id)));</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>