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.opening_time.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 javax.annotation.PostConstruct;
25 import javax.cache.Cache;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.ViewScoped;
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.addressbook.beans.BaseAddressbookBean;
38 * A general bean for opening times
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("openingTimeListController")
44 public class AddressbookOpeningTimeListWebViewBean extends BaseAddressbookBean implements AddressbookOpeningTimeListWebViewController {
49 private static final long serialVersionUID = 5_028_697_360_467L;
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/addressbook-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 transient Cache<Long, OpeningTime> openingTimesCache;
75 * Selected opening-time instance
77 private OpeningTime selectedOpeningTime;
82 public AddressbookOpeningTimeListWebViewBean () {
83 // Call super constructor
87 this.allOpeningTimes = new LinkedList<>();
91 * Observes events being thrown when a new opening time has been added
93 * @param event Event being fired
95 public void afterOpeningTimeAddedEvent (@Observes final ObservableOpeningTimeAddedEvent event) {
99 throw new NullPointerException("event is null");
100 } else if (event.getOpeningTime() == null) {
102 throw new NullPointerException("event.openingTime is null");
103 } else if (event.getOpeningTime().getOpeningTimeId() == null) {
105 throw new NullPointerException("event.openingTime.openingTimeId is null");
106 } else if (event.getOpeningTime().getOpeningTimeId() < 1) {
108 throw new NullPointerException(MessageFormat.format("event.openingTime.openingTimeId={0} is invalid", event.getOpeningTime().getOpeningTimeId()));
111 // Add to cache and list
112 this.openingTimesCache.put(event.getOpeningTime().getOpeningTimeId(), event.getOpeningTime());
113 this.getAllOpeningTimes().add(event.getOpeningTime());
117 public OpeningTime findOpeningTimeById (final Long openingTimeId) throws OpeningTimeNotFoundException {
118 // Validate parameter
119 if (null == openingTimeId) {
121 throw new NullPointerException("openingTimeId is null"); //NOI18N
122 } else if (openingTimeId < 1) {
124 throw new IllegalArgumentException("openingTimeId=" + openingTimeId + " is invalid"); //NOI18N
125 } else if (!this.openingTimesCache.containsKey(openingTimeId)) {
127 throw new OpeningTimeNotFoundException(openingTimeId);
131 final OpeningTime opening = this.openingTimesCache.get(openingTimeId);
138 * Returns a list of all opening times
140 * @return A list of all opening times
142 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
143 public List<OpeningTime> getAllOpeningTimes () {
144 return this.allOpeningTimes;
148 * Getter for a list of filtered opening times
150 * @return Filtered opening times
152 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
153 public List<OpeningTime> getFilteredOpeningTimes () {
154 return this.filteredOpeningTimes;
158 * Setter for a list of filtered opening times
160 * @param filteredOpeningTimes Filtered opening times
162 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
163 public void setFilteredOpeningTimes (final List<OpeningTime> filteredOpeningTimes) {
164 this.filteredOpeningTimes = filteredOpeningTimes;
168 * Getter for selected open-time instance
170 * @return Selected open-time instance
172 public OpeningTime getSelectedOpeningTime () {
173 return this.selectedOpeningTime;
177 * Setter for selected open-time instance
179 * @param selectedOpeningTime Selected open-time instance
181 public void setSelectedOpeningTime (final OpeningTime selectedOpeningTime) {
182 this.selectedOpeningTime = selectedOpeningTime;
189 public void initializeList () {
191 if (!this.openingTimesCache.iterator().hasNext()) {
193 for (final OpeningTime openingTime : this.openingTimesBean.fetchAllOpeningTimes()) {
195 this.openingTimesCache.put(openingTime.getOpeningTimeId(), openingTime);
199 // Is the list empty, but filled cache?
200 if (this.getAllOpeningTimes().isEmpty() && this.openingTimesCache.iterator().hasNext()) {
202 for (final Cache.Entry<Long, OpeningTime> currentEntry : this.openingTimesCache) {
204 this.getAllOpeningTimes().add(currentEntry.getValue());
208 this.getAllOpeningTimes().sort(new Comparator<OpeningTime>() {
210 public int compare (final OpeningTime openingTime1, final OpeningTime openingTime2) {
211 return openingTime1.getOpeningTimeId() > openingTime2.getOpeningTimeId() ? 1 : openingTime1.getOpeningTimeId() < openingTime2.getOpeningTimeId() ? -1 : 0;