]> git.mxchange.org Git - jfinancials-war.git/blob
7ae1d1d6b7b235aeedae81ac6db38372246c5278
[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.util.Comparator;
21 import java.util.Iterator;
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.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
31 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTimeSessionBeanRemote;
32 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
33
34 /**
35  * A general bean for opening times
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Named ("openingTimeController")
40 @RequestScoped
41 public class FinancialsOpeningTimeWebRequestBean extends BaseFinancialsBean implements FinancialsOpeningTimeWebRequestController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 5_028_697_360_462L;
47
48         /**
49          * A list of all opening times
50          */
51         private final List<OpeningTime> allOpeningTimes;
52
53         /**
54          * A list of filtered opening times
55          */
56         private List<OpeningTime> filteredOpeningTimes;
57
58         /**
59          * EJB for administrative purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/openingTimes!org.mxchange.jcontactsbusiness.model.opening_time.OpeningTimeSessionBeanRemote")
62         private OpeningTimeSessionBeanRemote openingTimesBean;
63
64         /**
65          * A list of all opening times (globally)
66          */
67         @Inject
68         @NamedCache (cacheName = "openingTimesCache")
69         private Cache<Long, OpeningTime> openingTimesCache;
70
71         /**
72          * Default constructor
73          */
74         public FinancialsOpeningTimeWebRequestBean () {
75                 // Call super constructor
76                 super();
77
78                 // Init list
79                 this.allOpeningTimes = new LinkedList<>();
80         }
81
82         @Override
83         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
84         public List<OpeningTime> allOpeningTimes () {
85                 return this.allOpeningTimes;
86         }
87
88         /**
89          * Getter for a list of filtered opening times
90          * <p>
91          * @return Filtered opening times
92          */
93         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
94         public List<OpeningTime> getFilteredOpeningTimes () {
95                 return this.filteredOpeningTimes;
96         }
97
98         /**
99          * Setter for a list of filtered opening times
100          * <p>
101          * @param filteredOpeningTimes Filtered opening times
102          */
103         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
104         public void setFilteredOpeningTimes (final List<OpeningTime> filteredOpeningTimes) {
105                 this.filteredOpeningTimes = filteredOpeningTimes;
106         }
107
108         /**
109          * Initializer method
110          */
111         @PostConstruct
112         public void initializeList () {
113                 // Is cache there?
114                 if (!this.openingTimesCache.iterator().hasNext()) {
115                         // Get whole list
116                         final List<OpeningTime> list = this.openingTimesBean.allOpeningTimes();
117
118                         // Add all
119                         for (final Iterator<OpeningTime> iterator = list.iterator(); iterator.hasNext();) {
120                                 // Get next element
121                                 final OpeningTime next = iterator.next();
122
123                                 // Add it to cache
124                                 this.openingTimesCache.put(next.getOpeningId(), next);
125                         }
126                 }
127
128                 // Is the list empty, but filled cache?
129                 if (this.allOpeningTimes.isEmpty() && this.openingTimesCache.iterator().hasNext()) {
130                         // Get iterator
131                         final Iterator<Cache.Entry<Long, OpeningTime>> iterator = this.openingTimesCache.iterator();
132
133                         // Build up list
134                         while (iterator.hasNext()) {
135                                 // GEt next element
136                                 final Cache.Entry<Long, OpeningTime> next = iterator.next();
137
138                                 // Add to list
139                                 this.allOpeningTimes.add(next.getValue());
140                         }
141
142                         // Sort list
143                         this.allOpeningTimes.sort(new Comparator<OpeningTime>() {
144                                 @Override
145                                 public int compare (final OpeningTime o1, final OpeningTime o2) {
146                                         return o1.getOpeningId() > o2.getOpeningId() ? 1 : o1.getOpeningId() < o2.getOpeningId() ? -1 : 0;
147                                 }
148                         }
149                         );
150                 }
151         }
152
153 }