]> 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 0b9697b7b4a5e639771c55c6d3b8e4065e6b7c25..98a5a7c387808bb35b510189525e0d2a8961b5e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2020 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
  */
 package org.mxchange.jjobs.beans.country;
 
-import java.util.Iterator;
-import java.util.List;
 import java.util.Objects;
-import javax.annotation.PostConstruct;
+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.faces.FacesException;
 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.ObservableAdminAddedCountryEvent;
+import org.mxchange.jcountry.events.added.AdminAddedCountryEvent;
+import org.mxchange.jcountry.events.added.ObservableAdminAddedCountryEvent;
 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
-import org.mxchange.jjobs.beans.BaseJobsController;
+import org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote;
+import org.mxchange.jcountry.model.data.Country;
+import org.mxchange.jcountry.model.data.CountryData;
+import org.mxchange.jjobs.beans.BaseJobsBean;
+import org.mxchange.jjobs.beans.country.list.JobsCountryListWebViewController;
 
 /**
  * An administrative country bean
@@ -44,7 +40,7 @@ import org.mxchange.jjobs.beans.BaseJobsController;
  */
 @Named ("adminCountryController")
 @RequestScoped
-public class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
+public class JobsAdminCountryWebRequestBean extends BaseJobsBean implements JobsAdminCountryWebRequestController {
 
        /**
         * Serial number
@@ -59,26 +55,21 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
        private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
 
        /**
-        * Abroad dial prefix
+        * Remote country EJB
         */
-       private String countryAbroadDialPrefix;
+       @EJB (lookup = "java:global/jjobs-ejb/adminCountry!org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote")
+       private AdminCountrySessionBeanRemote adminCountryBean;
 
        /**
-        * Remote country EJB
+        * Abroad dial prefix
         */
-       private CountrySingletonBeanRemote countryBean;
+       private String countryAbroadDialPrefix;
 
        /**
         * 2-letter country code
         */
        private String countryCode;
 
-       /**
-        * Regular country controller
-        */
-       @Inject
-       private JobsCountryWebApplicationController countryController;
-
        /**
         * Local dial prefix
         */
@@ -94,6 +85,12 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
         */
        private Boolean countryIsLocalPrefixRequired;
 
+       /**
+        * Regular country controller
+        */
+       @Inject
+       private JobsCountryListWebViewController countryListController;
+
        /**
         * Phone code
         */
@@ -111,13 +108,11 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
         * 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 () {
+       public void addCountry () {
                // Create new country object
-               Country country = new CountryData();
+               final Country country = new CountryData();
 
                // Add all data
                country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
@@ -130,18 +125,18 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
                // Does it already exist?
                if (this.isCountryAdded(country)) {
                        // Yes, then abort here
-                       throw new FaceletException(new CountryAlreadyAddedException(country));
+                       throw new FacesException(new CountryAlreadyAddedException(country));
                }
 
                // Init variable
-               Country updatedCountry = null;
+               final Country updatedCountry;
 
                try {
                        // Send country to bean
-                       updatedCountry = this.countryBean.addCountry(country);
+                       updatedCountry = this.adminCountryBean.addCountry(country);
                } catch (final CountryAlreadyAddedException ex) {
                        // Throw again
-                       throw new FaceletException(ex);
+                       throw new FacesException(ex);
                }
 
                // Fire event
@@ -149,9 +144,6 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
 
                // Clear this bean
                this.clear();
-
-               // Redirect to list
-               return "admin_list_country"; //NOI18N
        }
 
        /**
@@ -263,24 +255,6 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
                this.countryPhoneCode = countryPhoneCode;
        }
 
-       /**
-        * Post-construction method
-        */
-       @PostConstruct
-       public void init () {
-               // 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);
-               }
-       }
-
        /**
         * Clears this bean's data. This should be called after a form has been
         * submitted and the processing of the form was successful.
@@ -307,19 +281,10 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement
                // Default is not found
                boolean isAdded = false;
 
-               // Now get whole ist
-               List<Country> countries = this.countryController.allCountries();
-
-               // Get iterator from it
-               Iterator<Country> iterator = countries.iterator();
-
                // Check whole list
-               while (iterator.hasNext()) {
-                       // Get next country
-                       Country next = iterator.next();
-
+               for (final Country currentCountry : this.countryListController.getAllCountries()) {
                        // 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(), currentCountry.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), currentCountry.getCountryI18nKey()))) {
                                // Yes, then abort search
                                isAdded = true;
                                break;