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.LinkedList;
23 import java.util.List;
24 import javax.annotation.PostConstruct;
25 import javax.cache.Cache;
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;
38 * A view-scoped bean for receipt lists
40 * @author Roland Haeder<roland@mxchange.org>
42 @Named ("receiptListController")
44 public class FinancialsReceiptListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptListWebViewController {
49 private static final long serialVersionUID = 34_869_872_672_653L;
54 private final List<BillableReceipt> allReceipts;
57 * Filtered receipts list
59 private List<BillableReceipt> filteredReceipts;
62 * EJB for general financial receipt purposes
64 @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
65 private FinancialReceiptSessionBeanRemote receiptBean;
68 * A list of all branch offices (globally)
71 @NamedCache (cacheName = "receiptCache")
72 private transient Cache<Long, BillableReceipt> receiptCache;
77 private BillableReceipt selectedReceipt;
82 public FinancialsReceiptListWebViewBean () {
83 // Call super constructor
87 this.allReceipts = new LinkedList<>();
91 * Observes events being fired when a new receipt has been added
93 * @param event Event being fired
95 public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
99 throw new NullPointerException("event is null"); //NOI18N
100 } else if (event.getReceipt() == null) {
102 throw new NullPointerException("event.receipt is null"); //NOI18N
103 } else if (event.getReceipt().getReceiptId() == null) {
105 throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
106 } else if (event.getReceipt().getReceiptId() < 1) {
108 throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
111 // Add to cache and general list
112 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
113 this.getAllReceipts().add(event.getReceipt());
117 public BillableReceipt findReceiptById (final Long receiptId) throws ReceiptNotFoundException {
118 // Validate parameter
119 if (null == receiptId) {
121 throw new NullPointerException("receiptId is null"); //NOI18N
122 } else if (receiptId < 1) {
124 throw new IllegalArgumentException("receiptId=" + receiptId + " is invalid."); //NOI18N
125 } else if (!this.receiptCache.containsKey(receiptId)) {
127 throw new ReceiptNotFoundException(receiptId);
131 final BillableReceipt receipt = this.receiptCache.get(receiptId);
138 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
139 public List<BillableReceipt> getAllReceipts () {
140 return this.allReceipts;
144 * Getter for filtered receipts
146 * @return Filtered receipts
148 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149 public List<BillableReceipt> getFilteredReceipts () {
150 return this.filteredReceipts;
154 * Setter for filtered receipts
156 * @param filteredReceipts Filtered receipts
158 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159 public void setFilteredReceipts (final List<BillableReceipt> filteredReceipts) {
160 this.filteredReceipts = filteredReceipts;
164 * Getter for selected receipt
166 * @return Selected receipt
168 public BillableReceipt getSelectedReceipt () {
169 return this.selectedReceipt;
173 * Setter for selected receipt
175 * @param selectedReceipt Selected receipt
177 public void setSelectedReceipt (final BillableReceipt selectedReceipt) {
178 this.selectedReceipt = selectedReceipt;
182 public void initCache () {
184 if (!this.receiptCache.iterator().hasNext()) {
185 // Get whole list from EJB
186 final List<BillableReceipt> receipts = this.receiptBean.allReceipts();
189 for (final BillableReceipt receipt : receipts) {
191 this.receiptCache.put(receipt.getReceiptId(), receipt);
195 // Is the list empty, but filled cache?
196 if (this.getAllReceipts().isEmpty() && this.receiptCache.iterator().hasNext()) {
198 for (final Cache.Entry<Long, BillableReceipt> currentEntry : this.receiptCache) {
200 this.getAllReceipts().add(currentEntry.getValue());
204 this.getAllReceipts().sort(new Comparator<BillableReceipt>() {
206 public int compare (final BillableReceipt receipt1, final BillableReceipt receipt2) {
207 return receipt1.getReceiptId() > receipt2.getReceiptId() ? 1 : receipt1.getReceiptId() < receipt2.getReceiptId() ? -1 : 0;
215 public boolean isReceiptAdded (final BillableReceipt receipt) {
216 // Always trust the cache
217 return this.getAllReceipts().contains(receipt);