DefaultHtmlTextWithTitleViewController.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * NorthernWind - lightweight CMS
  6.  * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
  7.  * %%
  8.  * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  * *********************************************************************************************************************
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  13.  * the License. You may obtain a copy of the License at
  14.  *
  15.  *     http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  18.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations under the License.
  20.  *
  21.  * *********************************************************************************************************************
  22.  *
  23.  *
  24.  * *********************************************************************************************************************
  25.  * #L%
  26.  */
  27. package it.tidalwave.northernwind.frontend.ui.component.htmltextwithtitle;

  28. import javax.annotation.Nonnull;
  29. import java.util.List;
  30. import java.util.Optional;
  31. import it.tidalwave.northernwind.core.model.Content;
  32. import it.tidalwave.northernwind.core.model.SiteNode;
  33. import it.tidalwave.northernwind.frontend.ui.RenderContext;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;
  36. import static java.util.Collections.emptyList;
  37. import static java.util.stream.Collectors.*;
  38. import static it.tidalwave.northernwind.core.model.Content.*;
  39. import static it.tidalwave.northernwind.frontend.ui.component.Properties.*;

  40. /***********************************************************************************************************************
  41.  *
  42.  * <p>A default implementation of the {@link HtmlTextWithTitleViewController} that is independent of the presentation
  43.  * technology. This class is capable to render a sequence of texts with their titles.</p>
  44.  *
  45.  * <p>Supported properties of the {@link SiteNode}:</p>
  46.  *
  47.  * <ul>
  48.  * <li>{@code P_CONTENT_PATHS}: a set of paths to {@link Content}s;</li>
  49.  * <li>{@code P_CLASS}: an optional CSS class name for the wrapping {@code &lt;div&gt;}.</li>
  50.  * </ul>
  51.  *
  52.  * <p>For each {@code Content} the following properties are used:</p>
  53.  *
  54.  * <ul>
  55.  * <li>{@code P_TITLE}: for rendering the title;</li>
  56.  * <li>{@code P_FULL_TEXT}: for rendering the text.</li>
  57.  * </ul>
  58.  *
  59.  * <p>Concrete implementations must provide the following method:</p>
  60.  *
  61.  * <ul>
  62.  * <li>{@link #render(java.util.List)}</li>
  63.  * </ul>
  64.  *
  65.  * @author  Fabrizio Giudici
  66.  *
  67.  **********************************************************************************************************************/
  68. @RequiredArgsConstructor @Slf4j
  69. public abstract class DefaultHtmlTextWithTitleViewController implements HtmlTextWithTitleViewController
  70.   {
  71.     @RequiredArgsConstructor
  72.     public static class TextWithTitle
  73.       {
  74.         public final Optional<String> title;
  75.         public final Optional<String> text;
  76.         public final int level;
  77.       }

  78.     @Nonnull
  79.     private final HtmlTextWithTitleView view;

  80.     @Nonnull
  81.     private final SiteNode siteNode;

  82.     /*******************************************************************************************************************
  83.      *
  84.      * {@inheritDoc}
  85.      *
  86.      ******************************************************************************************************************/
  87.     @Override
  88.     public void renderView (@Nonnull final RenderContext context)
  89.       {
  90.         final var viewProperties = siteNode.getPropertyGroup(view.getId());
  91.         final int titleLevel = viewProperties.getProperty(P_LEVEL).orElse(2);
  92.         view.setClassName(viewProperties.getProperty(P_CLASS).orElse("nw-" + view.getId()));
  93.         render(viewProperties.getProperty(P_CONTENT_PATHS).orElse(emptyList())
  94.                 .stream()
  95.                 .flatMap(path -> siteNode.getSite().find(_Content_).withRelativePath(path).stream())
  96.                 .map(c -> new TextWithTitle(c.getProperty(P_TITLE), c.getProperty(P_FULL_TEXT), titleLevel))
  97.                 .collect(toList()));
  98.       }

  99.     /*******************************************************************************************************************
  100.      *
  101.      * Renders the collection of texts with their titles.
  102.      *
  103.      * @param   contents    the contents to render
  104.      *
  105.      ******************************************************************************************************************/
  106.     protected abstract void render (@Nonnull List<? extends TextWithTitle> contents);
  107.   }