2 * Copyright (C) 2017, 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.beans.financial.model.receipt.list;
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 javax.annotation.PostConstruct;
26 import javax.cache.Cache;
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.exceptions.receipt.ReceiptNotFoundException;
35 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
36 import org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote;
39 * A view-scoped bean for receipt lists
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("receiptListController")
45 public class FinancialsReceiptListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptListWebViewController {
50 private static final long serialVersionUID = 34_869_872_672_653L;
55 private final List<BillableReceipt> allReceipts;
58 * Filtered receipts list
60 private List<BillableReceipt> filteredReceipts;
63 * EJB for general financial receipt purposes
65 @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
66 private FinancialReceiptSessionBeanRemote receiptBean;
69 * A list of all branch offices (globally)
72 @NamedCache (cacheName = "receiptCache")
73 private transient Cache<Long, BillableReceipt> receiptCache;
78 private BillableReceipt selectedReceipt;
83 public FinancialsReceiptListWebViewBean () {
84 // Call super constructor
88 this.allReceipts = new LinkedList<>();
92 * Observes events being fired when a new receipt has been added
94 * @param event Event being fired
96 public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getReceipt() == null) {
103 throw new NullPointerException("event.receipt is null"); //NOI18N
104 } else if (event.getReceipt().getReceiptId() == null) {
106 throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
107 } else if (event.getReceipt().getReceiptId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
112 // Add to cache and general list
113 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
114 this.getAllReceipts().add(event.getReceipt());
118 public BillableReceipt findReceiptById (final Long receiptId) throws ReceiptNotFoundException {
119 // Validate parameter
120 if (null == receiptId) {
122 throw new NullPointerException("receiptId is null"); //NOI18N
123 } else if (receiptId < 1) {
125 throw new IllegalArgumentException("receiptId=" + receiptId + " is invalid."); //NOI18N
126 } else if (!this.receiptCache.containsKey(receiptId)) {
128 throw new ReceiptNotFoundException(receiptId);
132 final BillableReceipt receipt = this.receiptCache.get(receiptId);
139 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
140 public List<BillableReceipt> getAllReceipts () {
141 return this.allReceipts;
145 * Getter for filtered receipts
147 * @return Filtered receipts
149 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
150 public List<BillableReceipt> getFilteredReceipts () {
151 return this.filteredReceipts;
155 * Setter for filtered receipts
157 * @param filteredReceipts Filtered receipts
159 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
160 public void setFilteredReceipts (final List<BillableReceipt> filteredReceipts) {
161 this.filteredReceipts = filteredReceipts;
165 * Getter for selected receipt
167 * @return Selected receipt
169 public BillableReceipt getSelectedReceipt () {
170 return this.selectedReceipt;
174 * Setter for selected receipt
176 * @param selectedReceipt Selected receipt
178 public void setSelectedReceipt (final BillableReceipt selectedReceipt) {
179 this.selectedReceipt = selectedReceipt;
183 public void initCache () {
185 if (!this.receiptCache.iterator().hasNext()) {
186 // Get whole list from EJB
187 final List<BillableReceipt> receipts = this.receiptBean.allReceipts();
190 for (final BillableReceipt receipt : receipts) {
192 this.receiptCache.put(receipt.getReceiptId(), receipt);
196 // Is the list empty, but filled cache?
197 if (this.getAllReceipts().isEmpty() && this.receiptCache.iterator().hasNext()) {
199 final Iterator<Cache.Entry<Long, BillableReceipt>> iterator = this.receiptCache.iterator();
202 while (iterator.hasNext()) {
204 final Cache.Entry<Long, BillableReceipt> next = iterator.next();
207 this.getAllReceipts().add(next.getValue());
211 this.getAllReceipts().sort(new Comparator<BillableReceipt>() {
213 public int compare (final BillableReceipt o1, final BillableReceipt o2) {
214 return o1.getReceiptId() > o2.getReceiptId() ? 1 : o1.getReceiptId() < o2.getReceiptId() ? -1 : 0;
222 public boolean isReceiptAdded (final BillableReceipt receipt) {
223 // Always trust the cache
224 return this.getAllReceipts().contains(receipt);