]> git.mxchange.org Git - jfinancials-war.git/blob
0416b3d5a8b744a080fa7c8d8eb00e08e15c7a80
[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                         // Get whole list from EJB
195                         final List<Category> categories = this.categoryBean.allCategories();
196
197                         // "Walk" through all entries and add to cache
198                         for (final Category category : categories) {
199                                 // Add it by primary key
200                                 this.categoryCache.put(category.getCategoryId(), category);
201                         }
202                 }
203
204                 // Is the list empty, but filled cache?
205                 if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) {
206                         // Build up list
207                         for (final Cache.Entry<Long, Category> currentEntry : this.categoryCache) {
208                                 // Add to list
209                                 this.allCategories.add(currentEntry.getValue());
210                         }
211
212                         // Sort list
213                         this.allCategories.sort(new Comparator<Category>() {
214                                 @Override
215                                 public int compare (final Category category1, final Category category2) {
216                                         return category1.getCategoryId() > category2.getCategoryId() ? 1 : category1.getCategoryId() < category2.getCategoryId() ? -1 : 0;
217                                 }
218                         }
219                         );
220
221                         // Set whole list
222                         this.setFilteredCategories(this.getAllCategories());
223                 }
224         }
225
226         @Override
227         public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
228                 // Validate parameter
229                 if (null == categoryI18nKey) {
230                         // Throw NPE
231                         throw new NullPointerException("categoryI18nKey is null"); //NOI18N
232                 } else if (categoryI18nKey.isEmpty()) {
233                         // Throw IAE
234                         throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
235                 }
236
237                 // Default is not the same
238                 boolean isFound = false;
239
240                 // Check all added,products
241                 for (final Category category : this.getAllCategories()) {
242                         // Is i18n key the same?
243                         if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {
244                                 // Found it
245                                 isFound = true;
246                                 break;
247                         }
248                 }
249
250                 // Return flag
251                 return isFound;
252         }
253
254 }