]> git.mxchange.org Git - jfinancials-war.git/blob
03b360a2729ca6a7d5805784a1f6d07d3bd36096
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 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.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.events.receipt.added.ObservableReceiptAddedEvent;
34 import org.mxchange.jfinancials.events.receipt.updated.ObservableReceiptUpdatedEvent;
35 import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException;
36 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
37 import org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote;
38 import org.mxchange.jfinancials.model.receipt.Receipts;
39
40 /**
41  * A view-scoped bean for receipt lists
42  * <p>
43  * @author Roland Haeder<roland@mxchange.org>
44  */
45 @Named ("receiptListController")
46 @ViewScoped
47 public class FinancialsReceiptListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptListWebViewController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 34_869_872_672_653L;
53
54         /**
55          * All receipts list
56          */
57         private final List<BillableReceipt> allReceipts;
58
59         /**
60          * Filtered receipts list
61          */
62         private List<BillableReceipt> filteredReceipts;
63
64         /**
65          * EJB for general financial receipt purposes
66          */
67         @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
68         private FinancialReceiptSessionBeanRemote receiptBean;
69
70         /**
71          * A list of all branch offices (globally)
72          */
73         @Inject
74         @NamedCache (cacheName = "receiptCache")
75         private transient Cache<Long, BillableReceipt> receiptCache;
76
77         /**
78          * Selected receipt
79          */
80         private BillableReceipt selectedReceipt;
81
82         /**
83          * Default constructor
84          */
85         public FinancialsReceiptListWebViewBean () {
86                 // Call super constructor
87                 super();
88
89                 // Init lists
90                 this.allReceipts = new LinkedList<>();
91         }
92
93         /**
94          * Observes events being fired when a new receipt has been added
95          * <p>
96          * @param event Event being fired
97          */
98         public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
99                 // Validate parameter
100                 if (null == event) {
101                         // Throw NPE
102                         throw new NullPointerException("event is null"); //NOI18N
103                 } else if (event.getReceipt() == null) {
104                         // Throw NPE again
105                         throw new NullPointerException("event.receipt is null"); //NOI18N
106                 } else if (event.getReceipt().getReceiptId() == null) {
107                         // Throw it again
108                         throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
109                 } else if (event.getReceipt().getReceiptId() < 1) {
110                         // Throw IAE
111                         throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
112                 }
113
114                 // Add to cache and general list
115                 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
116                 this.getAllReceipts().add(event.getReceipt());
117         }
118
119         /**
120          * Observes events being fired when a receipt has been updated
121          * <p>
122          * @param event Event being fired
123          */
124         public void afterUpdatedReceiptEvent (@Observes final ObservableReceiptUpdatedEvent event) {
125                 // Validate parameter
126                 if (null == event) {
127                         // Throw NPE
128                         throw new NullPointerException("event is null"); //NOI18N
129                 } else if (event.getReceipt() == null) {
130                         // Throw NPE again
131                         throw new NullPointerException("event.receipt is null"); //NOI18N
132                 } else if (event.getReceipt().getReceiptId() == null) {
133                         // Throw it again
134                         throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
135                 } else if (event.getReceipt().getReceiptId() < 1) {
136                         // Throw IAE
137                         throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
138                 } else if (this.getAllReceipts().isEmpty()) {
139                         // Receipt list is empty
140                         throw new IllegalStateException("this.allReceipts cannot be empty."); //NOI18N
141                 }
142
143                 // Add to cache and general list
144                 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
145
146                 // Default is not found
147                 boolean isFound = false;
148
149                 // Update instance
150                 for (final BillableReceipt receipt : this.getAllReceipts()) {
151                         // Is the same id?
152                         if (Objects.equals(receipt.getReceiptId(), event.getReceipt().getReceiptId())) {
153                                 // Yes, same primary key found, then copy all elements
154                                 Receipts.copyAll(event.getReceipt(), receipt);
155
156                                 // Mark as found, break loop
157                                 isFound = true;
158                                 break;
159                         }
160                 }
161
162                 // Has not been found?
163                 if (!isFound) {
164                         // Receipt list does not contain receipt instance
165                         throw new IllegalStateException("this.allReceipts does not contain the receipt."); //NOI18N
166                 }
167         }
168
169         @Override
170         public BillableReceipt findReceiptById (final Long receiptId) throws ReceiptNotFoundException {
171                 // Validate parameter
172                 if (null == receiptId) {
173                         // Throw NPE
174                         throw new NullPointerException("receiptId is null"); //NOI18N
175                 } else if (receiptId < 1) {
176                         // Throw IAE
177                         throw new IllegalArgumentException(MessageFormat.format("receiptId={0} is invalid.", receiptId)); //NOI18N
178                 } else if (!this.receiptCache.containsKey(receiptId)) {
179                         // Not found
180                         throw new ReceiptNotFoundException(receiptId);
181                 }
182
183                 // Get it from cache
184                 final BillableReceipt receipt = this.receiptCache.get(receiptId);
185
186                 // Return it
187                 return receipt;
188         }
189
190         @Override
191         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
192         public List<BillableReceipt> getAllReceipts () {
193                 return this.allReceipts;
194         }
195
196         /**
197          * Getter for filtered receipts
198          * <p>
199          * @return Filtered receipts
200          */
201         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
202         public List<BillableReceipt> getFilteredReceipts () {
203                 return this.filteredReceipts;
204         }
205
206         /**
207          * Setter for filtered receipts
208          * <p>
209          * @param filteredReceipts Filtered receipts
210          */
211         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
212         public void setFilteredReceipts (final List<BillableReceipt> filteredReceipts) {
213                 this.filteredReceipts = filteredReceipts;
214         }
215
216         /**
217          * Getter for selected receipt
218          * <P>
219          * @return Selected receipt
220          */
221         public BillableReceipt getSelectedReceipt () {
222                 return this.selectedReceipt;
223         }
224
225         /**
226          * Setter for selected receipt
227          * <P>
228          * @param selectedReceipt Selected receipt
229          */
230         public void setSelectedReceipt (final BillableReceipt selectedReceipt) {
231                 this.selectedReceipt = selectedReceipt;
232         }
233
234         @PostConstruct
235         public void initCache () {
236                 // Is cache there?
237                 if (!this.receiptCache.iterator().hasNext()) {
238                         // Add all
239                         for (final BillableReceipt receipt : this.receiptBean.fetchAllReceipts()) {
240                                 // Add it to cache
241                                 this.receiptCache.put(receipt.getReceiptId(), receipt);
242                         }
243                 }
244
245                 // Is the list empty, but filled cache?
246                 if (this.getAllReceipts().isEmpty() && this.receiptCache.iterator().hasNext()) {
247                         // Build up list
248                         for (final Cache.Entry<Long, BillableReceipt> currentEntry : this.receiptCache) {
249                                 // Add to list
250                                 this.getAllReceipts().add(currentEntry.getValue());
251                         }
252
253                         // Sort list
254                         this.getAllReceipts().sort(new Comparator<BillableReceipt>() {
255                                 @Override
256                                 public int compare (final BillableReceipt receipt1, final BillableReceipt receipt2) {
257                                         return receipt1.getReceiptId() > receipt2.getReceiptId() ? 1 : receipt1.getReceiptId() < receipt2.getReceiptId() ? -1 : 0;
258                                 }
259                         }
260                         );
261                 }
262         }
263
264         @Override
265         public boolean isReceiptAdded (final BillableReceipt receipt) {
266                 // Always trust the cache
267                 return this.getAllReceipts().contains(receipt);
268         }
269
270 }