2 * Copyright (C) 2016 - 2022 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.ObservableAdminReceiptItemAddedEvent;
36 import org.mxchange.jfinancials.events.receipt_item.updated.ObservableAdminReceiptItemUpdatedEvent;
37 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemNotFoundException;
38 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
39 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote;
40 import org.mxchange.jfinancials.model.utils.ReceiptItemUtils;
41 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
44 * A list financial receipt item bean (controller)
46 * @author Roland Häder<roland@mxchange.org>
48 @Named ("receiptItemListController")
50 public class FinancialsReceiptItemListWebViewBean extends BaseFinancialsBean implements FinancialsReceiptItemListWebViewController {
55 private static final long serialVersionUID = 56_189_028_928_472L;
58 * All receipt items list
60 private final List<BillableReceiptItem> allReceiptItems;
63 * Filtered receipt items list
65 private List<BillableReceiptItem> filteredReceiptItems;
68 * EJB for general financial receipt purposes
70 @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote")
71 private FinancialReceiptItemSessionBeanRemote receiptItemBean;
74 * A list of all branch offices (globally)
77 @NamedCache (cacheName = "receiptItemCache")
78 private transient Cache<Long, BillableReceiptItem> receiptItemCache;
81 * Selected receipt item
83 private BillableReceiptItem selectedReceiptItem;
89 private FinancialsUserLoginWebSessionController userLoginController;
92 * User's receipt items
94 private final List<BillableReceiptItem> userReceiptItems;
99 @SuppressWarnings ("CollectionWithoutInitialCapacity")
100 public FinancialsReceiptItemListWebViewBean () {
101 // Call super constructor
105 this.allReceiptItems = new LinkedList<>();
106 this.userReceiptItems = new LinkedList<>();
110 * Observes events being fired when an administrator has added a new receipt
113 * @param event Event being fired
115 public void afterAdminAddedReceiptItemEvent (@Observes final ObservableAdminReceiptItemAddedEvent event) {
116 // Validate parameter
119 throw new NullPointerException("event is null"); //NOI18N
120 } else if (event.getAddedReceiptItem() == null) {
122 throw new NullPointerException("event.addedReceiptItem is null"); //NOI18N
123 } else if (event.getAddedReceiptItem().getItemId() == null) {
125 throw new NullPointerException("event.addedReceiptItem.itemId is null"); //NOI18N
126 } else if (event.getAddedReceiptItem().getItemId() < 1) {
128 throw new IllegalArgumentException(MessageFormat.format("event.addedReceiptItem.itemId={0} is not valid", event.getAddedReceiptItem().getItemId())); //NOI18N
131 // Uniquely add to cache
132 this.uniqueAddReceiptItem(event.getAddedReceiptItem());
136 * Observes events being fired when an administrator has updated a receipt
139 * @param event Event being fired
141 public void afterAdminUpdatedReceiptItemEvent (final @Observes ObservableAdminReceiptItemUpdatedEvent event) {
142 // Validate parameter
145 throw new NullPointerException("event is null"); //NOI18N
146 } else if (event.getUpdatedReceiptItem() == null) {
148 throw new NullPointerException("event.updatedReceiptItem is null"); //NOI18N
149 } else if (event.getUpdatedReceiptItem().getItemId() == null) {
151 throw new NullPointerException("event.updatedReceiptItem.itemId is null"); //NOI18N
152 } else if (event.getUpdatedReceiptItem().getItemId() < 1) {
154 throw new IllegalArgumentException(MessageFormat.format("event.updatedReceiptItem.itemId={0} is not valid", event.getUpdatedReceiptItem().getItemId())); //NOI18N
157 // Uniquely update instance
158 this.uniqueAddReceiptItem(event.getUpdatedReceiptItem());
162 * Event observer for logged-in user
164 * @param event Event instance
166 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
167 // Event and contained entity instance should not be null
170 throw new NullPointerException("event is null"); //NOI18N
171 } else if (event.getLoggedInUser() == null) {
173 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
174 } else if (event.getLoggedInUser().getUserId() == null) {
176 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
177 } else if (event.getLoggedInUser().getUserId() < 1) {
179 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
182 // Fill cache with all found user's receipts
183 for (final BillableReceiptItem receiptItem : this.allReceiptItems) {
185 if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
187 this.userReceiptItems.add(receiptItem);
193 public BillableReceiptItem findReceiptItemById (final Long receiptItemId) throws ReceiptItemNotFoundException {
194 // Validate parameter
195 if (null == receiptItemId) {
197 throw new NullPointerException("receiptItemId is null");
198 } else if (receiptItemId < 1) {
200 throw new IllegalArgumentException(MessageFormat.format("receiptItemId={0} is not valid.", receiptItemId));
204 BillableReceiptItem receiptItem = null;
206 // Iterate over whole list
207 for (final BillableReceiptItem currentReceiptItem : this.getAllReceiptItems()) {
208 // Is the primary key matching?
209 if (Objects.equals(currentReceiptItem.getItemId(), receiptItemId)) {
210 // Yes, then remember and exit iteration
211 receiptItem = currentReceiptItem;
217 if (null == receiptItem) {
219 throw new ReceiptItemNotFoundException(receiptItemId);
227 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
228 public List<BillableReceiptItem> getAllReceiptItems () {
229 return this.allReceiptItems;
233 * Getter for filtered receipt items
235 * @return Filtered receipts
237 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
238 public List<BillableReceiptItem> getFilteredReceiptItems () {
239 return this.filteredReceiptItems;
243 * Setter for filtered receipt items
245 * @param filteredReceiptItems Filtered receipt items
247 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
248 public void setFilteredReceiptItems (final List<BillableReceiptItem> filteredReceiptItems) {
249 this.filteredReceiptItems = filteredReceiptItems;
253 * Getter for selected receipt item
255 * @return Selected receipt item
257 public BillableReceiptItem getSelectedReceiptItem () {
258 return this.selectedReceiptItem;
262 * Setter for selected receipt item
264 * @param selectedReceiptItem Selected receipt item
266 public void setSelectedReceiptItem (final BillableReceiptItem selectedReceiptItem) {
267 this.selectedReceiptItem = selectedReceiptItem;
271 public void initCache () {
273 if (!this.receiptItemCache.iterator().hasNext()) {
275 for (final BillableReceiptItem receiptItem : this.receiptItemBean.fetchAllReceiptItems()) {
277 this.receiptItemCache.put(receiptItem.getItemId(), receiptItem);
281 // Is the list empty, but filled cache?
282 if (this.getAllReceiptItems().isEmpty() && this.receiptItemCache.iterator().hasNext()) {
284 for (final Cache.Entry<Long, BillableReceiptItem> currentEntry : this.receiptItemCache) {
286 this.getAllReceiptItems().add(currentEntry.getValue());
290 this.getAllReceiptItems().sort(new Comparator<BillableReceiptItem>() {
292 public int compare (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) {
293 return receiptItem1.getItemId() > receiptItem2.getItemId() ? 1 : receiptItem1.getItemId() < receiptItem2.getItemId() ? -1 : 0;
301 public boolean isReceiptItemAdded (final BillableReceiptItem receiptItem) {
302 // Does it contain the same object?
303 if (this.getAllReceiptItems().contains(receiptItem)) {
304 // Yes, skip below code
308 // Default is not the same
309 boolean alreadyAdded = false;
312 for (final BillableReceiptItem item : this.getAllReceiptItems()) {
314 if (ReceiptItemUtils.isSameReceiptItem(item, receiptItem)) {
326 * Uniquely add a receipt item instance.
328 * @param receiptItem To be added/updated receipt item instance
330 private void uniqueAddReceiptItem (final BillableReceiptItem receiptItem) {
331 // Add to cache and general list
332 this.receiptItemCache.put(receiptItem.getItemId(), receiptItem);
335 final Iterator<BillableReceiptItem> allIterator = this.getAllReceiptItems().iterator();
337 // Iterate over all entries
338 while (allIterator.hasNext()) {
340 final BillableReceiptItem currentReceiptItem = allIterator.next();
342 // It the id matching?
343 if (Objects.equals(receiptItem.getItemId(), currentReceiptItem.getItemId())) {
344 // Yes, then remove it and stop iterating
345 allIterator.remove();
351 this.getAllReceiptItems().add(receiptItem);
354 if (this.userLoginController.isUserLoggedIn() && Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
356 final Iterator<BillableReceiptItem> userIterator = this.userReceiptItems.iterator();
358 // Iterate over all entries
359 while (userIterator.hasNext()) {
361 final BillableReceiptItem currentReceiptItem = userIterator.next();
363 // It the id matching?
364 if (Objects.equals(receiptItem.getItemId(), currentReceiptItem.getItemId())) {
365 // Yes, then remove it and stop iterating
366 userIterator.remove();
372 this.userReceiptItems.add(receiptItem);