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.util.Key;
  36. import it.tidalwave.northernwind.core.model.HttpStatusException;
  37. import it.tidalwave.northernwind.core.model.ResourceProperties;
  38. import it.tidalwave.northernwind.core.model.Site;
  39. import it.tidalwave.northernwind.core.model.SiteNode;
  40. import it.tidalwave.northernwind.frontend.ui.Layout;
  41. import it.tidalwave.northernwind.frontend.ui.RenderContext;
  42. import lombok.EqualsAndHashCode;
  43. import lombok.Getter;
  44. import lombok.RequiredArgsConstructor;
  45. import lombok.ToString;
  46. import lombok.extern.slf4j.Slf4j;
  47. import static java.util.stream.Collectors.toList;
  48. import static it.tidalwave.northernwind.core.model.Content.*;
  49. import static it.tidalwave.northernwind.core.model.SiteNode._SiteNode_;
  50. import static it.tidalwave.northernwind.frontend.ui.component.blog.DefaultBlogViewController.TIME0;

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

  82.         @Nonnull
  83.         private final ZonedDateTime lastModification;

  84.         @Nonnull
  85.         private final String changeFrequency;

  86.         private final float priority;

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

  93.     @Nonnull
  94.     private final SiteNode siteNode;

  95.     @Nonnull
  96.     private final SitemapView view;

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

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

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

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

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

  143.                     @Nonnull
  144.                     @Override @SuppressWarnings("findbugs:NP_NONNULL_RETURN_VIOLATION")
  145.                     public Void getValue()
  146.                       {
  147.                         return null;
  148.                       }
  149.                   });
  150.               }
  151.           });

  152.         render(entries);
  153.       }

  154.     /*******************************************************************************************************************
  155.      *
  156.      *
  157.      *
  158.      ******************************************************************************************************************/
  159.     protected abstract void render (@Nonnull Set<Entry> entries);

  160.     /*******************************************************************************************************************
  161.      *
  162.      *
  163.      ******************************************************************************************************************/
  164.     @Nonnull
  165.     protected final ResourceProperties getViewProperties()
  166.       {
  167.         return siteNode.getPropertyGroup(view.getId());
  168.       }

  169.     /*******************************************************************************************************************
  170.      *
  171.      *
  172.      ******************************************************************************************************************/
  173.     @Nonnull
  174.     private static Optional<Entry> newEntry (@Nonnull final SiteNode siteNode,
  175.                                              @CheckForNull final SiteNode childSiteNode)
  176.       {
  177.         final SiteNode node = (childSiteNode != null) ? childSiteNode : siteNode;
  178.         final ResourceProperties properties = node.getProperties();
  179.         //
  180.         // FIXME: if you put the sitemap property straightly into the child site node, you can simplify a lot,
  181.         // just using a single property and only peeking into a single node
  182.         final Key<Float> priorityKey = (childSiteNode == null) ? P_SITEMAP_PRIORITY : P_SITEMAP_CHILDREN_PRIORITY;
  183.         final float sitemapPriority = siteNode.getProperty(priorityKey).orElse(0.5f);

  184.         return (sitemapPriority == 0)
  185.                 ? Optional.empty()
  186.                 : Optional.of(new Entry(siteNode.getSite().createLink(node.getRelativeUri()),
  187.                                   properties.getProperty(P_LATEST_MODIFICATION_DATE).orElse(TIME0),
  188.                                   properties.getProperty(P_SITEMAP_CHANGE_FREQUENCY).orElse("daily"),
  189.                                   sitemapPriority));
  190.       }

  191.     /*******************************************************************************************************************
  192.      *
  193.      *
  194.      ******************************************************************************************************************/
  195.     @Nonnull
  196.     private static Throwable rootCause (@Nonnull final Throwable t)
  197.       {
  198.         final Throwable cause = t.getCause();
  199.         return (cause != null) ? rootCause(cause) : t;
  200.       }
  201.   }