]> git.mxchange.org Git - jjobs-war.git/blob
d82c04043c8d14af5bef6df8b96234eef8f579a5
[jjobs-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.list;
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.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.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 ("openingTimeListController")
43 @ViewScoped
44 public class FinancialsOpeningTimeListWebViewBean extends BaseFinancialsBean implements FinancialsOpeningTimeListWebViewController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 5_028_697_360_467L;
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 transient Cache<Long, OpeningTime> openingTimesCache;
73
74         /**
75          * Selected opening-time instance
76          */
77         private OpeningTime selectedOpeningTime;
78
79         /**
80          * Default constructor
81          */
82         public FinancialsOpeningTimeListWebViewBean () {
83                 // Call super constructor
84                 super();
85
86                 // Init list
87                 this.allOpeningTimes = new LinkedList<>();
88         }
89
90         /**
91          * Observes events being thrown when a new opening time has been added
92          * <p>
93          * @param event Event being fired
94          */
95         public void afterOpeningTimeAddedEvent (@Observes final ObservableOpeningTimeAddedEvent event) {
96                 // Validate parameter
97                 if (null == event) {
98                         // Throw NPE
99                         throw new NullPointerException("event is null");
100                 } else if (event.getOpeningTime() == null) {
101                         // Throw it again
102                         throw new NullPointerException("event.openingTime is null");
103                 } else if (event.getOpeningTime().getOpeningTimeId() == null) {
104                         // Throw it again
105                         throw new NullPointerException("event.openingTime.openingTimeId is null");
106                 } else if (event.getOpeningTime().getOpeningTimeId() < 1) {
107                         // Throw it again
108                         throw new NullPointerException(MessageFormat.format("event.openingTime.openingTimeId={0} is invalid", event.getOpeningTime().getOpeningTimeId()));
109                 }
110
111                 // Add to cache and list
112                 this.openingTimesCache.put(event.getOpeningTime().getOpeningTimeId(), event.getOpeningTime());
113                 this.getAllOpeningTimes().add(event.getOpeningTime());
114         }
115
116         @Override
117         public OpeningTime findOpeningTimeById (final Long openingTimeId) throws OpeningTimeNotFoundException {
118                 // Validate parameter
119                 if (null == openingTimeId) {
120                         // Throw NPE
121                         throw new NullPointerException("openingTimeId is null"); //NOI18N
122                 } else if (openingTimeId < 1) {
123                         // Throw IAE
124                         throw new IllegalArgumentException("openingTimeId=" + openingTimeId + " is invalid"); //NOI18N
125                 } else if (!this.openingTimesCache.containsKey(openingTimeId)) {
126                         // Not found
127                         throw new OpeningTimeNotFoundException(openingTimeId);
128                 }
129
130                 // Get it from cache
131                 final OpeningTime opening = this.openingTimesCache.get(openingTimeId);
132
133                 // Return it
134                 return opening;
135         }
136
137         /**
138          * Returns a list of all opening times
139          * <p>
140          * @return A list of all opening times
141          */
142         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
143         public List<OpeningTime> getAllOpeningTimes () {
144                 return this.allOpeningTimes;
145         }
146
147         /**
148          * Getter for a list of filtered opening times
149          * <p>
150          * @return Filtered opening times
151          */
152         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
153         public List<OpeningTime> getFilteredOpeningTimes () {
154                 return this.filteredOpeningTimes;
155         }
156
157         /**
158          * Setter for a list of filtered opening times
159          * <p>
160          * @param filteredOpeningTimes Filtered opening times
161          */
162         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
163         public void setFilteredOpeningTimes (final List<OpeningTime> filteredOpeningTimes) {
164                 this.filteredOpeningTimes = filteredOpeningTimes;
165         }
166
167         /**
168          * Getter for selected open-time instance
169          * <p>
170          * @return Selected open-time instance
171          */
172         public OpeningTime getSelectedOpeningTime () {
173                 return this.selectedOpeningTime;
174         }
175
176         /**
177          * Setter for selected open-time instance
178          * <p>
179          * @param selectedOpeningTime Selected open-time instance
180          */
181         public void setSelectedOpeningTime (final OpeningTime selectedOpeningTime) {
182                 this.selectedOpeningTime = selectedOpeningTime;
183         }
184
185         /**
186          * Initializer method
187          */
188         @PostConstruct
189         public void initializeList () {
190                 // Is cache there?
191                 if (!this.openingTimesCache.iterator().hasNext()) {
192                         // Add all
193                         for (final OpeningTime openingTime : this.openingTimesBean.fetchAllOpeningTimes()) {
194                                 // Add it to cache
195                                 this.openingTimesCache.put(openingTime.getOpeningTimeId(), openingTime);
196                         }
197                 }
198
199                 // Is the list empty, but filled cache?
200                 if (this.getAllOpeningTimes().isEmpty() && this.openingTimesCache.iterator().hasNext()) {
201                         // Build up list
202                         for (final Cache.Entry<Long, OpeningTime> currentEntry : this.openingTimesCache) {
203                                 // Add to list
204                                 this.getAllOpeningTimes().add(currentEntry.getValue());
205                         }
206
207                         // Sort list
208                         this.getAllOpeningTimes().sort(new Comparator<OpeningTime>() {
209                                 @Override
210                                 public int compare (final OpeningTime openingTime1, final OpeningTime openingTime2) {
211                                         return openingTime1.getOpeningTimeId() > openingTime2.getOpeningTimeId() ? 1 : openingTime1.getOpeningTimeId() < openingTime2.getOpeningTimeId() ? -1 : 0;
212                                 }
213                         }
214                         );
215                 }
216         }
217
218 }