]> git.mxchange.org Git - jfinancials-war.git/blob
9b2b22a8bc99e08c4c03071911a62f4af94368ed
[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.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
27 import javax.ejb.EJB;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.ViewScoped;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
33 import org.mxchange.jproduct.events.category.added.AddedCategoryEvent;
34 import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException;
35 import org.mxchange.jproduct.model.category.Category;
36 import org.mxchange.jproduct.model.category.CategorySessionBeanRemote;
37
38 /**
39  * A view-scoped bean for product lists
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 @Named ("categoryListController")
44 @ViewScoped
45 public class FinancialsCategoryListWebViewBean extends BaseFinancialsBean implements FinancialsCategoryListWebViewController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 34_869_872_672_644L;
51
52         /**
53          * List of all categories
54          */
55         private final List<Category> allCategories;
56
57         /**
58          * EJB for general category stuff
59          */
60         @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote")
61         private CategorySessionBeanRemote categoryBean;
62
63         /**
64          * Cache for all product categories
65          */
66         @Inject
67         @NamedCache (cacheName = "categoryCache")
68         private transient Cache<Long, Category> categoryCache;
69
70         /**
71          * A list of filtered categories
72          */
73         private List<Category> filteredCategories;
74
75         /**
76          * Selected category
77          */
78         private Category selectedCategory;
79
80         /**
81          * Default constructor
82          */
83         public FinancialsCategoryListWebViewBean () {
84                 // Call super constructor
85                 super();
86
87                 // Init list
88                 this.allCategories = new LinkedList<>();
89         }
90
91         /**
92          * Observes events fired after a new product category has been added
93          * <p>
94          * @param event Event to be observed
95          */
96         public void afterCategoryAddedEvent (@Observes final AddedCategoryEvent event) {
97                 // Is all valid?
98                 if (null == event) {
99                         // Throw NPE
100                         throw new NullPointerException("event is null"); //NOI18N
101                 } else if (event.getAddedCategory() == null) {
102                         // Throw again ...
103                         throw new NullPointerException("event.addedCategory is null"); //NOI18N
104                 } else if (event.getAddedCategory().getCategoryId() == null) {
105                         // And again ...
106                         throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
107                 } else if (event.getAddedCategory().getCategoryId() < 1) {
108                         // Id is invalid
109                         throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
110                 }
111
112                 // Add the category
113                 this.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory());
114                 this.getAllCategories().add(event.getAddedCategory());
115         }
116
117         @Override
118         public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
119                 // Validate parameter
120                 if (null == categoryId) {
121                         // Throw NPE
122                         throw new NullPointerException("categoryId is null"); //NOI18N
123                 } else if (categoryId < 1) {
124                         // Throw IAE
125                         throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
126                 } else if (!this.categoryCache.containsKey(categoryId)) {
127                         // Not found
128                         throw new CategoryNotFoundException(categoryId);
129                 }
130
131                 // Get it from cache
132                 final Category category = this.categoryCache.get(categoryId);
133
134                 // Return it
135                 return category;
136         }
137
138         /**
139          * Getter for a list of all categories
140          * <p>
141          * @return All categories
142          */
143         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
144         public List<Category> getAllCategories () {
145                 // Return it
146                 return this.allCategories;
147         }
148
149         /**
150          * Getter for filtered category list
151          * <p>
152          * @return Filtered category list
153          */
154         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
155         public List<Category> getFilteredCategories () {
156                 return this.filteredCategories;
157         }
158
159         /**
160          * Setter for filtered category list
161          * <p>
162          * @param filteredCategories Filtered category list
163          */
164         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
165         public void setFilteredCategories (final List<Category> filteredCategories) {
166                 this.filteredCategories = filteredCategories;
167         }
168
169         /**
170          * Getter for selected category
171          * <p>
172          * @return Selected category
173          */
174         public Category getSelectedCategory () {
175                 return this.selectedCategory;
176         }
177
178         /**
179          * Setter for selected category
180          * <p>
181          * @param selectedCategory Selected category
182          */
183         public void setSelectedCategory (final Category selectedCategory) {
184                 this.selectedCategory = selectedCategory;
185         }
186
187         /**
188          * Initialization of this bean
189          */
190         @PostConstruct
191         public void init () {
192                 // Is cache there?
193                 if (!this.categoryCache.iterator().hasNext()) {
194                         // "Walk" through all entries and add to cache
195                         for (final Category category : this.categoryBean.allCategories()) {
196                                 // Add it by primary key
197                                 this.categoryCache.put(category.getCategoryId(), category);
198                         }
199                 }
200
201                 // Is the list empty, but filled cache?
202                 if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) {
203                         // Build up list
204                         for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
205                                 // Add to list
206                                 this.allCategories.add(currentEntry.getValue());
207                         }
208
209                         // Sort list
210                         this.allCategories.sort(new Comparator<Category>() {
211                                 @Override
212                                 public int compare (final Category category1, final Category category2) {
213                                         return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
214                                 }
215                         }
216                         );
217
218                         // Set whole list
219                         this.setFilteredCategories(this.getAllCategories());
220                 }
221         }
222
223         @Override
224         public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
225                 // Validate parameter
226                 if (null == categoryI18nKey) {
227                         // Throw NPE
228                         throw new NullPointerException("categoryI18nKey is null"); //NOI18N
229                 } else if (categoryI18nKey.isEmpty()) {
230                         // Throw IAE
231                         throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
232                 }
233
234                 // Default is not the same
235                 boolean isFound = false;
236
237                 // Check all added,products
238                 for (final Category category : this.getAllCategories()) {
239                         // Is i18n key the same?
240                         if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {
241                                 // Found it
242                                 isFound = true;
243                                 break;
244                         }
245                 }
246
247                 // Return flag
248                 return isFound;
249         }
250
251 }