2 * Copyright (C) 2017 - 2020 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.jfinancials.beans.business.headquarter;
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.context.RequestScoped;
29 import javax.enterprise.event.Observes;
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.jfinancials.beans.BaseFinancialsBean;
39 * A general bean for headquarter
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("headquarterController")
45 public class FinancialsHeadquarterWebRequestBean extends BaseFinancialsBean implements FinancialsHeadquarterWebRequestController {
50 private static final long serialVersionUID = 5_028_697_360_465L;
53 * A list of all headquarter
55 private final List<Headquarter> allHeadquarter;
58 * A list of filtered headquarter
60 private List<Headquarter> filteredHeadquarter;
63 * EJB for administrative purposes
65 @EJB (lookup = "java:global/jfinancials-ejb/headquarter!org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote")
66 private HeadquarterSessionBeanRemote headquarterBean;
69 * A list of all headquarter (globally)
72 @NamedCache (cacheName = "headquarterCache")
73 private Cache<Long, Headquarter> headquarterCache;
78 public FinancialsHeadquarterWebRequestBean () {
79 // Call super constructor
83 this.allHeadquarter = new LinkedList<>();
87 * Observes events being fired when a branch office has been added.
89 * @param event Event being fired
91 * @throws NullPointerException If the parameter or it's carried instance is
93 * @throws IllegalArgumentException If the branchId is zero or lower
95 public void afterHeadquarterAddedEvent (@Observes final ObservableHeadquarterAddedEvent event) {
99 throw new NullPointerException("event is null"); //NOI18N
100 } else if (event.getHeadquarter() == null) {
102 throw new NullPointerException("event.headquarter is null"); //NOI18N
103 } else if (event.getHeadquarter().getHeadquarterId() == null) {
105 throw new NullPointerException("event.headquarter.branchId is null"); //NOI18N
106 } else if (event.getHeadquarter().getHeadquarterId() < 1) {
108 throw new IllegalArgumentException(MessageFormat.format("event.headquarter.branchId={0} is not valid", event.getHeadquarter().getHeadquarterId())); //NOI18N
111 // Add instance to cache
112 this.headquarterCache.put(event.getHeadquarter().getHeadquarterId(), event.getHeadquarter());
113 this.allHeadquarter.add(event.getHeadquarter());
117 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
118 public List<Headquarter> allHeadquarter () {
119 return this.allHeadquarter;
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 * Getter for a list of filtered headquarter
146 * @return Filtered headquarter
148 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149 public List<Headquarter> getFilteredHeadquarter () {
150 return this.filteredHeadquarter;
154 * Setter for a list of filtered headquarter
156 * @param filteredHeadquarter Filtered headquarter
158 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159 public void setFilteredHeadquarter (final List<Headquarter> filteredHeadquarter) {
160 this.filteredHeadquarter = filteredHeadquarter;
167 public void initializeList () {
169 if (!this.headquarterCache.iterator().hasNext()) {
170 // Get whole list from EJB
171 final List<Headquarter> Headquarter = this.headquarterBean.allHeadquarters();
174 for (final Headquarter headquarter : Headquarter) {
176 this.headquarterCache.put(headquarter.getHeadquarterId(), headquarter);
180 // Is the list empty, but filled cache?
181 if (this.allHeadquarter.isEmpty() && this.headquarterCache.iterator().hasNext()) {
183 for (final Cache.Entry<Long, Headquarter> currentEntry : this.headquarterCache) {
185 this.allHeadquarter.add(currentEntry.getValue());
189 this.allHeadquarter.sort(new Comparator<Headquarter>() {
191 public int compare (final Headquarter headquarter1, final Headquarter headquarter2) {
192 return headquarter1.getHeadquarterId() > headquarter2.getHeadquarterId() ? 1 : headquarter1.getHeadquarterId() < headquarter2.getHeadquarterId() ? -1 : 0;
199 public Boolean isCompanyNameUsed (final String companyName) {
200 // Validate parameter
201 if (null == companyName) {
203 throw new NullPointerException("companyName is null"); //NOI18N
204 } else if (companyName.isEmpty()) {
206 throw new IllegalArgumentException("companyName is empty"); //NOI18N
209 // Default is not found
210 boolean isFound = false;
213 for (final Headquarter headquarter : this.allHeadquarter()) {
214 // Is same company name?
215 if (Objects.equals(headquarter.getHeadquarterCompanyName(), companyName)) {
227 public Boolean isEmailAddressRegistered (final String emailAddress) {
228 // Validate parameter
229 if (null == emailAddress) {
231 throw new NullPointerException("emailAddress is null"); //NOI18N
232 } else if (emailAddress.isEmpty()) {
234 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
237 // Default is not found
238 boolean isFound = false;
241 for (final Headquarter headquarter : this.allHeadquarter()) {
242 // Is email address used?
243 if (Objects.equals(headquarter.getHeadquarterEmailAddress(), emailAddress)) {