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