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