2 * Copyright (C) 2017 - 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.generic_product.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.jproduct.events.product.added.AddedProductEvent;
34 import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent;
35 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
36 import org.mxchange.jproduct.model.product.Product;
37 import org.mxchange.jproduct.model.product.ProductSessionBeanRemote;
40 * A view-scoped bean for product lists
42 * @author Roland Haeder<roland@mxchange.org>
44 @Named ("productListController")
46 public class FinancialsProductListWebViewBean extends BaseFinancialsBean implements FinancialsProductListWebViewController {
51 private static final long serialVersionUID = 34_869_872_672_643L;
54 * List for all products
56 private final List<Product> allProducts;
59 * List for filtered products
61 private List<Product> filteredProducts;
64 * EJB for general product purposes
66 @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote")
67 private ProductSessionBeanRemote productBean;
73 @NamedCache (cacheName = "productCache")
74 private transient Cache<Long, Product> productCache;
79 private Product selectedProduct;
84 public FinancialsProductListWebViewBean () {
85 // Call super constructor
89 this.allProducts = new LinkedList<>();
93 * Observes events fired after a new product has been added
95 * @param event Event to be observed
97 public void afterProductAddedEvent (@Observes final AddedProductEvent event) {
101 throw new NullPointerException("event is null"); //NOI18N
102 } else if (event.getAddedProduct() == null) {
104 throw new NullPointerException("event.addedProduct is null"); //NOI18N
105 } else if (event.getAddedProduct().getProductId() == null) {
107 throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N
108 } else if (event.getAddedProduct().getProductId() < 1) {
110 throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N
114 this.addProduct(event.getAddedProduct());
118 * Event observer for updated product data by administrators
120 * @param event Updated product data event
122 public void afterProductUpdatedEvent (@Observes final ObservableProductUpdatedEvent event) {
123 // Event and contained entity instance should not be null
126 throw new NullPointerException("event is null"); //NOI18N
127 } else if (event.getUpdatedProduct() == null) {
129 throw new NullPointerException("event.updatedProduct is null"); //NOI18N
130 } else if (event.getUpdatedProduct().getProductId() == null) {
132 throw new NullPointerException("event.updatedProduct.productId is null"); //NOI18N
133 } else if (event.getUpdatedProduct().getProductId() < 1) {
135 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedProduct(), event.getUpdatedProduct().getProductId())); //NOI18N
138 // Add product instance only once
139 this.addProduct(event.getUpdatedProduct());
143 public Product findProductById (final Long productId) throws ProductNotFoundException {
144 // Validate parameter
145 if (null == productId) {
147 throw new NullPointerException("productId is null"); //NOI18N
148 } else if (productId < 1) {
150 throw new IllegalArgumentException(MessageFormat.format("productId={0} is invalid", productId)); //NOI18N
151 } else if (!this.productCache.containsKey(productId)) {
153 throw new ProductNotFoundException(productId);
157 final Product product = this.productCache.get(productId);
164 * Some "getter" for a linked list of only available products
166 * @return Only available products
168 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
169 public List<Product> getAllProducts () {
171 return this.allProducts;
175 * Getter for filtered product list
177 * @return Filtered product list
179 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
180 public List<Product> getFilteredProducts () {
181 return this.filteredProducts;
185 * Setter for filtered product list
187 * @param filteredProducts Filtered product list
189 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
190 public void setFilteredProducts (final List<Product> filteredProducts) {
191 this.filteredProducts = filteredProducts;
195 * Getter for selected product
197 * @return Selected product
199 public Product getSelectedProduct () {
200 return this.selectedProduct;
204 * Setter for selected product
206 * @param selectedProduct Selected product
208 public void setSelectedProduct (final Product selectedProduct) {
209 this.selectedProduct = selectedProduct;
213 * Initialization of this bean
216 public void initializeList () {
218 if (!this.productCache.iterator().hasNext()) {
219 // "Walk" through all entries and add to cache
220 for (final Product product : this.productBean.fetchAllGenericProducts()) {
221 // Add it by primary key
222 this.productCache.put(product.getProductId(), product);
226 // Is the list empty, but filled cache?
227 if (this.getAllProducts().isEmpty() && this.productCache.iterator().hasNext()) {
229 for (final Cache.Entry<Long, Product> currentEntry : this.productCache) {
231 this.getAllProducts().add(currentEntry.getValue());
235 this.getAllProducts().sort(new Comparator<Product>() {
237 public int compare (final Product product1, final Product product2) {
238 return product1.getProductId() > product2.getProductId() ? 1 : product1.getProductId() < product2.getProductId() ? -1 : 0;
243 this.setFilteredProducts(this.getAllProducts());
248 public boolean isProductI18nKeyAdded (final String productI18nKey) {
249 // Validate parameter
250 if (null == productI18nKey) {
252 throw new NullPointerException("productI18nKey is null"); //NOI18N
253 } else if (productI18nKey.isEmpty()) {
255 throw new IllegalArgumentException("productI18nKey is empty"); //NOI18N
258 // Default is not the same
259 boolean isFound = false;
261 // Check all added,products
262 for (final Product product : this.getAllProducts()) {
263 // Is i18n key the same?
264 if (Objects.equals(product.getProductI18nKey(), productI18nKey)) {
276 * Adds unique instance to product list. First any existing instance is
277 * being removed, then the new instance is added. It also updates the cache
278 * regardless if found or not.
280 * @param product Product instance to add uniquely
282 private void addProduct (final Product product) {
283 // Add product to list
284 this.productCache.put(product.getProductId(), product);
286 // Does the list contain it?
287 if (this.getAllProducts().contains(product)) {
288 // Yes, then remove it
289 this.getAllProducts().remove(product);
293 this.getAllProducts().add(product);
297 * Removes given product from all lists
299 * @param product Product instance to remove
301 private void removeProduct (final Product product) {
302 // Remove from general list
303 this.productCache.remove(product.getProductId());