DefaultSitemapViewController.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.sitemap;

  28. import javax.annotation.CheckForNull;
  29. import javax.annotation.Nonnull;
  30. import java.time.ZonedDateTime;
  31. import java.util.Optional;
  32. import java.util.Set;
  33. import java.util.SortedSet;
  34. import java.util.TreeSet;
  35. import it.tidalwave.northernwind.core.model.HttpStatusException;
  36. import it.tidalwave.northernwind.core.model.ResourceProperties;
  37. import it.tidalwave.northernwind.core.model.Site;
  38. import it.tidalwave.northernwind.core.model.SiteNode;
  39. import it.tidalwave.northernwind.frontend.ui.Layout;
  40. import it.tidalwave.northernwind.frontend.ui.RenderContext;
  41. import lombok.EqualsAndHashCode;
  42. import lombok.Getter;
  43. import lombok.RequiredArgsConstructor;
  44. import lombok.ToString;
  45. import lombok.extern.slf4j.Slf4j;
  46. import static java.util.stream.Collectors.*;
  47. import static it.tidalwave.northernwind.core.model.Content.*;
  48. import static it.tidalwave.northernwind.core.model.SiteNode._SiteNode_;
  49. import static it.tidalwave.northernwind.frontend.ui.component.blog.DefaultBlogViewController.TIME0;

  50. /***********************************************************************************************************************
  51.  *
  52.  * <p>A default implementation of the {@link SitemapViewController} that is independent of the presentation technology.
  53.  * This class is capable to render the sitemap of a {@link Site}.</p>
  54.  *
  55.  * <p>Supported properties of any {@link SiteNode} in the site:</p>
  56.  *
  57.  * <ul>
  58.  * <li>{@code P_SITEMAP_PRIORITY}: the priority of the {@code SiteNode} - if zero, the node is ignored;</li>
  59.  * <li>{@code P_SITEMAP_CHILDREN_PRIORITY}: same as {@code P_SITEMAP_PRIORITY}, but for child nodes;</li>
  60.  * <li>{@code P_LATEST_MODIFICATION_DATE}: the date-time of the latest modification;</li>
  61.  * <li>{@code P_SITEMAP_CHANGE_FREQUENCY}: the supposed change frequency of the {@code SiteNode}.</li>
  62.  * </ul>
  63.  *
  64.  * <p>Concrete implementations must provide one method for rendering the calendar:</p>
  65.  *
  66.  * <ul>
  67.  * <li>{@link #render(java.util.List)}</li>
  68.  * </ul>
  69.  *
  70.  * @author  Fabrizio Giudici
  71.  *
  72.  **********************************************************************************************************************/
  73. @RequiredArgsConstructor @Slf4j
  74. public abstract class DefaultSitemapViewController implements SitemapViewController
  75.   {
  76.     @RequiredArgsConstructor @ToString @Getter @EqualsAndHashCode
  77.     protected static class Entry implements Comparable<Entry>
  78.       {
  79.         @Nonnull
  80.         private final String location;

  81.         @Nonnull
  82.         private final ZonedDateTime lastModification;

  83.         @Nonnull
  84.         private final String changeFrequency;

  85.         private final float priority;

  86.         @Override
  87.         public int compareTo (@Nonnull final Entry other)
  88.           {
  89.             return this.equals(other) ? 0 : this.location.compareTo(other.location);
  90.           }
  91.       }

  92.     @Nonnull
  93.     private final SiteNode siteNode;

  94.     @Nonnull
  95.     private final SitemapView view;

  96.     /*******************************************************************************************************************
  97.      *
  98.      * {@inheritDoc}
  99.      *
  100.      ******************************************************************************************************************/
  101.     @Override
  102.     public void renderView (@Nonnull final RenderContext context)
  103.       {
  104.         final SortedSet<Entry> entries = new TreeSet<>();

  105.         siteNode.getSite().find(_SiteNode_).stream().forEach(node ->
  106.           {
  107.             final var layout = node.getLayout();

  108.             // Prevents infinite recursion
  109.             if (!layout.getTypeUri().startsWith("http://northernwind.tidalwave.it/component/Sitemap/"))
  110.               {
  111.                 // FIXME: should probably skip children of sitenodes with managePathParams
  112.                 // FIXME: for instance, Calendar would benefit
  113.                 // FIXME: Would Blog benefit? It should, as it manages its own children
  114.                 // FIXME: Places and Themes should move managePathParams=true to each children
  115.                 // FIXME: Problem, the root gallery needs managePathParams=true to load images.xml
  116.                 log.debug(">>>> sitemap processing {} / layout {} ...", node.getRelativeUri(), layout);

  117.                 newEntry(node, null).ifPresent(entries::add);

  118.                 layout.accept(new Visitor<Layout, Void>()
  119.                   {
  120.                     @Override
  121.                     public void visit (@Nonnull final Layout childLayout)
  122.                       {
  123.                         try
  124.                           {
  125.                             entries.addAll(childLayout.createViewAndController(node).getController()
  126.                                 .findVirtualSiteNodes()
  127.                                 .results()
  128.                                 .stream()
  129.                                 .peek(e -> log.debug(">>>>>>>> added virtual node: {}", e.getRelativeUri()))
  130.                                 .flatMap(childNode -> newEntry(node, childNode).stream())
  131.                                 .collect(toList()));
  132.                           }
  133.                         catch (HttpStatusException e)
  134.                           {
  135.                             log.warn("sitemap for {} threw {}", node.getRelativeUri(), e.toString());
  136.                           }
  137.                         catch (Exception e)
  138.                           {
  139.                             log.warn("Skipped item because of {} - root cause {}", e, rootCause(e).toString());
  140.                           }
  141.                       }
  142.                   });
  143.               }
  144.           });

  145.         render(entries);
  146.       }

  147.     /*******************************************************************************************************************
  148.      *
  149.      *
  150.      *
  151.      ******************************************************************************************************************/
  152.     protected abstract void render (@Nonnull Set<? extends Entry> entries);

  153.     /*******************************************************************************************************************
  154.      *
  155.      *
  156.      ******************************************************************************************************************/
  157.     @Nonnull
  158.     protected final ResourceProperties getViewProperties()
  159.       {
  160.         return siteNode.getPropertyGroup(view.getId());
  161.       }

  162.     /*******************************************************************************************************************
  163.      *
  164.      *
  165.      ******************************************************************************************************************/
  166.     @Nonnull
  167.     private static Optional<Entry> newEntry (@Nonnull final SiteNode siteNode,
  168.                                              @CheckForNull final SiteNode childSiteNode)
  169.       {
  170.         final var node = (childSiteNode != null) ? childSiteNode : siteNode;
  171.         final var properties = node.getProperties();
  172.         //
  173.         // FIXME: if you put the sitemap property straightly into the child site node, you can simplify a lot,
  174.         // just using a single property and only peeking into a single node
  175.         final var priorityKey = (childSiteNode == null) ? P_SITEMAP_PRIORITY : P_SITEMAP_CHILDREN_PRIORITY;
  176.         final float sitemapPriority = siteNode.getProperty(priorityKey).orElse(0.5f);

  177.         return (sitemapPriority == 0)
  178.                 ? Optional.empty()
  179.                 : Optional.of(new Entry(siteNode.getSite().createLink(node.getRelativeUri()),
  180.                                   properties.getProperty(P_LATEST_MODIFICATION_DATE).orElse(TIME0),
  181.                                   properties.getProperty(P_SITEMAP_CHANGE_FREQUENCY).orElse("daily"),
  182.                                   sitemapPriority));
  183.       }

  184.     /*******************************************************************************************************************
  185.      *
  186.      *
  187.      ******************************************************************************************************************/
  188.     @Nonnull
  189.     private static Throwable rootCause (@Nonnull final Throwable t)
  190.       {
  191.         final var cause = t.getCause();
  192.         return (cause != null) ? rootCause(cause) : t;
  193.       }
  194.   }