]> git.mxchange.org Git - jjobs-war.git/blobdiff - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsAdminCountryWebRequestBean.java
index f8441a22e5517e7343625c0c39a31f25b0971345..e730dd54e51bf15dd00d6d32b18afe91ac288034 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016, 2017 Roland Häder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -19,30 +19,29 @@ package org.mxchange.jjobs.beans.country;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
+import javax.ejb.EJB;
 import javax.enterprise.context.RequestScoped;
 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.naming.Context;
-import javax.naming.InitialContext;
-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.events.ObservableAdminAddedCountryEvent;
 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
+import org.mxchange.jcountry.model.data.Country;
+import org.mxchange.jcountry.model.data.CountryData;
+import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
+import org.mxchange.jjobs.beans.BaseJobsBean;
 
 /**
  * An administrative country bean
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
  */
 @Named ("adminCountryController")
 @RequestScoped
-public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebRequestController {
+public class JobsAdminCountryWebRequestBean extends BaseJobsBean implements JobsAdminCountryWebRequestController {
 
        /**
         * Serial number
@@ -54,7 +53,7 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
         */
        @Inject
        @Any
-       private Event<AdminAddedCountryEvent> addedCountryEvent;
+       private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
 
        /**
         * Abroad dial prefix
@@ -64,6 +63,7 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
        /**
         * Remote country EJB
         */
+       @EJB (lookup = "java:global/jjobs-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
        private CountrySingletonBeanRemote countryBean;
 
        /**
@@ -75,7 +75,7 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
         * Regular country controller
         */
        @Inject
-       private JobsCountryWebApplicationController countryController;
+       private JobsCountryWebRequestController countryController;
 
        /**
         * Local dial prefix
@@ -101,29 +101,27 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
         * Default constructor
         */
        public JobsAdminCountryWebRequestBean () {
-               // Try this
-               try {
-                       // Get initial context
-                       Context context = new InitialContext();
-
-                       // Try to lookup the bean
-                       this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
-               } catch (final NamingException ex) {
-                       // Continue to throw
-                       throw new FaceletException(ex);
-               }
+               // Call super constructor
+               super();
        }
 
-       @Override
-       public void addCountry () {
+       /**
+        * Adds country to all relevant beans and sends it to the EJB. A redirect
+        * should happen after successfull creation.
+        * <p>
+        * @return Redirect outcome
+        * <p>
+        * @todo Add field validation
+        */
+       public String addCountry () {
                // Create new country object
-               Country country = new CountryData();
+               final Country country = new CountryData();
 
                // Add all data
                country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
                country.setCountryCode(this.getCountryCode());
                country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
-               country.setCountryI18nkey(this.getCountryI18nKey());
+               country.setCountryI18nKey(this.getCountryI18nKey());
                country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
                country.setCountryPhoneCode(this.getCountryPhoneCode());
 
@@ -134,7 +132,7 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
                }
 
                // Init variable
-               Country updatedCountry = null;
+               final Country updatedCountry;
 
                try {
                        // Send country to bean
@@ -145,78 +143,136 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
                }
 
                // Fire event
-               this.addedCountryEvent.fire(new AdminEventCountryAdded(updatedCountry));
-       }
+               this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
 
-       @Override
-       public List<Country> allCountries () {
-               // Return "cached" version
-               return this.countryController.allCountries();
+               // Clear this bean
+               this.clear();
+
+               // Redirect to list
+               return "admin_list_country"; //NOI18N
        }
 
-       @Override
+       /**
+        * Getter for abroad dial prefix
+        * <p>
+        * @return Abroad dial prefix
+        */
        public String getCountryAbroadDialPrefix () {
                return this.countryAbroadDialPrefix;
        }
 
-       @Override
+       /**
+        * Setter for abroad dial prefix
+        * <p>
+        * @param countryAbroadDialPrefix Abroad dial prefix
+        */
        public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
                this.countryAbroadDialPrefix = countryAbroadDialPrefix;
        }
 
-       @Override
+       /**
+        * Getter for 2-characters country code
+        * <p>
+        * @return Country code
+        */
        public String getCountryCode () {
                return this.countryCode;
        }
 
-       @Override
+       /**
+        * Setter for 2-characters country code
+        * <p>
+        * @param countryCode Country code
+        */
        public void setCountryCode (final String countryCode) {
                this.countryCode = countryCode;
        }
 
-       @Override
+       /**
+        * Getter for external dial prefix
+        * <p>
+        * @return External dial prefix
+        */
        public String getCountryExternalDialPrefix () {
                return this.countryExternalDialPrefix;
        }
 
-       @Override
+       /**
+        * Setter for external dial prefix
+        * <p>
+        * @param countryExternalDialPrefix External dial prefix
+        */
        public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
                this.countryExternalDialPrefix = countryExternalDialPrefix;
        }
 
-       @Override
+       /**
+        * Getter for i18n key for country name
+        * <p>
+        * @return i18n key for country name
+        */
        public String getCountryI18nKey () {
                return this.countryI18nKey;
        }
 
-       @Override
+       /**
+        * Setter for i18n key for country name
+        * <p>
+        * @param countryI18nKey i18n key for country name
+        */
        public void setCountryI18nKey (final String countryI18nKey) {
                this.countryI18nKey = countryI18nKey;
        }
 
-       @Override
+       /**
+        * Getter for whether the local dial prefix is required for local calls
+        * <p>
+        * @return Whether the local dial prefix is required
+        */
        public Boolean getCountryIsLocalPrefixRequired () {
                return this.countryIsLocalPrefixRequired;
        }
 
-       @Override
+       /**
+        * Setter for whether the local dial prefix is required for local calls
+        * <p>
+        * @param countryIsLocalPrefixRequired Whether the local dial prefix is
+        *                                     required
+        */
        public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
                this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
        }
 
-       @Override
+       /**
+        * Getter for country code (example: 49 for Germany, 63 for Philippines)
+        * <p>
+        * @return Dial number without prefix
+        */
        public Short getCountryPhoneCode () {
                return this.countryPhoneCode;
        }
 
-       @Override
+       /**
+        * Setter for country code (example: 49 for Germany, 63 for Philippines)
+        * <p>
+        * @param countryPhoneCode Country code
+        */
        public void setCountryPhoneCode (final Short countryPhoneCode) {
                this.countryPhoneCode = countryPhoneCode;
        }
 
-       @Override
-       public boolean hasCountries () {
-               return (!this.allCountries().isEmpty());
+       /**
+        * Clears this bean's data. This should be called after a form has been
+        * submitted and the processing of the form was successful.
+        */
+       private void clear () {
+               // Clear fields
+               this.setCountryAbroadDialPrefix(null);
+               this.setCountryCode(null);
+               this.setCountryExternalDialPrefix(null);
+               this.setCountryI18nKey(null);
+               this.setCountryIsLocalPrefixRequired(null);
+               this.setCountryPhoneCode(null);
        }
 
        /**
@@ -232,18 +288,18 @@ public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebReques
                boolean isAdded = false;
 
                // Now get whole ist
-               List<Country> countries = this.countryController.allCountries();
+               final List<Country> countries = this.countryController.allCountries();
 
                // Get iterator from it
-               Iterator<Country> iterator = countries.iterator();
+               final Iterator<Country> iterator = countries.iterator();
 
                // Check whole list
                while (iterator.hasNext()) {
                        // Get next country
-                       Country next = iterator.next();
+                       final Country next = iterator.next();
 
                        // Is country code or i18n the same?
-                       if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nkey(), next.getCountryI18nkey()))) {
+                       if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
                                // Yes, then abort search
                                isAdded = true;
                                break;