]> git.mxchange.org Git - jfinancials-war.git/blob
093b59b4c1e1c54b8b57a404c11bf050f3cce2f8
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 Roland Häder
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.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         /**
118          * Getter for a list of filtered opening times
119          * <p>
120          * @return Filtered opening times
121          */
122         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
123         public List<OpeningTime> getFilteredOpeningTimes () {
124                 return this.filteredOpeningTimes;
125         }
126
127         /**
128          * Setter for a list of filtered opening times
129          * <p>
130          * @param filteredOpeningTimes Filtered opening times
131          */
132         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
133         public void setFilteredOpeningTimes (final List<OpeningTime> filteredOpeningTimes) {
134                 this.filteredOpeningTimes = filteredOpeningTimes;
135         }
136
137         /**
138          * Initializer method
139          */
140         @PostConstruct
141         public void initializeList () {
142                 // Is cache there?
143                 if (!this.openingTimesCache.iterator().hasNext()) {
144                         // Get whole list
145                         final List<OpeningTime> list = this.openingTimesBean.allOpeningTimes();
146
147                         // Add all
148                         for (final Iterator<OpeningTime> iterator = list.iterator(); iterator.hasNext();) {
149                                 // Get next element
150                                 final OpeningTime next = iterator.next();
151
152                                 // Add it to cache
153                                 this.openingTimesCache.put(next.getOpeningId(), next);
154                         }
155                 }
156
157                 // Is the list empty, but filled cache?
158                 if (this.allOpeningTimes.isEmpty() && this.openingTimesCache.iterator().hasNext()) {
159                         // Get iterator
160                         final Iterator<Cache.Entry<Long, OpeningTime>> iterator = this.openingTimesCache.iterator();
161
162                         // Build up list
163                         while (iterator.hasNext()) {
164                                 // GEt next element
165                                 final Cache.Entry<Long, OpeningTime> next = iterator.next();
166
167                                 // Add to list
168                                 this.allOpeningTimes.add(next.getValue());
169                         }
170
171                         // Sort list
172                         this.allOpeningTimes.sort(new Comparator<OpeningTime>() {
173                                 @Override
174                                 public int compare (final OpeningTime o1, final OpeningTime o2) {
175                                         return o1.getOpeningId() > o2.getOpeningId() ? 1 : o1.getOpeningId() < o2.getOpeningId() ? -1 : 0;
176                                 }
177                         }
178                         );
179                 }
180         }
181
182 }