2 * Copyright (C) 2017 - 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.product_category.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.category.added.AddedCategoryEvent;
34 import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException;
35 import org.mxchange.jproduct.model.category.Category;
36 import org.mxchange.jproduct.model.category.CategorySessionBeanRemote;
39 * A view-scoped bean for product lists
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("categoryListController")
45 public class FinancialsCategoryListWebViewBean extends BaseFinancialsBean implements FinancialsCategoryListWebViewController {
50 private static final long serialVersionUID = 34_869_872_672_644L;
53 * List of all categories
55 private final List<Category> allCategories;
58 * EJB for general category stuff
60 @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote")
61 private CategorySessionBeanRemote categoryBean;
64 * Cache for all product categories
67 @NamedCache (cacheName = "categoryCache")
68 private transient Cache<Long, Category> categoryCache;
71 * A list of filtered categories
73 private List<Category> filteredCategories;
78 private Category selectedCategory;
83 public FinancialsCategoryListWebViewBean () {
84 // Call super constructor
88 this.allCategories = new LinkedList<>();
92 * Observes events fired after a new product category has been added
94 * @param event Event to be observed
96 public void afterCategoryAddedEvent (@Observes final AddedCategoryEvent event) {
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getAddedCategory() == null) {
103 throw new NullPointerException("event.addedCategory is null"); //NOI18N
104 } else if (event.getAddedCategory().getCategoryId() == null) {
106 throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
107 } else if (event.getAddedCategory().getCategoryId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
113 this.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory());
114 this.getAllCategories().add(event.getAddedCategory());
118 public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
119 // Validate parameter
120 if (null == categoryId) {
122 throw new NullPointerException("categoryId is null"); //NOI18N
123 } else if (categoryId < 1) {
125 throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
126 } else if (!this.categoryCache.containsKey(categoryId)) {
128 throw new CategoryNotFoundException(categoryId);
132 final Category category = this.categoryCache.get(categoryId);
139 * Getter for a list of all categories
141 * @return All categories
143 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
144 public List<Category> getAllCategories () {
146 return this.allCategories;
150 * Getter for filtered category list
152 * @return Filtered category list
154 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
155 public List<Category> getFilteredCategories () {
156 return this.filteredCategories;
160 * Setter for filtered category list
162 * @param filteredCategories Filtered category list
164 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
165 public void setFilteredCategories (final List<Category> filteredCategories) {
166 this.filteredCategories = filteredCategories;
170 * Getter for selected category
172 * @return Selected category
174 public Category getSelectedCategory () {
175 return this.selectedCategory;
179 * Setter for selected category
181 * @param selectedCategory Selected category
183 public void setSelectedCategory (final Category selectedCategory) {
184 this.selectedCategory = selectedCategory;
188 * Initialization of this bean
191 public void init () {
193 if (!this.categoryCache.iterator().hasNext()) {
194 // Get whole list from EJB
195 final List<Category> categories = this.categoryBean.allCategories();
197 // "Walk" through all entries and add to cache
198 for (final Category category : categories) {
199 // Add it by primary key
200 this.categoryCache.put(category.getCategoryId(), category);
204 // Is the list empty, but filled cache?
205 if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) {
207 for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
209 this.allCategories.add(currentEntry.getValue());
213 this.allCategories.sort(new Comparator<Category>() {
215 public int compare (final Category category1, final Category category2) {
216 return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
222 this.setFilteredCategories(this.getAllCategories());
227 public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
228 // Validate parameter
229 if (null == categoryI18nKey) {
231 throw new NullPointerException("categoryI18nKey is null"); //NOI18N
232 } else if (categoryI18nKey.isEmpty()) {
234 throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
237 // Default is not the same
238 boolean isFound = false;
240 // Check all added,products
241 for (final Category category : this.getAllCategories()) {
242 // Is i18n key the same?
243 if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {