]> 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 b8db871a22731c48f0b0f4ea93cd36a71bba7d53..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.text.MessageFormat;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.MissingResourceException;
 import java.util.Objects;
-import java.util.ResourceBundle;
-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.application.FacesMessage;
-import javax.faces.context.FacesContext;
-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
@@ -50,7 +40,7 @@ import org.mxchange.jjobs.beans.BaseJobsController;
  */
 @Named ("adminCountryController")
 @RequestScoped
-public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
+public class JobsAdminCountryWebRequestBean extends BaseJobsBean implements JobsAdminCountryWebRequestController {
 
        /**
         * Serial number
@@ -65,26 +55,21 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
        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
         */
@@ -100,6 +85,12 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
         */
        private Boolean countryIsLocalPrefixRequired;
 
+       /**
+        * Regular country controller
+        */
+       @Inject
+       private JobsCountryListWebViewController countryListController;
+
        /**
         * Phone code
         */
@@ -113,10 +104,15 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
                super();
        }
 
-       @Override
-       public String addCountry () {
+       /**
+        * Adds country to all relevant beans and sends it to the EJB. A redirect
+        * should happen after successfull creation.
+        * <p>
+        * @todo Add field validation
+        */
+       public void addCountry () {
                // Create new country object
-               Country country = new CountryData();
+               final Country country = new CountryData();
 
                // Add all data
                country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
@@ -129,18 +125,18 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
                // 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
@@ -148,98 +144,115 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
 
                // Clear this bean
                this.clear();
-
-               // Redirect to list
-               return "admin_list_country"; //NOI18N
-       }
-
-       @Override
-       public List<Country> allCountries () {
-               // Return "cached" version
-               return this.countryController.allCountries();
        }
 
-       @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
-       public void setCountryPhoneCode (final Short countryPhoneCode) {
-               this.countryPhoneCode = countryPhoneCode;
-       }
-
-       @Override
-       public boolean hasCountries () {
-               return (!this.allCountries().isEmpty());
-       }
-
        /**
-        * Post-construction method
+        * Setter for country code (example: 49 for Germany, 63 for Philippines)
+        * <p>
+        * @param countryPhoneCode Country code
         */
-       @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);
-               }
+       public void setCountryPhoneCode (final Short countryPhoneCode) {
+               this.countryPhoneCode = countryPhoneCode;
        }
 
        /**
@@ -268,19 +281,10 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
                // 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;
@@ -291,131 +295,4 @@ public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController
                return isAdded;
        }
 
-       /**
-        * Returns given property key or throws an exception if not found.
-        * <p>
-        * @param parameterKey Property key
-        * <p>
-        * @return Property value
-        * <p>
-        * @throws NullPointerException If given key is not found
-        * @throws NumberFormatException If no number is given in context parameter
-        */
-       protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
-               // Get context parameter
-               Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
-               // Return it
-               return contextValue;
-       }
-
-       /**
-        * Returns given property key or throws an exception if not found.
-        * <p>
-        * @param parameterKey Property key
-        * <p>
-        * @return Property value
-        * <p>
-        * @throws NullPointerException If given key is not found
-        */
-       protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
-               // Get context parameter
-               String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
-               // Is it null?
-               if (null == contextValue) {
-                       // Throw NPE
-                       throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
-               }
-               // Return it
-               return contextValue;
-       }
-
-       /**
-        * Checks whether debug mode is enabled for given controller
-        * <p>
-        * @param controllerName Name of controller
-        * <p>
-        * @return Whether debug mode is enabled
-        */
-       protected boolean isDebugModeEnabled (final String controllerName) {
-               // Parameters should be valid
-               if (null == controllerName) {
-                       // Throw NPE
-                       throw new NullPointerException("controllerName is null"); //NOI18N
-               } else if (controllerName.isEmpty()) {
-                       // Is empty
-                       throw new IllegalArgumentException("controllerName is empty"); //NOI18N
-               }
-               // Try to get context parameter
-               String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
-               // Is it set and true?
-               boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
-               // Return it
-               return isEnabled;
-       }
-
-       /**
-        * Loads resource bundle for given locale. This must be implemented per
-        * project so all projects can still customize their methods. Calling
-        * ResourceBundleloadBundle() in this class means that also the bundle files
-        * must be present here.
-        * <p>
-        * @param locale Locale from e.g. FacesContext
-        * <p>
-        * @return Initialized and loaded resource bundle
-        */
-       protected abstract ResourceBundle loadResourceBundle (final Locale locale);
-
-       /**
-        * Shows a faces message for given causing exception. The message from the
-        * exception is being inserted into the message.
-        * <p>
-        * @param clientId Client id to send message to
-        * @param cause    Causing exception
-        */
-       protected void showFacesMessage (final String clientId, final Throwable cause) {
-               // Get context and add message
-               this.showFacesMessage(clientId, cause.getMessage());
-       }
-
-       /**
-        * Shows a faces message with given message (i18n) key.
-        * <p>
-        * @param clientId Client id to send message to
-        * @param i18nKey  Message key
-        * <p>
-        * @throws NullPointerException If clientId or i18nKey is null
-        * @throws IllegalArgumentException If clientId or i18nKey is empty
-        */
-       protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
-               // Both parameter must be valid
-               if (null == clientId) {
-                       // Throw NPE
-                       throw new NullPointerException("clientId is null"); //NOI18N
-               } else if (clientId.isEmpty()) {
-                       // Is empty
-                       throw new IllegalArgumentException("clientId is null"); //NOI18N
-               } else if (null == i18nKey) {
-                       // Throw NPE
-                       throw new NullPointerException("i18nKey is null"); //NOI18N
-               } else if (i18nKey.isEmpty()) {
-                       // Is empty
-                       throw new IllegalArgumentException("i18nKey is null"); //NOI18N
-               }
-               // Get current locale
-               Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
-               // Get bundle bundle
-               ResourceBundle bundle = this.loadResourceBundle(locale);
-               // Default is i18nKey
-               String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
-               // Try it
-               try {
-                       // Get message
-                       message = bundle.getString(i18nKey);
-               } catch (final MissingResourceException ex) {
-                       // Did not find it, ignored
-               }
-               // Get context and add message
-               FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
-       }
-
 }