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