2 * Copyright (C) 2016 - 2022 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jfinancials.beans.financial.model.receipt_item.user;
19 import java.text.MessageFormat;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.enterprise.event.Observes;
24 import javax.faces.view.ViewScoped;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
28 import org.mxchange.jfinancials.beans.financial.model.receipt_item.list.FinancialsReceiptItemListWebViewController;
29 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
30 import org.mxchange.jfinancials.events.receipt_item.added.ObservableAdminReceiptItemAddedEvent;
31 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
32 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
35 * A list financial receipt item bean (controller)
37 * @author Roland Häder<roland@mxchange.org>
39 @Named ("receiptItemUserController")
41 public class FinancialsReceiptItemUserWebSessionBean extends BaseFinancialsBean implements FinancialsReceiptItemUserWebSessionController {
46 private static final long serialVersionUID = 56_189_028_928_572L;
49 * Receipt item list controller
52 private FinancialsReceiptItemListWebViewController receiptItemListController;
58 private FinancialsUserLoginWebSessionController userLoginController;
61 * User's receipt items
63 private final List<BillableReceiptItem> userReceiptItems;
68 @SuppressWarnings ("CollectionWithoutInitialCapacity")
69 public FinancialsReceiptItemUserWebSessionBean () {
70 // Call super constructor
74 this.userReceiptItems = new LinkedList<>();
78 * Observes events being fired when a new receipt item has been added
80 * @param event Event being fired
82 public void afterAddedReceiptItemEvent (@Observes final ObservableAdminReceiptItemAddedEvent event) {
86 throw new NullPointerException("event is null"); //NOI18N
90 if (this.userLoginController.isUserLoggedIn() && Objects.equals(event.getAddedReceiptItem().getItemReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
91 // Same user, then add it
92 this.userReceiptItems.add(event.getAddedReceiptItem());
97 * Event observer for logged-in user
99 * @param event Event instance
101 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
102 // Event and contained entity instance should not be null
105 throw new NullPointerException("event is null"); //NOI18N
106 } else if (event.getLoggedInUser() == null) {
108 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
109 } else if (event.getLoggedInUser().getUserId() == null) {
111 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
112 } else if (event.getLoggedInUser().getUserId() < 1) {
114 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
117 // Fill cache with all found user's receipts
118 for (final BillableReceiptItem receiptItem : this.receiptItemListController.getAllReceiptItems()) {
120 if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
122 this.userReceiptItems.add(receiptItem);