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.opening_time;
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 javax.annotation.PostConstruct;
25 import javax.cache.Cache;
27 import javax.enterprise.context.RequestScoped;
28 import javax.enterprise.event.Observes;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcontactsbusiness.events.opening_time.added.ObservableOpeningTimeAddedEvent;
32 import org.mxchange.jcontactsbusiness.exceptions.opening_time.OpeningTimeNotFoundException;
33 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
34 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTimeSessionBeanRemote;
35 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38 * A general bean for opening times
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("openingTimeController")
44 public class FinancialsOpeningTimeWebRequestBean extends BaseFinancialsBean implements FinancialsOpeningTimeWebRequestController {
49 private static final long serialVersionUID = 5_028_697_360_462L;
52 * A list of all opening times
54 private final List<OpeningTime> allOpeningTimes;
57 * A list of filtered opening times
59 private List<OpeningTime> filteredOpeningTimes;
62 * EJB for administrative purposes
64 @EJB (lookup = "java:global/jfinancials-ejb/openingTimes!org.mxchange.jcontactsbusiness.model.opening_time.OpeningTimeSessionBeanRemote")
65 private OpeningTimeSessionBeanRemote openingTimesBean;
68 * A list of all opening times (globally)
71 @NamedCache (cacheName = "openingTimesCache")
72 private Cache<Long, OpeningTime> openingTimesCache;
77 public FinancialsOpeningTimeWebRequestBean () {
78 // Call super constructor
82 this.allOpeningTimes = new LinkedList<>();
86 * Observes events being thrown when a new opening time has been added
88 * @param event Event being fired
90 public void afterOpeningTimeAddedEvent (@Observes final ObservableOpeningTimeAddedEvent event) {
94 throw new NullPointerException("event is null");
95 } else if (event.getOpeningTime() == null) {
97 throw new NullPointerException("event.openingTime is null");
98 } else if (event.getOpeningTime().getOpeningId() == null) {
100 throw new NullPointerException("event.openingTime.openingId is null");
101 } else if (event.getOpeningTime().getOpeningId() < 1) {
103 throw new NullPointerException(MessageFormat.format("event.openingTime.openingId={0} is invalid", event.getOpeningTime().getOpeningId()));
106 // Add to cache and list
107 this.openingTimesCache.put(event.getOpeningTime().getOpeningId(), event.getOpeningTime());
108 this.allOpeningTimes.add(event.getOpeningTime());
112 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
113 public List<OpeningTime> allOpeningTimes () {
114 return this.allOpeningTimes;
118 public OpeningTime findOpeningTimeById (final Long openingId) throws OpeningTimeNotFoundException {
119 // Validate parameter
120 if (null == openingId) {
122 throw new NullPointerException("openingId is null"); //NOI18N
123 } else if (openingId < 1) {
125 throw new IllegalArgumentException("openingId=" + openingId + " is invalid"); //NOI18N
126 } else if (!this.openingTimesCache.containsKey(openingId)) {
128 throw new OpeningTimeNotFoundException(openingId);
132 final OpeningTime opening = this.openingTimesCache.get(openingId);
139 * Getter for a list of filtered opening times
141 * @return Filtered opening times
143 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
144 public List<OpeningTime> getFilteredOpeningTimes () {
145 return this.filteredOpeningTimes;
149 * Setter for a list of filtered opening times
151 * @param filteredOpeningTimes Filtered opening times
153 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
154 public void setFilteredOpeningTimes (final List<OpeningTime> filteredOpeningTimes) {
155 this.filteredOpeningTimes = filteredOpeningTimes;
162 public void initializeList () {
164 if (!this.openingTimesCache.iterator().hasNext()) {
165 // Get whole list from EJB
166 final List<OpeningTime> openingTimes = this.openingTimesBean.allOpeningTimes();
169 for (final OpeningTime openingTime : openingTimes) {
171 this.openingTimesCache.put(openingTime.getOpeningId(), openingTime);
175 // Is the list empty, but filled cache?
176 if (this.allOpeningTimes.isEmpty() && this.openingTimesCache.iterator().hasNext()) {
178 for (final Cache.Entry<Long, OpeningTime> currentEntry : this.openingTimesCache) {
180 this.allOpeningTimes.add(currentEntry.getValue());
184 this.allOpeningTimes.sort(new Comparator<OpeningTime>() {
186 public int compare (final OpeningTime openingTime1, final OpeningTime openingTime2) {
187 return openingTime1.getOpeningId() > openingTime2.getOpeningId() ? 1 : openingTime1.getOpeningId() < openingTime2.getOpeningId() ? -1 : 0;