]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java
Don't cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / country / PizzaCountryConverter.java
index c8a0faf869b81344fa8ad43626177889b85e99eb..fda1cc20609dac2e982c2832939f313affae4387 100644 (file)
  */
 package org.mxchange.pizzaapplication.converter.country;
 
-import java.text.MessageFormat;
 import java.util.List;
 import java.util.Objects;
+import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.Converter;
 import javax.faces.convert.ConverterException;
 import javax.faces.convert.FacesConverter;
+import javax.faces.validator.ValidatorException;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
-import org.mxchange.jcountry.data.Country;
-import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
+import org.mxchange.jcountry.model.data.Country;
+import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
 
 /**
  * Converter for country instance
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-@FacesConverter (value = "CountryConverter")
-public class PizzaCountryConverter implements Converter {
+@FacesConverter ("CountryConverter")
+public class PizzaCountryConverter implements Converter<Country> {
 
        /**
         * Country bean
         */
        private static CountrySingletonBeanRemote COUNTRY_BEAN;
 
-       /**
-        * Initialization of this converter
-        */
-       public PizzaCountryConverter () {
-       }
-
        @Override
-       public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+       public Country getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+               // Is the instance there?
+               if (COUNTRY_BEAN == null) {
+                       try {
+                               // Not yet, attempt lookup
+                               final Context initial = new InitialContext();
+
+                               // Lookup EJB
+                               COUNTRY_BEAN = (CountrySingletonBeanRemote) initial.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote");
+                       } catch (final NamingException ex) {
+                               // Throw it again
+                               throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
+                       }
+               }
+
                // Is the value null or empty?
                if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
                        // Warning message
@@ -60,24 +69,8 @@ public class PizzaCountryConverter implements Converter {
                        return null;
                }
 
-               // Is the bean there?
-               // @TODO Requires this synchronization or is it (sync) confusing the container?
-               if (null == PizzaCountryConverter.COUNTRY_BEAN) {
-                       // Try to get it
-                       try {
-                               // Get initial context
-                               Context initialContext = new InitialContext();
-
-                               // ... and country bean
-                               PizzaCountryConverter.COUNTRY_BEAN = (CountrySingletonBeanRemote) initialContext.lookup("java:global/pizzaapplication-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
-                       } catch (final NamingException ex) {
-                               // Continue to throw it
-                               throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
-                       }
-               }
-
                // Get full list
-               List<Country> countryList = PizzaCountryConverter.COUNTRY_BEAN.allCountries();
+               final List<Country> countryList = COUNTRY_BEAN.allCountries();
 
                // Init value
                Country country = null;
@@ -85,7 +78,7 @@ public class PizzaCountryConverter implements Converter {
                // Try this better
                try {
                        // Convert it to long
-                       Long countryId = Long.parseLong(submittedValue);
+                       final Long countryId = Long.parseLong(submittedValue);
 
                        // Category id should not be below 1
                        assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N
@@ -109,18 +102,15 @@ public class PizzaCountryConverter implements Converter {
        }
 
        @Override
-       public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
+       public String getAsString (final FacesContext context, final UIComponent component, final Country value) {
                // Is the object null?
-               if ((null == value) || ((String.valueOf(value)).isEmpty())) {
+               if ((null == value) || (String.valueOf(value).isEmpty())) {
                        // Is null
                        return ""; //NOI18N
-               } else if (!(value instanceof Country)) {
-                       // Not same interface
-                       throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Country.", value)); //NOI18N
                }
 
-               // Return category id
-               return String.valueOf(((Country) value).getCountryId());
+               // Return id number
+               return String.valueOf(value.getCountryId());
        }
 
 }