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.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.added.ObservableAdminAddedCategoryEvent;
35 import org.mxchange.jproduct.events.category.updated.ObservableAdminUpdatedCategoryEvent;
36 import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException;
37 import org.mxchange.jproduct.model.category.Category;
38 import org.mxchange.jproduct.model.category.CategorySessionBeanRemote;
41 * A view-scoped bean for product lists
43 * @author Roland Haeder<roland@mxchange.org>
45 @Named ("productCategoryListController")
47 public class FinancialsCategoryListWebViewBean extends BaseFinancialsBean implements FinancialsCategoryListWebViewController {
52 private static final long serialVersionUID = 34_869_872_672_644L;
55 * List of all categories
57 private final List<Category> allProductCategories;
60 * EJB for general category stuff
62 @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote")
63 private CategorySessionBeanRemote categoryBean;
66 * Cache for all product categories
69 @NamedCache (cacheName = "categoryCache")
70 private transient Cache<Long, Category> categoryCache;
73 * A list of filtered categories
75 private List<Category> filteredProductCategories;
80 private Category selectedCategory;
85 public FinancialsCategoryListWebViewBean () {
86 // Call super constructor
90 this.allProductCategories = new LinkedList<>();
94 * Observes events fired after a new product category has been added
96 * @param event Event to be observed
98 public void afterAdminAddedCategoryEvent (@Observes final ObservableAdminAddedCategoryEvent event) {
102 throw new NullPointerException("event is null"); //NOI18N
103 } else if (event.getAddedCategory() == null) {
105 throw new NullPointerException("event.addedCategory is null"); //NOI18N
106 } else if (event.getAddedCategory().getCategoryId() == null) {
108 throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
109 } else if (event.getAddedCategory().getCategoryId() < 1) {
111 throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
115 this.uniqueAddProductCategory(event.getAddedCategory());
119 * Observes events fired after a new product category has been updated
121 * @param event Event to be observed
123 public void afterAdminUpdatedCategoryEvent (@Observes final ObservableAdminUpdatedCategoryEvent event) {
127 throw new NullPointerException("event is null"); //NOI18N
128 } else if (event.getUpdatedCategory() == null) {
130 throw new NullPointerException("event.updatedCategory is null"); //NOI18N
131 } else if (event.getUpdatedCategory().getCategoryId() == null) {
133 throw new NullPointerException("event.updatedCategory.categoryId is null"); //NOI18N
134 } else if (event.getUpdatedCategory().getCategoryId() < 1) {
136 throw new IllegalArgumentException(MessageFormat.format("event.updatedCategory.categoryId={0} is not valid.", event.getUpdatedCategory().getCategoryId())); //NOI18N
140 this.uniqueAddProductCategory(event.getUpdatedCategory());
144 public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
145 // Validate parameter
146 if (null == categoryId) {
148 throw new NullPointerException("categoryId is null"); //NOI18N
149 } else if (categoryId < 1) {
151 throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
152 } else if (!this.categoryCache.containsKey(categoryId)) {
154 throw new CategoryNotFoundException(categoryId);
158 final Category category = this.categoryCache.get(categoryId);
165 * Getter for a list of all categories
167 * @return All categories
169 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
170 public List<Category> getAllProductCategories () {
172 return this.allProductCategories;
176 * Getter for filtered category list
178 * @return Filtered category list
180 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
181 public List<Category> getFilteredProductCategories () {
182 return this.filteredProductCategories;
186 * Setter for filtered category list
188 * @param filteredProductCategories Filtered category list
190 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
191 public void setFilteredProductCategories (final List<Category> filteredProductCategories) {
192 this.filteredProductCategories = filteredProductCategories;
196 * Getter for selected category
198 * @return Selected category
200 public Category getSelectedCategory () {
201 return this.selectedCategory;
205 * Setter for selected category
207 * @param selectedCategory Selected category
209 public void setSelectedCategory (final Category selectedCategory) {
210 this.selectedCategory = selectedCategory;
214 * Initialization of this bean
217 public void initializeList () {
219 if (!this.categoryCache.iterator().hasNext()) {
220 // "Walk" through all entries and add to cache
221 for (final Category category : this.categoryBean.fetchAllProductCategories()) {
222 // Add it by primary key
223 this.categoryCache.put(category.getCategoryId(), category);
227 // Is the list empty, but filled cache?
228 if (this.getAllProductCategories().isEmpty() && this.categoryCache.iterator().hasNext()) {
230 for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
232 this.getAllProductCategories().add(currentEntry.getValue());
236 this.getAllProductCategories().sort(new Comparator<Category>() {
238 public int compare (final Category category1, final Category category2) {
239 return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
245 this.setFilteredProductCategories(this.getAllProductCategories());
250 public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
251 // Validate parameter
252 if (null == categoryI18nKey) {
254 throw new NullPointerException("categoryI18nKey is null"); //NOI18N
255 } else if (categoryI18nKey.isEmpty()) {
257 throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
260 // Default is not the same
261 boolean isFound = false;
263 // Check all added,products
264 for (final Category category : this.getAllProductCategories()) {
265 // Is i18n key the same?
266 if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {
278 * Uniquely adds given category to "all" list
280 * @param category Category instance
282 private void uniqueAddProductCategory (final Category category) {
284 this.categoryCache.put(category.getCategoryId(), category);
287 final Iterator<Category> iterator = this.getAllProductCategories().iterator();
290 while (iterator.hasNext()) {
291 // Get current element
292 final Category currentCategory = iterator.next();
294 // Does primary key match?
295 if (Objects.equals(category.getCategoryId(), currentCategory.getCategoryId())) {
296 // Remove it and stop iterating
303 this.getAllProductCategories().add(category);