]> git.mxchange.org Git - addressbook-war.git/blob
fe1342ad832f97d3ad53ba11d12e5b069ed124f5
[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.headquarter.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.headquarter.added.ObservableHeadquarterAddedEvent;
33 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterNotFoundException;
34 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
35 import org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote;
36 import org.mxchange.jcontactsbusiness.model.utils.HeadquarterUtils;
37 import org.mxchange.addressbook.beans.BaseAddressbookBean;
38
39 /**
40  * A list bean for headquarter
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("headquarterListController")
45 @ViewScoped
46 public class AddressbookHeadquarterListWebViewBean extends BaseAddressbookBean implements AddressbookHeadquarterListWebViewController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 5_028_697_360_466L;
52
53         /**
54          * A list of all headquarter
55          */
56         private final List<Headquarter> allHeadquarters;
57
58         /**
59          * A list of filtered headquarter
60          */
61         private List<Headquarter> filteredHeadquarters;
62
63         /**
64          * EJB for administrative purposes
65          */
66         @EJB (lookup = "java:global/addressbook-ejb/headquarter!org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote")
67         private HeadquarterSessionBeanRemote headquarterBean;
68
69         /**
70          * A list of all headquarter (globally)
71          */
72         @Inject
73         @NamedCache (cacheName = "headquarterCache")
74         private transient Cache<Long, Headquarter> headquarterCache;
75
76         /**
77          * Currently selected headquarter
78          */
79         private Headquarter selectedHeadquarter;
80
81         /**
82          * Default constructor
83          */
84         public AddressbookHeadquarterListWebViewBean () {
85                 // Call super constructor
86                 super();
87
88                 // Init list
89                 this.allHeadquarters = new LinkedList<>();
90         }
91
92         /**
93          * Observes events being fired when a branch office has been added.
94          * <p>
95          * @param event Event being fired
96          * <p>
97          * @throws NullPointerException If the parameter or it's carried instance is
98          * null
99          * @throws IllegalArgumentException If the branchId is zero or lower
100          */
101         public void afterHeadquarterAddedEvent (@Observes final ObservableHeadquarterAddedEvent event) {
102                 // Validate parameter
103                 if (null == event) {
104                         // Throw NPE
105                         throw new NullPointerException("event is null"); //NOI18N
106                 } else if (event.getHeadquarter() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("event.headquarter is null"); //NOI18N
109                 } else if (event.getHeadquarter().getHeadquarterId() == null) {
110                         // Throw it again
111                         throw new NullPointerException("event.headquarter.branchId is null"); //NOI18N
112                 } else if (event.getHeadquarter().getHeadquarterId() < 1) {
113                         // Throw IAE
114                         throw new IllegalArgumentException(MessageFormat.format("event.headquarter.branchId={0} is not valid", event.getHeadquarter().getHeadquarterId())); //NOI18N
115                 }
116
117                 // Add instance to cache
118                 this.headquarterCache.put(event.getHeadquarter().getHeadquarterId(), event.getHeadquarter());
119                 this.getAllHeadquarters().add(event.getHeadquarter());
120         }
121
122         @Override
123         public Headquarter findHeadquarterById (final Long headquarterId) throws HeadquarterNotFoundException {
124                 // Validate parameter
125                 if (null == headquarterId) {
126                         // Throw NPE
127                         throw new NullPointerException("headquarterId is null"); //NOI18N
128                 } else if (headquarterId < 1) {
129                         // Throw IAE
130                         throw new IllegalArgumentException(MessageFormat.format("headquarterId={0} is invalid", headquarterId)); //NOI18N
131                 } else if (!this.headquarterCache.containsKey(headquarterId)) {
132                         // Not found
133                         throw new HeadquarterNotFoundException(headquarterId);
134                 }
135
136                 // Get it from cache
137                 final Headquarter headquarter = this.headquarterCache.get(headquarterId);
138
139                 // Return it
140                 return headquarter;
141         }
142
143         @Override
144         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
145         public List<Headquarter> getAllHeadquarters () {
146                 return this.allHeadquarters;
147         }
148
149         /**
150          * Getter for a list of filtered headquarter
151          * <p>
152          * @return Filtered headquarter
153          */
154         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
155         public List<Headquarter> getFilteredHeadquarters () {
156                 return this.filteredHeadquarters;
157         }
158
159         /**
160          * Setter for a list of filtered headquarter
161          * <p>
162          * @param filteredHeadquarters Filtered headquarter
163          */
164         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
165         public void setFilteredHeadquarters (final List<Headquarter> filteredHeadquarters) {
166                 this.filteredHeadquarters = filteredHeadquarters;
167         }
168
169         /**
170          * Getter for selected headquarter
171          * <p>
172          * @return Selected headquarter
173          */
174         public Headquarter getSelectedHeadquarter () {
175                 return this.selectedHeadquarter;
176         }
177
178         /**
179          * Setter for selected headquarter
180          * <p>
181          * @param selectedHeadquarter Selected headquarter
182          */
183         public void setSelectedHeadquarter (final Headquarter selectedHeadquarter) {
184                 this.selectedHeadquarter = selectedHeadquarter;
185         }
186
187         /**
188          * Initializer method
189          */
190         @PostConstruct
191         public void initializeList () {
192                 // Is cache there?
193                 if (!this.headquarterCache.iterator().hasNext()) {
194                         // Add all
195                         for (final Headquarter headquarter : this.headquarterBean.fetchAllHeadquarters()) {
196                                 // Add it to cache
197                                 this.headquarterCache.put(headquarter.getHeadquarterId(), headquarter);
198                         }
199                 }
200
201                 // Is the list empty, but filled cache?
202                 if (this.getAllHeadquarters().isEmpty() && this.headquarterCache.iterator().hasNext()) {
203                         // Build up list
204                         for (final Cache.Entry<Long, Headquarter> currentEntry : this.headquarterCache) {
205                                 // Add to list
206                                 this.getAllHeadquarters().add(currentEntry.getValue());
207                         }
208
209                         // Sort list
210                         this.getAllHeadquarters().sort(new Comparator<Headquarter>() {
211                                 @Override
212                                 public int compare (final Headquarter headquarter1, final Headquarter headquarter2) {
213                                         return headquarter1.getHeadquarterId() > headquarter2.getHeadquarterId() ? 1 : headquarter1.getHeadquarterId() < headquarter2.getHeadquarterId() ? -1 : 0;
214                                 }
215                         });
216                 }
217         }
218
219         @Override
220         public Boolean isCompanyNameUsed (final String companyName) {
221                 // Validate parameter
222                 if (null == companyName) {
223                         // Throw NPE
224                         throw new NullPointerException("companyName is null"); //NOI18N
225                 } else if (companyName.isEmpty()) {
226                         // Throw IAE
227                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
228                 }
229
230                 // Default is not found
231                 boolean isFound = false;
232
233                 // Check all entries
234                 for (final Headquarter headquarter : this.getAllHeadquarters()) {
235                         // Is same company name?
236                         if (Objects.equals(headquarter.getHeadquarterCompanyName(), companyName)) {
237                                 // Found it
238                                 isFound = true;
239                                 break;
240                         }
241                 }
242
243                 // Return flag
244                 return isFound;
245         }
246
247         @Override
248         public Boolean isEmailAddressRegistered (final String emailAddress) {
249                 // Validate parameter
250                 if (null == emailAddress) {
251                         // Throw NPE
252                         throw new NullPointerException("emailAddress is null"); //NOI18N
253                 } else if (emailAddress.isEmpty()) {
254                         // Throw IAE
255                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
256                 }
257
258                 // Default is not found
259                 boolean isFound = false;
260
261                 // Check all entries
262                 for (final Headquarter headquarter : this.getAllHeadquarters()) {
263                         // Is email address used?
264                         if (Objects.equals(headquarter.getHeadquarterEmailAddress(), emailAddress)) {
265                                 // Found it
266                                 isFound = true;
267                                 break;
268                         }
269                 }
270
271                 // Return flag
272                 return isFound;
273         }
274
275         /**
276          * Checks whether the given headquarter' address is already found in local
277          * cache. Please note that this method fully relies on the cache, so you
278          * must always fire proper events that add/update/delete entries in cache.
279          * <p>
280          * @param headquarter Headquarter to check it's address
281          * <p>
282          * @return Whether the address has been found
283          */
284         @Override
285         public boolean isHeadquarterCreatedByRequiredData (final Headquarter headquarter) {
286                 // Default is not found
287                 boolean isFound = false;
288
289                 // Now check each entry
290                 for (final Headquarter hq : this.getAllHeadquarters()) {
291                         // Is same address?
292                         if (HeadquarterUtils.isSameAddress(hq, headquarter)) {
293                                 // Found one
294                                 isFound = true;
295                                 break;
296                         }
297                 }
298
299                 // Return flag
300                 return isFound;
301         }
302
303 }