]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/product_category/list/FinancialsCategoryListWebViewBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / product_category / list / FinancialsCategoryListWebViewBean.java
1 /*
2  * Copyright (C) 2017 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.product_category.list;
18
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;
28 import javax.ejb.EJB;
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;
39
40 /**
41  * A view-scoped bean for product lists
42  * <p>
43  * @author Roland Haeder<roland@mxchange.org>
44  */
45 @Named ("productCategoryListController")
46 @ViewScoped
47 public class FinancialsCategoryListWebViewBean extends BaseFinancialsBean implements FinancialsCategoryListWebViewController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 34_869_872_672_644L;
53
54         /**
55          * List of all categories
56          */
57         private final List<Category> allProductCategories;
58
59         /**
60          * EJB for general category stuff
61          */
62         @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote")
63         private CategorySessionBeanRemote categoryBean;
64
65         /**
66          * Cache for all product categories
67          */
68         @Inject
69         @NamedCache (cacheName = "categoryCache")
70         private transient Cache<Long, Category> categoryCache;
71
72         /**
73          * A list of filtered categories
74          */
75         private List<Category> filteredProductCategories;
76
77         /**
78          * Selected category
79          */
80         private Category selectedCategory;
81
82         /**
83          * Default constructor
84          */
85         public FinancialsCategoryListWebViewBean () {
86                 // Call super constructor
87                 super();
88
89                 // Init list
90                 this.allProductCategories = new LinkedList<>();
91         }
92
93         /**
94          * Observes events fired after a new product category has been added
95          * <p>
96          * @param event Event to be observed
97          */
98         public void afterAdminAddedCategoryEvent (@Observes final ObservableAdminAddedCategoryEvent event) {
99                 // Is all valid?
100                 if (null == event) {
101                         // Throw NPE
102                         throw new NullPointerException("event is null"); //NOI18N
103                 } else if (event.getAddedCategory() == null) {
104                         // Throw again ...
105                         throw new NullPointerException("event.addedCategory is null"); //NOI18N
106                 } else if (event.getAddedCategory().getCategoryId() == null) {
107                         // And again ...
108                         throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
109                 } else if (event.getAddedCategory().getCategoryId() < 1) {
110                         // Id is invalid
111                         throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
112                 }
113
114                 // Add category
115                 this.uniqueAddProductCategory(event.getAddedCategory());
116         }
117
118         /**
119          * Observes events fired after a new product category has been updated
120          * <p>
121          * @param event Event to be observed
122          */
123         public void afterAdminUpdatedCategoryEvent (@Observes final ObservableAdminUpdatedCategoryEvent event) {
124                 // Is all valid?
125                 if (null == event) {
126                         // Throw NPE
127                         throw new NullPointerException("event is null"); //NOI18N
128                 } else if (event.getUpdatedCategory() == null) {
129                         // Throw again ...
130                         throw new NullPointerException("event.updatedCategory is null"); //NOI18N
131                 } else if (event.getUpdatedCategory().getCategoryId() == null) {
132                         // And again ...
133                         throw new NullPointerException("event.updatedCategory.categoryId is null"); //NOI18N
134                 } else if (event.getUpdatedCategory().getCategoryId() < 1) {
135                         // Id is invalid
136                         throw new IllegalArgumentException(MessageFormat.format("event.updatedCategory.categoryId={0} is not valid.", event.getUpdatedCategory().getCategoryId())); //NOI18N
137                 }
138
139                 // Update category
140                 this.uniqueAddProductCategory(event.getUpdatedCategory());
141         }
142
143         @Override
144         public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
145                 // Validate parameter
146                 if (null == categoryId) {
147                         // Throw NPE
148                         throw new NullPointerException("categoryId is null"); //NOI18N
149                 } else if (categoryId < 1) {
150                         // Throw IAE
151                         throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
152                 } else if (!this.categoryCache.containsKey(categoryId)) {
153                         // Not found
154                         throw new CategoryNotFoundException(categoryId);
155                 }
156
157                 // Get it from cache
158                 final Category category = this.categoryCache.get(categoryId);
159
160                 // Return it
161                 return category;
162         }
163
164         /**
165          * Getter for a list of all categories
166          * <p>
167          * @return All categories
168          */
169         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
170         public List<Category> getAllProductCategories () {
171                 // Return it
172                 return this.allProductCategories;
173         }
174
175         /**
176          * Getter for filtered category list
177          * <p>
178          * @return Filtered category list
179          */
180         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
181         public List<Category> getFilteredProductCategories () {
182                 return this.filteredProductCategories;
183         }
184
185         /**
186          * Setter for filtered category list
187          * <p>
188          * @param filteredProductCategories Filtered category list
189          */
190         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
191         public void setFilteredProductCategories (final List<Category> filteredProductCategories) {
192                 this.filteredProductCategories = filteredProductCategories;
193         }
194
195         /**
196          * Getter for selected category
197          * <p>
198          * @return Selected category
199          */
200         public Category getSelectedCategory () {
201                 return this.selectedCategory;
202         }
203
204         /**
205          * Setter for selected category
206          * <p>
207          * @param selectedCategory Selected category
208          */
209         public void setSelectedCategory (final Category selectedCategory) {
210                 this.selectedCategory = selectedCategory;
211         }
212
213         /**
214          * Initialization of this bean
215          */
216         @PostConstruct
217         public void initializeList () {
218                 // Is cache there?
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);
224                         }
225                 }
226
227                 // Is the list empty, but filled cache?
228                 if (this.getAllProductCategories().isEmpty() && this.categoryCache.iterator().hasNext()) {
229                         // Build up list
230                         for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
231                                 // Add to list
232                                 this.getAllProductCategories().add(currentEntry.getValue());
233                         }
234
235                         // Sort list
236                         this.getAllProductCategories().sort(new Comparator<Category>() {
237                                 @Override
238                                 public int compare (final Category category1, final Category category2) {
239                                         return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
240                                 }
241                         }
242                         );
243
244                         // Set whole list
245                         this.setFilteredProductCategories(this.getAllProductCategories());
246                 }
247         }
248
249         @Override
250         public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
251                 // Validate parameter
252                 if (null == categoryI18nKey) {
253                         // Throw NPE
254                         throw new NullPointerException("categoryI18nKey is null"); //NOI18N
255                 } else if (categoryI18nKey.isEmpty()) {
256                         // Throw IAE
257                         throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
258                 }
259
260                 // Default is not the same
261                 boolean isFound = false;
262
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)) {
267                                 // Found it
268                                 isFound = true;
269                                 break;
270                         }
271                 }
272
273                 // Return flag
274                 return isFound;
275         }
276
277         /**
278          * Uniquely adds given category to "all" list
279          * <p>
280          * @param category Category instance
281          */
282         private void uniqueAddProductCategory (final Category category) {
283                 // Update cache
284                 this.categoryCache.put(category.getCategoryId(), category);
285
286                 // Get iterator from
287                 final Iterator<Category> iterator = this.getAllProductCategories().iterator();
288
289                 // Loop through all
290                 while (iterator.hasNext()) {
291                         // Get current element
292                         final Category currentCategory = iterator.next();
293
294                         // Does primary key match?
295                         if (Objects.equals(category.getCategoryId(), currentCategory.getCategoryId())) {
296                                 // Remove it and stop iterating
297                                 iterator.remove();
298                                 break;
299                         }
300                 }
301
302                 // Re-add it
303                 this.getAllProductCategories().add(category);
304         }
305
306 }