]> git.mxchange.org Git - jfinancials-war.git/blob
370f0c5beac477c29b4db7ab941a4e60c9e25f7a
[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.business.basicdata.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.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
36 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38
39 /**
40  * A view-scoped bean for product lists
41  * <p>
42  * @author Roland Haeder<roland@mxchange.org>
43  */
44 @Named ("basicDataListController")
45 @ViewScoped
46 public class FinancialsBasicDataListWebViewBean extends BaseFinancialsBean implements FinancialsBasicDataListWebViewController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 34_869_872_672_645L;
52
53         /**
54          * List of all basic company data
55          */
56         private final List<BasicData> allBasicData;
57
58         /**
59          * EJB for general basic business data purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote", description = "A stateless session bean for general purposes.")
62         private BasicCompanyDataSessionBeanRemote basicDataBean;
63
64         /**
65          * A list of all registered companies (globally)
66          */
67         @Inject
68         @NamedCache (cacheName = "basicDataCache")
69         private transient Cache<Long, BasicData> basicDataCache;
70
71         /**
72          * List of filtered basic company data
73          */
74         private List<BasicData> filteredBasicData;
75
76         /**
77          * Selected basic data
78          */
79         private BasicData selectedBasicData;
80
81         /**
82          * Default constructor
83          */
84         public FinancialsBasicDataListWebViewBean () {
85                 // Call super constructor
86                 super();
87
88                 // Init list
89                 this.allBasicData = new LinkedList<>();
90         }
91
92         /**
93          * Observers events being fired when an administrator has added company
94          * basic data.
95          * <p>
96          * @param event Event being fired
97          */
98         public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
99                 // Is the parameter valid?
100                 if (null == event) {
101                         // Throw NPE
102                         throw new NullPointerException("event is null"); //NOI18N
103                 } else if (event.getBasicData() == null) {
104                         // Throw NPE again
105                         throw new NullPointerException("event.basicData is null"); //NOI18N
106                 } else if (event.getBasicData().getBasicDataId() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("event.basicData.basicDataId is null"); //NOI18N
109                 } else if (event.getBasicData().getBasicDataId() < 1) {
110                         // Throw IAE
111                         throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); //NOI18N
112                 } else if (event.getBasicData().getCompanyShortName() == null) {
113                         // Throw NPE again
114                         throw new NullPointerException("event.basicData.companyName is null"); //NOI18N
115                 } else if (event.getBasicData().getCompanyShortName().isEmpty()) {
116                         // Throw IAE again
117                         throw new IllegalArgumentException("event.basicData.companyName is empty"); //NOI18N
118                 }
119
120                 // Add it to list
121                 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
122                 this.getAllBasicData().add(event.getBasicData());
123         }
124
125         @Override
126         public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
127                 // Validate parameter
128                 if (null == basicDataId) {
129                         // Throw NPE
130                         throw new NullPointerException("basicDataId is null"); //NOI18N
131                 } else if (basicDataId < 1) {
132                         // Throw IAE
133                         throw new IllegalArgumentException(MessageFormat.format("basicDataId={0} is invalid", basicDataId)); //NOI18N
134                 } else if (!this.basicDataCache.containsKey(basicDataId)) {
135                         // Not found
136                         throw new BasicDataNotFoundException(basicDataId);
137                 }
138
139                 // Get it from cache
140                 final BasicData basicData = this.basicDataCache.get(basicDataId);
141
142                 // Return it
143                 return basicData;
144         }
145
146         /**
147          * Getter for a list of all business contacts
148          * <p>
149          * @return A list of all business contacts
150          */
151         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
152         public List<BasicData> getAllBasicData () {
153                 return this.allBasicData;
154         }
155
156         /**
157          * Getter for filtered basic company data
158          * <p>
159          * @return Filtered basic company data
160          */
161         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
162         public List<BasicData> getFilteredBasicData () {
163                 return this.filteredBasicData;
164         }
165
166         /**
167          * Setter for filtered basic company data
168          * <p>
169          * @param filteredBasicData Filtered basic company data
170          */
171         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
172         public void setFilteredBasicData (final List<BasicData> filteredBasicData) {
173                 this.filteredBasicData = filteredBasicData;
174         }
175
176         /**
177          * Getter for selected basic data
178          * <p>
179          * @return Selected basic data
180          */
181         public BasicData getSelectedBasicData () {
182                 return this.selectedBasicData;
183         }
184
185         /**
186          * Setter for selected basic data
187          * <p>
188          * @param selectedBasicData Selected basic data
189          */
190         public void setSelectedBasicData (final BasicData selectedBasicData) {
191                 this.selectedBasicData = selectedBasicData;
192         }
193
194         /**
195          * Initializer method
196          */
197         @PostConstruct
198         public void initializeList () {
199                 // Is cache there?
200                 if (!this.basicDataCache.iterator().hasNext()) {
201                         // Get whole list from EJB
202                         final List<BasicData> basicDatas = this.basicDataBean.allBusinessBasicData();
203
204                         // Add all
205                         for (final BasicData basicData : basicDatas) {
206                                 // Add it to cache
207                                 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
208                         }
209                 }
210
211                 // Is cache there and list is not full?
212                 if ((this.getAllBasicData().isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
213                         // Get iterator
214                         final Iterator<Cache.Entry<Long, BasicData>> iterator = this.basicDataCache.iterator();
215
216                         // Build up list
217                         while (iterator.hasNext()) {
218                                 // GEt next element
219                                 final Cache.Entry<Long, BasicData> next = iterator.next();
220
221                                 // Add to list
222                                 this.getAllBasicData().add(next.getValue());
223                         }
224
225                         // Sort list
226                         this.getAllBasicData().sort(new Comparator<BasicData>() {
227                                 @Override
228                                 public int compare (final BasicData o1, final BasicData o2) {
229                                         return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
230                                 }
231                         });
232
233                         // Set full list
234                         this.setFilteredBasicData(this.getAllBasicData());
235                 }
236         }
237
238         @Override
239         public Boolean isCompanyNameUsed (final String companyName) {
240                 // Validate parameter
241                 if (null == companyName) {
242                         // Throw NPE
243                         throw new NullPointerException("companyName is null"); //NOI18N
244                 } else if (companyName.isEmpty()) {
245                         // Throw IAE
246                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
247                 }
248
249                 // Default is not found
250                 boolean isFound = false;
251
252                 // Check all entries
253                 for (final BasicData basicData : this.getAllBasicData()) {
254                         // Is same company name?
255                         if (Objects.equals(basicData.getCompanyName(), companyName)) {
256                                 // Found it
257                                 isFound = true;
258                                 break;
259                         }
260                 }
261
262                 // Return flag
263                 return isFound;
264         }
265
266         @Override
267         public Boolean isCompanyShortNameUsed (final String companyShortName) {
268                 // Validate parameter
269                 if (null == companyShortName) {
270                         // Throw NPE
271                         throw new NullPointerException("companyShortName is null"); //NOI18N
272                 } else if (companyShortName.isEmpty()) {
273                         // Throw IAE
274                         throw new IllegalArgumentException("companyShortName is empty"); //NOI18N
275                 }
276
277                 // Default is not found
278                 boolean isFound = false;
279
280                 // Check all entries
281                 for (final BasicData basicData : this.getAllBasicData()) {
282                         // Is same company name?
283                         if (Objects.equals(basicData.getCompanyShortName(), companyShortName)) {
284                                 // Found it
285                                 isFound = true;
286                                 break;
287                         }
288                 }
289
290                 // Return flag
291                 return isFound;
292         }
293
294         @Override
295         public Boolean isEmailAddressRegistered (final String emailAddress) {
296                 // Validate parameter
297                 if (null == emailAddress) {
298                         // Throw NPE
299                         throw new NullPointerException("emailAddress is null"); //NOI18N
300                 } else if (emailAddress.isEmpty()) {
301                         // Throw IAE
302                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
303                 }
304
305                 // Default is not found
306                 boolean isFound = false;
307
308                 // Check all entries
309                 for (final BasicData basicData : this.getAllBasicData()) {
310                         // Is email address used?
311                         if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {
312                                 // Found it
313                                 isFound = true;
314                                 break;
315                         }
316                 }
317
318                 // Return flag
319                 return isFound;
320         }
321
322 }