2 * Copyright (C) 2017 - 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.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.context.SessionScoped;
24 import javax.enterprise.event.Observes;
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.list.FinancialsReceiptListWebViewController;
29 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
30 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
31 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
32 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
35 * A view-scoped bean for receipt lists
37 * @author Roland Haeder<roland@mxchange.org>
39 @Named ("userReceiptController")
41 public class FinancialsReceiptUserWebSessionBean extends BaseFinancialsBean implements FinancialsReceiptUserWebSessionController {
46 private static final long serialVersionUID = 34_869_872_672_654L;
49 * Receipt list controller
52 private FinancialsReceiptListWebViewController receiptListController;
58 private FinancialsUserLoginWebSessionController userLoginController;
63 private final List<BillableReceipt> userReceipts;
68 public FinancialsReceiptUserWebSessionBean () {
69 // Call super constructor
73 this.userReceipts = new LinkedList<>();
77 * Observes events being fired when a new receipt has been added
79 * @param event Event being fired
81 public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
85 throw new NullPointerException("event is null"); //NOI18N
86 } else if (event.getReceipt() == null) {
88 throw new NullPointerException("event.receipt is null"); //NOI18N
89 } else if (event.getReceipt().getReceiptId() == null) {
91 throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
92 } else if (event.getReceipt().getReceiptId() < 1) {
94 throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
98 if (this.userLoginController.isUserLoggedIn() && Objects.equals(event.getReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
99 // Same user, then add it
100 this.getUserReceipts().add(event.getReceipt());
105 * Event observer for logged-in user
107 * @param event Event instance
109 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
110 // Event and contained entity instance should not be null
113 throw new NullPointerException("event is null"); //NOI18N
114 } else if (event.getLoggedInUser() == null) {
116 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
117 } else if (event.getLoggedInUser().getUserId() == null) {
119 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
120 } else if (event.getLoggedInUser().getUserId() < 1) {
122 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
125 // Fill cache with all found user's receipts
126 for (final BillableReceipt receipt : this.receiptListController.getAllReceipts()) {
128 if (Objects.equals(receipt.getReceiptUser(), event.getLoggedInUser())) {
130 this.getUserReceipts().add(receipt);
136 * Getter for user's receipts
138 * @return User's receipts
140 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
141 public List<BillableReceipt> getUserReceipts () {
142 return this.userReceipts;