]> git.mxchange.org Git - pizzaservice-war.git/blob
055647938e269c76710a8749eea4cf780d84373e
[pizzaservice-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.pizzaapplication.beans.business.branchoffice;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.cache.Cache;
25 import javax.ejb.EJB;
26 import javax.enterprise.context.RequestScoped;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
31 import org.mxchange.pizzaapplication.beans.BasePizzaController;
32
33 /**
34  * A general bean for branch offices
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Named ("branchOfficeController")
39 @RequestScoped
40 public class PizzaBranchOfficeWebRequestBean extends BasePizzaController implements PizzaBranchOfficeWebRequestController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 5_028_697_360_461L;
46
47         /**
48          * EJB for administrative purposes
49          */
50         @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.branchoffice.BranchOfficeSessionBeanRemote")
51         private BranchOfficeSessionBeanRemote branchOfficeBean;
52
53         /**
54          * A list of all branch offices (globally)
55          */
56         @Inject
57         @NamedCache (cacheName = "branchOfficeCache")
58         private Cache<Long, BranchOffice> branchOfficeCache;
59
60         /**
61          * Default constructor
62          */
63         public PizzaBranchOfficeWebRequestBean () {
64                 // Call super constructor
65                 super();
66         }
67
68         /**
69          * Returns a list of all branch offices
70          * <p>
71          * @return A list of all branch offices
72          */
73         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
74         public List<BranchOffice> allBranchOffices () {
75                 // Init list
76                 final List<BranchOffice> list = new LinkedList<>();
77
78                 // Get iterator
79                 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
80
81                 // Loop over all
82                 while (iterator.hasNext()) {
83                         // Get next entry
84                         final Cache.Entry<Long, BranchOffice> next = iterator.next();
85
86                         // Add value to list
87                         list.add(next.getValue());
88                 }
89
90                 // Return it
91                 return list;
92         }
93
94         /**
95          * Initializer method
96          */
97         @PostConstruct
98         public void initializeList () {
99                 // Is cache there?
100                 if (!this.branchOfficeCache.iterator().hasNext()) {
101                         // Get whole list
102                         final List<BranchOffice> list = this.branchOfficeBean.allBranchOffices();
103
104                         // Add all
105                         for (final Iterator<BranchOffice> iterator = list.iterator(); iterator.hasNext();) {
106                                 // Get next element
107                                 final BranchOffice next = iterator.next();
108
109                                 // Add it to cache
110                                 this.branchOfficeCache.put(next.getBranchId(), next);
111                         }
112                 }
113         }
114
115 }