2 * Copyright (C) 2016 - 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_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.receipt_item.ReceiptItems;
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 // Add to cache and general list
132 this.receiptItemCache.put(event.getAddedReceiptItem().getItemId(), event.getAddedReceiptItem());
133 this.uniqueAddReceiptItem(event.getAddedReceiptItem());
137 * Observes events being fired when an administrator has updated a receipt
140 * @param event Event being fired
142 public void afterAdminUpdatedReceiptItemEvent (final @Observes ObservableAdminReceiptItemUpdatedEvent event) {
143 // Validate parameter
146 throw new NullPointerException("event is null"); //NOI18N
147 } else if (event.getUpdatedReceiptItem() == null) {
149 throw new NullPointerException("event.updatedReceiptItem is null"); //NOI18N
150 } else if (event.getUpdatedReceiptItem().getItemId() == null) {
152 throw new NullPointerException("event.updatedReceiptItem.itemId is null"); //NOI18N
153 } else if (event.getUpdatedReceiptItem().getItemId() < 1) {
155 throw new IllegalArgumentException(MessageFormat.format("event.updatedReceiptItem.itemId={0} is not valid", event.getUpdatedReceiptItem().getItemId())); //NOI18N
158 // Add to cache and general list
159 this.receiptItemCache.put(event.getUpdatedReceiptItem().getItemId(), event.getUpdatedReceiptItem());
160 this.uniqueAddReceiptItem(event.getUpdatedReceiptItem());
164 * Event observer for logged-in user
166 * @param event Event instance
168 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
169 // Event and contained entity instance should not be null
172 throw new NullPointerException("event is null"); //NOI18N
173 } else if (event.getLoggedInUser() == null) {
175 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
176 } else if (event.getLoggedInUser().getUserId() == null) {
178 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
179 } else if (event.getLoggedInUser().getUserId() < 1) {
181 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
184 // Fill cache with all found user's receipts
185 for (final BillableReceiptItem receiptItem : this.allReceiptItems) {
187 if (Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), event.getLoggedInUser())) {
189 this.userReceiptItems.add(receiptItem);
195 public BillableReceiptItem findReceiptItemById (final Long receiptItemId) throws ReceiptItemNotFoundException {
196 // Validate parameter
197 if (null == receiptItemId) {
199 throw new NullPointerException("receiptItemId is null");
200 } else if (receiptItemId < 1) {
202 throw new IllegalArgumentException(MessageFormat.format("receiptItemId={0} is not valid.", receiptItemId));
206 BillableReceiptItem receiptItem = null;
208 // Iterate over whole list
209 for (final BillableReceiptItem currentReceiptItem : this.getAllReceiptItems()) {
210 // Is the primary key matching?
211 if (Objects.equals(currentReceiptItem.getItemId(), receiptItemId)) {
212 // Yes, then remember and exit iteration
213 receiptItem = currentReceiptItem;
219 if (null == receiptItem) {
221 throw new ReceiptItemNotFoundException(receiptItemId);
229 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
230 public List<BillableReceiptItem> getAllReceiptItems () {
231 return this.allReceiptItems;
235 * Getter for filtered receipt items
237 * @return Filtered receipts
239 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
240 public List<BillableReceiptItem> getFilteredReceiptItems () {
241 return this.filteredReceiptItems;
245 * Setter for filtered receipt items
247 * @param filteredReceiptItems Filtered receipt items
249 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
250 public void setFilteredReceiptItems (final List<BillableReceiptItem> filteredReceiptItems) {
251 this.filteredReceiptItems = filteredReceiptItems;
255 * Getter for selected receipt item
257 * @return Selected receipt item
259 public BillableReceiptItem getSelectedReceiptItem () {
260 return this.selectedReceiptItem;
264 * Setter for selected receipt item
266 * @param selectedReceiptItem Selected receipt item
268 public void setSelectedReceiptItem (final BillableReceiptItem selectedReceiptItem) {
269 this.selectedReceiptItem = selectedReceiptItem;
273 public void initCache () {
275 if (!this.receiptItemCache.iterator().hasNext()) {
277 for (final BillableReceiptItem receiptItem : this.receiptItemBean.fetchAllReceiptItems()) {
279 this.receiptItemCache.put(receiptItem.getItemId(), receiptItem);
283 // Is the list empty, but filled cache?
284 if (this.getAllReceiptItems().isEmpty() && this.receiptItemCache.iterator().hasNext()) {
286 for (final Cache.Entry<Long, BillableReceiptItem> currentEntry : this.receiptItemCache) {
288 this.getAllReceiptItems().add(currentEntry.getValue());
292 this.getAllReceiptItems().sort(new Comparator<BillableReceiptItem>() {
294 public int compare (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) {
295 return receiptItem1.getItemId() > receiptItem2.getItemId() ? 1 : receiptItem1.getItemId() < receiptItem2.getItemId() ? -1 : 0;
303 public boolean isReceiptItemAdded (final BillableReceiptItem receiptItem) {
304 // Does it contain the same object?
305 if (this.getAllReceiptItems().contains(receiptItem)) {
306 // Yes, skip below code
310 // Default is not the same
311 boolean alreadyAdded = false;
314 for (final BillableReceiptItem item : this.getAllReceiptItems()) {
316 if (ReceiptItems.isSameReceiptItem(item, receiptItem)) {
328 * Uniquely add a receipt item instance.
330 * @param receiptItem To be added/updated receipt item instance
332 private void uniqueAddReceiptItem (final BillableReceiptItem receiptItem) {
334 final Iterator<BillableReceiptItem> allIterator = this.getAllReceiptItems().iterator();
336 // Iterate over all entries
337 while (allIterator.hasNext()) {
339 final BillableReceiptItem currentReceiptItem = allIterator.next();
341 // It the id matching?
342 if (Objects.equals(receiptItem.getItemId(), currentReceiptItem.getItemId())) {
343 // Yes, then remove it and stop iterating
344 allIterator.remove();
350 this.getAllReceiptItems().add(receiptItem);
353 if (this.userLoginController.isUserLoggedIn() && Objects.equals(receiptItem.getItemReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
355 final Iterator<BillableReceiptItem> userIterator = this.userReceiptItems.iterator();
357 // Iterate over all entries
358 while (userIterator.hasNext()) {
360 final BillableReceiptItem currentReceiptItem = userIterator.next();
362 // It the id matching?
363 if (Objects.equals(receiptItem.getItemId(), currentReceiptItem.getItemId())) {
364 // Yes, then remove it and stop iterating
365 userIterator.remove();
371 this.userReceiptItems.add(receiptItem);