From 8310c0655b5f7f876cdf48983f81d069f032e172 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 11 Apr 2016 19:34:53 +0200 Subject: [PATCH] Added firing newly added event when an administrator has added a new country and it got handled by the EJB. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../PizzaAdminCountryWebApplicationBean.java | 14 +++++++++++ .../PizzaCountryWebApplicationBean.java | 24 +++++++++++++++++++ .../PizzaCountryWebApplicationController.java | 8 +++++++ 3 files changed, 46 insertions(+) diff --git a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebApplicationBean.java b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebApplicationBean.java index e3663743..2abe0aa1 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebApplicationBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebApplicationBean.java @@ -20,6 +20,8 @@ import java.util.Iterator; import java.util.List; import java.util.Objects; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -29,6 +31,8 @@ import javax.naming.NamingException; import org.mxchange.jcountry.data.Country; import org.mxchange.jcountry.data.CountryData; import org.mxchange.jcountry.data.CountrySingletonBeanRemote; +import org.mxchange.jcountry.events.AdminAddedCountryEvent; +import org.mxchange.jcountry.events.AdminEventCountryAdded; import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; /** @@ -66,6 +70,13 @@ public class PizzaAdminCountryWebApplicationBean implements PizzaAdminCountryWeb @Inject private PizzaCountryWebApplicationController countryController; + /** + * An event triggered when the administrator has added a country + */ + @Inject + @Any + private Event addedCountryEvent; + /** * Local dial prefix */ @@ -126,6 +137,9 @@ public class PizzaAdminCountryWebApplicationBean implements PizzaAdminCountryWeb // Send country to bean this.countryBean.addCountry(country); + // Fire event + this.addedCountryEvent.fire(new AdminEventCountryAdded(country)); + // Clear bean this.clear(); } catch (final CountryAlreadyAddedException ex) { diff --git a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java index 2431d44e..623ced51 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java @@ -16,10 +16,12 @@ */ package org.mxchange.pizzaapplication.beans.country; +import java.text.MessageFormat; import java.util.Collections; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; import javax.faces.view.facelets.FaceletException; import javax.inject.Named; import javax.naming.Context; @@ -27,6 +29,7 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcountry.data.Country; import org.mxchange.jcountry.data.CountrySingletonBeanRemote; +import org.mxchange.jcountry.events.AdminAddedCountryEvent; /** * A country bean @@ -69,6 +72,27 @@ public class PizzaCountryWebApplicationBean implements PizzaCountryWebApplicatio } } + @Override + public void afterAdminAddedCountry (@Observes final AdminAddedCountryEvent event) { + // Is all valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getAddedCountry() == null) { + // Throw again ... + throw new NullPointerException("event.addedCountry is null"); //NOI18N + } else if (event.getAddedCountry().getCountryId() == null) { + // And again ... + throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N + } else if (event.getAddedCountry().getCountryId() < 1) { + // Id is invalid + throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N + } + + // Add the event + this.countryList.add(event.getAddedCountry()); + } + @Override public List allCountries () { // Return "cached" version diff --git a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java index 70d54870..cb9d267f 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java @@ -19,6 +19,7 @@ package org.mxchange.pizzaapplication.beans.country; import java.io.Serializable; import java.util.List; import org.mxchange.jcountry.data.Country; +import org.mxchange.jcountry.events.AdminAddedCountryEvent; /** * An interface for country beans @@ -34,4 +35,11 @@ public interface PizzaCountryWebApplicationController extends Serializable { */ List allCountries (); + /** + * Observing method when the event is fired that an administrator added a + * new country + *

+ * @param event Event instance + */ + void afterAdminAddedCountry (final AdminAddedCountryEvent event); } -- 2.39.5