DefaultGalleryViewController.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.gallery;

  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import org.springframework.beans.factory.BeanFactory;
  34. import it.tidalwave.util.Finder;
  35. import it.tidalwave.util.Id;
  36. import it.tidalwave.util.spi.HierarchicFinderSupport;
  37. import it.tidalwave.northernwind.core.model.RequestLocaleManager;
  38. import it.tidalwave.northernwind.core.model.SiteNode;
  39. import it.tidalwave.northernwind.frontend.ui.component.gallery.spi.GalleryAdapter;
  40. import it.tidalwave.northernwind.frontend.ui.component.gallery.spi.GalleryLoader;
  41. import it.tidalwave.northernwind.frontend.ui.component.gallery.spi.loader.SlideShowProPlayerGalleryLoader;
  42. import it.tidalwave.northernwind.frontend.ui.component.nodecontainer.DefaultNodeContainerViewController;
  43. import it.tidalwave.northernwind.frontend.ui.component.nodecontainer.NodeContainerView;
  44. import it.tidalwave.northernwind.frontend.ui.spi.VirtualSiteNode;
  45. import lombok.RequiredArgsConstructor;
  46. import lombok.extern.slf4j.Slf4j;
  47. import static java.util.stream.Collectors.*;

  48. /***********************************************************************************************************************
  49.  *
  50.  * @author  Fabrizio Giudici
  51.  *
  52.  **********************************************************************************************************************/
  53. @Slf4j
  54. public class DefaultGalleryViewController extends DefaultNodeContainerViewController implements GalleryViewController
  55.   {
  56.     /*******************************************************************************************************************
  57.      *
  58.      * A {@link Finder} which returns virtual {@link SiteNode}s representing the multiple contents served by the
  59.      * {@link SiteNode} associated to this controller. This is typically used to create site maps.
  60.      *
  61.      ******************************************************************************************************************/
  62.     @RequiredArgsConstructor
  63.     private static class VirtualSiteNodeFinder extends HierarchicFinderSupport<SiteNode, VirtualSiteNodeFinder>
  64.       {
  65.         private static final long serialVersionUID = 1L;

  66.         @Nonnull
  67.         private final transient DefaultGalleryViewController controller;

  68.         public VirtualSiteNodeFinder (@Nonnull final VirtualSiteNodeFinder other, @Nonnull final Object override)
  69.           {
  70.             super(other, override);
  71.             final var source = getSource(VirtualSiteNodeFinder.class, other, override);
  72.             this.controller = source.controller;
  73.           }

  74.         @Override @Nonnull
  75.         protected List<SiteNode> computeResults()
  76.           {
  77.             final var siteNode = controller.siteNode;
  78.             final List<SiteNode> result = controller.itemMapById.values().stream()
  79.                     .map(gallery -> createVirtualNode(siteNode, gallery.getId().stringValue()))
  80.                     .collect(toList());
  81.             result.add(0, createVirtualNode(siteNode, "lightbox"));
  82.             return result;
  83.           }

  84.         @Nonnull
  85.         private static VirtualSiteNode createVirtualNode (@Nonnull final SiteNode siteNode, final String relativeUri)
  86.           {
  87.             return new VirtualSiteNode(siteNode,
  88.                                        siteNode.getRelativeUri().appendedWith(relativeUri),
  89.                                        siteNode.getProperties());
  90.           }
  91.       }

  92.     @Nonnull
  93.     private final SiteNode siteNode;

  94.     @Nonnull
  95.     private final BeanFactory beanFactory;

  96.     @Nonnull
  97.     protected GalleryAdapter galleryAdapter;

  98.     protected final List<GalleryItem> items = new ArrayList<>();

  99.     protected final Map<Id, GalleryItem> itemMapById = new HashMap<>();

  100.     /*******************************************************************************************************************
  101.      *
  102.      *
  103.      ******************************************************************************************************************/
  104.     public DefaultGalleryViewController (@Nonnull final NodeContainerView view,
  105.                                          @Nonnull final SiteNode siteNode,
  106.                                          @Nonnull final RequestLocaleManager requestLocaleManager,
  107.                                          @Nonnull final BeanFactory beanFactory)
  108.       {
  109.         super(view, siteNode, requestLocaleManager);
  110.         this.siteNode = siteNode;
  111.         this.beanFactory = beanFactory;
  112.       }

  113.     /*******************************************************************************************************************
  114.      *
  115.      * {@inheritDoc}
  116.      *
  117.      ******************************************************************************************************************/
  118.     @Override
  119.     public void initialize()
  120.       throws Exception
  121.       {
  122.         super.initialize();
  123.         log.info("initialize() - {}", siteNode.getRelativeUri());
  124.         final var time = System.currentTimeMillis();
  125.         final GalleryLoader loader = new SlideShowProPlayerGalleryLoader(beanFactory, siteNode.getProperties()); // FIXME: make it configurable
  126.         items.addAll(loader.loadGallery(siteNode));
  127.         itemMapById.putAll(items.stream().collect(toMap(GalleryItem::getId, i -> i)));
  128.         log.info(">>>> {} gallery items loaded in {} msec", items.size(), System.currentTimeMillis() - time);
  129.       }

  130.     /*******************************************************************************************************************
  131.      *
  132.      * {@inheritDoc}
  133.      *
  134.      ******************************************************************************************************************/
  135.     @Override @Nonnull
  136.     public Finder<SiteNode> findVirtualSiteNodes()
  137.       {
  138.         return new VirtualSiteNodeFinder(this);
  139.       }
  140.   }