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.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory());
116 this.uniqueAddProductCategory(event.getAddedCategory());
120 * Observes events fired after a new product category has been updated
122 * @param event Event to be observed
124 public void afterAdminUpdatedCategoryEvent (@Observes final ObservableAdminUpdatedCategoryEvent event) {
128 throw new NullPointerException("event is null"); //NOI18N
129 } else if (event.getUpdatedCategory() == null) {
131 throw new NullPointerException("event.updatedCategory is null"); //NOI18N
132 } else if (event.getUpdatedCategory().getCategoryId() == null) {
134 throw new NullPointerException("event.updatedCategory.categoryId is null"); //NOI18N
135 } else if (event.getUpdatedCategory().getCategoryId() < 1) {
137 throw new IllegalArgumentException(MessageFormat.format("event.updatedCategory.categoryId={0} is not valid.", event.getUpdatedCategory().getCategoryId())); //NOI18N
141 this.categoryCache.put(event.getUpdatedCategory().getCategoryId(), event.getUpdatedCategory());
142 this.uniqueAddProductCategory(event.getUpdatedCategory());
146 public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
147 // Validate parameter
148 if (null == categoryId) {
150 throw new NullPointerException("categoryId is null"); //NOI18N
151 } else if (categoryId < 1) {
153 throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
154 } else if (!this.categoryCache.containsKey(categoryId)) {
156 throw new CategoryNotFoundException(categoryId);
160 final Category category = this.categoryCache.get(categoryId);
167 * Getter for a list of all categories
169 * @return All categories
171 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
172 public List<Category> getAllProductCategories () {
174 return this.allProductCategories;
178 * Getter for filtered category list
180 * @return Filtered category list
182 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
183 public List<Category> getFilteredProductCategories () {
184 return this.filteredProductCategories;
188 * Setter for filtered category list
190 * @param filteredProductCategories Filtered category list
192 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
193 public void setFilteredProductCategories (final List<Category> filteredProductCategories) {
194 this.filteredProductCategories = filteredProductCategories;
198 * Getter for selected category
200 * @return Selected category
202 public Category getSelectedCategory () {
203 return this.selectedCategory;
207 * Setter for selected category
209 * @param selectedCategory Selected category
211 public void setSelectedCategory (final Category selectedCategory) {
212 this.selectedCategory = selectedCategory;
216 * Initialization of this bean
219 public void initializeList () {
221 if (!this.categoryCache.iterator().hasNext()) {
222 // "Walk" through all entries and add to cache
223 for (final Category category : this.categoryBean.fetchAllProductCategories()) {
224 // Add it by primary key
225 this.categoryCache.put(category.getCategoryId(), category);
229 // Is the list empty, but filled cache?
230 if (this.getAllProductCategories().isEmpty() && this.categoryCache.iterator().hasNext()) {
232 for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
234 this.getAllProductCategories().add(currentEntry.getValue());
238 this.getAllProductCategories().sort(new Comparator<Category>() {
240 public int compare (final Category category1, final Category category2) {
241 return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
247 this.setFilteredProductCategories(this.getAllProductCategories());
252 public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
253 // Validate parameter
254 if (null == categoryI18nKey) {
256 throw new NullPointerException("categoryI18nKey is null"); //NOI18N
257 } else if (categoryI18nKey.isEmpty()) {
259 throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
262 // Default is not the same
263 boolean isFound = false;
265 // Check all added,products
266 for (final Category category : this.getAllProductCategories()) {
267 // Is i18n key the same?
268 if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {
280 * Uniquely adds given category to "all" list
282 * @param category Category instance
284 private void uniqueAddProductCategory (final Category category) {
286 final Iterator<Category> iterator = this.getAllProductCategories().iterator();
289 while (iterator.hasNext()) {
290 // Get current element
291 final Category currentCategory = iterator.next();
293 // Does primary key match?
294 if (Objects.equals(category.getCategoryId(), currentCategory.getCategoryId())) {
295 // Remove it and stop iterating
302 this.getAllProductCategories().add(category);