]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Added firing newly added event when an administrator has added a new country and...
authorRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 17:34:53 +0000 (19:34 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 17:34:53 +0000 (19:34 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebApplicationBean.java
src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java
src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java

index e3663743aa9b12323c8f934c6649d5722f25d6fd..2abe0aa1bd87cbc63976f08761401ca55a7e0a7d 100644 (file)
@@ -20,6 +20,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
 import javax.enterprise.context.ApplicationScoped;
 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;
 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.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;
 
 /**
 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
 
 /**
@@ -66,6 +70,13 @@ public class PizzaAdminCountryWebApplicationBean implements PizzaAdminCountryWeb
        @Inject
        private PizzaCountryWebApplicationController countryController;
 
        @Inject
        private PizzaCountryWebApplicationController countryController;
 
+       /**
+        * An event triggered when the administrator has added a country
+        */
+       @Inject
+       @Any
+       private Event<AdminAddedCountryEvent> addedCountryEvent;
+
        /**
         * Local dial prefix
         */
        /**
         * Local dial prefix
         */
@@ -126,6 +137,9 @@ public class PizzaAdminCountryWebApplicationBean implements PizzaAdminCountryWeb
                        // Send country to bean
                        this.countryBean.addCountry(country);
 
                        // Send country to bean
                        this.countryBean.addCountry(country);
 
+                       // Fire event
+                       this.addedCountryEvent.fire(new AdminEventCountryAdded(country));
+
                        // Clear bean
                        this.clear();
                } catch (final CountryAlreadyAddedException ex) {
                        // Clear bean
                        this.clear();
                } catch (final CountryAlreadyAddedException ex) {
index 2431d44e6f69dddda5441d7c6934000da8f6e411..623ced5195255b5d4e381f1c5e3eb69e856b0897 100644 (file)
  */
 package org.mxchange.pizzaapplication.beans.country;
 
  */
 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 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;
 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 javax.naming.NamingException;
 import org.mxchange.jcountry.data.Country;
 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
+import org.mxchange.jcountry.events.AdminAddedCountryEvent;
 
 /**
  * A country bean
 
 /**
  * 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<Country> allCountries () {
                // Return "cached" version
        @Override
        public List<Country> allCountries () {
                // Return "cached" version
index 70d548704a028c91c72d83d7849acdccc6361591..cb9d267f23a8583ff950814e2c9f23681c22391c 100644 (file)
@@ -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 java.io.Serializable;
 import java.util.List;
 import org.mxchange.jcountry.data.Country;
+import org.mxchange.jcountry.events.AdminAddedCountryEvent;
 
 /**
  * An interface for country beans
 
 /**
  * An interface for country beans
@@ -34,4 +35,11 @@ public interface PizzaCountryWebApplicationController extends Serializable {
         */
        List<Country> allCountries ();
 
         */
        List<Country> allCountries ();
 
+       /**
+        * Observing method when the event is fired that an administrator added a
+        * new country
+        * <p>
+        * @param event Event instance
+        */
+       void afterAdminAddedCountry (final AdminAddedCountryEvent event);
 }
 }