]> git.mxchange.org Git - jfinancials-war.git/blob
bef24c59eca52573863ab150c9b9f65eb5ac9036
[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 javax.annotation.PostConstruct;
25 import javax.cache.Cache;
26 import javax.ejb.EJB;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.ViewScoped;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
32 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
33 import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException;
34 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
35 import org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote;
36
37 /**
38  * A view-scoped bean for receipt lists
39  * <p>
40  * @author Roland Haeder<roland@mxchange.org>
41  */
42 @Named ("receiptListController")
43 @ViewScoped
44 public class FinancialsReceiptListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptListWebViewController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 34_869_872_672_653L;
50
51         /**
52          * All receipts list
53          */
54         private final List<BillableReceipt> allReceipts;
55
56         /**
57          * Filtered receipts list
58          */
59         private List<BillableReceipt> filteredReceipts;
60
61         /**
62          * EJB for general financial receipt purposes
63          */
64         @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
65         private FinancialReceiptSessionBeanRemote receiptBean;
66
67         /**
68          * A list of all branch offices (globally)
69          */
70         @Inject
71         @NamedCache (cacheName = "receiptCache")
72         private transient Cache<Long, BillableReceipt> receiptCache;
73
74         /**
75          * Selected receipt
76          */
77         private BillableReceipt selectedReceipt;
78
79         /**
80          * Default constructor
81          */
82         public FinancialsReceiptListWebViewBean () {
83                 // Call super constructor
84                 super();
85
86                 // Init lists
87                 this.allReceipts = new LinkedList<>();
88         }
89
90         /**
91          * Observes events being fired when a new receipt has been added
92          * <p>
93          * @param event Event being fired
94          */
95         public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
96                 // Validate parameter
97                 if (null == event) {
98                         // Throw NPE
99                         throw new NullPointerException("event is null"); //NOI18N
100                 } else if (event.getReceipt() == null) {
101                         // Throw NPE again
102                         throw new NullPointerException("event.receipt is null"); //NOI18N
103                 } else if (event.getReceipt().getReceiptId() == null) {
104                         // Throw it again
105                         throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
106                 } else if (event.getReceipt().getReceiptId() < 1) {
107                         // Throw IAE
108                         throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
109                 }
110
111                 // Add to cache and general list
112                 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
113                 this.getAllReceipts().add(event.getReceipt());
114         }
115
116         @Override
117         public BillableReceipt findReceiptById (final Long receiptId) throws ReceiptNotFoundException {
118                 // Validate parameter
119                 if (null == receiptId) {
120                         // Throw NPE
121                         throw new NullPointerException("receiptId is null"); //NOI18N
122                 } else if (receiptId < 1) {
123                         // Throw IAE
124                         throw new IllegalArgumentException("receiptId=" + receiptId + " is invalid."); //NOI18N
125                 } else if (!this.receiptCache.containsKey(receiptId)) {
126                         // Not found
127                         throw new ReceiptNotFoundException(receiptId);
128                 }
129
130                 // Get it from cache
131                 final BillableReceipt receipt = this.receiptCache.get(receiptId);
132
133                 // Return it
134                 return receipt;
135         }
136
137         @Override
138         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
139         public List<BillableReceipt> getAllReceipts () {
140                 return this.allReceipts;
141         }
142
143         /**
144          * Getter for filtered receipts
145          * <p>
146          * @return Filtered receipts
147          */
148         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149         public List<BillableReceipt> getFilteredReceipts () {
150                 return this.filteredReceipts;
151         }
152
153         /**
154          * Setter for filtered receipts
155          * <p>
156          * @param filteredReceipts Filtered receipts
157          */
158         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159         public void setFilteredReceipts (final List<BillableReceipt> filteredReceipts) {
160                 this.filteredReceipts = filteredReceipts;
161         }
162
163         /**
164          * Getter for selected receipt
165          * <P>
166          * @return Selected receipt
167          */
168         public BillableReceipt getSelectedReceipt () {
169                 return this.selectedReceipt;
170         }
171
172         /**
173          * Setter for selected receipt
174          * <P>
175          * @param selectedReceipt Selected receipt
176          */
177         public void setSelectedReceipt (final BillableReceipt selectedReceipt) {
178                 this.selectedReceipt = selectedReceipt;
179         }
180
181         @PostConstruct
182         public void initCache () {
183                 // Is cache there?
184                 if (!this.receiptCache.iterator().hasNext()) {
185                         // Get whole list from EJB
186                         final List<BillableReceipt> receipts = this.receiptBean.allReceipts();
187
188                         // Add all
189                         for (final BillableReceipt receipt : receipts) {
190                                 // Add it to cache
191                                 this.receiptCache.put(receipt.getReceiptId(), receipt);
192                         }
193                 }
194
195                 // Is the list empty, but filled cache?
196                 if (this.getAllReceipts().isEmpty() && this.receiptCache.iterator().hasNext()) {
197                         // Build up list
198                         for (final Cache.Entry<Long, BillableReceipt> currentEntry : this.receiptCache) {
199                                 // Add to list
200                                 this.getAllReceipts().add(currentEntry.getValue());
201                         }
202
203                         // Sort list
204                         this.getAllReceipts().sort(new Comparator<BillableReceipt>() {
205                                 @Override
206                                 public int compare (final BillableReceipt receipt1, final BillableReceipt receipt2) {
207                                         return receipt1.getReceiptId() > receipt2.getReceiptId() ? 1 : receipt1.getReceiptId() < receipt2.getReceiptId() ? -1 : 0;
208                                 }
209                         }
210                         );
211                 }
212         }
213
214         @Override
215         public boolean isReceiptAdded (final BillableReceipt receipt) {
216                 // Always trust the cache
217                 return this.getAllReceipts().contains(receipt);
218         }
219
220 }