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