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