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 jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import it.tidalwave.ui.core.role.Presentable;
  31. import it.tidalwave.ui.core.role.PresentationModel;
  32. import it.tidalwave.ui.core.role.PresentationModelFactory;
  33. import it.tidalwave.util.As;
  34. import it.tidalwave.util.RoleFactory;
  35. import it.tidalwave.util.Task;
  36. import it.tidalwave.util.spi.ContextSnapshot;
  37. import it.tidalwave.util.spi.SimpleFinderSupport;
  38. import it.tidalwave.role.SimpleComposite;
  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.     private static final As.Type<SimpleComposite<As>> _SimpleAsComposite_ = As.type(SimpleComposite.class);

  58.     @RequiredArgsConstructor
  59.     public static class SCPFinder extends SimpleFinderSupport<PresentationModel>
  60.       {
  61.         // private static final long serialVersionUID = -3235827383866946732L;

  62.         @Nonnull
  63.         private final SimpleCompositePresentable scp;

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

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

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

  90.     @Nonnull
  91.     private final As datum;

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

  95.     private final ContextSnapshot contextSnapshot;

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

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

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

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

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

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

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

  141.                 return defaultPresentationModelFactory.createPresentationModel(datum, r);
  142.               }
  143.           });
  144.       }
  145.   }