Skip to contentMethod: lambda$onAccountingOpenedEvent$0(Customer)
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.customerexplorer.impl;
27:
28: import javax.annotation.Nonnull;
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.commons.CustomerSelectedEvent;
33: import it.tidalwave.accounting.model.Accounting;
34: import it.tidalwave.accounting.model.Customer;
35: import it.tidalwave.accounting.model.spi.CustomerSpi;
36: import it.tidalwave.accounting.ui.customerexplorer.CustomerExplorerPresentation;
37: import it.tidalwave.message.PowerOnEvent;
38: import it.tidalwave.util.annotation.VisibleForTesting;
39: import it.tidalwave.role.ui.PresentationModel;
40: import it.tidalwave.role.ui.Selectable;
41: import it.tidalwave.dci.annotation.DciContext;
42: import it.tidalwave.messagebus.MessageBus;
43: import it.tidalwave.messagebus.annotation.ListensTo;
44: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
45: import lombok.RequiredArgsConstructor;
46: import lombok.extern.slf4j.Slf4j;
47: import static java.util.Comparator.*;
48: import static it.tidalwave.role.ui.Presentable._Presentable_;
49: import static it.tidalwave.role.ui.spi.PresentationModelCollectors.toCompositePresentationModel;
50:
51: /***************************************************************************************************************************************************************
52: *
53: * @author Fabrizio Giudici
54: *
55: **************************************************************************************************************************************************************/
56: @Component @RequiredArgsConstructor @DciContext @SimpleMessageSubscriber @Slf4j
57: public class DefaultCustomerExplorerPresentationControl
58: {
59: @Nonnull
60: private final MessageBus messageBus;
61:
62: @Nonnull
63: private final CustomerExplorerPresentation presentation;
64:
65: /***********************************************************************************************************************************************************
66: * Requests the opening of an {@link Accounting} during initialization.
67: **********************************************************************************************************************************************************/
68: @VisibleForTesting void onInitialize (@ListensTo final PowerOnEvent event)
69: {
70: log.info("onInitialize({})", event);
71: messageBus.publish(new AccountingOpenRequest());
72: }
73:
74: /***********************************************************************************************************************************************************
75: * Reacts to the notification that an {@link Accounting} has been opened by populating the presentation with
76: * the customers.
77: *
78: * @param event the notification event
79: **********************************************************************************************************************************************************/
80: @VisibleForTesting void onAccountingOpenedEvent (@Nonnull @ListensTo final AccountingOpenedEvent event)
81: {
82: log.info("onAccountingOpenedEvent({})", event);
83: presentation.populate(event.getAccounting().getCustomerRegistry().findCustomers().stream()
84: .map(customer -> (CustomerSpi)customer)
85: .sorted(comparing(CustomerSpi::getName))
86: .map(this::createPresentationModelFor)
87: .collect(toCompositePresentationModel()));
88: }
89:
90: /***********************************************************************************************************************************************************
91: * Creates a {@link PresentationModel} for a {@link Customer} injecting a {@link Selectable} role which fires a
92: * {@link CustomerSelectedEvent} on selection.
93: *
94: * @param customer the {@code Customer}
95: * @return the {@code PresentationModel}
96: **********************************************************************************************************************************************************/
97: @Nonnull
98: @VisibleForTesting PresentationModel createPresentationModelFor (@Nonnull final Customer customer)
99: {
100: final Selectable publishEventOnSelection = () -> messageBus.publish(new CustomerSelectedEvent(customer));
101: return customer.as(_Presentable_).createPresentationModel(publishEventOnSelection);
102: }
103: }