]> git.mxchange.org Git - pizzaservice-war.git/blob
e154c3971c328bb27028f36ab19fba0a5c40f01e
[pizzaservice-war.git] /
1 /*
2  * Copyright (C) 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.pizzaapplication.beans.business.branchoffice;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
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.enterprise.event.Observes;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
32 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
33 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
34 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
35
36 /**
37  * A general bean for branch offices
38  * <p>
39  * @author Roland Häder<roland@mxchange.org>
40  */
41 @Named ("branchOfficeController")
42 @RequestScoped
43 public class PizzaBranchOfficeWebRequestBean extends BasePizzaBean implements PizzaBranchOfficeWebRequestController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 5_028_697_360_461L;
49
50         /**
51          * EJB for administrative purposes
52          */
53         @EJB (lookup = "java:global/pizzaservice-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
54         private BranchOfficeSessionBeanRemote branchOfficeBean;
55
56         /**
57          * A list of all branch offices (globally)
58          */
59         @Inject
60         @NamedCache (cacheName = "branchOfficeCache")
61         private Cache<Long, BranchOffice> branchOfficeCache;
62
63         /**
64          * Default constructor
65          */
66         public PizzaBranchOfficeWebRequestBean () {
67                 // Call super constructor
68                 super();
69         }
70
71         /**
72          * Observes events being fired when a branch office has been added.
73          * <p>
74          * @param event Event being fired
75          * <p>
76          * @throws NullPointerException If the parameter or it's carried instance is null
77          * @throws IllegalArgumentException If the branchId is zero or lower
78          */
79         public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
80                 // Validate parameter
81                 if (null == event) {
82                         // Throw NPE
83                         throw new NullPointerException("event is null"); //NOI18N
84                 } else if (event.getBranchOffice() == null) {
85                         // Throw NPE again
86                         throw new NullPointerException("event.branchOffice is null"); //NOI18N
87                 } else if (event.getBranchOffice().getBranchId() == null) {
88                         // Throw it again
89                         throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
90                 } else if (event.getBranchOffice().getBranchId() < 1) {
91                         // Throw IAE
92                         throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
93                 }
94
95                 // Add instance to cache
96                 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
97         }
98
99         @Override
100         public List<BranchOffice> allBranchOffices () {
101                 // Init list
102                 final List<BranchOffice> list = new LinkedList<>();
103
104                 // Get iterator
105                 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
106
107                 // Loop over all
108                 while (iterator.hasNext()) {
109                         // Get next entry
110                         final Cache.Entry<Long, BranchOffice> next = iterator.next();
111
112                         // Add value to list
113                         list.add(next.getValue());
114                 }
115
116                 // Return it
117                 return list;
118         }
119
120         /**
121          * Initializer method
122          */
123         @PostConstruct
124         public void initializeList () {
125                 // Is cache there?
126                 if (!this.branchOfficeCache.iterator().hasNext()) {
127                         // Get whole list
128                         final List<BranchOffice> list = this.branchOfficeBean.allBranchOffices();
129
130                         // Add all
131                         for (final Iterator<BranchOffice> iterator = list.iterator(); iterator.hasNext();) {
132                                 // Get next element
133                                 final BranchOffice next = iterator.next();
134
135                                 // Add it to cache
136                                 this.branchOfficeCache.put(next.getBranchId(), next);
137                         }
138                 }
139         }
140
141 }