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