]> git.mxchange.org Git - jfinancials-war.git/blob
e00d01157c68c45c2ae00713ba64e9de516c73e3
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016 - 2020 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_item.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.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.model.receipt_item.BillableReceiptItem;
31 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
32 import org.mxchange.jfinancials.events.receipt_item.added.ObservableAdminReceiptItemAddedEvent;
33
34 /**
35  * A list financial receipt item bean (controller)
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Named ("receiptItemUserController")
40 @ViewScoped
41 public class FinancialsReceiptItemUserWebSessionBean extends BaseFinancialsBean implements FinancialsReceiptItemUserWebSessionController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 56_189_028_928_572L;
47
48         /**
49          * Receipt item list controller
50          */
51         @Inject
52         private FinancialsReceiptItemListWebViewController receiptItemListController;
53
54         /**
55          * User instance
56          */
57         @Inject
58         private FinancialsUserLoginWebSessionController userLoginController;
59
60         /**
61          * User's receipt items
62          */
63         private final List<BillableReceiptItem> userReceiptItems;
64
65         /**
66          * Constructor
67          */
68         @SuppressWarnings ("CollectionWithoutInitialCapacity")
69         public FinancialsReceiptItemUserWebSessionBean () {
70                 // Call super constructor
71                 super();
72
73                 // Init lists
74                 this.userReceiptItems = new LinkedList<>();
75         }
76
77         /**
78          * Observes events being fired when a new receipt item has been added
79          * <p>
80          * @param event Event being fired
81          */
82         public void afterAddedReceiptItemEvent (@Observes final ObservableAdminReceiptItemAddedEvent event) {
83                 // Validate parameter
84                 if (null == event) {
85                         // Throw NPE
86                         throw new NullPointerException("event is null"); //NOI18N
87                 }
88
89                 // Is same user?
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());
93                 }
94         }
95
96         /**
97          * Event observer for logged-in user
98          * <p>
99          * @param event Event instance
100          */
101         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
102                 // Event and contained entity instance should not be null
103                 if (null == event) {
104                         // Throw NPE
105                         throw new NullPointerException("event is null"); //NOI18N
106                 } else if (event.getLoggedInUser() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
109                 } else if (event.getLoggedInUser().getUserId() == null) {
110                         // userId is null
111                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
112                 } else if (event.getLoggedInUser().getUserId() < 1) {
113                         // Not avalid id
114                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
115                 }
116
117                 // Fill cache with all found user's receipts
118                 for (final BillableReceiptItem receiptItem : this.receiptItemListController.getAllReceiptItems()) {
119                         // Is same user?
120                         if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
121                                 // Yes, then add it
122                                 this.userReceiptItems.add(receiptItem);
123                         }
124                 }
125         }
126
127 }