2 * Copyright (C) 2016 - 2018 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_item.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 java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.event.Observes;
30 import javax.faces.view.ViewScoped;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
34 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
35 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
36 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
37 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote;
38 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
41 * A list financial receipt item bean (controller)
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("receiptItemListController")
47 public class FinancialsReceiptItemListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptItemListWebViewController {
52 private static final long serialVersionUID = 56_189_028_928_472L;
55 * All receipt items list
57 private final List<BillableReceiptItem> allReceiptItems;
60 * Filtered receipt items list
62 private List<BillableReceiptItem> filteredReceiptItems;
65 * EJB for general financial receipt purposes
67 @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote")
68 private FinancialReceiptItemSessionBeanRemote receiptItemBean;
71 * A list of all branch offices (globally)
74 @NamedCache (cacheName = "receiptItemCache")
75 private transient Cache<Long, BillableReceiptItem> receiptItemCache;
78 * Selected receipt item
80 private BillableReceiptItem selectedReceiptItem;
86 private FinancialsUserLoginWebSessionController userLoginController;
89 * User's receipt items
91 private final List<BillableReceiptItem> userReceiptItems;
96 @SuppressWarnings ("CollectionWithoutInitialCapacity")
97 public FinancialsReceiptItemListWebViewBean () {
98 // Call super constructor
102 this.allReceiptItems = new LinkedList<>();
103 this.userReceiptItems = new LinkedList<>();
107 * Observes events being fired when a new receipt item has been added
109 * @param event Event being fired
111 public void afterAddedReceiptItemEvent (@Observes final ObservableReceiptItemAddedEvent event) {
112 // Validate parameter
115 throw new NullPointerException("event is null"); //NOI18N
118 // Add to cache and general list
119 this.receiptItemCache.put(event.getReceiptItem().getItemId(), event.getReceiptItem());
120 this.getAllReceiptItems().add(event.getReceiptItem());
123 if (this.userLoginController.isUserLoggedIn() && Objects.equals(event.getReceiptItem().getItemReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
124 // Same user, then add it
125 this.userReceiptItems.add(event.getReceiptItem());
130 * Event observer for logged-in user
132 * @param event Event instance
134 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
135 // event should not be null
138 throw new NullPointerException("event is null"); //NOI18N
139 } else if (event.getLoggedInUser() == null) {
141 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
142 } else if (event.getLoggedInUser().getUserId() == null) {
144 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
145 } else if (event.getLoggedInUser().getUserId() < 1) {
147 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
150 // Fill cache with all found user's receipts
151 for (final BillableReceiptItem receiptItem : this.allReceiptItems) {
153 if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
155 this.userReceiptItems.add(receiptItem);
161 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
162 public List<BillableReceiptItem> getAllReceiptItems () {
163 return this.allReceiptItems;
167 * Getter for filtered receipt items
169 * @return Filtered receipts
171 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
172 public List<BillableReceiptItem> getFilteredReceiptItems () {
173 return this.filteredReceiptItems;
177 * Setter for filtered receipt items
179 * @param filteredReceiptItems Filtered receipt items
181 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
182 public void setFilteredReceiptItems (final List<BillableReceiptItem> filteredReceiptItems) {
183 this.filteredReceiptItems = filteredReceiptItems;
187 * Getter for selected receipt item
189 * @return Selected receipt item
191 public BillableReceiptItem getSelectedReceiptItem () {
192 return this.selectedReceiptItem;
196 * Setter for selected receipt item
198 * @param selectedReceiptItem Selected receipt item
200 public void setSelectedReceiptItem (final BillableReceiptItem selectedReceiptItem) {
201 this.selectedReceiptItem = selectedReceiptItem;
205 public void initCache () {
207 if (!this.receiptItemCache.iterator().hasNext()) {
208 // Get whole list from EJB
209 final List<BillableReceiptItem> receiptItems = this.receiptItemBean.allReceiptItems();
212 for (final BillableReceiptItem receiptItem : receiptItems) {
214 this.receiptItemCache.put(receiptItem.getItemId(), receiptItem);
218 // Is the list empty, but filled cache?
219 if (this.getAllReceiptItems().isEmpty() && this.receiptItemCache.iterator().hasNext()) {
221 final Iterator<Cache.Entry<Long, BillableReceiptItem>> iterator = this.receiptItemCache.iterator();
224 while (iterator.hasNext()) {
226 final Cache.Entry<Long, BillableReceiptItem> next = iterator.next();
229 this.getAllReceiptItems().add(next.getValue());
233 this.getAllReceiptItems().sort(new Comparator<BillableReceiptItem>() {
235 public int compare (final BillableReceiptItem o1, final BillableReceiptItem o2) {
236 return o1.getItemId() > o2.getItemId() ? 1 : o1.getItemId() < o2.getItemId() ? -1 : 0;
244 public boolean isReceiptItemAdded (final BillableReceiptItem receiptItem) {
245 // Always trust the cache
246 return this.getAllReceiptItems().contains(receiptItem);