]> git.mxchange.org Git - jfinancials-war.git/blob
a0a00b1dc9058d6672c1185b990199bb91a30c35
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017, 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.financial.model.receipt.user;
18
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;
33
34 /**
35  * A view-scoped bean for receipt lists
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @Named ("userReceiptController")
40 @SessionScoped
41 public class FinancialsReceiptUserWebSessionBean extends BaseFinancialsBean implements FinancialsReceiptUserWebSessionController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 34_869_872_672_654L;
47
48         /**
49          * Receipt list controller
50          */
51         @Inject
52         private FinancialsReceiptListWebViewController receiptListController;
53
54         /**
55          * User instance
56          */
57         @Inject
58         private FinancialsUserLoginWebSessionController userLoginController;
59
60         /**
61          * User's receipts
62          */
63         private final List<BillableReceipt> userReceipts;
64
65         /**
66          * Default constructor
67          */
68         public FinancialsReceiptUserWebSessionBean () {
69                 // Call super constructor
70                 super();
71
72                 // Init lists
73                 this.userReceipts = new LinkedList<>();
74         }
75
76         /**
77          * Observes events being fired when a new receipt has been added
78          * <p>
79          * @param event Event being fired
80          */
81         public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
82                 // Validate parameter
83                 if (null == event) {
84                         // Throw NPE
85                         throw new NullPointerException("event is null"); //NOI18N
86                 } else if (event.getReceipt() == null) {
87                         // Throw NPE again
88                         throw new NullPointerException("event.receipt is null"); //NOI18N
89                 } else if (event.getReceipt().getReceiptId() == null) {
90                         // Throw it again
91                         throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
92                 } else if (event.getReceipt().getReceiptId() < 1) {
93                         // Throw IAE
94                         throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
95                 }
96
97                 // Is same user?
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());
101                 }
102         }
103
104         /**
105          * Event observer for logged-in user
106          * <p>
107          * @param event Event instance
108          */
109         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
110                 // event should not be null
111                 if (null == event) {
112                         // Throw NPE
113                         throw new NullPointerException("event is null"); //NOI18N
114                 } else if (event.getLoggedInUser() == null) {
115                         // Throw NPE again
116                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
117                 } else if (event.getLoggedInUser().getUserId() == null) {
118                         // userId is null
119                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
120                 } else if (event.getLoggedInUser().getUserId() < 1) {
121                         // Not avalid id
122                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
123                 }
124
125                 // Fill cache with all found user's receipts
126                 for (final BillableReceipt receipt : this.receiptListController.getAllReceipts()) {
127                         // Is same user?
128                         if (Objects.equals(receipt.getReceiptUser(), event.getLoggedInUser())) {
129                                 // Yes, then add it
130                                 this.getUserReceipts().add(receipt);
131                         }
132                 }
133         }
134
135         /**
136          * Getter for user's receipts
137          * <p>
138          * @return User's receipts
139          */
140         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
141         public List<BillableReceipt> getUserReceipts () {
142                 return this.userReceipts;
143         }
144
145 }