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 java.util.Objects;
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.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;
41 * A view-scoped bean for receipt lists
43 * @author Roland Haeder<roland@mxchange.org>
45 @Named ("receiptListController")
47 public class FinancialsReceiptListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptListWebViewController {
52 private static final long serialVersionUID = 34_869_872_672_653L;
57 private final List<BillableReceipt> allReceipts;
60 * Filtered receipts list
62 private List<BillableReceipt> filteredReceipts;
65 * EJB for general financial receipt purposes
67 @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
68 private FinancialReceiptSessionBeanRemote receiptBean;
71 * A list of all branch offices (globally)
74 @NamedCache (cacheName = "receiptCache")
75 private transient Cache<Long, BillableReceipt> receiptCache;
80 private BillableReceipt selectedReceipt;
85 public FinancialsReceiptListWebViewBean () {
86 // Call super constructor
90 this.allReceipts = new LinkedList<>();
94 * Observes events being fired when a new receipt has been added
96 * @param event Event being fired
98 public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
102 throw new NullPointerException("event is null"); //NOI18N
103 } else if (event.getReceipt() == null) {
105 throw new NullPointerException("event.receipt is null"); //NOI18N
106 } else if (event.getReceipt().getReceiptId() == null) {
108 throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
109 } else if (event.getReceipt().getReceiptId() < 1) {
111 throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
114 // Add to cache and general list
115 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
116 this.getAllReceipts().add(event.getReceipt());
120 * Observes events being fired when a receipt has been updated
122 * @param event Event being fired
124 public void afterUpdatedReceiptEvent (@Observes final ObservableReceiptUpdatedEvent event) {
125 // Validate parameter
128 throw new NullPointerException("event is null"); //NOI18N
129 } else if (event.getReceipt() == null) {
131 throw new NullPointerException("event.receipt is null"); //NOI18N
132 } else if (event.getReceipt().getReceiptId() == null) {
134 throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
135 } else if (event.getReceipt().getReceiptId() < 1) {
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
143 // Add to cache and general list
144 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
146 // Default is not found
147 boolean isFound = false;
150 for (final BillableReceipt receipt : this.getAllReceipts()) {
152 if (Objects.equals(receipt.getReceiptId(), event.getReceipt().getReceiptId())) {
153 // Yes, same primary key found, then copy all elements
154 Receipts.copyReceiptData(event.getReceipt(), receipt);
156 // Mark as found, break loop
162 // Has not been found?
164 // Receipt list does not contain receipt instance
165 throw new IllegalStateException("this.allReceipts does not contain the receipt."); //NOI18N
170 public BillableReceipt findReceiptById (final Long receiptId) throws ReceiptNotFoundException {
171 // Validate parameter
172 if (null == receiptId) {
174 throw new NullPointerException("receiptId is null"); //NOI18N
175 } else if (receiptId < 1) {
177 throw new IllegalArgumentException(MessageFormat.format("receiptId={0} is invalid.", receiptId)); //NOI18N
178 } else if (!this.receiptCache.containsKey(receiptId)) {
180 throw new ReceiptNotFoundException(receiptId);
184 final BillableReceipt receipt = this.receiptCache.get(receiptId);
191 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
192 public List<BillableReceipt> getAllReceipts () {
193 return this.allReceipts;
197 * Getter for filtered receipts
199 * @return Filtered receipts
201 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
202 public List<BillableReceipt> getFilteredReceipts () {
203 return this.filteredReceipts;
207 * Setter for filtered receipts
209 * @param filteredReceipts Filtered receipts
211 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
212 public void setFilteredReceipts (final List<BillableReceipt> filteredReceipts) {
213 this.filteredReceipts = filteredReceipts;
217 * Getter for selected receipt
219 * @return Selected receipt
221 public BillableReceipt getSelectedReceipt () {
222 return this.selectedReceipt;
226 * Setter for selected receipt
228 * @param selectedReceipt Selected receipt
230 public void setSelectedReceipt (final BillableReceipt selectedReceipt) {
231 this.selectedReceipt = selectedReceipt;
235 public void initCache () {
237 if (!this.receiptCache.iterator().hasNext()) {
239 for (final BillableReceipt receipt : this.receiptBean.fetchAllReceipts()) {
241 this.receiptCache.put(receipt.getReceiptId(), receipt);
245 // Is the list empty, but filled cache?
246 if (this.getAllReceipts().isEmpty() && this.receiptCache.iterator().hasNext()) {
248 for (final Cache.Entry<Long, BillableReceipt> currentEntry : this.receiptCache) {
250 this.getAllReceipts().add(currentEntry.getValue());
254 this.getAllReceipts().sort(new Comparator<BillableReceipt>() {
256 public int compare (final BillableReceipt receipt1, final BillableReceipt receipt2) {
257 return receipt1.getReceiptId() > receipt2.getReceiptId() ? 1 : receipt1.getReceiptId() < receipt2.getReceiptId() ? -1 : 0;
265 public boolean isReceiptAdded (final BillableReceipt receipt) {
266 // Always trust the cache
267 return this.getAllReceipts().contains(receipt);