2 * Copyright (C) 2017, 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.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.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.jproduct.events.category.AddedCategoryEvent;
35 import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException;
36 import org.mxchange.jproduct.model.category.Category;
37 import org.mxchange.jproduct.model.category.CategorySessionBeanRemote;
40 * A view-scoped bean for product lists
42 * @author Roland Haeder<roland@mxchange.org>
44 @Named ("categoryListController")
46 public class FinancialsCategoryListWebViewBean extends BaseFinancialsBean implements FinancialsCategoryListWebViewController {
51 private static final long serialVersionUID = 34_869_872_672_644L;
54 * List of all categories
56 private final List<Category> allCategories;
59 * EJB for general category stuff
61 @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote")
62 private CategorySessionBeanRemote categoryBean;
65 * Cache for all product categories
68 @NamedCache (cacheName = "categoryCache")
69 private transient Cache<Long, Category> categoryCache;
72 * A list of filtered categories
74 private List<Category> filteredCategories;
79 private Category selectedCategory;
84 public FinancialsCategoryListWebViewBean () {
85 // Call super constructor
89 this.allCategories = new LinkedList<>();
93 * Observes events fired after a new product category has been added
95 * @param event Event to be observed
97 public void afterCategoryAddedEvent (@Observes final AddedCategoryEvent event) {
101 throw new NullPointerException("event is null"); //NOI18N
102 } else if (event.getAddedCategory() == null) {
104 throw new NullPointerException("event.addedCategory is null"); //NOI18N
105 } else if (event.getAddedCategory().getCategoryId() == null) {
107 throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
108 } else if (event.getAddedCategory().getCategoryId() < 1) {
110 throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
114 this.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory());
115 this.getAllCategories().add(event.getAddedCategory());
119 public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
120 // Validate parameter
121 if (null == categoryId) {
123 throw new NullPointerException("categoryId is null"); //NOI18N
124 } else if (categoryId < 1) {
126 throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
127 } else if (!this.categoryCache.containsKey(categoryId)) {
129 throw new CategoryNotFoundException(categoryId);
133 final Category category = this.categoryCache.get(categoryId);
140 * Getter for a list of all categories
142 * @return All categories
144 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
145 public List<Category> getAllCategories () {
147 return this.allCategories;
151 * Getter for filtered category list
153 * @return Filtered category list
155 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
156 public List<Category> getFilteredCategories () {
157 return this.filteredCategories;
161 * Setter for filtered category list
163 * @param filteredCategories Filtered category list
165 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
166 public void setFilteredCategories (final List<Category> filteredCategories) {
167 this.filteredCategories = filteredCategories;
171 * Getter for selected category
173 * @return Selected category
175 public Category getSelectedCategory () {
176 return this.selectedCategory;
180 * Setter for selected category
182 * @param selectedCategory Selected category
184 public void setSelectedCategory (final Category selectedCategory) {
185 this.selectedCategory = selectedCategory;
189 * Initialization of this bean
192 public void init () {
194 if (!this.categoryCache.iterator().hasNext()) {
196 final List<Category> categories = this.categoryBean.allCategories();
198 // "Walk" through all entries and add to cache
199 for (final Category category : categories) {
200 // Add it by primary key
201 this.categoryCache.put(category.getCategoryId(), category);
205 // Is the list empty, but filled cache?
206 if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) {
208 final Iterator<Cache.Entry<Long, Category>> iterator = this.categoryCache.iterator();
211 while (iterator.hasNext()) {
213 final Cache.Entry<Long, Category> next = iterator.next();
216 this.allCategories.add(next.getValue());
220 this.allCategories.sort(new Comparator<Category>() {
222 public int compare (final Category o1, final Category o2) {
223 return o1.getCategoryId() > o2.getCategoryId() ? 1 : o1.getCategoryId() < o2.getCategoryId() ? -1 : 0;
229 this.setFilteredCategories(this.getAllCategories());
234 public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
235 // Validate parameter
236 if (null == categoryI18nKey) {
238 throw new NullPointerException("categoryI18nKey is null"); //NOI18N
239 } else if (categoryI18nKey.isEmpty()) {
241 throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
244 // Default is not the same
245 boolean isFound = false;
247 // Check all added,products
248 for (final Category category : this.getAllCategories()) {
249 // Is i18n key the same?
250 if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {