Content of file DefaultSitemapViewController.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>DefaultSitemapViewController.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 :: Media</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-frontend-components</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.ui.component.sitemap</a> > <span class="el_source">DefaultSitemapViewController.java</span></div><h1>DefaultSitemapViewController.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 "License"); 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 "AS IS" 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.sitemap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import it.tidalwave.northernwind.core.model.HttpStatusException;
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.frontend.ui.Layout;
import it.tidalwave.northernwind.frontend.ui.RenderContext;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static java.util.stream.Collectors.*;
import static it.tidalwave.northernwind.core.model.Content.*;
import static it.tidalwave.northernwind.core.model.SiteNode._SiteNode_;
import static it.tidalwave.northernwind.frontend.ui.component.blog.DefaultBlogViewController.TIME0;
/***********************************************************************************************************************
*
* <p>A default implementation of the {@link SitemapViewController} that is independent of the presentation technology.
* This class is capable to render the sitemap of a {@link Site}.</p>
*
* <p>Supported properties of any {@link SiteNode} in the site:</p>
*
* <ul>
* <li>{@code P_SITEMAP_PRIORITY}: the priority of the {@code SiteNode} - if zero, the node is ignored;</li>
* <li>{@code P_SITEMAP_CHILDREN_PRIORITY}: same as {@code P_SITEMAP_PRIORITY}, but for child nodes;</li>
* <li>{@code P_LATEST_MODIFICATION_DATE}: the date-time of the latest modification;</li>
* <li>{@code P_SITEMAP_CHANGE_FREQUENCY}: the supposed change frequency of the {@code SiteNode}.</li>
* </ul>
*
* <p>Concrete implementations must provide one method for rendering the calendar:</p>
*
* <ul>
* <li>{@link #render(java.util.List)}</li>
* </ul>
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L75">@RequiredArgsConstructor @Slf4j</span>
public abstract class DefaultSitemapViewController implements SitemapViewController
{
<span class="pc bpc" id="L78" title="19 of 32 branches missed."> @RequiredArgsConstructor @ToString @Getter @EqualsAndHashCode</span>
protected static class Entry implements Comparable<Entry>
{
@Nonnull
<span class="fc" id="L82"> private final String location;</span>
@Nonnull
<span class="fc" id="L85"> private final ZonedDateTime lastModification;</span>
@Nonnull
<span class="fc" id="L88"> private final String changeFrequency;</span>
<span class="fc" id="L90"> private final float priority;</span>
@Override
public int compareTo (@Nonnull final Entry other)
{
<span class="fc bfc" id="L95" title="All 2 branches covered."> return this.equals(other) ? 0 : this.location.compareTo(other.location);</span>
}
}
@Nonnull
private final SiteNode siteNode;
@Nonnull
private final SitemapView view;
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public void renderView (@Nonnull final RenderContext context)
{
<span class="fc" id="L113"> final SortedSet<Entry> entries = new TreeSet<>();</span>
<span class="fc" id="L115"> siteNode.getSite().find(_SiteNode_).stream().forEach(node -></span>
{
<span class="fc" id="L117"> final var layout = node.getLayout();</span>
// Prevents infinite recursion
<span class="pc bpc" id="L120" title="1 of 2 branches missed."> if (!layout.getTypeUri().startsWith("http://northernwind.tidalwave.it/component/Sitemap/"))</span>
{
// FIXME: should probably skip children of sitenodes with managePathParams
should probably skip children of sitenodes with managePathParams
// FIXME: for instance, Calendar would benefit
// FIXME: Would Blog benefit? It should, as it manages its own children
// FIXME: Places and Themes should move managePathParams=true to each children
// FIXME: Problem, the root gallery needs managePathParams=true to load images.xml
<span class="fc" id="L127"> log.debug(">>>> sitemap processing {} / layout {} ...", node.getRelativeUri(), layout);</span>
<span class="fc" id="L129"> newEntry(node, null).ifPresent(entries::add);</span>
<span class="fc" id="L131"> layout.accept(new Visitor<Layout, Void>()</span>
<span class="fc" id="L132"> {</span>
@Override
public void visit (@Nonnull final Layout childLayout)
{
try
{
<span class="fc" id="L138"> entries.addAll(childLayout.createViewAndController(node).getController()</span>
<span class="fc" id="L139"> .findVirtualSiteNodes()</span>
<span class="fc" id="L140"> .results()</span>
<span class="fc" id="L141"> .stream()</span>
<span class="fc" id="L142"> .peek(e -> log.debug(">>>>>>>> added virtual node: {}", e.getRelativeUri()))</span>
<span class="fc" id="L143"> .flatMap(childNode -> newEntry(node, childNode).stream())</span>
<span class="fc" id="L144"> .collect(toList()));</span>
}
<span class="nc" id="L146"> catch (HttpStatusException e)</span>
{
<span class="nc" id="L148"> log.warn("sitemap for {} threw {}", node.getRelativeUri(), e.toString());</span>
}
<span class="nc" id="L150"> catch (Exception e)</span>
{
<span class="nc" id="L152"> log.warn("Skipped item because of {} - root cause {}", e, rootCause(e).toString());</span>
<span class="pc" id="L153"> }</span>
<span class="fc" id="L154"> }</span>
});
}
<span class="fc" id="L157"> });</span>
<span class="fc" id="L159"> render(entries);</span>
<span class="fc" id="L160"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
protected abstract void render (@Nonnull Set<? extends Entry> entries);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected final ResourceProperties getViewProperties()
{
<span class="nc" id="L176"> return siteNode.getPropertyGroup(view.getId());</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private static Optional<Entry> newEntry (@Nonnull final SiteNode siteNode,
@CheckForNull final SiteNode childSiteNode)
{
<span class="fc bfc" id="L187" title="All 2 branches covered."> final var node = (childSiteNode != null) ? childSiteNode : siteNode;</span>
<span class="fc" id="L188"> final var properties = node.getProperties();</span>
//
// FIXME: if you put the sitemap property straightly into the child site node, you can simplify a lot,
// just using a single property and only peeking into a single node
<span class="fc bfc" id="L192" title="All 2 branches covered."> final var priorityKey = (childSiteNode == null) ? P_SITEMAP_PRIORITY : P_SITEMAP_CHILDREN_PRIORITY;</span>
<span class="fc" id="L193"> final float sitemapPriority = siteNode.getProperty(priorityKey).orElse(0.5f);</span>
<span class="fc bfc" id="L195" title="All 2 branches covered."> return (sitemapPriority == 0)</span>
<span class="fc" id="L196"> ? Optional.empty()</span>
<span class="fc" id="L197"> : Optional.of(new Entry(siteNode.getSite().createLink(node.getRelativeUri()),</span>
<span class="fc" id="L198"> properties.getProperty(P_LATEST_MODIFICATION_DATE).orElse(TIME0),</span>
<span class="fc" id="L199"> properties.getProperty(P_SITEMAP_CHANGE_FREQUENCY).orElse("daily"),</span>
sitemapPriority));
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private static Throwable rootCause (@Nonnull final Throwable t)
{
<span class="nc" id="L210"> final var cause = t.getCause();</span>
<span class="nc bnc" id="L211" title="All 2 branches missed."> return (cause != null) ? rootCause(cause) : t;</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>