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