]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebRequestBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / country / PizzaCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 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.country;
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.jcountry.events.ObservableAdminAddedCountryEvent;
32 import org.mxchange.jcountry.model.data.Country;
33 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
34 import org.mxchange.pizzaapplication.beans.BasePizzaController;
35
36 /**
37  * A country bean
38  * <p>
39  * @author Roland Häder<roland@mxchange.org>
40  */
41 @Named ("countryController")
42 @RequestScoped
43 public class PizzaCountryWebRequestBean extends BasePizzaController implements PizzaCountryWebRequestController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 176_985_298_681_742_960L;
49
50         /**
51          * Remote country EJB
52          */
53         @EJB (lookup = "java:global/jfinancials-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote")
54         private CountrySingletonBeanRemote countryBean;
55
56         /**
57          * List of all countries
58          */
59         @Inject
60         @NamedCache (cacheName = "countryCache", managementEnabled = true)
61         private transient Cache<Long, Country> countryCache;
62
63         /**
64          * Default constructor
65          */
66         public PizzaCountryWebRequestBean () {
67                 // Call super constructor
68                 super();
69         }
70
71         /**
72          * Observing method when the event is fired that an administrator added a
73          * new country
74          * <p>
75          * @param event Event instance
76          */
77         public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
78                 // Is all valid?
79                 if (null == event) {
80                         // Throw NPE
81                         throw new NullPointerException("event is null"); //NOI18N
82                 } else if (event.getAddedCountry() == null) {
83                         // Throw again ...
84                         throw new NullPointerException("event.addedCountry is null"); //NOI18N
85                 } else if (event.getAddedCountry().getCountryId() == null) {
86                         // And again ...
87                         throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
88                 } else if (event.getAddedCountry().getCountryId() < 1) {
89                         // Id is invalid
90                         throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
91                 }
92
93                 // Add the event
94                 this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry());
95         }
96
97         @Override
98         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
99         public List<Country> allCountries () {
100                 // Init list
101                 List<Country> list = new LinkedList<>();
102
103                 // Get iterator
104                 Iterator<Cache.Entry<Long, Country>> iterator = this.countryCache.iterator();
105
106                 // Loop over all
107                 while (iterator.hasNext()) {
108                         // Get next entry
109                         final Cache.Entry<Long, Country> next = iterator.next();
110
111                         // Add value to list
112                         list.add(next.getValue());
113                 }
114
115                 // Return it
116                 return list;
117         }
118
119         /**
120          * Post-construction method
121          */
122         @PostConstruct
123         public void init () {
124                 // Is cache there?
125                 if (!this.countryCache.iterator().hasNext()) {
126                         // Get whole list
127                         List<Country> list = this.countryBean.allCountries();
128
129                         // Add all
130                         for (final Iterator<Country> iterator = list.iterator(); iterator.hasNext();) {
131                                 // Get next element
132                                 final Country next = iterator.next();
133
134                                 // Add it to cache
135                                 this.countryCache.put(next.getCountryId(), next);
136                         }
137                 }
138         }
139
140 }