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