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