]> git.mxchange.org Git - jfinancials-war.git/blob
9da906a0b61ec4560fe2fe913e068f34f434b69e
[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.list;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
27 import javax.ejb.EJB;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.ViewScoped;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
33 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
34 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
35 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
36 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote;
37 import org.mxchange.jfinancials.model.receipt_item.ReceiptItems;
38 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
39
40 /**
41  * A list financial receipt item bean (controller)
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("receiptItemListController")
46 @ViewScoped
47 public class FinancialsReceiptItemListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptItemListWebViewController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 56_189_028_928_472L;
53
54         /**
55          * All receipt items list
56          */
57         private final List<BillableReceiptItem> allReceiptItems;
58
59         /**
60          * Filtered receipt items list
61          */
62         private List<BillableReceiptItem> filteredReceiptItems;
63
64         /**
65          * EJB for general financial receipt purposes
66          */
67         @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote")
68         private FinancialReceiptItemSessionBeanRemote receiptItemBean;
69
70         /**
71          * A list of all branch offices (globally)
72          */
73         @Inject
74         @NamedCache (cacheName = "receiptItemCache")
75         private transient Cache<Long, BillableReceiptItem> receiptItemCache;
76
77         /**
78          * Selected receipt item
79          */
80         private BillableReceiptItem selectedReceiptItem;
81
82         /**
83          * User instance
84          */
85         @Inject
86         private FinancialsUserLoginWebSessionController userLoginController;
87
88         /**
89          * User's receipt items
90          */
91         private final List<BillableReceiptItem> userReceiptItems;
92
93         /**
94          * Constructor
95          */
96         @SuppressWarnings ("CollectionWithoutInitialCapacity")
97         public FinancialsReceiptItemListWebViewBean () {
98                 // Call super constructor
99                 super();
100
101                 // Init lists
102                 this.allReceiptItems = new LinkedList<>();
103                 this.userReceiptItems = new LinkedList<>();
104         }
105
106         /**
107          * Observes events being fired when a new receipt item has been added
108          * <p>
109          * @param event Event being fired
110          */
111         public void afterAddedReceiptItemEvent (@Observes final ObservableReceiptItemAddedEvent event) {
112                 // Validate parameter
113                 if (null == event) {
114                         // Throw NPE
115                         throw new NullPointerException("event is null"); //NOI18N
116                 }
117
118                 // Add to cache and general list
119                 this.receiptItemCache.put(event.getReceiptItem().getItemId(), event.getReceiptItem());
120                 this.getAllReceiptItems().add(event.getReceiptItem());
121
122                 // Is same user?
123                 if (this.userLoginController.isUserLoggedIn() && Objects.equals(event.getReceiptItem().getItemReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
124                         // Same user, then add it
125                         this.userReceiptItems.add(event.getReceiptItem());
126                 }
127         }
128
129         /**
130          * Event observer for logged-in user
131          * <p>
132          * @param event Event instance
133          */
134         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
135                 // Event and contained entity instance should not be null
136                 if (null == event) {
137                         // Throw NPE
138                         throw new NullPointerException("event is null"); //NOI18N
139                 } else if (event.getLoggedInUser() == null) {
140                         // Throw NPE again
141                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
142                 } else if (event.getLoggedInUser().getUserId() == null) {
143                         // userId is null
144                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
145                 } else if (event.getLoggedInUser().getUserId() < 1) {
146                         // Not avalid id
147                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
148                 }
149
150                 // Fill cache with all found user's receipts
151                 for (final BillableReceiptItem receiptItem : this.allReceiptItems) {
152                         // Is same user?
153                         if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
154                                 // Yes, then add it
155                                 this.userReceiptItems.add(receiptItem);
156                         }
157                 }
158         }
159
160         @Override
161         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
162         public List<BillableReceiptItem> getAllReceiptItems () {
163                 return this.allReceiptItems;
164         }
165
166         /**
167          * Getter for filtered receipt items
168          * <p>
169          * @return Filtered receipts
170          */
171         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
172         public List<BillableReceiptItem> getFilteredReceiptItems () {
173                 return this.filteredReceiptItems;
174         }
175
176         /**
177          * Setter for filtered receipt items
178          * <p>
179          * @param filteredReceiptItems Filtered receipt items
180          */
181         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
182         public void setFilteredReceiptItems (final List<BillableReceiptItem> filteredReceiptItems) {
183                 this.filteredReceiptItems = filteredReceiptItems;
184         }
185
186         /**
187          * Getter for selected receipt item
188          * <p>
189          * @return Selected receipt item
190          */
191         public BillableReceiptItem getSelectedReceiptItem () {
192                 return this.selectedReceiptItem;
193         }
194
195         /**
196          * Setter for selected receipt item
197          * <p>
198          * @param selectedReceiptItem Selected receipt item
199          */
200         public void setSelectedReceiptItem (final BillableReceiptItem selectedReceiptItem) {
201                 this.selectedReceiptItem = selectedReceiptItem;
202         }
203
204         @PostConstruct
205         public void initCache () {
206                 // Is cache there?
207                 if (!this.receiptItemCache.iterator().hasNext()) {
208                         // Add all
209                         for (final BillableReceiptItem receiptItem : this.receiptItemBean.fetchAllReceiptItems()) {
210                                 // Add it to cache
211                                 this.receiptItemCache.put(receiptItem.getItemId(), receiptItem);
212                         }
213                 }
214
215                 // Is the list empty, but filled cache?
216                 if (this.getAllReceiptItems().isEmpty() && this.receiptItemCache.iterator().hasNext()) {
217                         // Build up list
218                         for (final Cache.Entry<Long, BillableReceiptItem> currentEntry : this.receiptItemCache) {
219                                 // Add to list
220                                 this.getAllReceiptItems().add(currentEntry.getValue());
221                         }
222
223                         // Sort list
224                         this.getAllReceiptItems().sort(new Comparator<BillableReceiptItem>() {
225                                 @Override
226                                 public int compare (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) {
227                                         return receiptItem1.getItemId() > receiptItem2.getItemId() ? 1 : receiptItem1.getItemId() < receiptItem2.getItemId() ? -1 : 0;
228                                 }
229                         }
230                         );
231                 }
232         }
233
234         @Override
235         public boolean isReceiptItemAdded (final BillableReceiptItem receiptItem) {
236                 // Does it contain the same object?
237                 if (this.getAllReceiptItems().contains(receiptItem)) {
238                         // Yes, skip below code
239                         return true;
240                 }
241
242                 // Default is not the same
243                 boolean alreadyAdded = false;
244
245                 // Loop over all
246                 for (final BillableReceiptItem item : this.getAllReceiptItems()) {
247                         // Is the same?
248                         if (ReceiptItems.isSameReceiptItem(item, receiptItem)) {
249                                 // Yes, found it
250                                 alreadyAdded = true;
251                                 break;
252                         }
253                 }
254
255                 // Return flag
256                 return alreadyAdded;
257         }
258
259 }