]> git.mxchange.org Git - jfinancials-war.git/blob
7577eec10c51c3d5a88cddcc92db0722eb0b324a
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.business.opening_time;
18
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;
26 import javax.ejb.EJB;
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;
36
37 /**
38  * A general bean for opening times
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("openingTimeController")
43 @RequestScoped
44 public class FinancialsOpeningTimeWebRequestBean extends BaseFinancialsBean implements FinancialsOpeningTimeWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 5_028_697_360_462L;
50
51         /**
52          * A list of all opening times
53          */
54         private final List<OpeningTime> allOpeningTimes;
55
56         /**
57          * A list of filtered opening times
58          */
59         private List<OpeningTime> filteredOpeningTimes;
60
61         /**
62          * EJB for administrative purposes
63          */
64         @EJB (lookup = "java:global/jfinancials-ejb/openingTimes!org.mxchange.jcontactsbusiness.model.opening_time.OpeningTimeSessionBeanRemote")
65         private OpeningTimeSessionBeanRemote openingTimesBean;
66
67         /**
68          * A list of all opening times (globally)
69          */
70         @Inject
71         @NamedCache (cacheName = "openingTimesCache")
72         private Cache<Long, OpeningTime> openingTimesCache;
73
74         /**
75          * Default constructor
76          */
77         public FinancialsOpeningTimeWebRequestBean () {
78                 // Call super constructor
79                 super();
80
81                 // Init list
82                 this.allOpeningTimes = new LinkedList<>();
83         }
84
85         /**
86          * Observes events being thrown when a new opening time has been added
87          * <p>
88          * @param event Event being fired
89          */
90         public void afterOpeningTimeAddedEvent (@Observes final ObservableOpeningTimeAddedEvent event) {
91                 // Validate parameter
92                 if (null == event) {
93                         // Throw NPE
94                         throw new NullPointerException("event is null");
95                 } else if (event.getOpeningTime() == null) {
96                         // Throw it again
97                         throw new NullPointerException("event.openingTime is null");
98                 } else if (event.getOpeningTime().getOpeningId() == null) {
99                         // Throw it again
100                         throw new NullPointerException("event.openingTime.openingId is null");
101                 } else if (event.getOpeningTime().getOpeningId() < 1) {
102                         // Throw it again
103                         throw new NullPointerException(MessageFormat.format("event.openingTime.openingId={0} is invalid", event.getOpeningTime().getOpeningId()));
104                 }
105
106                 // Add to cache and list
107                 this.openingTimesCache.put(event.getOpeningTime().getOpeningId(), event.getOpeningTime());
108                 this.allOpeningTimes.add(event.getOpeningTime());
109         }
110
111         @Override
112         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
113         public List<OpeningTime> allOpeningTimes () {
114                 return this.allOpeningTimes;
115         }
116
117         @Override
118         public OpeningTime findOpeningTimeById (final Long openingId) throws OpeningTimeNotFoundException {
119                 // Validate parameter
120                 if (null == openingId) {
121                         // Throw NPE
122                         throw new NullPointerException("openingId is null"); //NOI18N
123                 } else if (openingId < 1) {
124                         // Throw IAE
125                         throw new IllegalArgumentException("openingId=" + openingId + " is invalid"); //NOI18N
126                 } else if (!this.openingTimesCache.containsKey(openingId)) {
127                         // Not found
128                         throw new OpeningTimeNotFoundException(openingId);
129                 }
130
131                 // Get it from cache
132                 final OpeningTime opening = this.openingTimesCache.get(openingId);
133
134                 // Return it
135                 return opening;
136         }
137
138         /**
139          * Getter for a list of filtered opening times
140          * <p>
141          * @return Filtered opening times
142          */
143         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
144         public List<OpeningTime> getFilteredOpeningTimes () {
145                 return this.filteredOpeningTimes;
146         }
147
148         /**
149          * Setter for a list of filtered opening times
150          * <p>
151          * @param filteredOpeningTimes Filtered opening times
152          */
153         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
154         public void setFilteredOpeningTimes (final List<OpeningTime> filteredOpeningTimes) {
155                 this.filteredOpeningTimes = filteredOpeningTimes;
156         }
157
158         /**
159          * Initializer method
160          */
161         @PostConstruct
162         public void initializeList () {
163                 // Is cache there?
164                 if (!this.openingTimesCache.iterator().hasNext()) {
165                         // Get whole list from EJB
166                         final List<OpeningTime> openingTimes = this.openingTimesBean.allOpeningTimes();
167
168                         // Add all
169                         for (final OpeningTime openingTime : openingTimes) {
170                                 // Add it to cache
171                                 this.openingTimesCache.put(openingTime.getOpeningId(), openingTime);
172                         }
173                 }
174
175                 // Is the list empty, but filled cache?
176                 if (this.allOpeningTimes.isEmpty() && this.openingTimesCache.iterator().hasNext()) {
177                         // Build up list
178                         for (final Cache.Entry<Long, OpeningTime> currentEntry : this.openingTimesCache) {
179                                 // Add to list
180                                 this.allOpeningTimes.add(currentEntry.getValue());
181                         }
182
183                         // Sort list
184                         this.allOpeningTimes.sort(new Comparator<OpeningTime>() {
185                                 @Override
186                                 public int compare (final OpeningTime openingTime1, final OpeningTime openingTime2) {
187                                         return openingTime1.getOpeningId() > openingTime2.getOpeningId() ? 1 : openingTime1.getOpeningId() < openingTime2.getOpeningId() ? -1 : 0;
188                                 }
189                         }
190                         );
191                 }
192         }
193
194 }