DefaultMenuViewController.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.menu;

  28. import javax.annotation.Nonnull;
  29. import org.springframework.context.annotation.Scope;
  30. import it.tidalwave.northernwind.core.model.ResourceProperties;
  31. import it.tidalwave.northernwind.core.model.Site;
  32. import it.tidalwave.northernwind.core.model.SiteNode;
  33. import it.tidalwave.northernwind.frontend.ui.RenderContext;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;
  36. import static java.util.Collections.*;
  37. import static it.tidalwave.northernwind.core.model.Content.P_TITLE;
  38. import static it.tidalwave.northernwind.core.model.SiteNode.*;
  39. import static it.tidalwave.northernwind.frontend.ui.component.Properties.*;

  40. /***********************************************************************************************************************
  41.  *
  42.  * The default implementation of {@link MenuViewController}.
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @RequiredArgsConstructor @Scope("session") @Slf4j
  48. public class DefaultMenuViewController implements MenuViewController
  49.   {
  50.     @Nonnull
  51.     protected final MenuView view;

  52.     @Nonnull
  53.     protected final SiteNode siteNode;

  54.     /*******************************************************************************************************************
  55.      *
  56.      * {@inheritDoc}
  57.      *
  58.      ******************************************************************************************************************/
  59.     @Override
  60.     public void renderView (@Nonnull final RenderContext context)
  61.      {
  62.         final Site site = siteNode.getSite();
  63.         final ResourceProperties viewProperties = siteNode.getPropertyGroup(view.getId());
  64.         viewProperties.getProperty(P_TITLE).ifPresent(view::setTitle);
  65.         viewProperties.getProperty(P_TEMPLATE_PATH).flatMap(p -> site.getTemplate(getClass(), p))
  66.                                                    .ifPresent(view::setTemplate);

  67.         viewProperties.getProperty(P_LINKS).orElse(emptyList())
  68.                 .stream()
  69.                 .flatMap(path -> site.find(_SiteNode_).withRelativePath(path).stream())
  70.                 .forEach(node -> view.addLink(node.getProperty(P_NAVIGATION_LABEL).orElse("no nav. label"), // FIXME
  71.                                               site.createLink(node.getRelativeUri())));
  72.       }
  73.   }