]> git.mxchange.org Git - jfinancials-war.git/blob
0d935b70450055b591b9714fe5790675e0138c5c
[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.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
28 import javax.ejb.EJB;
29 import javax.enterprise.event.Observes;
30 import javax.faces.view.ViewScoped;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
34 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
35 import org.mxchange.jfinancials.events.receipt_item.added.ObservableAdminReceiptItemAddedEvent;
36 import org.mxchange.jfinancials.events.receipt_item.updated.ObservableAdminReceiptItemUpdatedEvent;
37 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemNotFoundException;
38 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
39 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote;
40 import org.mxchange.jfinancials.model.receipt_item.ReceiptItems;
41 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
42
43 /**
44  * A list financial receipt item bean (controller)
45  * <p>
46  * @author Roland Häder<roland@mxchange.org>
47  */
48 @Named ("receiptItemListController")
49 @ViewScoped
50 public class FinancialsReceiptItemListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptItemListWebViewController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 56_189_028_928_472L;
56
57         /**
58          * All receipt items list
59          */
60         private final List<BillableReceiptItem> allReceiptItems;
61
62         /**
63          * Filtered receipt items list
64          */
65         private List<BillableReceiptItem> filteredReceiptItems;
66
67         /**
68          * EJB for general financial receipt purposes
69          */
70         @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote")
71         private FinancialReceiptItemSessionBeanRemote receiptItemBean;
72
73         /**
74          * A list of all branch offices (globally)
75          */
76         @Inject
77         @NamedCache (cacheName = "receiptItemCache")
78         private transient Cache<Long, BillableReceiptItem> receiptItemCache;
79
80         /**
81          * Selected receipt item
82          */
83         private BillableReceiptItem selectedReceiptItem;
84
85         /**
86          * User instance
87          */
88         @Inject
89         private FinancialsUserLoginWebSessionController userLoginController;
90
91         /**
92          * User's receipt items
93          */
94         private final List<BillableReceiptItem> userReceiptItems;
95
96         /**
97          * Constructor
98          */
99         @SuppressWarnings ("CollectionWithoutInitialCapacity")
100         public FinancialsReceiptItemListWebViewBean () {
101                 // Call super constructor
102                 super();
103
104                 // Init lists
105                 this.allReceiptItems = new LinkedList<>();
106                 this.userReceiptItems = new LinkedList<>();
107         }
108
109         /**
110          * Observes events being fired when an administrator has added a new receipt
111          * item.
112          * <p>
113          * @param event Event being fired
114          */
115         public void afterAdminAddedReceiptItemEvent (@Observes final ObservableAdminReceiptItemAddedEvent event) {
116                 // Validate parameter
117                 if (null == event) {
118                         // Throw NPE
119                         throw new NullPointerException("event is null"); //NOI18N
120                 } else if (event.getAddedReceiptItem() == null) {
121                         //Throw NPE again
122                         throw new NullPointerException("event.addedReceiptItem is null"); //NOI18N
123                 } else if (event.getAddedReceiptItem().getItemId() == null) {
124                         //Throw NPE again
125                         throw new NullPointerException("event.addedReceiptItem.itemId is null"); //NOI18N
126                 } else if (event.getAddedReceiptItem().getItemId() < 1) {
127                         //Throw IAE
128                         throw new IllegalArgumentException(MessageFormat.format("event.addedReceiptItem.itemId={0} is not valid", event.getAddedReceiptItem().getItemId())); //NOI18N
129                 }
130
131                 // Add to cache and general list
132                 this.receiptItemCache.put(event.getAddedReceiptItem().getItemId(), event.getAddedReceiptItem());
133                 this.uniqueAddReceiptItem(event.getAddedReceiptItem());
134         }
135
136         /**
137          * Observes events being fired when an administrator has updated a receipt
138          * item instance.
139          * <p>
140          * @param event Event being fired
141          */
142         public void afterAdminUpdatedReceiptItemEvent (final @Observes ObservableAdminReceiptItemUpdatedEvent event) {
143                 // Validate parameter
144                 if (null == event) {
145                         // Throw NPE
146                         throw new NullPointerException("event is null"); //NOI18N
147                 } else if (event.getUpdatedReceiptItem() == null) {
148                         //Throw NPE again
149                         throw new NullPointerException("event.updatedReceiptItem is null"); //NOI18N
150                 } else if (event.getUpdatedReceiptItem().getItemId() == null) {
151                         //Throw NPE again
152                         throw new NullPointerException("event.updatedReceiptItem.itemId is null"); //NOI18N
153                 } else if (event.getUpdatedReceiptItem().getItemId() < 1) {
154                         //Throw IAE
155                         throw new IllegalArgumentException(MessageFormat.format("event.updatedReceiptItem.itemId={0} is not valid", event.getUpdatedReceiptItem().getItemId())); //NOI18N
156                 }
157
158                 // Add to cache and general list
159                 this.receiptItemCache.put(event.getUpdatedReceiptItem().getItemId(), event.getUpdatedReceiptItem());
160                 this.uniqueAddReceiptItem(event.getUpdatedReceiptItem());
161         }
162
163         /**
164          * Event observer for logged-in user
165          * <p>
166          * @param event Event instance
167          */
168         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
169                 // Event and contained entity instance should not be null
170                 if (null == event) {
171                         // Throw NPE
172                         throw new NullPointerException("event is null"); //NOI18N
173                 } else if (event.getLoggedInUser() == null) {
174                         // Throw NPE again
175                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
176                 } else if (event.getLoggedInUser().getUserId() == null) {
177                         // userId is null
178                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
179                 } else if (event.getLoggedInUser().getUserId() < 1) {
180                         // Not avalid id
181                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
182                 }
183
184                 // Fill cache with all found user's receipts
185                 for (final BillableReceiptItem receiptItem : this.allReceiptItems) {
186                         // Is same user?
187                         if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
188                                 // Yes, then add it
189                                 this.userReceiptItems.add(receiptItem);
190                         }
191                 }
192         }
193
194         @Override
195         public BillableReceiptItem findReceiptItemById (final Long receiptItemId) throws ReceiptItemNotFoundException {
196                 // Validate parameter
197                 if (null == receiptItemId) {
198                         // Throw NPE
199                         throw new NullPointerException("receiptItemId is null");
200                 } else if (receiptItemId < 1) {
201                         // Throw IAE
202                         throw new IllegalArgumentException(MessageFormat.format("receiptItemId={0} is not valid.", receiptItemId));
203                 }
204
205                 // Init instance
206                 BillableReceiptItem receiptItem = null;
207
208                 // Iterate over whole list
209                 for (final BillableReceiptItem currentReceiptItem : this.getAllReceiptItems()) {
210                         // Is the primary key matching?
211                         if (Objects.equals(currentReceiptItem.getItemId(), receiptItemId)) {
212                                 // Yes, then remember and exit iteration
213                                 receiptItem = currentReceiptItem;
214                                 break;
215                         }
216                 }
217
218                 // Is found?
219                 if (null == receiptItem) {
220                         // Not found
221                         throw new ReceiptItemNotFoundException(receiptItemId);
222                 }
223
224                 // Return it
225                 return receiptItem;
226         }
227
228         @Override
229         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
230         public List<BillableReceiptItem> getAllReceiptItems () {
231                 return this.allReceiptItems;
232         }
233
234         /**
235          * Getter for filtered receipt items
236          * <p>
237          * @return Filtered receipts
238          */
239         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
240         public List<BillableReceiptItem> getFilteredReceiptItems () {
241                 return this.filteredReceiptItems;
242         }
243
244         /**
245          * Setter for filtered receipt items
246          * <p>
247          * @param filteredReceiptItems Filtered receipt items
248          */
249         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
250         public void setFilteredReceiptItems (final List<BillableReceiptItem> filteredReceiptItems) {
251                 this.filteredReceiptItems = filteredReceiptItems;
252         }
253
254         /**
255          * Getter for selected receipt item
256          * <p>
257          * @return Selected receipt item
258          */
259         public BillableReceiptItem getSelectedReceiptItem () {
260                 return this.selectedReceiptItem;
261         }
262
263         /**
264          * Setter for selected receipt item
265          * <p>
266          * @param selectedReceiptItem Selected receipt item
267          */
268         public void setSelectedReceiptItem (final BillableReceiptItem selectedReceiptItem) {
269                 this.selectedReceiptItem = selectedReceiptItem;
270         }
271
272         @PostConstruct
273         public void initCache () {
274                 // Is cache there?
275                 if (!this.receiptItemCache.iterator().hasNext()) {
276                         // Add all
277                         for (final BillableReceiptItem receiptItem : this.receiptItemBean.fetchAllReceiptItems()) {
278                                 // Add it to cache
279                                 this.receiptItemCache.put(receiptItem.getItemId(), receiptItem);
280                         }
281                 }
282
283                 // Is the list empty, but filled cache?
284                 if (this.getAllReceiptItems().isEmpty() && this.receiptItemCache.iterator().hasNext()) {
285                         // Build up list
286                         for (final Cache.Entry<Long, BillableReceiptItem> currentEntry : this.receiptItemCache) {
287                                 // Add to list
288                                 this.getAllReceiptItems().add(currentEntry.getValue());
289                         }
290
291                         // Sort list
292                         this.getAllReceiptItems().sort(new Comparator<BillableReceiptItem>() {
293                                 @Override
294                                 public int compare (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) {
295                                         return receiptItem1.getItemId() > receiptItem2.getItemId() ? 1 : receiptItem1.getItemId() < receiptItem2.getItemId() ? -1 : 0;
296                                 }
297                         }
298                         );
299                 }
300         }
301
302         @Override
303         public boolean isReceiptItemAdded (final BillableReceiptItem receiptItem) {
304                 // Does it contain the same object?
305                 if (this.getAllReceiptItems().contains(receiptItem)) {
306                         // Yes, skip below code
307                         return true;
308                 }
309
310                 // Default is not the same
311                 boolean alreadyAdded = false;
312
313                 // Loop over all
314                 for (final BillableReceiptItem item : this.getAllReceiptItems()) {
315                         // Is the same?
316                         if (ReceiptItems.isSameReceiptItem(item, receiptItem)) {
317                                 // Yes, found it
318                                 alreadyAdded = true;
319                                 break;
320                         }
321                 }
322
323                 // Return flag
324                 return alreadyAdded;
325         }
326
327         /**
328          * Uniquely add a receipt item instance.
329          * <p>
330          * @param receiptItem To be added/updated receipt item instance
331          */
332         private void uniqueAddReceiptItem (final BillableReceiptItem receiptItem) {
333                 // Get iterator
334                 final Iterator<BillableReceiptItem> allIterator = this.getAllReceiptItems().iterator();
335
336                 // Iterate over all entries
337                 while (allIterator.hasNext()) {
338                         // Get current item
339                         final BillableReceiptItem currentReceiptItem = allIterator.next();
340
341                         // It the id matching?
342                         if (Objects.equals(receiptItem.getItemId(), currentReceiptItem.getItemId())) {
343                                 // Yes, then remove it and stop iterating
344                                 allIterator.remove();
345                                 break;
346                         }
347                 }
348
349                 // Re-add it
350                 this.getAllReceiptItems().add(receiptItem);
351
352                 // Is same user?
353                 if (this.userLoginController.isUserLoggedIn() && Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
354                         // Get iterator
355                         final Iterator<BillableReceiptItem> userIterator = this.userReceiptItems.iterator();
356
357                         // Iterate over all entries
358                         while (userIterator.hasNext()) {
359                                 // Get current item
360                                 final BillableReceiptItem currentReceiptItem = userIterator.next();
361
362                                 // It the id matching?
363                                 if (Objects.equals(receiptItem.getItemId(), currentReceiptItem.getItemId())) {
364                                         // Yes, then remove it and stop iterating
365                                         userIterator.remove();
366                                         break;
367                                 }
368                         }
369
370                         // Re-add it
371                         this.userReceiptItems.add(receiptItem);
372                 }
373         }
374 }