2 * Copyright (C) 2017, 2018 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.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;
29 import javax.enterprise.context.RequestScoped;
30 import javax.enterprise.event.Observes;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jcontactsbusiness.events.headquarter.added.ObservableHeadquarterAddedEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
36 import org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
40 * A general bean for headquarter
42 * @author Roland Häder<roland@mxchange.org>
44 @Named ("headquarterController")
46 public class FinancialsHeadquarterWebRequestBean extends BaseFinancialsBean implements FinancialsHeadquarterWebRequestController {
51 private static final long serialVersionUID = 5_028_697_360_465L;
54 * A list of all headquarter
56 private final List<Headquarter> allHeadquarter;
59 * A list of filtered headquarter
61 private List<Headquarter> filteredHeadquarter;
64 * EJB for administrative purposes
66 @EJB (lookup = "java:global/jfinancials-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 Cache<Long, Headquarter> headquarterCache;
79 public FinancialsHeadquarterWebRequestBean () {
80 // Call super constructor
84 this.allHeadquarter = new LinkedList<>();
88 * Observes events being fired when a branch office has been added.
90 * @param event Event being fired
92 * @throws NullPointerException If the parameter or it's carried instance is
94 * @throws IllegalArgumentException If the branchId is zero or lower
96 public void afterHeadquarterAddedEvent (@Observes final ObservableHeadquarterAddedEvent event) {
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getHeadquarter() == null) {
103 throw new NullPointerException("event.headquarter is null"); //NOI18N
104 } else if (event.getHeadquarter().getHeadquarterId() == null) {
106 throw new NullPointerException("event.headquarter.branchId is null"); //NOI18N
107 } else if (event.getHeadquarter().getHeadquarterId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("event.headquarter.branchId={0} is not valid", event.getHeadquarter().getHeadquarterId())); //NOI18N
112 // Add instance to cache
113 this.headquarterCache.put(event.getHeadquarter().getHeadquarterId(), event.getHeadquarter());
114 this.allHeadquarter.add(event.getHeadquarter());
118 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
119 public List<Headquarter> allHeadquarter () {
120 return this.allHeadquarter;
124 public Headquarter findHeadquarterById (final Long headquarterId) throws HeadquarterNotFoundException {
125 // Validate parameter
126 if (null == headquarterId) {
128 throw new NullPointerException("headquarterId is null"); //NOI18N
129 } else if (headquarterId < 1) {
131 throw new IllegalArgumentException(MessageFormat.format("headquarterId={0} is invalid", headquarterId)); //NOI18N
132 } else if (!this.headquarterCache.containsKey(headquarterId)) {
134 throw new HeadquarterNotFoundException(headquarterId);
138 final Headquarter headquarter = this.headquarterCache.get(headquarterId);
145 * Getter for a list of filtered headquarter
147 * @return Filtered headquarter
149 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
150 public List<Headquarter> getFilteredHeadquarter () {
151 return this.filteredHeadquarter;
155 * Setter for a list of filtered headquarter
157 * @param filteredHeadquarter Filtered headquarter
159 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
160 public void setFilteredHeadquarter (final List<Headquarter> filteredHeadquarter) {
161 this.filteredHeadquarter = filteredHeadquarter;
168 public void initializeList () {
170 if (!this.headquarterCache.iterator().hasNext()) {
171 // Get whole list from EJB
172 final List<Headquarter> Headquarter = this.headquarterBean.allHeadquarters();
175 for (final Headquarter headquarter : Headquarter) {
177 this.headquarterCache.put(headquarter.getHeadquarterId(), headquarter);
181 // Is the list empty, but filled cache?
182 if (this.allHeadquarter.isEmpty() && this.headquarterCache.iterator().hasNext()) {
184 final Iterator<Cache.Entry<Long, Headquarter>> iterator = this.headquarterCache.iterator();
187 while (iterator.hasNext()) {
189 final Cache.Entry<Long, Headquarter> next = iterator.next();
192 this.allHeadquarter.add(next.getValue());
196 this.allHeadquarter.sort(new Comparator<Headquarter>() {
198 public int compare (final Headquarter o1, final Headquarter o2) {
199 return o1.getHeadquarterId() > o2.getHeadquarterId() ? 1 : o1.getHeadquarterId() < o2.getHeadquarterId() ? -1 : 0;
206 public Boolean isCompanyNameUsed (final String companyName) {
207 // Validate parameter
208 if (null == companyName) {
210 throw new NullPointerException("companyName is null"); //NOI18N
211 } else if (companyName.isEmpty()) {
213 throw new IllegalArgumentException("companyName is empty"); //NOI18N
216 // Default is not found
217 boolean isFound = false;
220 for (final Headquarter headquarter : this.allHeadquarter()) {
221 // Is same company name?
222 if (Objects.equals(headquarter.getHeadquarterCompanyName(), companyName)) {
234 public Boolean isEmailAddressRegistered (final String emailAddress) {
235 // Validate parameter
236 if (null == emailAddress) {
238 throw new NullPointerException("emailAddress is null"); //NOI18N
239 } else if (emailAddress.isEmpty()) {
241 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
244 // Default is not found
245 boolean isFound = false;
248 for (final Headquarter headquarter : this.allHeadquarter()) {
249 // Is email address used?
250 if (Objects.equals(headquarter.getHeadquarterEmailAddress(), emailAddress)) {