View Javadoc
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.impl;
27  
28  import javax.annotation.Nonnull;
29  import java.io.IOException;
30  import org.springframework.stereotype.Component;
31  import it.tidalwave.accounting.commons.AccountingOpenRequest;
32  import it.tidalwave.accounting.commons.AccountingOpenedEvent;
33  import it.tidalwave.accounting.model.Accounting;
34  import it.tidalwave.message.PowerOnEvent;
35  import it.tidalwave.util.annotation.VisibleForTesting;
36  import it.tidalwave.dci.annotation.DciContext;
37  import it.tidalwave.messagebus.MessageBus;
38  import it.tidalwave.messagebus.annotation.ListensTo;
39  import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
40  import lombok.RequiredArgsConstructor;
41  import lombok.extern.slf4j.Slf4j;
42  import static it.tidalwave.accounting.role.Loadable._Loadable_;
43  
44  /***************************************************************************************************************************************************************
45   *
46   * @stereotype Controller
47   * 
48   * This business controller manages the life cycle of the {@link Accounting} instance, loading it during the 
49   * initialization.
50   * 
51   * @author  Fabrizio Giudici
52   *
53   **************************************************************************************************************************************************************/
54  @Component @RequiredArgsConstructor @DciContext @SimpleMessageSubscriber @Slf4j
55  public class DefaultAccountingController 
56    {
57      @Nonnull
58      private final MessageBus messageBus;
59  
60      // Don't use @Nonnull or Lombok will try to initialize it in the constructor
61      private Accounting accounting;
62  
63      /***********************************************************************************************************************************************************
64       * Loads the {@link Accounting} at initialization.
65       **********************************************************************************************************************************************************/
66      @VisibleForTesting void onInitialize (@ListensTo final PowerOnEvent event)
67        {
68          try
69            {
70              log.info("onInitialize({})", event);
71              accounting = Accounting.createNew().as(_Loadable_).load();
72              messageBus.publish(new AccountingOpenedEvent(accounting));
73            }
74          catch (IOException e)
75            {
76              throw new RuntimeException(e);
77            }
78        }
79      
80      /***********************************************************************************************************************************************************
81       * Reply with a message carrying a reference to the {@link Accounting} instance. 
82       **********************************************************************************************************************************************************/
83      @VisibleForTesting void onAccountingOpenRequest (@Nonnull @ListensTo final AccountingOpenRequest request)
84        {
85          // already done at this point, just send a response
86          messageBus.publish(new AccountingOpenedEvent(accounting));
87        }
88    }