]> git.mxchange.org Git - jfinancials-war.git/blob
3f7aac14199416e54f0f62cd1a8108742f6e41be
[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.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          * Default constructor
78          */
79         public FinancialsCategoryListWebViewBean () {
80                 // Call super constructor
81                 super();
82
83                 // Init list
84                 this.allCategories = new LinkedList<>();
85         }
86
87         /**
88          * Observes events fired after a new product category has been added
89          * <p>
90          * @param event Event to be observed
91          */
92         public void afterCategoryAddedEvent (@Observes final AddedCategoryEvent event) {
93                 // Is all valid?
94                 if (null == event) {
95                         // Throw NPE
96                         throw new NullPointerException("event is null"); //NOI18N
97                 } else if (event.getAddedCategory() == null) {
98                         // Throw again ...
99                         throw new NullPointerException("event.addedCategory is null"); //NOI18N
100                 } else if (event.getAddedCategory().getCategoryId() == null) {
101                         // And again ...
102                         throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
103                 } else if (event.getAddedCategory().getCategoryId() < 1) {
104                         // Id is invalid
105                         throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
106                 }
107
108                 // Add the category
109                 this.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory());
110                 this.getAllCategories().add(event.getAddedCategory());
111         }
112
113         @Override
114         public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException {
115                 // Validate parameter
116                 if (null == categoryId) {
117                         // Throw NPE
118                         throw new NullPointerException("categoryId is null"); //NOI18N
119                 } else if (categoryId < 1) {
120                         // Throw IAE
121                         throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N
122                 } else if (!this.categoryCache.containsKey(categoryId)) {
123                         // Not found
124                         throw new CategoryNotFoundException(categoryId);
125                 }
126
127                 // Get it from cache
128                 final Category category = this.categoryCache.get(categoryId);
129
130                 // Return it
131                 return category;
132         }
133
134         /**
135          * Getter for a list of all categories
136          * <p>
137          * @return All categories
138          */
139         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
140         public List<Category> getAllCategories () {
141                 // Return it
142                 return this.allCategories;
143         }
144
145         /**
146          * Getter for filtered category list
147          * <p>
148          * @return Filtered category list
149          */
150         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
151         public List<Category> getFilteredCategories () {
152                 return this.filteredCategories;
153         }
154
155         /**
156          * Setter for filtered category list
157          * <p>
158          * @param filteredCategories Filtered category list
159          */
160         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
161         public void setFilteredCategories (final List<Category> filteredCategories) {
162                 this.filteredCategories = filteredCategories;
163         }
164
165         /**
166          * Initialization of this bean
167          */
168         @PostConstruct
169         public void init () {
170                 // Is cache there?
171                 if (!this.categoryCache.iterator().hasNext()) {
172                         // Get whole list
173                         final List<Category> categories = this.categoryBean.allCategories();
174
175                         // "Walk" through all entries and add to cache
176                         for (final Category category : categories) {
177                                 // Add it by primary key
178                                 this.categoryCache.put(category.getCategoryId(), category);
179                         }
180                 }
181
182                 // Is the list empty, but filled cache?
183                 if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) {
184                         // Get iterator
185                         final Iterator<Cache.Entry<Long, Category>> iterator = this.categoryCache.iterator();
186
187                         // Build up list
188                         while (iterator.hasNext()) {
189                                 // GEt next element
190                                 final Cache.Entry<Long, Category> next = iterator.next();
191
192                                 // Add to list
193                                 this.allCategories.add(next.getValue());
194                         }
195
196                         // Sort list
197                         this.allCategories.sort(new Comparator<Category>() {
198                                 @Override
199                                 public int compare (final Category o1, final Category o2) {
200                                         return o1.getCategoryId() > o2.getCategoryId() ? 1 : o1.getCategoryId() < o2.getCategoryId() ? -1 : 0;
201                                 }
202                         }
203                         );
204
205                         // Set whole list
206                         this.setFilteredCategories(this.getAllCategories());
207                 }
208         }
209
210         @Override
211         public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) {
212                 // Validate parameter
213                 if (null == categoryI18nKey) {
214                         // Throw NPE
215                         throw new NullPointerException("categoryI18nKey is null"); //NOI18N
216                 } else if (categoryI18nKey.isEmpty()) {
217                         // Throw IAE
218                         throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
219                 }
220
221                 // Default is not the same
222                 boolean isFound = false;
223
224                 // Check all added,products
225                 for (final Category category : this.getAllCategories()) {
226                         // Is i18n key the same?
227                         if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) {
228                                 // Found it
229                                 isFound = true;
230                                 break;
231                         }
232                 }
233
234                 // Return flag
235                 return isFound;
236         }
237
238 }