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 :: Core :: Default Implementation</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.impl.ui</a> > <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 "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.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.Optional;
import java.util.Map;
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.util.spi.AsSupport;
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.experimental.Delegate;
import lombok.Getter;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
@Configurable @Getter
public class DefaultLayout implements Layout, Cloneable
{
@Inject
<span class="nc" id="L61"> private ViewFactory viewFactory;</span>
@Nonnull
<span class="nc" id="L64"> private final Id id;</span>
@Nonnull
<span class="nc" id="L67"> private /* final FIXME */ String typeUri;</span>
<span class="nc" id="L69"> private final List<Layout> children = new ArrayList<>();</span>
<span class="nc" id="L71"> private final Map<Id, Layout> childrenMapById = new HashMap<>();</span>
<span class="nc" id="L73"> @Delegate</span>
<span class="nc" id="L74"> private final As asSupport = new AsSupport(this);</span>
// FIXME: make it Immutable
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
<span class="nc" id="L83"> static class CloneVisitor extends VisitorSupport<Layout, DefaultLayout></span>
{
private DefaultLayout rootLayout;
<span class="nc" id="L87"> private final Deque<DefaultLayout> layoutStack = new ArrayDeque<>();</span>
@Override
public void preVisit (@Nonnull final Layout layout)
{
<span class="nc" id="L92"> final DefaultLayout clone = new DefaultLayout(((DefaultLayout)layout).id,</span>
((DefaultLayout)layout).typeUri);
<span class="nc bnc" id="L95" title="All 2 branches missed."> if (rootLayout == null)</span>
{
<span class="nc" id="L97"> rootLayout = clone;</span>
}
else
{
<span class="nc" id="L101"> layoutStack.peek().add(clone);</span>
}
<span class="nc" id="L104"> layoutStack.push(clone);</span>
<span class="nc" id="L105"> }</span>
@Override
public void postVisit (@Nonnull final Layout layout)
{
<span class="nc" id="L110"> layoutStack.pop();</span>
<span class="nc" id="L111"> }</span>
@Override @Nonnull
public DefaultLayout getValue()
{
<span class="nc" id="L116"> return rootLayout;</span>
}
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public DefaultLayout()
<span class="nc bnc" id="L126" title="All 18 branches missed."> {</span>
<span class="nc" id="L127"> this.id = new Id("");</span>
<span class="nc" id="L128"> this.typeUri = "";</span>
<span class="nc bnc" id="L129" 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="L138" title="All 18 branches missed."> {</span>
<span class="nc" id="L139"> this.id = id;</span>
<span class="nc" id="L140"> this.typeUri = typeUri;</span>
<span class="nc bnc" id="L141" title="All 4 branches missed."> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public DefaultLayout (@Nonnull final Builder builder)
<span class="nc bnc" id="L149" title="All 18 branches missed."> {</span>
<span class="nc" id="L150"> this.id = builder.getId();</span>
<span class="nc" id="L151"> this.typeUri = builder.getType();</span>
<span class="nc bnc" id="L152" title="All 4 branches missed."> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public DefaultLayout clone()
{
<span class="nc" id="L162"> return accept(new CloneVisitor()).orElseThrow(RuntimeException::new);</span>
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public Layout withOverride (@Nonnull final Layout override)
{
<span class="nc" id="L173"> final DefaultLayout result = clone();</span>
<span class="nc" id="L174"> result.applyOverride(((DefaultLayout)override).clone());</span>
<span class="nc" id="L175"> return result;</span>
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public Layout withChild (@Nonnull final Layout layout)
{
<span class="nc" id="L186"> final DefaultLayout clone = clone();</span>
<span class="nc" id="L187"> clone.children.add(layout);</span>
<span class="nc" id="L188"> clone.childrenMapById.put(layout.getId(), layout);</span>
<span class="nc" id="L190"> return clone;</span>
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public LayoutFinder findChildren()
{
<span class="nc" id="L201"> 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="L211"> children.add(layout); // FIXME: clone</span>
<span class="nc" id="L212"> childrenMapById.put(layout.getId(), layout);// FIXME: clone</span>
<span class="nc" id="L213"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public ViewAndController createViewAndController (@Nonnull final SiteNode siteNode)
throws NotFoundException, HttpStatusException
{
<span class="nc" id="L224"> return viewFactory.createViewAndController(typeUri, id, siteNode);</span>
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull // TODO: push up to CompositeSupport
| push up to CompositeSupport | |
public <T> Optional<T> accept (@Nonnull final Visitor<Layout, T> visitor)
{
<span class="nc" id="L235"> visitor.preVisit(this);</span>
<span class="nc" id="L236"> visitor.visit(this);</span>
<span class="nc bnc" id="L238" title="All 2 branches missed."> for (final Layout child : children)</span>
{
<span class="nc" id="L240"> child.accept(visitor);</span>
<span class="nc" id="L241"> }</span>
<span class="nc" id="L243"> visitor.postVisit(this);</span>
try
{
<span class="nc" id="L247"> return Optional.ofNullable(visitor.getValue()); // TODO: visitor.getOptionalValue()</span>
}
<span class="nc" id="L249"> catch (NotFoundException e)</span>
{
<span class="nc" id="L251"> return Optional.empty();</span>
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public String toString()
{
<span class="nc" id="L263"> return String.format("DefaultLayout(id=%s, typeUri=%s, children.count=%d)", id, typeUri, children.size());</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
// Here everything is already cloned
private void applyOverride (@Nonnull final Layout override)
{
<span class="nc" id="L274"> final boolean sameType = this.getTypeUri().equals(override.getTypeUri());</span>
<span class="nc" id="L275"> 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="L278" title="All 2 branches missed."> if (sameType)</span>
{
<span class="nc bnc" id="L280" title="All 2 branches missed."> for (final Layout overridingChild : override.findChildren().results())</span>
{
<span class="nc" id="L282"> final Layout overriddenChild = childrenMapById.get(overridingChild.getId());</span>
<span class="nc bnc" id="L284" title="All 2 branches missed."> if (overriddenChild == null)</span>
{
<span class="nc" id="L286"> add(overridingChild);</span>
}
else
{
<span class="nc" id="L290"> childrenMapById.put(overridingChild.getId(), overridingChild);</span>
<span class="nc" id="L291"> final int i = children.indexOf(overriddenChild);</span>
<span class="nc bnc" id="L293" title="All 2 branches missed."> if (i < 0)</span>
{
<span class="nc" id="L295"> throw new IllegalArgumentException();</span>
}
<span class="nc" id="L298"> children.set(i, overridingChild);</span>
// ((DefaultLayout)overriddenChild).applyOverride(overridingChild);
}
<span class="nc" id="L301"> }</span>
}
else
{
<span class="nc" id="L305"> this.children.clear();</span>
<span class="nc" id="L306"> this.childrenMapById.clear();</span>
<span class="nc bnc" id="L308" title="All 2 branches missed."> for (final Layout overridingChild : override.findChildren().results())</span>
{
<span class="nc" id="L310"> add(overridingChild);</span>
<span class="nc" id="L311"> }</span>
}
<span class="nc" id="L313"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>