DefaultProjectExplorerPresentationControl.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * blueHour: open source accounting
  5.  * http://tidalwave.it/projects/bluehour
  6.  *
  7.  * Copyright (C) 2013 - 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/bluehour-src
  22.  * git clone https://github.com/tidalwave-it/bluehour-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.accounting.ui.projectexplorer.impl;

  27. import javax.annotation.Nonnull;
  28. import org.springframework.stereotype.Component;
  29. import it.tidalwave.accounting.commons.CustomerSelectedEvent;
  30. import it.tidalwave.accounting.commons.ProjectSelectedEvent;
  31. import it.tidalwave.accounting.model.Customer;
  32. import it.tidalwave.accounting.model.Project;
  33. import it.tidalwave.accounting.model.spi.ProjectSpi;
  34. import it.tidalwave.accounting.ui.projectexplorer.ProjectExplorerPresentation;
  35. import it.tidalwave.util.annotation.VisibleForTesting;
  36. import it.tidalwave.role.ui.PresentationModel;
  37. import it.tidalwave.role.ui.Selectable;
  38. import it.tidalwave.dci.annotation.DciContext;
  39. import it.tidalwave.messagebus.MessageBus;
  40. import it.tidalwave.messagebus.annotation.ListensTo;
  41. import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
  42. import lombok.RequiredArgsConstructor;
  43. import lombok.extern.slf4j.Slf4j;
  44. import static java.util.Comparator.*;
  45. import static it.tidalwave.role.ui.Presentable._Presentable_;
  46. import static it.tidalwave.role.ui.spi.PresentationModelCollectors.toCompositePresentationModel;

  47. /***************************************************************************************************************************************************************
  48.  *
  49.  * @author  Fabrizio Giudici
  50.  *
  51.  **************************************************************************************************************************************************************/
  52. @Component @RequiredArgsConstructor @DciContext @SimpleMessageSubscriber @Slf4j
  53. public class DefaultProjectExplorerPresentationControl
  54.   {
  55.     @Nonnull
  56.     private final MessageBus messageBus;

  57.     @Nonnull
  58.     private final ProjectExplorerPresentation presentation;

  59.     /***********************************************************************************************************************************************************
  60.      * Reacts to the notification that a {@link Customer} has been selected by populating the presentation with
  61.      * his projects.
  62.      *
  63.      * @param  event  the notification event
  64.      **********************************************************************************************************************************************************/
  65.     @VisibleForTesting void onCustomerSelectedEvent (@Nonnull @ListensTo final CustomerSelectedEvent event)
  66.       {
  67.         log.info("onCustomerSelectedEvent({})", event);
  68.         presentation.populate(event.getCustomer().findProjects()
  69.                                                  .stream()
  70.                                                  .map(project -> (ProjectSpi)project)
  71.                                                  .sorted(comparing(ProjectSpi::getName))
  72.                                                  .map(this::createPresentationModelFor)
  73.                                                  .collect(toCompositePresentationModel()));
  74.       }
  75.    
  76.     /***********************************************************************************************************************************************************
  77.      * Creates a {@link PresentationModel} for a {@link Project} injecting a {@link Selectable} role which fires a
  78.      * {@link ProjectSelectedEvent} on selection.
  79.      *
  80.      * @param  project      the {@code Project}
  81.      * @return              the {@code PresentationModel}
  82.      **********************************************************************************************************************************************************/
  83.     @Nonnull
  84.     @VisibleForTesting PresentationModel createPresentationModelFor (@Nonnull final Project project)
  85.       {
  86.         final Selectable publishEventOnSelection = () -> messageBus.publish(new ProjectSelectedEvent(project));
  87.         return project.as(_Presentable_).createPresentationModel(publishEventOnSelection);
  88.       }
  89.   }