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 javax.annotation.PostConstruct;
30 import java.io.IOException;
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.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 *
45 * @stereotype Controller
46 *
47 * This business controller manages the life cycle of the {@link Accounting} instance, loading it during the
48 * initialization.
49 *
50 * @author Fabrizio Giudici
51 *
52 **************************************************************************************************************************************************************/
53 @RequiredArgsConstructor @DciContext @SimpleMessageSubscriber @Slf4j
54 public class DefaultAccountingController
55 {
56 @Nonnull
57 private final MessageBus messageBus;
58
59 // Don't use @Nonnull or Lombok will try to initialize it in the constructor
60 private Accounting accounting;
61
62 /***********************************************************************************************************************************************************
63 * Loads the {@link Accounting} at initialization.
64 **********************************************************************************************************************************************************/
65 @PostConstruct
66 @VisibleForTesting void initialize()
67 {
68 try
69 {
70 log.info("initialize()");
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
84 void onAccountingOpenRequest (@Nonnull @ListensTo final AccountingOpenRequest request)
85 {
86 // already done at this point, just send a response
87 messageBus.publish(new AccountingOpenedEvent(accounting));
88 }
89 }