SimpleCompositePresentable.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.role.ui.spi;

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

  46. /***************************************************************************************************************************************************************
  47.  *
  48.  * An implementation of {@link Presentable} for datum instances having the {@link SimpleComposite} role.
  49.  *
  50.  * @stereotype Role
  51.  *
  52.  * @author  Fabrizio Giudici
  53.  *
  54.  **************************************************************************************************************************************************************/
  55. @Slf4j
  56. public class SimpleCompositePresentable implements Presentable
  57.   {
  58.     private static final As.Type<SimpleComposite<As>> _SimpleCompositeOfAs_ = As.type(_SimpleComposite_);

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

  63.         @Nonnull
  64.         private final SimpleCompositePresentable scp;

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

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

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

  93.     private static final long serialVersionUID = 324646965695684L;

  94.     @Nonnull
  95.     private final As datum;

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

  99.     private final ContextSnapshot contextSnapshot;

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

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

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

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

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

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

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

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

  149.     /***********************************************************************************************************************************************************
  150.      *
  151.      **********************************************************************************************************************************************************/
  152.     @Nonnull
  153.     private List<Object> resolveRoles (@Nonnull final As datum, @Nonnull final Collection<Object> roles)
  154.       {
  155.         final List<Object> r = new ArrayList<>();

  156.         for (final var roleOrFactory : roles)
  157.           {
  158.             if (roleOrFactory instanceof RoleFactory)
  159.               {
  160.                 r.add(((RoleFactory<As>)roleOrFactory).createRoleFor(datum));
  161.               }
  162.             else
  163.               {
  164.                 r.add(roleOrFactory);
  165.               }
  166.           }

  167.         return r;
  168.       }
  169.   }