]> git.mxchange.org Git - jfinancials-war.git/blob
34aa90e33a573edeac2cd46f11b8ecb1e9d55297
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2020 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 the category
115                 this.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory());
116                 this.uniqueAddProductCategory(event.getAddedCategory());
117         }
118
119         /**
120          * Observes events fired after a new product category has been updated
121          * <p>
122          * @param event Event to be observed
123          */
124         public void afterAdminUpdatedCategoryEvent (@Observes final ObservableAdminUpdatedCategoryEvent event) {
125                 // Is all valid?
126                 if (null == event) {
127                         // Throw NPE
128                         throw new NullPointerException("event is null"); //NOI18N
129                 } else if (event.getUpdatedCategory() == null) {
130                         // Throw again ...
131                         throw new NullPointerException("event.updatedCategory is null"); //NOI18N
132                 } else if (event.getUpdatedCategory().getCategoryId() == null) {
133                         // And again ...
134                         throw new NullPointerException("event.updatedCategory.categoryId is null"); //NOI18N
135                 } else if (event.getUpdatedCategory().getCategoryId() < 1) {
136                         // Id is invalid
137                         throw new IllegalArgumentException(MessageFormat.format("event.updatedCategory.categoryId={0} is not valid.", event.getUpdatedCategory().getCategoryId())); //NOI18N
138                 }
139
140                 // Add the category
141                 this.categoryCache.put(event.getUpdatedCategory().getCategoryId(), event.getUpdatedCategory());
142                 this.uniqueAddProductCategory(event.getUpdatedCategory());
143         }
144
145         @Override
146         public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
147                 // Validate parameter
148                 if (null == categoryId) {
149                         // Throw NPE
150                         throw new NullPointerException("categoryId is null"); //NOI18N
151                 } else if (categoryId < 1) {
152                         // Throw IAE
153                         throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
154                 } else if (!this.categoryCache.containsKey(categoryId)) {
155                         // Not found
156                         throw new CategoryNotFoundException(categoryId);
157                 }
158
159                 // Get it from cache
160                 final Category category = this.categoryCache.get(categoryId);
161
162                 // Return it
163                 return category;
164         }
165
166         /**
167          * Getter for a list of all categories
168          * <p>
169          * @return All categories
170          */
171         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
172         public List<Category> getAllProductCategories () {
173                 // Return it
174                 return this.allProductCategories;
175         }
176
177         /**
178          * Getter for filtered category list
179          * <p>
180          * @return Filtered category list
181          */
182         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
183         public List<Category> getFilteredProductCategories () {
184                 return this.filteredProductCategories;
185         }
186
187         /**
188          * Setter for filtered category list
189          * <p>
190          * @param filteredProductCategories Filtered category list
191          */
192         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
193         public void setFilteredProductCategories (final List<Category> filteredProductCategories) {
194                 this.filteredProductCategories = filteredProductCategories;
195         }
196
197         /**
198          * Getter for selected category
199          * <p>
200          * @return Selected category
201          */
202         public Category getSelectedCategory () {
203                 return this.selectedCategory;
204         }
205
206         /**
207          * Setter for selected category
208          * <p>
209          * @param selectedCategory Selected category
210          */
211         public void setSelectedCategory (final Category selectedCategory) {
212                 this.selectedCategory = selectedCategory;
213         }
214
215         /**
216          * Initialization of this bean
217          */
218         @PostConstruct
219         public void initializeList () {
220                 // Is cache there?
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);
226                         }
227                 }
228
229                 // Is the list empty, but filled cache?
230                 if (this.getAllProductCategories().isEmpty() && this.categoryCache.iterator().hasNext()) {
231                         // Build up list
232                         for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
233                                 // Add to list
234                                 this.getAllProductCategories().add(currentEntry.getValue());
235                         }
236
237                         // Sort list
238                         this.getAllProductCategories().sort(new Comparator<Category>() {
239                                 @Override
240                                 public int compare (final Category category1, final Category category2) {
241                                         return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
242                                 }
243                         }
244                         );
245
246                         // Set whole list
247                         this.setFilteredProductCategories(this.getAllProductCategories());
248                 }
249         }
250
251         @Override
252         public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
253                 // Validate parameter
254                 if (null == categoryI18nKey) {
255                         // Throw NPE
256                         throw new NullPointerException("categoryI18nKey is null"); //NOI18N
257                 } else if (categoryI18nKey.isEmpty()) {
258                         // Throw IAE
259                         throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
260                 }
261
262                 // Default is not the same
263                 boolean isFound = false;
264
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)) {
269                                 // Found it
270                                 isFound = true;
271                                 break;
272                         }
273                 }
274
275                 // Return flag
276                 return isFound;
277         }
278
279         /**
280          * Uniquely adds given category to "all" list
281          * <p>
282          * @param category Category instance
283          */
284         private void uniqueAddProductCategory (final Category category) {
285                 // Get iterator from
286                 final Iterator<Category> iterator = this.getAllProductCategories().iterator();
287
288                 // Loop through all
289                 while (iterator.hasNext()) {
290                         // Get current element
291                         final Category currentCategory = iterator.next();
292
293                         // Does primary key match?
294                         if (Objects.equals(category.getCategoryId(), currentCategory.getCategoryId())) {
295                                 // Remove it and stop iterating
296                                 iterator.remove();
297                                 break;
298                         }
299                 }
300
301                 // Re-add it
302                 this.getAllProductCategories().add(category);
303         }
304
305 }