]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java
be3a6513f1a0e60d29820b653ba7cd1b7747622e
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / country / PizzaCountryWebApplicationBean.java
1 /*
2  * Copyright (C) 2016 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.List;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.ApplicationScoped;
23 import javax.enterprise.event.Observes;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcountry.data.Country;
30 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
31 import org.mxchange.pizzaapplication.beans.BasePizzaController;
32 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
33
34 /**
35  * A country bean
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Named ("countryController")
40 @ApplicationScoped
41 public class PizzaCountryWebApplicationBean extends BasePizzaController implements PizzaCountryWebApplicationController {
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         private CountrySingletonBeanRemote countryBean;
52
53         /**
54          * List of all countries
55          */
56         private List<Country> countryList;
57
58         /**
59          * Default constructor
60          */
61         public PizzaCountryWebApplicationBean () {
62                 // Try this
63                 try {
64                         // Get initial context
65                         Context context = new InitialContext();
66
67                         // Try to lookup the bean
68                         this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
69                 } catch (final NamingException ex) {
70                         // Continue to throw
71                         throw new FaceletException(ex);
72                 }
73         }
74
75         @Override
76         public void afterAdminAddedCountry (@Observes final ObservableAdminAddedCountryEvent event) {
77                 // Is all valid?
78                 if (null == event) {
79                         // Throw NPE
80                         throw new NullPointerException("event is null"); //NOI18N
81                 } else if (event.getAddedCountry() == null) {
82                         // Throw again ...
83                         throw new NullPointerException("event.addedCountry is null"); //NOI18N
84                 } else if (event.getAddedCountry().getCountryId() == null) {
85                         // And again ...
86                         throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
87                 } else if (event.getAddedCountry().getCountryId() < 1) {
88                         // Id is invalid
89                         throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
90                 }
91
92                 // Add the event
93                 this.countryList.add(event.getAddedCountry());
94         }
95
96         @Override
97         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
98         public List<Country> allCountries () {
99                 // Return "cached" version
100                 return this.countryList;
101         }
102
103         /**
104          * Post-initialization of this class
105          */
106         @PostConstruct
107         public void init () {
108                 // "Cache" country list as this will not change so often.
109                 this.countryList = this.countryBean.allCountries();
110         }
111
112 }