]> git.mxchange.org Git - addressbook-war.git/blob
8326d3cc5e0461dbc0288c0e0746cde0c980c411
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2017 - 2022 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.addressbook.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.addressbook.beans.BaseAddressbookBean;
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
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 AddressbookBasicDataListWebViewBean extends BaseAddressbookBean implements AddressbookBasicDataListWebViewController {
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/addressbook-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 AddressbookBasicDataListWebViewBean () {
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                         // Add all
201                         for (final BasicData basicData : this.basicDataBean.fetchAllBusinessBasicData()) {
202                                 // Add it to cache
203                                 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
204                         }
205                 }
206
207                 // Is cache there and list is not full?
208                 if ((this.getAllBasicData().isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
209                         // Build up list
210                         for (final Cache.Entry<Long, BasicData> currentEntry : this.basicDataCache) {
211                                 // Add to list
212                                 this.getAllBasicData().add(currentEntry.getValue());
213                         }
214
215                         // Sort list
216                         this.getAllBasicData().sort(new Comparator<BasicData>() {
217                                 @Override
218                                 public int compare (final BasicData basicData1, final BasicData basicData2) {
219                                         return basicData1.getBasicDataId() > basicData2.getBasicDataId() ? 1 : basicData1.getBasicDataId() < basicData2.getBasicDataId() ? -1 : 0;
220                                 }
221                         });
222
223                         // Set full list
224                         this.setFilteredBasicData(this.getAllBasicData());
225                 }
226         }
227
228         @Override
229         public Boolean isCompanyNameUsed (final String companyName) {
230                 // Validate parameter
231                 if (null == companyName) {
232                         // Throw NPE
233                         throw new NullPointerException("companyName is null"); //NOI18N
234                 } else if (companyName.isEmpty()) {
235                         // Throw IAE
236                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
237                 }
238
239                 // Default is not found
240                 boolean isFound = false;
241
242                 // Check all entries
243                 for (final BasicData basicData : this.getAllBasicData()) {
244                         // Is same company name?
245                         if (Objects.equals(basicData.getCompanyName(), companyName)) {
246                                 // Found it
247                                 isFound = true;
248                                 break;
249                         }
250                 }
251
252                 // Return flag
253                 return isFound;
254         }
255
256         @Override
257         public boolean isCompanyRoadNumberUsed (final String companyRoadNumber) {
258                 // Validate parameter
259                 if (null == companyRoadNumber) {
260                         // Throw NPE
261                         throw new NullPointerException("companyRoadNumber is null"); //NOI18N
262                 } else if (companyRoadNumber.isEmpty()) {
263                         // Throw IAE
264                         throw new IllegalArgumentException("companyRoadNumber is empty"); //NOI18N
265                 }
266
267                 // Default is not found
268                 boolean isFound = false;
269
270                 // Check all entries
271                 for (final BasicData basicData : this.getAllBasicData()) {
272                         // Is same company name?
273                         if (Objects.equals(basicData.getCompanyRoadNumber(), companyRoadNumber)) {
274                                 // Found it
275                                 isFound = true;
276                                 break;
277                         }
278                 }
279
280                 // Return flag
281                 return isFound;
282         }
283
284         @Override
285         public Boolean isCompanyShortNameUsed (final String companyShortName) {
286                 // Validate parameter
287                 if (null == companyShortName) {
288                         // Throw NPE
289                         throw new NullPointerException("companyShortName is null"); //NOI18N
290                 } else if (companyShortName.isEmpty()) {
291                         // Throw IAE
292                         throw new IllegalArgumentException("companyShortName is empty"); //NOI18N
293                 }
294
295                 // Default is not found
296                 boolean isFound = false;
297
298                 // Check all entries
299                 for (final BasicData basicData : this.getAllBasicData()) {
300                         // Is same company name?
301                         if (Objects.equals(basicData.getCompanyShortName(), companyShortName)) {
302                                 // Found it
303                                 isFound = true;
304                                 break;
305                         }
306                 }
307
308                 // Return flag
309                 return isFound;
310         }
311
312         @Override
313         public Boolean isEmailAddressRegistered (final String emailAddress) {
314                 // Validate parameter
315                 if (null == emailAddress) {
316                         // Throw NPE
317                         throw new NullPointerException("emailAddress is null"); //NOI18N
318                 } else if (emailAddress.isEmpty()) {
319                         // Throw IAE
320                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
321                 }
322
323                 // Default is not found
324                 boolean isFound = false;
325
326                 // Check all entries
327                 for (final BasicData basicData : this.getAllBasicData()) {
328                         // Is email address used?
329                         if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {
330                                 // Found it
331                                 isFound = true;
332                                 break;
333                         }
334                 }
335
336                 // Return flag
337                 return isFound;
338         }
339
340 }