DefaultAccountingController.java

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

  27. import jakarta.annotation.Nonnull;
  28. import java.io.IOException;
  29. import org.springframework.stereotype.Component;
  30. import it.tidalwave.accounting.commons.AccountingOpenRequest;
  31. import it.tidalwave.accounting.commons.AccountingOpenedEvent;
  32. import it.tidalwave.accounting.model.Accounting;
  33. import it.tidalwave.ui.core.message.PowerOnEvent;
  34. import it.tidalwave.util.annotation.VisibleForTesting;
  35. import it.tidalwave.dci.annotation.DciContext;
  36. import it.tidalwave.messagebus.MessageBus;
  37. import it.tidalwave.messagebus.annotation.ListensTo;
  38. import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
  39. import lombok.RequiredArgsConstructor;
  40. import lombok.extern.slf4j.Slf4j;
  41. import static it.tidalwave.accounting.role.Loadable._Loadable_;

  42. /***************************************************************************************************************************************************************
  43.  *
  44.  * @stereotype Controller
  45.  *
  46.  * This business controller manages the life cycle of the {@link Accounting} instance, loading it during the
  47.  * initialization.
  48.  *
  49.  * @author  Fabrizio Giudici
  50.  *
  51.  **************************************************************************************************************************************************************/
  52. @Component @RequiredArgsConstructor @DciContext @SimpleMessageSubscriber @Slf4j
  53. public class DefaultAccountingController
  54.   {
  55.     @Nonnull
  56.     private final MessageBus messageBus;

  57.     // Don't use @Nonnull or Lombok will try to initialize it in the constructor
  58.     private Accounting accounting;

  59.     /***********************************************************************************************************************************************************
  60.      * Loads the {@link Accounting} at initialization.
  61.      **********************************************************************************************************************************************************/
  62.     @VisibleForTesting void onInitialize (@ListensTo final PowerOnEvent event)
  63.       {
  64.         try
  65.           {
  66.             log.info("onInitialize({})", event);
  67.             accounting = Accounting.createNew().as(_Loadable_).load();
  68.             messageBus.publish(new AccountingOpenedEvent(accounting));
  69.           }
  70.         catch (IOException e)
  71.           {
  72.             throw new RuntimeException(e);
  73.           }
  74.       }
  75.    
  76.     /***********************************************************************************************************************************************************
  77.      * Reply with a message carrying a reference to the {@link Accounting} instance.
  78.      **********************************************************************************************************************************************************/
  79.     @VisibleForTesting void onAccountingOpenRequest (@Nonnull @ListensTo final AccountingOpenRequest request)
  80.       {
  81.         // already done at this point, just send a response
  82.         messageBus.publish(new AccountingOpenedEvent(accounting));
  83.       }
  84.   }