Content of file DefaultLayout.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>DefaultLayout.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 :: Filesystems :: SCM :: Mercurial</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.impl.ui</a> &gt; <span class="el_source">DefaultLayout.java</span></div><h1>DefaultLayout.java</h1><pre class="source lang-java linenums"><span class="nc" id="L1">/*</span>
 * #%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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.impl.ui;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Configurable;
import it.tidalwave.util.As;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.HttpStatusException;
import it.tidalwave.northernwind.core.model.SiteNode;
import it.tidalwave.northernwind.frontend.ui.Layout;
import it.tidalwave.northernwind.frontend.ui.LayoutFinder;
import it.tidalwave.northernwind.frontend.ui.ViewFactory;
import it.tidalwave.northernwind.frontend.ui.ViewFactory.ViewAndController;
import lombok.Getter;
import lombok.experimental.Delegate;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
@Configurable @Getter
public class DefaultLayout implements Layout, Cloneable
  {
    @Inject
<span class="nc" id="L60">    private ViewFactory viewFactory;</span>

    @Nonnull
<span class="nc" id="L63">    private final Id id;</span>

    @Nonnull
<span class="nc" id="L66">    private /* final FIXME */ String typeUri;</span>

<span class="nc" id="L68">    private final List&lt;Layout&gt; children = new ArrayList&lt;&gt;();</span>

<span class="nc" id="L70">    private final Map&lt;Id, Layout&gt; childrenMapById = new HashMap&lt;&gt;();</span>

<span class="nc" id="L72">    @Delegate</span>
<span class="nc" id="L73">    private final As asSupport = As.forObject(this);</span>

    // FIXME: make it Immutable
