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.ResourceProperties;
  33. import it.tidalwave.northernwind.core.model.SiteNode;
  34. import it.tidalwave.northernwind.frontend.ui.RenderContext;
  35. import lombok.RequiredArgsConstructor;
  36. import lombok.extern.slf4j.Slf4j;
  37. import static java.util.Collections.*;
  38. import static java.util.stream.Collectors.toList;
  39. import static it.tidalwave.northernwind.core.model.Content.*;
  40. import static it.tidalwave.northernwind.frontend.ui.component.Properties.*;

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

  79.     @Nonnull
  80.     private final HtmlTextWithTitleView view;

  81.     @Nonnull
  82.     private final SiteNode siteNode;

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

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