ResourceFinder.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * NorthernWind - lightweight CMS
  6.  * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
  7.  * %%
  8.  * Copyright (C) 2013 - 2021 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.model.impl.admin;

  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import it.tidalwave.text.AsDisplayableComparator;
  33. import it.tidalwave.util.spi.SimpleFinderSupport;
  34. import it.tidalwave.northernwind.core.model.Resource;
  35. import it.tidalwave.northernwind.core.model.ResourceFile;
  36. import lombok.RequiredArgsConstructor;

  37. /***********************************************************************************************************************
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **********************************************************************************************************************/
  42. @RequiredArgsConstructor
  43. public final class ResourceFinder<T extends Resource> extends SimpleFinderSupport<T>
  44.   {
  45.     @FunctionalInterface
  46.     static interface ProductCreator<T>
  47.       {
  48.         @Nonnull
  49.         public T createProduct (final @Nonnull ResourceFile folder);
  50.       }

  51.     @Nonnull
  52.     private final ResourceFile resourceFile;

  53.     @Nonnull
  54.     private final ProductCreator<T> productCreator;

  55.     public ResourceFinder (final @Nonnull ResourceFinder<T> other, final @Nonnull Object override)
  56.       {
  57.         super(other, override);
  58.         final ResourceFinder<T> source = getSource(ResourceFinder.class, other, override);
  59.         this.resourceFile = source.resourceFile;
  60.         this.productCreator = source.productCreator;
  61.       }

  62.     @Override @Nonnull
  63.     protected List<? extends T> computeResults()
  64.       {
  65.         // FIXME: it's not flyweight
  66.         final List<T> results = new ArrayList<>();

  67.         for (final ResourceFile childFile : resourceFile.findChildren().results())
  68.           {
  69.             if (childFile.isFolder())
  70.               {
  71.                 results.add(productCreator.createProduct(childFile));
  72.               }
  73.           }

  74.         // TODO: sorting should rather be done in the PresentationModel.
  75.         Collections.sort(results, AsDisplayableComparator.getInstance());

  76.         return results;
  77.       }
  78.   }