make it Immutable
/******************************************************************************************************************* * * * ******************************************************************************************************************/ <span class="nc" id="L82"> static class CloneVisitor implements Visitor&lt;Layout, DefaultLayout&gt;</span> { private DefaultLayout rootLayout; <span class="nc" id="L86"> private final Deque&lt;DefaultLayout&gt; layoutStack = new ArrayDeque&lt;&gt;();</span> @Override public void preVisit (@Nonnull final Layout layout) { <span class="nc" id="L91"> final var clone = new DefaultLayout(((DefaultLayout)layout).id,</span> ((DefaultLayout)layout).typeUri); <span class="nc bnc" id="L94" title="All 2 branches missed."> if (rootLayout == null)</span> { <span class="nc" id="L96"> rootLayout = clone;</span> } else { <span class="nc" id="L100"> layoutStack.peek().add(clone);</span> } <span class="nc" id="L103"> layoutStack.push(clone);</span> <span class="nc" id="L104"> }</span> @Override public void postVisit (@Nonnull final Layout layout) { <span class="nc" id="L109"> layoutStack.pop();</span> <span class="nc" id="L110"> }</span> @Override @Nonnull public Optional&lt;DefaultLayout&gt; getValue() { <span class="nc" id="L115"> return Optional.of(rootLayout);</span> } } /******************************************************************************************************************* * * * ******************************************************************************************************************/ public DefaultLayout() <span class="nc bnc" id="L125" title="All 18 branches missed."> {</span> <span class="nc" id="L126"> this.id = new Id(&quot;&quot;);</span> <span class="nc" id="L127"> this.typeUri = &quot;&quot;;</span> <span class="nc bnc" id="L128" title="All 4 branches missed."> }</span> /** Clone - FIXME: public only for InfoglueImporter */ /******************************************************************************************************************* * * * ******************************************************************************************************************/ public DefaultLayout (@Nonnull final Id id, @Nonnull final String typeUri) <span class="nc bnc" id="L137" title="All 18 branches missed."> {</span> <span class="nc" id="L138"> this.id = id;</span> <span class="nc" id="L139"> this.typeUri = typeUri;</span> <span class="nc bnc" id="L140" title="All 4 branches missed."> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ public DefaultLayout (@Nonnull final Builder builder) <span class="nc bnc" id="L148" title="All 18 branches missed."> {</span> <span class="nc" id="L149"> this.id = builder.getId();</span> <span class="nc" id="L150"> this.typeUri = builder.getType();</span> <span class="nc bnc" id="L151" title="All 4 branches missed."> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public DefaultLayout clone() { <span class="nc" id="L161"> return accept(new CloneVisitor()).orElseThrow(RuntimeException::new);</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Layout withOverride (@Nonnull final Layout override) { <span class="nc" id="L172"> final var result = clone();</span> <span class="nc" id="L173"> result.applyOverride(((DefaultLayout)override).clone());</span> <span class="nc" id="L174"> return result;</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Layout withChild (@Nonnull final Layout layout) { <span class="nc" id="L185"> final var clone = clone();</span> <span class="nc" id="L186"> clone.children.add(layout);</span> <span class="nc" id="L187"> clone.childrenMapById.put(layout.getId(), layout);</span> <span class="nc" id="L189"> return clone;</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public LayoutFinder findChildren() { <span class="nc" id="L200"> return new DefaultLayoutFinder(children, childrenMapById);</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ public void add (@Nonnull final Layout layout) // FIXME: drop this - used only by the CloneVisitor and Infoglue converter { <span class="nc" id="L210"> children.add(layout); // FIXME: clone</span> <span class="nc" id="L211"> childrenMapById.put(layout.getId(), layout);// FIXME: clone</span> <span class="nc" id="L212"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public ViewAndController createViewAndController (@Nonnull final SiteNode siteNode) throws NotFoundException, HttpStatusException { <span class="nc" id="L223"> return viewFactory.createViewAndController(typeUri, id, siteNode);</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull // TODO: push up to CompositeSupport public &lt;T&gt; Optional&lt;T&gt; accept (@Nonnull final Visitor&lt;? super Layout, T&gt; visitor) { <span class="nc" id="L234"> visitor.preVisit(this);</span> <span class="nc" id="L235"> visitor.visit(this);</span> <span class="nc bnc" id="L237" title="All 2 branches missed."> for (final var child : children)</span> { <span class="nc" id="L239"> child.accept(visitor);</span> <span class="nc" id="L240"> }</span> <span class="nc" id="L242"> visitor.postVisit(this);</span> <span class="nc" id="L243"> return visitor.getValue();</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public String toString() { <span class="nc" id="L254"> return String.format(&quot;DefaultLayout(id=%s, typeUri=%s, children.count=%d)&quot;, id, typeUri, children.size());</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ // Here everything is already cloned private void applyOverride (@Nonnull final Layout override) { <span class="nc" id="L265"> final var sameType = this.getTypeUri().equals(override.getTypeUri());</span> <span class="nc" id="L266"> this.typeUri = override.getTypeUri(); // FIXME: don't like this approach, as it requires typeUri non final</span> // Complex rule, but it's to keep compatibility with Infoglue. <span class="nc bnc" id="L269" title="All 2 branches missed."> if (sameType)</span> { <span class="nc bnc" id="L271" title="All 2 branches missed."> for (final var overridingChild : override.findChildren().results())</span> { <span class="nc" id="L273"> final var overriddenChild = childrenMapById.get(overridingChild.getId());</span> <span class="nc bnc" id="L275" title="All 2 branches missed."> if (overriddenChild == null)</span> { <span class="nc" id="L277"> add(overridingChild);</span> } else { <span class="nc" id="L281"> childrenMapById.put(overridingChild.getId(), overridingChild);</span> <span class="nc" id="L282"> final var i = children.indexOf(overriddenChild);</span> <span class="nc bnc" id="L284" title="All 2 branches missed."> if (i &lt; 0)</span> { <span class="nc" id="L286"> throw new IllegalArgumentException();</span> } <span class="nc" id="L289"> children.set(i, overridingChild);</span> // ((DefaultLayout)overriddenChild).applyOverride(overridingChild); } <span class="nc" id="L292"> }</span> } else { <span class="nc" id="L296"> this.children.clear();</span> <span class="nc" id="L297"> this.childrenMapById.clear();</span> <span class="nc bnc" id="L299" title="All 2 branches missed."> for (final var overridingChild : override.findChildren().results())</span> { <span class="nc" id="L301"> add(overridingChild);</span> <span class="nc" id="L302"> }</span> } <span class="nc" id="L304"> }</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>