2 * Copyright (C) 2017 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.business.headquarter.list;
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;
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;
40 * A list bean for headquarter
42 * @author Roland Häder<roland@mxchange.org>
44 @Named ("headquarterListController")
46 public class AddressbookHeadquarterListWebViewBean extends BaseAddressbookBean implements AddressbookHeadquarterListWebViewController {
51 private static final long serialVersionUID = 5_028_697_360_466L;
54 * A list of all headquarter
56 private final List<Headquarter> allHeadquarters;
59 * A list of filtered headquarter
61 private List<Headquarter> filteredHeadquarters;
64 * EJB for administrative purposes
66 @EJB (lookup = "java:global/addressbook-ejb/headquarter!org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote")
67 private HeadquarterSessionBeanRemote headquarterBean;
70 * A list of all headquarter (globally)
73 @NamedCache (cacheName = "headquarterCache")
74 private transient Cache<Long, Headquarter> headquarterCache;
77 * Currently selected headquarter
79 private Headquarter selectedHeadquarter;
84 public AddressbookHeadquarterListWebViewBean () {
85 // Call super constructor
89 this.allHeadquarters = new LinkedList<>();
93 * Observes events being fired when a branch office has been added.
95 * @param event Event being fired
97 * @throws NullPointerException If the parameter or it's carried instance is
99 * @throws IllegalArgumentException If the branchId is zero or lower
101 public void afterHeadquarterAddedEvent (@Observes final ObservableHeadquarterAddedEvent event) {
102 // Validate parameter
105 throw new NullPointerException("event is null"); //NOI18N
106 } else if (event.getHeadquarter() == null) {
108 throw new NullPointerException("event.headquarter is null"); //NOI18N
109 } else if (event.getHeadquarter().getHeadquarterId() == null) {
111 throw new NullPointerException("event.headquarter.branchId is null"); //NOI18N
112 } else if (event.getHeadquarter().getHeadquarterId() < 1) {
114 throw new IllegalArgumentException(MessageFormat.format("event.headquarter.branchId={0} is not valid", event.getHeadquarter().getHeadquarterId())); //NOI18N
117 // Add instance to cache
118 this.headquarterCache.put(event.getHeadquarter().getHeadquarterId(), event.getHeadquarter());
119 this.getAllHeadquarters().add(event.getHeadquarter());
123 public Headquarter findHeadquarterById (final Long headquarterId) throws HeadquarterNotFoundException {
124 // Validate parameter
125 if (null == headquarterId) {
127 throw new NullPointerException("headquarterId is null"); //NOI18N
128 } else if (headquarterId < 1) {
130 throw new IllegalArgumentException(MessageFormat.format("headquarterId={0} is invalid", headquarterId)); //NOI18N
131 } else if (!this.headquarterCache.containsKey(headquarterId)) {
133 throw new HeadquarterNotFoundException(headquarterId);
137 final Headquarter headquarter = this.headquarterCache.get(headquarterId);
144 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
145 public List<Headquarter> getAllHeadquarters () {
146 return this.allHeadquarters;
150 * Getter for a list of filtered headquarter
152 * @return Filtered headquarter
154 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
155 public List<Headquarter> getFilteredHeadquarters () {
156 return this.filteredHeadquarters;
160 * Setter for a list of filtered headquarter
162 * @param filteredHeadquarters Filtered headquarter
164 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
165 public void setFilteredHeadquarters (final List<Headquarter> filteredHeadquarters) {
166 this.filteredHeadquarters = filteredHeadquarters;
170 * Getter for selected headquarter
172 * @return Selected headquarter
174 public Headquarter getSelectedHeadquarter () {
175 return this.selectedHeadquarter;
179 * Setter for selected headquarter
181 * @param selectedHeadquarter Selected headquarter
183 public void setSelectedHeadquarter (final Headquarter selectedHeadquarter) {
184 this.selectedHeadquarter = selectedHeadquarter;
191 public void initializeList () {
193 if (!this.headquarterCache.iterator().hasNext()) {
195 for (final Headquarter headquarter : this.headquarterBean.fetchAllHeadquarters()) {
197 this.headquarterCache.put(headquarter.getHeadquarterId(), headquarter);
201 // Is the list empty, but filled cache?
202 if (this.getAllHeadquarters().isEmpty() && this.headquarterCache.iterator().hasNext()) {
204 for (final Cache.Entry<Long, Headquarter> currentEntry : this.headquarterCache) {
206 this.getAllHeadquarters().add(currentEntry.getValue());
210 this.getAllHeadquarters().sort(new Comparator<Headquarter>() {
212 public int compare (final Headquarter headquarter1, final Headquarter headquarter2) {
213 return headquarter1.getHeadquarterId() > headquarter2.getHeadquarterId() ? 1 : headquarter1.getHeadquarterId() < headquarter2.getHeadquarterId() ? -1 : 0;
220 public Boolean isCompanyNameUsed (final String companyName) {
221 // Validate parameter
222 if (null == companyName) {
224 throw new NullPointerException("companyName is null"); //NOI18N
225 } else if (companyName.isEmpty()) {
227 throw new IllegalArgumentException("companyName is empty"); //NOI18N
230 // Default is not found
231 boolean isFound = false;
234 for (final Headquarter headquarter : this.getAllHeadquarters()) {
235 // Is same company name?
236 if (Objects.equals(headquarter.getHeadquarterCompanyName(), companyName)) {
248 public Boolean isEmailAddressRegistered (final String emailAddress) {
249 // Validate parameter
250 if (null == emailAddress) {
252 throw new NullPointerException("emailAddress is null"); //NOI18N
253 } else if (emailAddress.isEmpty()) {
255 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
258 // Default is not found
259 boolean isFound = false;
262 for (final Headquarter headquarter : this.getAllHeadquarters()) {
263 // Is email address used?
264 if (Objects.equals(headquarter.getHeadquarterEmailAddress(), emailAddress)) {
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.
280 * @param headquarter Headquarter to check it's address
282 * @return Whether the address has been found
285 public boolean isHeadquarterCreatedByRequiredData (final Headquarter headquarter) {
286 // Default is not found
287 boolean isFound = false;
289 // Now check each entry
290 for (final Headquarter hq : this.getAllHeadquarters()) {
292 if (HeadquarterUtils.isSameAddress(hq, headquarter)) {