SimpleCompositePresentable.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * 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
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/steelblue-src
  22.  * git clone https://github.com/tidalwave-it/steelblue-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.ui.core.role.spi;

  27. import javax.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import it.tidalwave.util.As;
  31. import it.tidalwave.util.RoleFactory;
  32. import it.tidalwave.util.Task;
  33. import it.tidalwave.util.spi.ContextSnapshot;
  34. import it.tidalwave.util.spi.SimpleFinderSupport;
  35. import it.tidalwave.role.SimpleComposite;
  36. import it.tidalwave.ui.core.role.Presentable;
  37. import it.tidalwave.ui.core.role.PresentationModel;
  38. import it.tidalwave.ui.core.role.PresentationModelFactory;
  39. import lombok.RequiredArgsConstructor;
  40. import lombok.extern.slf4j.Slf4j;
  41. import static java.util.Collections.emptyList;
  42. import static java.util.stream.Collectors.*;
  43. import static it.tidalwave.util.ShortNames.*;
  44. import static it.tidalwave.role.SimpleComposite._SimpleComposite_;

  45. /***************************************************************************************************************************************************************
  46.  *
  47.  * An implementation of {@link Presentable} for datum instances having the {@link SimpleComposite} role.
  48.  *
  49.  * @stereotype Role
  50.  * @since   2.0-ALPHA-1
  51.  * @author  Fabrizio Giudici
  52.  *
  53.  **************************************************************************************************************************************************************/
  54. @Slf4j
  55. public class SimpleCompositePresentable implements Presentable
  56.   {
  57.     @RequiredArgsConstructor
  58.     static class SCPFinder extends SimpleFinderSupport<PresentationModel>
  59.       {
  60.         // private static final long serialVersionUID = -3235827383866946732L;

  61.         @Nonnull
  62.         private final SimpleCompositePresentable scp;

  63.         @Nonnull
  64.         private final Collection<Object> roles;

  65.         public SCPFinder (@Nonnull final SCPFinder other, @Nonnull final Object override)
  66.           {
  67.             super(other, override);
  68.             final var source = getSource(SCPFinder.class, other, override);
  69.             this.scp = source.scp;
  70.             this.roles = source.roles;
  71.           }

  72.         @Override @Nonnull
  73.         protected List<PresentationModel> computeResults()
  74.           {
  75.             return scp.contextSnapshot.runWithContexts(new Task<>()
  76.               {
  77.                 @Override @Nonnull
  78.                 public List<PresentationModel> run()
  79.                   {
  80.                     final List<As> children = scp.datum.maybeAs(_SimpleComposite_)
  81.                                                        .map(c -> c.findChildren().results()).orElse(emptyList());
  82.                     return children.stream()
  83.                                    .map(child -> child.maybeAs(_Presentable_)
  84.                                                       .orElseGet(() -> new SimpleCompositePresentable(child)))
  85.                                    .map(presentable -> presentable.createPresentationModel(roles))
  86.                                    .collect(toList());
  87.                   }
  88.               });
  89.           }
  90.       }

  91.     private static final long serialVersionUID = 324646965695684L;

  92.     @Nonnull
  93.     private final As datum;

  94.     // This is not @Injected to avoid a dependency on Spring AOP
  95.     @Nonnull
  96.     private final PresentationModelFactory defaultPresentationModelFactory;

  97.     private final ContextSnapshot contextSnapshot;

  98.     /***********************************************************************************************************************************************************
  99.      * @param datum     the owner
  100.      **********************************************************************************************************************************************************/
  101.     public SimpleCompositePresentable (@Nonnull final As datum)
  102.       {
  103.         this(datum, new DefaultPresentationModelFactory());
  104.       }

  105.     /***********************************************************************************************************************************************************
  106.      * @param datum                             the owner
  107.      * @param defaultPresentationModelFactory   the {@code PresentationModelFactory}
  108.      **********************************************************************************************************************************************************/
  109.     public SimpleCompositePresentable (@Nonnull final As datum,
  110.                                        @Nonnull final PresentationModelFactory defaultPresentationModelFactory)
  111.       {
  112.         this.datum = datum;
  113.         this.defaultPresentationModelFactory = defaultPresentationModelFactory;
  114.         contextSnapshot = new ContextSnapshot(datum);
  115.       }

  116.     /***********************************************************************************************************************************************************
  117.      * {@inheritDoc}
  118.      **********************************************************************************************************************************************************/
  119.     @Override @Nonnull
  120.     public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
  121.       {
  122.         return internalCreatePresentationModel(datum, roles);
  123.       }

  124.     /***********************************************************************************************************************************************************
  125.      *
  126.      **********************************************************************************************************************************************************/
  127.     @Nonnull
  128.     private PresentationModel internalCreatePresentationModel (@Nonnull final As datum,
  129.                                                                @Nonnull final Collection<Object> roles)
  130.       {
  131.         final var pmFinder = new SCPFinder(this, roles);

  132.         return contextSnapshot.runWithContexts(new Task<>()
  133.           {
  134.             @Override @Nonnull
  135.             public PresentationModel run()
  136.               {
  137.                 final var r = RoleFactory.resolveFactories(datum, roles);

  138.                 if (datum.maybeAs(_SimpleComposite_).isPresent())
  139.                   {
  140.                     r.add(SimpleComposite.of(pmFinder));
  141.                   }

  142.                 log.trace(">>>> r for {}: {}", shortId(datum), shortIds(r));

  143.                 return defaultPresentationModelFactory.createPresentationModel(datum, r);
  144.               }
  145.           });
  146.       }
  147.   }