]> git.mxchange.org Git - jfinancials-war.git/blob
fc91ae9c2c3596a4e643cf95615db6696cf0fbbe
[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.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          * Default constructor
78          */
79         public FinancialsBasicDataListWebViewBean () {
80                 // Call super constructor
81                 super();
82
83                 // Init list
84                 this.allBasicData = new LinkedList<>();
85         }
86
87         /**
88          * Observers events being fired when an administrator has added company
89          * basic data.
90          * <p>
91          * @param event Event being fired
92          */
93         public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
94                 // Is the parameter valid?
95                 if (null == event) {
96                         // Throw NPE
97                         throw new NullPointerException("event is null"); //NOI18N
98                 } else if (event.getBasicData() == null) {
99                         // Throw NPE again
100                         throw new NullPointerException("event.basicData is null"); //NOI18N
101                 } else if (event.getBasicData().getBasicDataId() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("event.basicData.basicDataId is null"); //NOI18N
104                 } else if (event.getBasicData().getBasicDataId() < 1) {
105                         // Throw IAE
106                         throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); //NOI18N
107                 } else if (event.getBasicData().getCompanyName() == null) {
108                         // Throw NPE again
109                         throw new NullPointerException("event.basicData.companyName is null"); //NOI18N
110                 } else if (event.getBasicData().getCompanyName().isEmpty()) {
111                         // Throw IAE again
112                         throw new IllegalArgumentException("event.basicData.companyName is empty"); //NOI18N
113                 }
114
115                 // Add it to list
116                 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
117                 this.getAllBasicData().add(event.getBasicData());
118         }
119
120         @Override
121         public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
122                 // Validate parameter
123                 if (null == basicDataId) {
124                         // Throw NPE
125                         throw new NullPointerException("basicDataId is null"); //NOI18N
126                 } else if (basicDataId < 1) {
127                         // Throw IAE
128                         throw new IllegalArgumentException(MessageFormat.format("basicDataId={0} is invalid", basicDataId)); //NOI18N
129                 } else if (!this.basicDataCache.containsKey(basicDataId)) {
130                         // Not found
131                         throw new BasicDataNotFoundException(basicDataId);
132                 }
133
134                 // Get it from cache
135                 final BasicData basicData = this.basicDataCache.get(basicDataId);
136
137                 // Return it
138                 return basicData;
139         }
140
141         /**
142          * Getter for a list of all business contacts
143          * <p>
144          * @return A list of all business contacts
145          */
146         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
147         public List<BasicData> getAllBasicData () {
148                 return this.allBasicData;
149         }
150
151         /**
152          * Getter for filtered basic company data
153          * <p>
154          * @return Filtered basic company data
155          */
156         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
157         public List<BasicData> getFilteredBasicData () {
158                 return this.filteredBasicData;
159         }
160
161         /**
162          * Setter for filtered basic company data
163          * <p>
164          * @param filteredBasicData Filtered basic company data
165          */
166         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
167         public void setFilteredBasicData (final List<BasicData> filteredBasicData) {
168                 this.filteredBasicData = filteredBasicData;
169         }
170
171         /**
172          * Initializer method
173          */
174         @PostConstruct
175         public void initializeList () {
176                 // Is cache there?
177                 if (!this.basicDataCache.iterator().hasNext()) {
178                         // Get whole list
179                         final List<BasicData> basicDatas = this.basicDataBean.allBusinessBasicData();
180
181                         // Add all
182                         for (final BasicData basicData : basicDatas) {
183                                 // Add it to cache
184                                 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
185                         }
186                 }
187
188                 // Is cache there and list is not full?
189                 if ((this.getAllBasicData().isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
190                         // Get iterator
191                         final Iterator<Cache.Entry<Long, BasicData>> iterator = this.basicDataCache.iterator();
192
193                         // Build up list
194                         while (iterator.hasNext()) {
195                                 // GEt next element
196                                 final Cache.Entry<Long, BasicData> next = iterator.next();
197
198                                 // Add to list
199                                 this.getAllBasicData().add(next.getValue());
200                         }
201
202                         // Sort list
203                         this.getAllBasicData().sort(new Comparator<BasicData>() {
204                                 @Override
205                                 public int compare (final BasicData o1, final BasicData o2) {
206                                         return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
207                                 }
208                         });
209
210                         // Set full list
211                         this.setFilteredBasicData(this.getAllBasicData());
212                 }
213         }
214
215         @Override
216         public Boolean isCompanyNameUsed (final String companyName) {
217                 // Validate parameter
218                 if (null == companyName) {
219                         // Throw NPE
220                         throw new NullPointerException("companyName is null"); //NOI18N
221                 } else if (companyName.isEmpty()) {
222                         // Throw IAE
223                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
224                 }
225
226                 // Default is not found
227                 boolean isFound = false;
228
229                 // Check all entries
230                 for (final BasicData basicData : this.getAllBasicData()) {
231                         // Is same company name?
232                         if (Objects.equals(basicData.getCompanyName(), companyName)) {
233                                 // Found it
234                                 isFound = true;
235                                 break;
236                         }
237                 }
238
239                 // Return flag
240                 return isFound;
241         }
242
243         @Override
244         public Boolean isEmailAddressRegistered (final String emailAddress) {
245                 // Validate parameter
246                 if (null == emailAddress) {
247                         // Throw NPE
248                         throw new NullPointerException("emailAddress is null"); //NOI18N
249                 } else if (emailAddress.isEmpty()) {
250                         // Throw IAE
251                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
252                 }
253
254                 // Default is not found
255                 boolean isFound = false;
256
257                 // Check all entries
258                 for (final BasicData basicData : this.getAllBasicData()) {
259                         // Is email address used?
260                         if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {
261                                 // Found it
262                                 isFound = true;
263                                 break;
264                         }
265                 }
266
267                 // Return flag
268                 return isFound;
269         }
270
271 }